system files use new identifiers

This commit is contained in:
Araq 2014-08-23 00:58:04 +02:00
commit fc0fda14ae
13 changed files with 371 additions and 338 deletions

View file

@ -1,7 +1,7 @@
# #
# #
# Nimrod's Runtime Library # Nim's Runtime Library
# (c) Copyright 2013 Andreas Rumpf # (c) Copyright 2014 Andreas Rumpf
# #
# See the file "copying.txt", included in this # See the file "copying.txt", included in this
# distribution, for details about the copyright. # distribution, for details about the copyright.
@ -77,23 +77,23 @@ type
auto* = expr auto* = expr
any* = distinct auto any* = distinct auto
TSignedInt* = int|int8|int16|int32|int64 SomeSignedInt* = int|int8|int16|int32|int64
## type class matching all signed integer types ## type class matching all signed integer types
TUnsignedInt* = uint|uint8|uint16|uint32|uint64 SomeUnsignedInt* = uint|uint8|uint16|uint32|uint64
## type class matching all unsigned integer types ## type class matching all unsigned integer types
TInteger* = TSignedInt|TUnsignedInt SomeInteger* = SomeSignedInt|SomeUnsignedInt
## type class matching all integer types ## type class matching all integer types
TOrdinal* = int|int8|int16|int32|int64|bool|enum|uint8|uint16|uint32 SomeOrdinal* = int|int8|int16|int32|int64|bool|enum|uint8|uint16|uint32
## type class matching all ordinal types; however this includes enums with ## type class matching all ordinal types; however this includes enums with
## holes. ## holes.
TReal* = float|float32|float64 SomeReal* = float|float32|float64
## type class matching all floating point number types ## type class matching all floating point number types
TNumber* = TInteger|TReal SomeNumber* = SomeInteger|SomeReal
## type class matching all number types ## type class matching all number types
proc defined*(x: expr): bool {.magic: "Defined", noSideEffect.} proc defined*(x: expr): bool {.magic: "Defined", noSideEffect.}
@ -108,6 +108,15 @@ proc defined*(x: expr): bool {.magic: "Defined", noSideEffect.}
## # Do here programmer friendly expensive sanity checks. ## # Do here programmer friendly expensive sanity checks.
## # Put here the normal code ## # Put here the normal code
when defined(nimalias):
{.deprecated: [
TSignedInt: SomeSignedInt,
TUnsignedInt: SomeUnsignedInt,
TInteger: SomeInteger,
TReal: SomeReal,
TNumber: SomeNumber,
TOrdinal: SomeOrdinal].}
proc declared*(x: expr): bool {.magic: "Defined", noSideEffect.} proc declared*(x: expr): bool {.magic: "Defined", noSideEffect.}
## Special compile-time procedure that checks whether `x` is ## Special compile-time procedure that checks whether `x` is
## declared. `x` has to be an identifier or a qualified identifier. ## declared. `x` has to be an identifier or a qualified identifier.
@ -202,10 +211,13 @@ type
set*{.magic: "Set".}[T] ## Generic type to construct bit sets. set*{.magic: "Set".}[T] ## Generic type to construct bit sets.
type type
TSlice* {.final, pure.}[T] = object ## builtin slice type Slice* {.final, pure.}[T] = object ## builtin slice type
a*, b*: T ## the bounds a*, b*: T ## the bounds
proc `..`*[T](a, b: T): TSlice[T] {.noSideEffect, inline.} = when defined(nimalias):
{.deprecated: [TSlice: Slice].}
proc `..`*[T](a, b: T): Slice[T] {.noSideEffect, inline.} =
## `slice`:idx: operator that constructs an interval ``[a, b]``, both `a` ## `slice`:idx: operator that constructs an interval ``[a, b]``, both `a`
## and `b` are inclusive. Slices can also be used in the set constructor ## and `b` are inclusive. Slices can also be used in the set constructor
## and in ordinal case statements, but then they are special-cased by the ## and in ordinal case statements, but then they are special-cased by the
@ -213,7 +225,7 @@ proc `..`*[T](a, b: T): TSlice[T] {.noSideEffect, inline.} =
result.a = a result.a = a
result.b = b result.b = b
proc `..`*[T](b: T): TSlice[T] {.noSideEffect, inline.} = proc `..`*[T](b: T): Slice[T] {.noSideEffect, inline.} =
## `slice`:idx: operator that constructs an interval ``[default(T), b]`` ## `slice`:idx: operator that constructs an interval ``[default(T), b]``
result.b = b result.b = b
@ -307,29 +319,29 @@ type
## is an int type ranging from one to the maximum value ## is an int type ranging from one to the maximum value
## of an int. This type is often useful for documentation and debugging. ## of an int. This type is often useful for documentation and debugging.
TObject* {.exportc: "TNimObject", inheritable.} = RootObj* {.exportc: "TNimObject", inheritable.} =
object ## the root of Nimrod's object hierarchy. Objects should object ## the root of Nimrod's object hierarchy. Objects should
## inherit from TObject or one of its descendants. However, ## inherit from TObject or one of its descendants. However,
## objects that have no ancestor are allowed. ## objects that have no ancestor are allowed.
PObject* = ref TObject ## reference to TObject RootRef* = ref RootObj ## reference to RootObj
TEffect* {.compilerproc.} = object of TObject ## \ RootEffect* {.compilerproc.} = object of RootObj ## \
## base effect class; each effect should ## base effect class; each effect should
## inherit from `TEffect` unless you know what ## inherit from `TEffect` unless you know what
## you doing. ## you doing.
FTime* = object of TEffect ## Time effect. TimeEffect* = object of RootEffect ## Time effect.
FIO* = object of TEffect ## IO effect. IOEffect* = object of RootEffect ## IO effect.
FReadIO* = object of FIO ## Effect describing a read IO operation. ReadIOEffect* = object of IOEffect ## Effect describing a read IO operation.
FWriteIO* = object of FIO ## Effect describing a write IO operation. WriteIOEffect* = object of IOEffect ## Effect describing a write IO operation.
FExecIO* = object of FIO ## Effect describing an executing IO operation. ExecIOEffect* = object of IOEffect ## Effect describing an executing IO operation.
E_Base* {.compilerproc.} = object of TObject ## \ Exception* {.compilerproc.} = object of RootObj ## \
## Base exception class. ## Base exception class.
## ##
## Each exception has to inherit from `E_Base`. See the full `exception ## Each exception has to inherit from `Exception`. See the full `exception
## hierarchy`_. ## hierarchy`_.
parent: ref E_Base ## parent exception (can be used as a stack) parent: ref Exception ## parent exception (can be used as a stack)
name: cstring ## The exception's name is its Nimrod identifier. name: cstring ## The exception's name is its Nim identifier.
## This field is filled automatically in the ## This field is filled automatically in the
## ``raise`` statement. ## ``raise`` statement.
msg* {.exportc: "message".}: string ## the exception's message. Not msg* {.exportc: "message".}: string ## the exception's message. Not
@ -337,130 +349,117 @@ type
## is bad style. ## is bad style.
trace: string trace: string
EAsynch* = object of E_Base ## \ SystemError* = object of Exception ## \
## Abstract exception class for *asynchronous exceptions* (interrupts).
##
## This is rarely needed: most exception types inherit from `ESynch
## <#ESynch>`_. See the full `exception hierarchy`_.
EControlC* = object of EAsynch ## \
## Raised for Ctrl+C key presses in console applications.
##
## See the full `exception hierarchy`_.
ESynch* = object of E_Base ## \
## Abstract exception class for *synchronous exceptions*.
##
## Most exceptions should be inherited (directly or indirectly) from
## `ESynch` instead of from `EAsynch <#EAsynch>`_. See the full `exception
## hierarchy`_.
ESystem* = object of ESynch ## \
## Abstract class for exceptions that the runtime system raises. ## Abstract class for exceptions that the runtime system raises.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EIO* = object of ESystem ## \ IOError* = object of SystemError ## \
## Raised if an IO error occured. ## Raised if an IO error occured.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EOS* = object of ESystem ## \ OSError* = object of SystemError ## \
## Raised if an operating system service failed. ## Raised if an operating system service failed.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
errorCode*: int32 ## OS-defined error code describing this error. errorCode*: int32 ## OS-defined error code describing this error.
EInvalidLibrary* = object of EOS ## \ LibraryError* = object of OSError ## \
## Raised if a dynamic library could not be loaded. ## Raised if a dynamic library could not be loaded.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EResourceExhausted* = object of ESystem ## \ ResourceExhaustedError* = object of SystemError ## \
## Raised if a resource request could not be fullfilled. ## Raised if a resource request could not be fullfilled.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EArithmetic* = object of ESynch ## \ ArithmeticError* = object of Exception ## \
## Raised if any kind of arithmetic error occured. ## Raised if any kind of arithmetic error occured.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EDivByZero* {.compilerproc.} = object of EArithmetic ## \ DivByZeroError* = object of ArithmeticError ## \
## Raised for runtime integer divide-by-zero errors. ## Raised for runtime integer divide-by-zero errors.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EOverflow* {.compilerproc.} = object of EArithmetic ## \
OverflowError* = object of ArithmeticError ## \
## Raised for runtime integer overflows. ## Raised for runtime integer overflows.
## ##
## This happens for calculations whose results are too large to fit in the ## This happens for calculations whose results are too large to fit in the
## provided bits. See the full `exception hierarchy`_. ## provided bits. See the full `exception hierarchy`_.
EAccessViolation* {.compilerproc.} = object of ESynch ## \ AccessViolationError* {.compilerproc.} = object of Exception ## \
## Raised for invalid memory access errors ## Raised for invalid memory access errors
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EAssertionFailed* {.compilerproc.} = object of ESynch ## \ AssertionError* {.compilerproc.} = object of Exception ## \
## Raised when assertion is proved wrong. ## Raised when assertion is proved wrong.
## ##
## Usually the result of using the `assert() template <#assert>`_. See the ## Usually the result of using the `assert() template <#assert>`_. See the
## full `exception hierarchy`_. ## full `exception hierarchy`_.
EInvalidValue* = object of ESynch ## \ ValueError* = object of Exception ## \
## Raised for string and object conversion errors. ## Raised for string and object conversion errors.
EInvalidKey* = object of EInvalidValue ## \ KeyError* = object of ValueError ## \
## Raised if a key cannot be found in a table. ## Raised if a key cannot be found in a table.
## ##
## Mostly used by the `tables <tables.html>`_ module, it can also be raised ## Mostly used by the `tables <tables.html>`_ module, it can also be raised
## by other collection modules like `sets <sets.html>`_ or `strtabs ## by other collection modules like `sets <sets.html>`_ or `strtabs
## <strtabs.html>`_. See the full `exception hierarchy`_. ## <strtabs.html>`_. See the full `exception hierarchy`_.
EOutOfMemory* = object of ESystem ## \ OutOfMemError* = object of SystemError ## \
## Raised for unsuccessful attempts to allocate memory. ## Raised for unsuccessful attempts to allocate memory.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EInvalidIndex* = object of ESynch ## \ IndexError* = object of Exception ## \
## Raised if an array index is out of bounds. ## Raised if an array index is out of bounds.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EInvalidField* = object of ESynch ## \
FieldError* = object of Exception ## \
## Raised if a record field is not accessible because its dicriminant's ## Raised if a record field is not accessible because its dicriminant's
## value does not fit. ## value does not fit.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EOutOfRange* = object of ESynch ## \ RangeError* = object of Exception ## \
## Raised if a range check error occurred. ## Raised if a range check error occurred.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EStackOverflow* = object of ESystem ## \ StackOverflowError* = object of SystemError ## \
## Raised if the hardware stack used for subroutine calls overflowed. ## Raised if the hardware stack used for subroutine calls overflowed.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
ENoExceptionToReraise* = object of ESynch ## \ ReraiseError* = object of Exception ## \
## Raised if there is no exception to reraise. ## Raised if there is no exception to reraise.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EInvalidObjectAssignment* = object of ESynch ## \ ObjectAssignmentError* = object of Exception ## \
## Raised if an object gets assigned to its parent's object. ## Raised if an object gets assigned to its parent's object.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EInvalidObjectConversion* = object of ESynch ## \ ObjectConversionError* = object of Exception ## \
## Raised if an object is converted to an incompatible object type. ## Raised if an object is converted to an incompatible object type.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EFloatingPoint* = object of ESynch ## \ FloatingPointError* = object of Exception ## \
## Base class for floating point exceptions. ## Base class for floating point exceptions.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EFloatInvalidOp* {.compilerproc.} = object of EFloatingPoint ## \ FloatInvalidOpError* = object of FloatingPointError ## \
## Raised by invalid operations according to IEEE. ## Raised by invalid operations according to IEEE.
## ##
## Raised by ``0.0/0.0``, for example. See the full `exception ## Raised by ``0.0/0.0``, for example. See the full `exception
## hierarchy`_. ## hierarchy`_.
EFloatDivByZero* {.compilerproc.} = object of EFloatingPoint ## \ FloatDivByZeroError* = object of FloatingPointError ## \
## Raised by division by zero. ## Raised by division by zero.
## ##
## Divisor is zero and dividend is a finite nonzero number. See the full ## Divisor is zero and dividend is a finite nonzero number. See the full
## `exception hierarchy`_. ## `exception hierarchy`_.
EFloatOverflow* {.compilerproc.} = object of EFloatingPoint ## \ FloatOverflowError* = object of FloatingPointError ## \
## Raised for overflows. ## Raised for overflows.
## ##
## The operation produced a result that exceeds the range of the exponent. ## The operation produced a result that exceeds the range of the exponent.
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
EFloatUnderflow* {.compilerproc.} = object of EFloatingPoint ## \ FloatUnderflowError* = object of FloatingPointError ## \
## Raised for underflows. ## Raised for underflows.
## ##
## The operation produced a result that is too small to be represented as a ## The operation produced a result that is too small to be represented as a
## normal number. See the full `exception hierarchy`_. ## normal number. See the full `exception hierarchy`_.
EFloatInexact* {.compilerproc.} = object of EFloatingPoint ## \ FloatInexactError* = object of FloatingPointError ## \
## Raised for inexact results. ## Raised for inexact results.
## ##
## The operation produced a result that cannot be represented with infinite ## The operation produced a result that cannot be represented with infinite
@ -468,12 +467,37 @@ type
## ##
## **NOTE**: Nimrod currently does not detect these! See the full ## **NOTE**: Nimrod currently does not detect these! See the full
## `exception hierarchy`_. ## `exception hierarchy`_.
EDeadThread* = object of ESynch ## \ DeadThreadError* = object of Exception ## \
## Raised if it is attempted to send a message to a dead thread. ## Raised if it is attempted to send a message to a dead thread.
## ##
## See the full `exception hierarchy`_. ## See the full `exception hierarchy`_.
TResult* = enum Failure, Success TResult* {.deprecated.} = enum Failure, Success
{.deprecated: [TObject: RootObj, PObject: RootRef, TEffect: RootEffect,
FTime: TimeEffect, FIO: IOEffect, FReadIO: ReadIOEffect,
FWriteIO: WriteIOEffect, FExecIO: ExecIOEffect,
E_Base: Exception, ESystem: SystemError, EIO: IOError,
EOS: OSError, EInvalidLibrary: LibraryError,
EResourceExhausted: ResourceExhaustedError,
EArithmetic: ArithmeticError, EDivByZero: DivByZeroError,
EOverflow: OverflowError, EAccessViolation: AccessViolationError,
EAssertionFailed: AssertionError, EInvalidValue: ValueError,
EInvalidKey: KeyError, EOutOfMemory: OutOfMemError,
EInvalidIndex: IndexError, EInvalidField: FieldError,
EOutOfRange: RangeError, EStackOverflow: StackOverflowError,
ENoExceptionToReraise: ReraiseError,
EInvalidObjectAssignment: ObjectAssignmentError,
EInvalidObjectConversion: ObjectConversionError,
EDeadThread: DeadThreadError,
EFloatInexact: FloatInexactError,
EFloatUnderflow: FloatUnderflowError,
EFloatingPoint: FloatingPointError,
EFloatInvalidOp: FloatInvalidOpError,
EFloatDivByZero: FloatDivByZeroError,
EFloatOverflow: FloatOverflowError
].}
proc sizeof*[T](x: T): Natural {.magic: "SizeOf", noSideEffect.} proc sizeof*[T](x: T): Natural {.magic: "SizeOf", noSideEffect.}
## returns the size of ``x`` in bytes. Since this is a low-level proc, ## returns the size of ``x`` in bytes. Since this is a low-level proc,
@ -839,7 +863,7 @@ proc contains*[T](x: set[T], y: T): bool {.magic: "InSet", noSideEffect.}
## is achieved by reversing the parameters for ``contains``; ``in`` then ## is achieved by reversing the parameters for ``contains``; ``in`` then
## passes its arguments in reverse order. ## passes its arguments in reverse order.
proc contains*[T](s: TSlice[T], value: T): bool {.noSideEffect, inline.} = proc contains*[T](s: Slice[T], value: T): bool {.noSideEffect, inline.} =
## Checks if `value` is withing the range of `s`; returns true iff ## Checks if `value` is withing the range of `s`; returns true iff
## `value >= s.a and value <= s.b` ## `value >= s.a and value <= s.b`
## ##
@ -978,7 +1002,7 @@ proc add*(x: var string, y: string) {.magic: "AppendStrStr", noSideEffect.}
## assert(tmp == "abcd") ## assert(tmp == "abcd")
type type
TEndian* = enum ## is a type describing the endianness of a processor. Endianness* = enum ## is a type describing the endianness of a processor.
littleEndian, bigEndian littleEndian, bigEndian
const const
@ -994,23 +1018,23 @@ const
## is the time of compilation as a string of the form ## is the time of compilation as a string of the form
## ``HH:MM:SS``. This works thanks to compiler magic. ## ``HH:MM:SS``. This works thanks to compiler magic.
NimrodVersion* {.magic: "NimrodVersion"}: string = "0.0.0" NimVersion* {.magic: "NimrodVersion"}: string = "0.0.0"
## is the version of Nimrod as a string. ## is the version of Nimrod as a string.
## This works thanks to compiler magic. ## This works thanks to compiler magic.
NimrodMajor* {.magic: "NimrodMajor"}: int = 0 NimMajor* {.magic: "NimrodMajor"}: int = 0
## is the major number of Nimrod's version. ## is the major number of Nimrod's version.
## This works thanks to compiler magic. ## This works thanks to compiler magic.
NimrodMinor* {.magic: "NimrodMinor"}: int = 0 NimMinor* {.magic: "NimrodMinor"}: int = 0
## is the minor number of Nimrod's version. ## is the minor number of Nimrod's version.
## This works thanks to compiler magic. ## This works thanks to compiler magic.
NimrodPatch* {.magic: "NimrodPatch"}: int = 0 NimPatch* {.magic: "NimrodPatch"}: int = 0
## is the patch number of Nimrod's version. ## is the patch number of Nimrod's version.
## This works thanks to compiler magic. ## This works thanks to compiler magic.
cpuEndian* {.magic: "CpuEndian"}: TEndian = littleEndian cpuEndian* {.magic: "CpuEndian"}: Endianness = littleEndian
## is the endianness of the target CPU. This is a valuable piece of ## is the endianness of the target CPU. This is a valuable piece of
## information for low-level code only. This works thanks to compiler ## information for low-level code only. This works thanks to compiler
## magic. ## magic.
@ -1026,6 +1050,9 @@ const
seqShallowFlag = low(int) seqShallowFlag = low(int)
{.deprecated: [TEndian: Endianness, NimrodVersion: NimVersion,
NimrodMajor: NimMajor, NimrodMinor: NimMinor, NimrodPatch: NimPatch].}
proc compileOption*(option: string): bool {. proc compileOption*(option: string): bool {.
magic: "CompileOption", noSideEffect.} magic: "CompileOption", noSideEffect.}
## can be used to determine an on|off compile-time option. Example: ## can be used to determine an on|off compile-time option. Example:
@ -1160,7 +1187,7 @@ proc repr*[T](x: T): string {.magic: "Repr", noSideEffect.}
## debugging tool. ## debugging tool.
type type
TAddress* = int ByteAddress* = int
## is the signed integer type that should be used for converting ## is the signed integer type that should be used for converting
## pointers to integer addresses for readability. ## pointers to integer addresses for readability.
@ -1174,6 +1201,8 @@ type
## compiler supports. Currently this is ``float64``, but it is ## compiler supports. Currently this is ``float64``, but it is
## platform-dependant in general. ## platform-dependant in general.
{.deprecated: [TAddress: ByteAddress].}
when defined(windows): when defined(windows):
type type
clong* {.importc: "long", nodecl.} = int32 clong* {.importc: "long", nodecl.} = int32
@ -1833,7 +1862,7 @@ proc map*[T](data: var openArray[T], op: proc (x: var T) {.closure.}) =
## # --> ["142", "242", "342", "442"] ## # --> ["142", "242", "342", "442"]
for i in 0..data.len-1: op(data[i]) for i in 0..data.len-1: op(data[i])
iterator fields*[T: tuple|object](x: T): TObject {. iterator fields*[T: tuple|object](x: T): RootObj {.
magic: "Fields", noSideEffect.} magic: "Fields", noSideEffect.}
## iterates over every field of `x`. Warning: This really transforms ## iterates over every field of `x`. Warning: This really transforms
## the 'for' and unrolls the loop. The current implementation also has a bug ## the 'for' and unrolls the loop. The current implementation also has a bug
@ -1844,7 +1873,7 @@ iterator fields*[S:tuple|object, T:tuple|object](x: S, y: T): tuple[a,b: expr] {
## Warning: This is really transforms the 'for' and unrolls the loop. ## Warning: This is really transforms the 'for' and unrolls the loop.
## The current implementation also has a bug that affects symbol binding ## The current implementation also has a bug that affects symbol binding
## in the loop body. ## in the loop body.
iterator fieldPairs*[T: tuple|object](x: T): TObject {. iterator fieldPairs*[T: tuple|object](x: T): RootObj {.
magic: "FieldPairs", noSideEffect.} magic: "FieldPairs", noSideEffect.}
## Iterates over every field of `x` returning their name and value. ## Iterates over every field of `x` returning their name and value.
## ##
@ -1975,13 +2004,15 @@ when not defined(nimrodVM) and hostOS != "standalone":
## Ordinary code does not need to call this (and should not). ## Ordinary code does not need to call this (and should not).
type type
TGC_Strategy* = enum ## the strategy the GC should use for the application GC_Strategy* = enum ## the strategy the GC should use for the application
gcThroughput, ## optimize for throughput gcThroughput, ## optimize for throughput
gcResponsiveness, ## optimize for responsiveness (default) gcResponsiveness, ## optimize for responsiveness (default)
gcOptimizeTime, ## optimize for speed gcOptimizeTime, ## optimize for speed
gcOptimizeSpace ## optimize for memory footprint gcOptimizeSpace ## optimize for memory footprint
proc GC_setStrategy*(strategy: TGC_Strategy) {.rtl, deprecated.} {.deprecated: [TGC_Strategy: GC_Strategy].}
proc GC_setStrategy*(strategy: GC_Strategy) {.rtl, deprecated.}
## tells the GC the desired strategy for the application. ## tells the GC the desired strategy for the application.
## **Deprecated** since version 0.8.14. This has always been a nop. ## **Deprecated** since version 0.8.14. This has always been a nop.
@ -2024,7 +2055,7 @@ const NimStackTrace = compileOption("stacktrace")
# of the code # of the code
var var
globalRaiseHook*: proc (e: ref E_Base): bool {.nimcall, gcsafe.} globalRaiseHook*: proc (e: ref Exception): bool {.nimcall, gcsafe.}
## with this hook you can influence exception handling on a global level. ## with this hook you can influence exception handling on a global level.
## If not nil, every 'raise' statement ends up calling this hook. Ordinary ## If not nil, every 'raise' statement ends up calling this hook. Ordinary
## application code should never set this hook! You better know what you ## application code should never set this hook! You better know what you
@ -2032,7 +2063,7 @@ var
## exception is caught and does not propagate further through the call ## exception is caught and does not propagate further through the call
## stack. ## stack.
localRaiseHook* {.threadvar.}: proc (e: ref E_Base): bool {.nimcall, gcsafe.} localRaiseHook* {.threadvar.}: proc (e: ref Exception): bool {.nimcall, gcsafe.}
## with this hook you can influence exception handling on a ## with this hook you can influence exception handling on a
## thread local level. ## thread local level.
## If not nil, every 'raise' statement ends up calling this hook. Ordinary ## If not nil, every 'raise' statement ends up calling this hook. Ordinary
@ -2092,7 +2123,7 @@ elif hostOS != "standalone":
inc(i) inc(i)
{.pop.} {.pop.}
proc echo*[T](x: varargs[T, `$`]) {.magic: "Echo", tags: [FWriteIO], gcsafe.} proc echo*[T](x: varargs[T, `$`]) {.magic: "Echo", tags: [WriteIOEffect], gcsafe.}
## Writes and flushes the parameters to the standard output. ## Writes and flushes the parameters to the standard output.
## ##
## Special built-in that takes a variable number of arguments. Each argument ## Special built-in that takes a variable number of arguments. Each argument
@ -2224,9 +2255,9 @@ when not defined(JS): #and not defined(NimrodVM):
type type
CFile {.importc: "FILE", header: "<stdio.h>", CFile {.importc: "FILE", header: "<stdio.h>",
final, incompletestruct.} = object final, incompletestruct.} = object
TFile* = ptr CFile ## The type representing a file handle. File* = ptr CFile ## The type representing a file handle.
TFileMode* = enum ## The file mode when opening a file. FileMode* = enum ## The file mode when opening a file.
fmRead, ## Open the file for read access only. fmRead, ## Open the file for read access only.
fmWrite, ## Open the file for write access only. fmWrite, ## Open the file for write access only.
fmReadWrite, ## Open the file for read and write access. fmReadWrite, ## Open the file for read and write access.
@ -2238,49 +2269,51 @@ when not defined(JS): #and not defined(NimrodVM):
fmAppend ## Open the file for writing only; append data fmAppend ## Open the file for writing only; append data
## at the end. ## at the end.
TFileHandle* = cint ## type that represents an OS file handle; this is FileHandle* = cint ## type that represents an OS file handle; this is
## useful for low-level file access ## useful for low-level file access
{.deprecated: [TFile: File, TFileHandle: FileHandle, TFileMode: FileMode].}
# text file handling: # text file handling:
var var
stdin* {.importc: "stdin", header: "<stdio.h>".}: TFile stdin* {.importc: "stdin", header: "<stdio.h>".}: File
## The standard input stream. ## The standard input stream.
stdout* {.importc: "stdout", header: "<stdio.h>".}: TFile stdout* {.importc: "stdout", header: "<stdio.h>".}: File
## The standard output stream. ## The standard output stream.
stderr* {.importc: "stderr", header: "<stdio.h>".}: TFile stderr* {.importc: "stderr", header: "<stdio.h>".}: File
## The standard error stream. ## The standard error stream.
when defined(useStdoutAsStdmsg): when defined(useStdoutAsStdmsg):
template stdmsg*: TFile = stdout template stdmsg*: File = stdout
else: else:
template stdmsg*: TFile = stderr template stdmsg*: File = stderr
## Template which expands to either stdout or stderr depending on ## Template which expands to either stdout or stderr depending on
## `useStdoutAsStdmsg` compile-time switch. ## `useStdoutAsStdmsg` compile-time switch.
proc open*(f: var TFile, filename: string, proc open*(f: var File, filename: string,
mode: TFileMode = fmRead, bufSize: int = -1): bool {.tags: [], mode: FileMode = fmRead, bufSize: int = -1): bool {.tags: [],
gcsafe.} gcsafe.}
## Opens a file named `filename` with given `mode`. ## Opens a file named `filename` with given `mode`.
## ##
## Default mode is readonly. Returns true iff the file could be opened. ## Default mode is readonly. Returns true iff the file could be opened.
## This throws no exception if the file could not be opened. ## This throws no exception if the file could not be opened.
proc open*(f: var TFile, filehandle: TFileHandle, proc open*(f: var File, filehandle: FileHandle,
mode: TFileMode = fmRead): bool {.tags: [], gcsafe.} mode: FileMode = fmRead): bool {.tags: [], gcsafe.}
## Creates a ``TFile`` from a `filehandle` with given `mode`. ## Creates a ``TFile`` from a `filehandle` with given `mode`.
## ##
## Default mode is readonly. Returns true iff the file could be opened. ## Default mode is readonly. Returns true iff the file could be opened.
proc open*(filename: string, proc open*(filename: string,
mode: TFileMode = fmRead, bufSize: int = -1): TFile = mode: FileMode = fmRead, bufSize: int = -1): File =
## Opens a file named `filename` with given `mode`. ## Opens a file named `filename` with given `mode`.
## ##
## Default mode is readonly. Raises an ``IO`` exception if the file ## Default mode is readonly. Raises an ``IO`` exception if the file
## could not be opened. ## could not be opened.
if not open(result, filename, mode, bufSize): if not open(result, filename, mode, bufSize):
sysFatal(EIO, "cannot open: ", filename) sysFatal(IOError, "cannot open: ", filename)
proc reopen*(f: TFile, filename: string, mode: TFileMode = fmRead): bool {. proc reopen*(f: File, filename: string, mode: FileMode = fmRead): bool {.
tags: [], gcsafe.} tags: [], gcsafe.}
## reopens the file `f` with given `filename` and `mode`. This ## reopens the file `f` with given `filename` and `mode`. This
## is often used to redirect the `stdin`, `stdout` or `stderr` ## is often used to redirect the `stdin`, `stdout` or `stderr`
@ -2288,52 +2321,52 @@ when not defined(JS): #and not defined(NimrodVM):
## ##
## Default mode is readonly. Returns true iff the file could be reopened. ## Default mode is readonly. Returns true iff the file could be reopened.
proc close*(f: TFile) {.importc: "fclose", header: "<stdio.h>", tags: [].} proc close*(f: File) {.importc: "fclose", header: "<stdio.h>", tags: [].}
## Closes the file. ## Closes the file.
proc endOfFile*(f: TFile): bool {.tags: [], gcsafe.} proc endOfFile*(f: File): bool {.tags: [], gcsafe.}
## Returns true iff `f` is at the end. ## Returns true iff `f` is at the end.
proc readChar*(f: TFile): char {. proc readChar*(f: File): char {.
importc: "fgetc", header: "<stdio.h>", tags: [FReadIO].} importc: "fgetc", header: "<stdio.h>", tags: [ReadIOEffect].}
## Reads a single character from the stream `f`. ## Reads a single character from the stream `f`.
proc flushFile*(f: TFile) {. proc flushFile*(f: File) {.
importc: "fflush", header: "<stdio.h>", tags: [FWriteIO].} importc: "fflush", header: "<stdio.h>", tags: [WriteIOEffect].}
## Flushes `f`'s buffer. ## Flushes `f`'s buffer.
proc readAll*(file: TFile): TaintedString {.tags: [FReadIO], gcsafe.} proc readAll*(file: File): TaintedString {.tags: [ReadIOEffect], gcsafe.}
## Reads all data from the stream `file`. ## Reads all data from the stream `file`.
## ##
## Raises an IO exception in case of an error. It is an error if the ## Raises an IO exception in case of an error. It is an error if the
## current file position is not at the beginning of the file. ## current file position is not at the beginning of the file.
proc readFile*(filename: string): TaintedString {.tags: [FReadIO], gcsafe.} proc readFile*(filename: string): TaintedString {.tags: [ReadIOEffect], gcsafe.}
## Opens a file named `filename` for reading. Then calls `readAll` ## Opens a file named `filename` for reading. Then calls `readAll`
## and closes the file afterwards. Returns the string. ## and closes the file afterwards. Returns the string.
## Raises an IO exception in case of an error. ## Raises an IO exception in case of an error.
proc writeFile*(filename, content: string) {.tags: [FWriteIO], gcsafe.} proc writeFile*(filename, content: string) {.tags: [WriteIOEffect], gcsafe.}
## Opens a file named `filename` for writing. Then writes the ## Opens a file named `filename` for writing. Then writes the
## `content` completely to the file and closes the file afterwards. ## `content` completely to the file and closes the file afterwards.
## Raises an IO exception in case of an error. ## Raises an IO exception in case of an error.
proc write*(f: TFile, r: float32) {.tags: [FWriteIO], gcsafe.} proc write*(f: File, r: float32) {.tags: [WriteIOEffect], gcsafe.}
proc write*(f: TFile, i: int) {.tags: [FWriteIO], gcsafe.} proc write*(f: File, i: int) {.tags: [WriteIOEffect], gcsafe.}
proc write*(f: TFile, i: BiggestInt) {.tags: [FWriteIO], gcsafe.} proc write*(f: File, i: BiggestInt) {.tags: [WriteIOEffect], gcsafe.}
proc write*(f: TFile, r: BiggestFloat) {.tags: [FWriteIO], gcsafe.} proc write*(f: File, r: BiggestFloat) {.tags: [WriteIOEffect], gcsafe.}
proc write*(f: TFile, s: string) {.tags: [FWriteIO], gcsafe.} proc write*(f: File, s: string) {.tags: [WriteIOEffect], gcsafe.}
proc write*(f: TFile, b: bool) {.tags: [FWriteIO], gcsafe.} proc write*(f: File, b: bool) {.tags: [WriteIOEffect], gcsafe.}
proc write*(f: TFile, c: char) {.tags: [FWriteIO], gcsafe.} proc write*(f: File, c: char) {.tags: [WriteIOEffect], gcsafe.}
proc write*(f: TFile, c: cstring) {.tags: [FWriteIO], gcsafe.} proc write*(f: File, c: cstring) {.tags: [WriteIOEffect], gcsafe.}
proc write*(f: TFile, a: varargs[string, `$`]) {.tags: [FWriteIO], gcsafe.} proc write*(f: File, a: varargs[string, `$`]) {.tags: [WriteIOEffect], gcsafe.}
## Writes a value to the file `f`. May throw an IO exception. ## Writes a value to the file `f`. May throw an IO exception.
proc readLine*(f: TFile): TaintedString {.tags: [FReadIO], gcsafe.} proc readLine*(f: File): TaintedString {.tags: [ReadIOEffect], gcsafe.}
## reads a line of text from the file `f`. May throw an IO exception. ## reads a line of text from the file `f`. May throw an IO exception.
## A line of text may be delimited by ``CR``, ``LF`` or ## A line of text may be delimited by ``CR``, ``LF`` or
## ``CRLF``. The newline character(s) are not part of the returned string. ## ``CRLF``. The newline character(s) are not part of the returned string.
proc readLine*(f: TFile, line: var TaintedString): bool {.tags: [FReadIO], proc readLine*(f: File, line: var TaintedString): bool {.tags: [ReadIOEffect],
gcsafe.} gcsafe.}
## reads a line of text from the file `f` into `line`. `line` must not be ## reads a line of text from the file `f` into `line`. `line` must not be
## ``nil``! May throw an IO exception. ## ``nil``! May throw an IO exception.
@ -2343,63 +2376,63 @@ when not defined(JS): #and not defined(NimrodVM):
## otherwise. If ``false`` is returned `line` contains no new data. ## otherwise. If ``false`` is returned `line` contains no new data.
when not defined(booting): when not defined(booting):
proc writeln*[Ty](f: TFile, x: varargs[Ty, `$`]) {.inline, proc writeln*[Ty](f: File, x: varargs[Ty, `$`]) {.inline,
tags: [FWriteIO], gcsafe.} tags: [WriteIOEffect], gcsafe.}
## writes the values `x` to `f` and then writes "\n". ## writes the values `x` to `f` and then writes "\n".
## May throw an IO exception. ## May throw an IO exception.
else: else:
proc writeln*[Ty](f: TFile, x: varargs[Ty, `$`]) {.inline, proc writeln*[Ty](f: File, x: varargs[Ty, `$`]) {.inline,
tags: [FWriteIO].} tags: [WriteIOEffect].}
proc getFileSize*(f: TFile): int64 {.tags: [FReadIO], gcsafe.} proc getFileSize*(f: File): int64 {.tags: [ReadIOEffect], gcsafe.}
## retrieves the file size (in bytes) of `f`. ## retrieves the file size (in bytes) of `f`.
proc readBytes*(f: TFile, a: var openArray[int8], start, len: int): int {. proc readBytes*(f: File, a: var openArray[int8], start, len: int): int {.
tags: [FReadIO], gcsafe.} tags: [ReadIOEffect], gcsafe.}
## reads `len` bytes into the buffer `a` starting at ``a[start]``. Returns ## reads `len` bytes into the buffer `a` starting at ``a[start]``. Returns
## the actual number of bytes that have been read which may be less than ## the actual number of bytes that have been read which may be less than
## `len` (if not as many bytes are remaining), but not greater. ## `len` (if not as many bytes are remaining), but not greater.
proc readChars*(f: TFile, a: var openArray[char], start, len: int): int {. proc readChars*(f: File, a: var openArray[char], start, len: int): int {.
tags: [FReadIO], gcsafe.} tags: [ReadIOEffect], gcsafe.}
## reads `len` bytes into the buffer `a` starting at ``a[start]``. Returns ## reads `len` bytes into the buffer `a` starting at ``a[start]``. Returns
## the actual number of bytes that have been read which may be less than ## the actual number of bytes that have been read which may be less than
## `len` (if not as many bytes are remaining), but not greater. ## `len` (if not as many bytes are remaining), but not greater.
proc readBuffer*(f: TFile, buffer: pointer, len: int): int {. proc readBuffer*(f: File, buffer: pointer, len: int): int {.
tags: [FReadIO], gcsafe.} tags: [ReadIOEffect], gcsafe.}
## reads `len` bytes into the buffer pointed to by `buffer`. Returns ## reads `len` bytes into the buffer pointed to by `buffer`. Returns
## the actual number of bytes that have been read which may be less than ## the actual number of bytes that have been read which may be less than
## `len` (if not as many bytes are remaining), but not greater. ## `len` (if not as many bytes are remaining), but not greater.
proc writeBytes*(f: TFile, a: openArray[int8], start, len: int): int {. proc writeBytes*(f: File, a: openArray[int8], start, len: int): int {.
tags: [FWriteIO], gcsafe.} tags: [WriteIOEffect], gcsafe.}
## writes the bytes of ``a[start..start+len-1]`` to the file `f`. Returns ## writes the bytes of ``a[start..start+len-1]`` to the file `f`. Returns
## the number of actual written bytes, which may be less than `len` in case ## the number of actual written bytes, which may be less than `len` in case
## of an error. ## of an error.
proc writeChars*(f: TFile, a: openArray[char], start, len: int): int {. proc writeChars*(f: File, a: openArray[char], start, len: int): int {.
tags: [FWriteIO], gcsafe.} tags: [WriteIOEffect], gcsafe.}
## writes the bytes of ``a[start..start+len-1]`` to the file `f`. Returns ## writes the bytes of ``a[start..start+len-1]`` to the file `f`. Returns
## the number of actual written bytes, which may be less than `len` in case ## the number of actual written bytes, which may be less than `len` in case
## of an error. ## of an error.
proc writeBuffer*(f: TFile, buffer: pointer, len: int): int {. proc writeBuffer*(f: File, buffer: pointer, len: int): int {.
tags: [FWriteIO], gcsafe.} tags: [WriteIOEffect], gcsafe.}
## writes the bytes of buffer pointed to by the parameter `buffer` to the ## writes the bytes of buffer pointed to by the parameter `buffer` to the
## file `f`. Returns the number of actual written bytes, which may be less ## file `f`. Returns the number of actual written bytes, which may be less
## than `len` in case of an error. ## than `len` in case of an error.
proc setFilePos*(f: TFile, pos: int64) {.gcsafe.} proc setFilePos*(f: File, pos: int64) {.gcsafe.}
## sets the position of the file pointer that is used for read/write ## sets the position of the file pointer that is used for read/write
## operations. The file's first byte has the index zero. ## operations. The file's first byte has the index zero.
proc getFilePos*(f: TFile): int64 {.gcsafe.} proc getFilePos*(f: File): int64 {.gcsafe.}
## retrieves the current position of the file pointer that is used to ## retrieves the current position of the file pointer that is used to
## read from the file `f`. The file's first byte has the index zero. ## read from the file `f`. The file's first byte has the index zero.
proc fileHandle*(f: TFile): TFileHandle {.importc: "fileno", proc getFileHandle*(f: File): FileHandle {.importc: "fileno",
header: "<stdio.h>"} header: "<stdio.h>"}
## returns the OS file handle of the file ``f``. This is only useful for ## returns the OS file handle of the file ``f``. This is only useful for
## platform specific programming. ## platform specific programming.
@ -2454,7 +2487,7 @@ when not defined(JS): #and not defined(NimrodVM):
status: int status: int
context: C_JmpBuf context: C_JmpBuf
hasRaiseAction: bool hasRaiseAction: bool
raiseAction: proc (e: ref E_Base): bool {.closure.} raiseAction: proc (e: ref Exception): bool {.closure.}
when declared(initAllocator): when declared(initAllocator):
initAllocator() initAllocator()
@ -2470,14 +2503,14 @@ when not defined(JS): #and not defined(NimrodVM):
## allows you to override the behaviour of your application when CTRL+C ## allows you to override the behaviour of your application when CTRL+C
## is pressed. Only one such hook is supported. ## is pressed. Only one such hook is supported.
proc writeStackTrace*() {.tags: [FWriteIO].} proc writeStackTrace*() {.tags: [WriteIOEffect].}
## writes the current stack trace to ``stderr``. This is only works ## writes the current stack trace to ``stderr``. This is only works
## for debug builds. ## for debug builds.
when hostOS != "standalone": when hostOS != "standalone":
proc getStackTrace*(): string proc getStackTrace*(): string
## gets the current stack trace. This only works for debug builds. ## gets the current stack trace. This only works for debug builds.
proc getStackTrace*(e: ref E_Base): string proc getStackTrace*(e: ref Exception): string
## gets the stack trace associated with `e`, which is the stack that ## gets the stack trace associated with `e`, which is the stack that
## lead to the ``raise`` statement. This only works for debug builds. ## lead to the ``raise`` statement. This only works for debug builds.
@ -2505,7 +2538,7 @@ when not defined(JS): #and not defined(NimrodVM):
proc getDiscriminant(aa: pointer, n: ptr TNimNode): int = proc getDiscriminant(aa: pointer, n: ptr TNimNode): int =
sysAssert(n.kind == nkCase, "getDiscriminant: node != nkCase") sysAssert(n.kind == nkCase, "getDiscriminant: node != nkCase")
var d: int var d: int
var a = cast[TAddress](aa) var a = cast[ByteAddress](aa)
case n.typ.size case n.typ.size
of 1: d = ze(cast[ptr int8](a +% n.offset)[]) of 1: d = ze(cast[ptr int8](a +% n.offset)[])
of 2: d = ze(cast[ptr int16](a +% n.offset)[]) of 2: d = ze(cast[ptr int16](a +% n.offset)[])
@ -2534,7 +2567,7 @@ when not defined(JS): #and not defined(NimrodVM):
include "system/sysio" include "system/sysio"
when hostOS != "standalone": when hostOS != "standalone":
iterator lines*(filename: string): TaintedString {.tags: [FReadIO].} = iterator lines*(filename: string): TaintedString {.tags: [ReadIOEffect].} =
## Iterates over any line in the file named `filename`. ## Iterates over any line in the file named `filename`.
## ##
## If the file does not exist `EIO` is raised. The trailing newline ## If the file does not exist `EIO` is raised. The trailing newline
@ -2553,14 +2586,14 @@ when not defined(JS): #and not defined(NimrodVM):
while f.readLine(res): yield res while f.readLine(res): yield res
close(f) close(f)
iterator lines*(f: TFile): TaintedString {.tags: [FReadIO].} = iterator lines*(f: File): TaintedString {.tags: [ReadIOEffect].} =
## Iterate over any line in the file `f`. ## Iterate over any line in the file `f`.
## ##
## The trailing newline character(s) are removed from the iterated lines. ## The trailing newline character(s) are removed from the iterated lines.
## Example: ## Example:
## ##
## .. code-block:: nimrod ## .. code-block:: nimrod
## proc countZeros(filename: TFile): tuple[lines, zeros: int] = ## proc countZeros(filename: File): tuple[lines, zeros: int] =
## for line in filename.lines: ## for line in filename.lines:
## for letter in line: ## for letter in line:
## if letter == '0': ## if letter == '0':
@ -2573,7 +2606,7 @@ when not defined(JS): #and not defined(NimrodVM):
include "system/assign" include "system/assign"
include "system/repr" include "system/repr"
proc getCurrentException*(): ref E_Base {.compilerRtl, inl, gcsafe.} = proc getCurrentException*(): ref Exception {.compilerRtl, inl, gcsafe.} =
## retrieves the current exception; if there is none, nil is returned. ## retrieves the current exception; if there is none, nil is returned.
result = currException result = currException
@ -2583,7 +2616,7 @@ when not defined(JS): #and not defined(NimrodVM):
var e = getCurrentException() var e = getCurrentException()
return if e == nil: "" else: e.msg return if e == nil: "" else: e.msg
proc onRaise*(action: proc(e: ref E_Base): bool{.closure.}) = proc onRaise*(action: proc(e: ref Exception): bool{.closure.}) =
## can be used in a ``try`` statement to setup a Lisp-like ## can be used in a ``try`` statement to setup a Lisp-like
## `condition system`:idx:\: This prevents the 'raise' statement to ## `condition system`:idx:\: This prevents the 'raise' statement to
## raise an exception but instead calls ``action``. ## raise an exception but instead calls ``action``.
@ -2721,11 +2754,11 @@ template spliceImpl(s, a, L, b: expr): stmt {.immediate.} =
for i in 0 .. <b.len: s[i+a] = b[i] for i in 0 .. <b.len: s[i+a] = b[i]
when hostOS != "standalone": when hostOS != "standalone":
proc `[]`*(s: string, x: TSlice[int]): string {.inline.} = proc `[]`*(s: string, x: Slice[int]): string {.inline.} =
## slice operation for strings. Negative indexes are supported. ## slice operation for strings. Negative indexes are supported.
result = s.substr(x.a-|s, x.b-|s) result = s.substr(x.a-|s, x.b-|s)
proc `[]=`*(s: var string, x: TSlice[int], b: string) = proc `[]=`*(s: var string, x: Slice[int], b: string) =
## slice assignment for strings. Negative indexes are supported. If ## slice assignment for strings. Negative indexes are supported. If
## ``b.len`` is not exactly the number of elements that are referred to ## ``b.len`` is not exactly the number of elements that are referred to
## by `x`, a `splice`:idx: is performed: ## by `x`, a `splice`:idx: is performed:
@ -2741,7 +2774,7 @@ when hostOS != "standalone":
else: else:
spliceImpl(s, a, L, b) spliceImpl(s, a, L, b)
proc `[]`*[Idx, T](a: array[Idx, T], x: TSlice[int]): seq[T] = proc `[]`*[Idx, T](a: array[Idx, T], x: Slice[int]): seq[T] =
## slice operation for arrays. Negative indexes are **not** supported ## slice operation for arrays. Negative indexes are **not** supported
## because the array might have negative bounds. ## because the array might have negative bounds.
when low(a) < 0: when low(a) < 0:
@ -2750,7 +2783,7 @@ proc `[]`*[Idx, T](a: array[Idx, T], x: TSlice[int]): seq[T] =
newSeq(result, L) newSeq(result, L)
for i in 0.. <L: result[i] = a[i + x.a] for i in 0.. <L: result[i] = a[i + x.a]
proc `[]=`*[Idx, T](a: var array[Idx, T], x: TSlice[int], b: openArray[T]) = proc `[]=`*[Idx, T](a: var array[Idx, T], x: Slice[int], b: openArray[T]) =
## slice assignment for arrays. Negative indexes are **not** supported ## slice assignment for arrays. Negative indexes are **not** supported
## because the array might have negative bounds. ## because the array might have negative bounds.
when low(a) < 0: when low(a) < 0:
@ -2759,9 +2792,9 @@ proc `[]=`*[Idx, T](a: var array[Idx, T], x: TSlice[int], b: openArray[T]) =
if L == b.len: if L == b.len:
for i in 0 .. <L: a[i+x.a] = b[i] for i in 0 .. <L: a[i+x.a] = b[i]
else: else:
sysFatal(EOutOfRange, "different lengths for slice assignment") sysFatal(RangeError, "different lengths for slice assignment")
proc `[]`*[Idx, T](a: array[Idx, T], x: TSlice[Idx]): seq[T] = proc `[]`*[Idx, T](a: array[Idx, T], x: Slice[Idx]): seq[T] =
## slice operation for arrays. Negative indexes are **not** supported ## slice operation for arrays. Negative indexes are **not** supported
## because the array might have negative bounds. ## because the array might have negative bounds.
var L = ord(x.b) - ord(x.a) + 1 var L = ord(x.b) - ord(x.a) + 1
@ -2771,7 +2804,7 @@ proc `[]`*[Idx, T](a: array[Idx, T], x: TSlice[Idx]): seq[T] =
result[i] = a[j] result[i] = a[j]
inc(j) inc(j)
proc `[]=`*[Idx, T](a: var array[Idx, T], x: TSlice[Idx], b: openArray[T]) = proc `[]=`*[Idx, T](a: var array[Idx, T], x: Slice[Idx], b: openArray[T]) =
## slice assignment for arrays. Negative indexes are **not** supported ## slice assignment for arrays. Negative indexes are **not** supported
## because the array might have negative bounds. ## because the array might have negative bounds.
var L = ord(x.b) - ord(x.a) + 1 var L = ord(x.b) - ord(x.a) + 1
@ -2781,16 +2814,16 @@ proc `[]=`*[Idx, T](a: var array[Idx, T], x: TSlice[Idx], b: openArray[T]) =
a[j] = b[i] a[j] = b[i]
inc(j) inc(j)
else: else:
sysFatal(EOutOfRange, "different lengths for slice assignment") sysFatal(RangeError, "different lengths for slice assignment")
proc `[]`*[T](s: seq[T], x: TSlice[int]): seq[T] = proc `[]`*[T](s: seq[T], x: Slice[int]): seq[T] =
## slice operation for sequences. Negative indexes are supported. ## slice operation for sequences. Negative indexes are supported.
var a = x.a-|s var a = x.a-|s
var L = x.b-|s - a + 1 var L = x.b-|s - a + 1
newSeq(result, L) newSeq(result, L)
for i in 0.. <L: result[i] = s[i + a] for i in 0.. <L: result[i] = s[i + a]
proc `[]=`*[T](s: var seq[T], x: TSlice[int], b: openArray[T]) = proc `[]=`*[T](s: var seq[T], x: Slice[int], b: openArray[T]) =
## slice assignment for sequences. Negative indexes are supported. If ## slice assignment for sequences. Negative indexes are supported. If
## ``b.len`` is not exactly the number of elements that are referred to ## ``b.len`` is not exactly the number of elements that are referred to
## by `x`, a `splice`:idx: is performed. ## by `x`, a `splice`:idx: is performed.
@ -2830,13 +2863,13 @@ proc staticExec*(command: string, input = ""): string {.
## inside a pragma like `passC <nimrodc.html#passc-pragma>`_ or `passL ## inside a pragma like `passC <nimrodc.html#passc-pragma>`_ or `passL
## <nimrodc.html#passl-pragma>`_. ## <nimrodc.html#passl-pragma>`_.
proc `+=`*[T: TOrdinal|uint|uint64](x: var T, y: T) {.magic: "Inc", noSideEffect.} proc `+=`*[T: SomeOrdinal|uint|uint64](x: var T, y: T) {.magic: "Inc", noSideEffect.}
## Increments an ordinal ## Increments an ordinal
proc `-=`*[T: TOrdinal|uint|uint64](x: var T, y: T) {.magic: "Dec", noSideEffect.} proc `-=`*[T: SomeOrdinal|uint|uint64](x: var T, y: T) {.magic: "Dec", noSideEffect.}
## Decrements an ordinal ## Decrements an ordinal
proc `*=`*[T: TOrdinal|uint|uint64](x: var T, y: T) {.inline, noSideEffect.} = proc `*=`*[T: SomeOrdinal|uint|uint64](x: var T, y: T) {.inline, noSideEffect.} =
## Binary `*=` operator for ordinals ## Binary `*=` operator for ordinals
x = x * y x = x * y
@ -2897,7 +2930,7 @@ template currentSourcePath*: string = instantiationInfo(-1, true).filename
## returns the full file-system path of the current source ## returns the full file-system path of the current source
proc raiseAssert*(msg: string) {.noinline.} = proc raiseAssert*(msg: string) {.noinline.} =
sysFatal(EAssertionFailed, msg) sysFatal(AssertionError, msg)
when true: when true:
proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} = proc failedAssertImpl*(msg: string) {.raises: [], tags: [].} =
@ -3046,7 +3079,7 @@ when hostOS != "standalone":
if x == nil: x = y if x == nil: x = y
else: x.add(y) else: x.add(y)
proc locals*(): TObject {.magic: "Locals", noSideEffect.} = proc locals*(): RootObj {.magic: "Locals", noSideEffect.} =
## generates a tuple constructor expression listing all the local variables ## generates a tuple constructor expression listing all the local variables
## in the current scope. This is quite fast as it does not rely ## in the current scope. This is quite fast as it does not rely
## on any debug or runtime information. Note that in constrast to what ## on any debug or runtime information. Note that in constrast to what

View file

@ -218,7 +218,7 @@ proc llAlloc(a: var TMemRegion, size: int): pointer =
a.llmem.size = PageSize - sizeof(TLLChunk) a.llmem.size = PageSize - sizeof(TLLChunk)
a.llmem.acc = sizeof(TLLChunk) a.llmem.acc = sizeof(TLLChunk)
a.llmem.next = old a.llmem.next = old
result = cast[pointer](cast[TAddress](a.llmem) + a.llmem.acc) result = cast[pointer](cast[ByteAddress](a.llmem) + a.llmem.acc)
dec(a.llmem.size, size) dec(a.llmem.size, size)
inc(a.llmem.acc, size) inc(a.llmem.acc, size)
zeroMem(result, size) zeroMem(result, size)
@ -321,7 +321,7 @@ iterator allObjects(m: TMemRegion): pointer {.inline.} =
var c = cast[PSmallChunk](c) var c = cast[PSmallChunk](c)
let size = c.size let size = c.size
var a = cast[TAddress](addr(c.data)) var a = cast[ByteAddress](addr(c.data))
let limit = a + c.acc let limit = a + c.acc
while a <% limit: while a <% limit:
yield cast[pointer](a) yield cast[pointer](a)
@ -335,27 +335,27 @@ proc isCell(p: pointer): bool {.inline.} =
# ------------- chunk management ---------------------------------------------- # ------------- chunk management ----------------------------------------------
proc pageIndex(c: PChunk): int {.inline.} = proc pageIndex(c: PChunk): int {.inline.} =
result = cast[TAddress](c) shr PageShift result = cast[ByteAddress](c) shr PageShift
proc pageIndex(p: pointer): int {.inline.} = proc pageIndex(p: pointer): int {.inline.} =
result = cast[TAddress](p) shr PageShift result = cast[ByteAddress](p) shr PageShift
proc pageAddr(p: pointer): PChunk {.inline.} = proc pageAddr(p: pointer): PChunk {.inline.} =
result = cast[PChunk](cast[TAddress](p) and not PageMask) result = cast[PChunk](cast[ByteAddress](p) and not PageMask)
#sysAssert(Contains(allocator.chunkStarts, pageIndex(result))) #sysAssert(Contains(allocator.chunkStarts, pageIndex(result)))
proc requestOsChunks(a: var TMemRegion, size: int): PBigChunk = proc requestOsChunks(a: var TMemRegion, size: int): PBigChunk =
incCurrMem(a, size) incCurrMem(a, size)
inc(a.freeMem, size) inc(a.freeMem, size)
result = cast[PBigChunk](osAllocPages(size)) result = cast[PBigChunk](osAllocPages(size))
sysAssert((cast[TAddress](result) and PageMask) == 0, "requestOsChunks 1") sysAssert((cast[ByteAddress](result) and PageMask) == 0, "requestOsChunks 1")
#zeroMem(result, size) #zeroMem(result, size)
result.next = nil result.next = nil
result.prev = nil result.prev = nil
result.used = false result.used = false
result.size = size result.size = size
# update next.prevSize: # update next.prevSize:
var nxt = cast[TAddress](result) +% size var nxt = cast[ByteAddress](result) +% size
sysAssert((nxt and PageMask) == 0, "requestOsChunks 2") sysAssert((nxt and PageMask) == 0, "requestOsChunks 2")
var next = cast[PChunk](nxt) var next = cast[PChunk](nxt)
if pageIndex(next) in a.chunkStarts: if pageIndex(next) in a.chunkStarts:
@ -363,7 +363,7 @@ proc requestOsChunks(a: var TMemRegion, size: int): PBigChunk =
next.prevSize = size next.prevSize = size
# set result.prevSize: # set result.prevSize:
var lastSize = if a.lastSize != 0: a.lastSize else: PageSize var lastSize = if a.lastSize != 0: a.lastSize else: PageSize
var prv = cast[TAddress](result) -% lastSize var prv = cast[ByteAddress](result) -% lastSize
sysAssert((nxt and PageMask) == 0, "requestOsChunks 3") sysAssert((nxt and PageMask) == 0, "requestOsChunks 3")
var prev = cast[PChunk](prv) var prev = cast[PChunk](prv)
if pageIndex(prev) in a.chunkStarts and prev.size == lastSize: if pageIndex(prev) in a.chunkStarts and prev.size == lastSize:
@ -376,7 +376,7 @@ proc requestOsChunks(a: var TMemRegion, size: int): PBigChunk =
proc freeOsChunks(a: var TMemRegion, p: pointer, size: int) = proc freeOsChunks(a: var TMemRegion, p: pointer, size: int) =
# update next.prevSize: # update next.prevSize:
var c = cast[PChunk](p) var c = cast[PChunk](p)
var nxt = cast[TAddress](p) +% c.size var nxt = cast[ByteAddress](p) +% c.size
sysAssert((nxt and PageMask) == 0, "freeOsChunks") sysAssert((nxt and PageMask) == 0, "freeOsChunks")
var next = cast[PChunk](nxt) var next = cast[PChunk](nxt)
if pageIndex(next) in a.chunkStarts: if pageIndex(next) in a.chunkStarts:
@ -429,8 +429,8 @@ proc listRemove[T](head: var T, c: T) {.inline.} =
proc updatePrevSize(a: var TMemRegion, c: PBigChunk, proc updatePrevSize(a: var TMemRegion, c: PBigChunk,
prevSize: int) {.inline.} = prevSize: int) {.inline.} =
var ri = cast[PChunk](cast[TAddress](c) +% c.size) var ri = cast[PChunk](cast[ByteAddress](c) +% c.size)
sysAssert((cast[TAddress](ri) and PageMask) == 0, "updatePrevSize") sysAssert((cast[ByteAddress](ri) and PageMask) == 0, "updatePrevSize")
if isAccessible(a, ri): if isAccessible(a, ri):
ri.prevSize = prevSize ri.prevSize = prevSize
@ -439,8 +439,8 @@ proc freeBigChunk(a: var TMemRegion, c: PBigChunk) =
sysAssert(c.size >= PageSize, "freeBigChunk") sysAssert(c.size >= PageSize, "freeBigChunk")
inc(a.freeMem, c.size) inc(a.freeMem, c.size)
when coalescRight: when coalescRight:
var ri = cast[PChunk](cast[TAddress](c) +% c.size) var ri = cast[PChunk](cast[ByteAddress](c) +% c.size)
sysAssert((cast[TAddress](ri) and PageMask) == 0, "freeBigChunk 2") sysAssert((cast[ByteAddress](ri) and PageMask) == 0, "freeBigChunk 2")
if isAccessible(a, ri) and chunkUnused(ri): if isAccessible(a, ri) and chunkUnused(ri):
sysAssert(not isSmallChunk(ri), "freeBigChunk 3") sysAssert(not isSmallChunk(ri), "freeBigChunk 3")
if not isSmallChunk(ri): if not isSmallChunk(ri):
@ -449,8 +449,8 @@ proc freeBigChunk(a: var TMemRegion, c: PBigChunk) =
excl(a.chunkStarts, pageIndex(ri)) excl(a.chunkStarts, pageIndex(ri))
when coalescLeft: when coalescLeft:
if c.prevSize != 0: if c.prevSize != 0:
var le = cast[PChunk](cast[TAddress](c) -% c.prevSize) var le = cast[PChunk](cast[ByteAddress](c) -% c.prevSize)
sysAssert((cast[TAddress](le) and PageMask) == 0, "freeBigChunk 4") sysAssert((cast[ByteAddress](le) and PageMask) == 0, "freeBigChunk 4")
if isAccessible(a, le) and chunkUnused(le): if isAccessible(a, le) and chunkUnused(le):
sysAssert(not isSmallChunk(le), "freeBigChunk 5") sysAssert(not isSmallChunk(le), "freeBigChunk 5")
if not isSmallChunk(le): if not isSmallChunk(le):
@ -468,7 +468,7 @@ proc freeBigChunk(a: var TMemRegion, c: PBigChunk) =
freeOsChunks(a, c, c.size) freeOsChunks(a, c, c.size)
proc splitChunk(a: var TMemRegion, c: PBigChunk, size: int) = proc splitChunk(a: var TMemRegion, c: PBigChunk, size: int) =
var rest = cast[PBigChunk](cast[TAddress](c) +% size) var rest = cast[PBigChunk](cast[ByteAddress](c) +% size)
sysAssert(rest notin a.freeChunksList, "splitChunk") sysAssert(rest notin a.freeChunksList, "splitChunk")
rest.size = c.size - size rest.size = c.size - size
rest.used = false rest.used = false
@ -559,7 +559,7 @@ proc rawAlloc(a: var TMemRegion, requestedSize: int): pointer =
c.prev = nil c.prev = nil
listAdd(a.freeSmallChunks[s], c) listAdd(a.freeSmallChunks[s], c)
result = addr(c.data) result = addr(c.data)
sysAssert((cast[TAddress](result) and (MemAlign-1)) == 0, "rawAlloc 4") sysAssert((cast[ByteAddress](result) and (MemAlign-1)) == 0, "rawAlloc 4")
else: else:
sysAssert(allocInv(a), "rawAlloc: begin c != nil") sysAssert(allocInv(a), "rawAlloc: begin c != nil")
sysAssert c.next != c, "rawAlloc 5" sysAssert c.next != c, "rawAlloc 5"
@ -569,21 +569,21 @@ proc rawAlloc(a: var TMemRegion, requestedSize: int): pointer =
if c.freeList == nil: if c.freeList == nil:
sysAssert(c.acc + smallChunkOverhead() + size <= SmallChunkSize, sysAssert(c.acc + smallChunkOverhead() + size <= SmallChunkSize,
"rawAlloc 7") "rawAlloc 7")
result = cast[pointer](cast[TAddress](addr(c.data)) +% c.acc) result = cast[pointer](cast[ByteAddress](addr(c.data)) +% c.acc)
inc(c.acc, size) inc(c.acc, size)
else: else:
result = c.freeList result = c.freeList
sysAssert(c.freeList.zeroField == 0, "rawAlloc 8") sysAssert(c.freeList.zeroField == 0, "rawAlloc 8")
c.freeList = c.freeList.next c.freeList = c.freeList.next
dec(c.free, size) dec(c.free, size)
sysAssert((cast[TAddress](result) and (MemAlign-1)) == 0, "rawAlloc 9") sysAssert((cast[ByteAddress](result) and (MemAlign-1)) == 0, "rawAlloc 9")
sysAssert(allocInv(a), "rawAlloc: end c != nil") sysAssert(allocInv(a), "rawAlloc: end c != nil")
sysAssert(allocInv(a), "rawAlloc: before c.free < size") sysAssert(allocInv(a), "rawAlloc: before c.free < size")
if c.free < size: if c.free < size:
sysAssert(allocInv(a), "rawAlloc: before listRemove test") sysAssert(allocInv(a), "rawAlloc: before listRemove test")
listRemove(a.freeSmallChunks[s], c) listRemove(a.freeSmallChunks[s], c)
sysAssert(allocInv(a), "rawAlloc: end listRemove test") sysAssert(allocInv(a), "rawAlloc: end listRemove test")
sysAssert(((cast[TAddress](result) and PageMask) - smallChunkOverhead()) %% sysAssert(((cast[ByteAddress](result) and PageMask) - smallChunkOverhead()) %%
size == 0, "rawAlloc 21") size == 0, "rawAlloc 21")
sysAssert(allocInv(a), "rawAlloc: end small size") sysAssert(allocInv(a), "rawAlloc: end small size")
else: else:
@ -594,9 +594,9 @@ proc rawAlloc(a: var TMemRegion, requestedSize: int): pointer =
sysAssert c.next == nil, "rawAlloc 11" sysAssert c.next == nil, "rawAlloc 11"
sysAssert c.size == size, "rawAlloc 12" sysAssert c.size == size, "rawAlloc 12"
result = addr(c.data) result = addr(c.data)
sysAssert((cast[TAddress](result) and (MemAlign-1)) == 0, "rawAlloc 13") sysAssert((cast[ByteAddress](result) and (MemAlign-1)) == 0, "rawAlloc 13")
if a.root == nil: a.root = bottom if a.root == nil: a.root = bottom
add(a, a.root, cast[TAddress](result), cast[TAddress](result)+%size) add(a, a.root, cast[ByteAddress](result), cast[TAddress](result)+%size)
sysAssert(isAccessible(a, result), "rawAlloc 14") sysAssert(isAccessible(a, result), "rawAlloc 14")
sysAssert(allocInv(a), "rawAlloc: end") sysAssert(allocInv(a), "rawAlloc: end")
when logAlloc: cprintf("rawAlloc: %ld %p\n", requestedSize, result) when logAlloc: cprintf("rawAlloc: %ld %p\n", requestedSize, result)
@ -613,7 +613,7 @@ proc rawDealloc(a: var TMemRegion, p: pointer) =
# `p` is within a small chunk: # `p` is within a small chunk:
var c = cast[PSmallChunk](c) var c = cast[PSmallChunk](c)
var s = c.size var s = c.size
sysAssert(((cast[TAddress](p) and PageMask) - smallChunkOverhead()) %% sysAssert(((cast[ByteAddress](p) and PageMask) - smallChunkOverhead()) %%
s == 0, "rawDealloc 3") s == 0, "rawDealloc 3")
var f = cast[ptr TFreeCell](p) var f = cast[ptr TFreeCell](p)
#echo("setting to nil: ", $cast[TAddress](addr(f.zeroField))) #echo("setting to nil: ", $cast[TAddress](addr(f.zeroField)))
@ -636,7 +636,7 @@ proc rawDealloc(a: var TMemRegion, p: pointer) =
listRemove(a.freeSmallChunks[s div MemAlign], c) listRemove(a.freeSmallChunks[s div MemAlign], c)
c.size = SmallChunkSize c.size = SmallChunkSize
freeBigChunk(a, cast[PBigChunk](c)) freeBigChunk(a, cast[PBigChunk](c))
sysAssert(((cast[TAddress](p) and PageMask) - smallChunkOverhead()) %% sysAssert(((cast[ByteAddress](p) and PageMask) - smallChunkOverhead()) %%
s == 0, "rawDealloc 2") s == 0, "rawDealloc 2")
else: else:
# set to 0xff to check for usage after free bugs: # set to 0xff to check for usage after free bugs:
@ -655,7 +655,7 @@ proc isAllocatedPtr(a: TMemRegion, p: pointer): bool =
if not chunkUnused(c): if not chunkUnused(c):
if isSmallChunk(c): if isSmallChunk(c):
var c = cast[PSmallChunk](c) var c = cast[PSmallChunk](c)
var offset = (cast[TAddress](p) and (PageSize-1)) -% var offset = (cast[ByteAddress](p) and (PageSize-1)) -%
smallChunkOverhead() smallChunkOverhead()
result = (c.acc >% offset) and (offset %% c.size == 0) and result = (c.acc >% offset) and (offset %% c.size == 0) and
(cast[ptr TFreeCell](p).zeroField >% 1) (cast[ptr TFreeCell](p).zeroField >% 1)
@ -673,12 +673,12 @@ proc interiorAllocatedPtr(a: TMemRegion, p: pointer): pointer =
if not chunkUnused(c): if not chunkUnused(c):
if isSmallChunk(c): if isSmallChunk(c):
var c = cast[PSmallChunk](c) var c = cast[PSmallChunk](c)
var offset = (cast[TAddress](p) and (PageSize-1)) -% var offset = (cast[ByteAddress](p) and (PageSize-1)) -%
smallChunkOverhead() smallChunkOverhead()
if c.acc >% offset: if c.acc >% offset:
sysAssert(cast[TAddress](addr(c.data)) +% offset == sysAssert(cast[ByteAddress](addr(c.data)) +% offset ==
cast[TAddress](p), "offset is not what you think it is") cast[ByteAddress](p), "offset is not what you think it is")
var d = cast[ptr TFreeCell](cast[TAddress](addr(c.data)) +% var d = cast[ptr TFreeCell](cast[ByteAddress](addr(c.data)) +%
offset -% (offset %% c.size)) offset -% (offset %% c.size))
if d.zeroField >% 1: if d.zeroField >% 1:
result = d result = d
@ -704,7 +704,7 @@ proc interiorAllocatedPtr(a: TMemRegion, p: pointer): pointer =
sysAssert isAllocatedPtr(a, result), " result wrong pointer!" sysAssert isAllocatedPtr(a, result), " result wrong pointer!"
proc ptrSize(p: pointer): int = proc ptrSize(p: pointer): int =
var x = cast[pointer](cast[TAddress](p) -% sizeof(TFreeCell)) var x = cast[pointer](cast[ByteAddress](p) -% sizeof(TFreeCell))
var c = pageAddr(p) var c = pageAddr(p)
sysAssert(not chunkUnused(c), "ptrSize") sysAssert(not chunkUnused(c), "ptrSize")
result = c.size -% sizeof(TFreeCell) result = c.size -% sizeof(TFreeCell)
@ -715,7 +715,7 @@ proc alloc(allocator: var TMemRegion, size: int): pointer =
result = rawAlloc(allocator, size+sizeof(TFreeCell)) result = rawAlloc(allocator, size+sizeof(TFreeCell))
cast[ptr TFreeCell](result).zeroField = 1 # mark it as used cast[ptr TFreeCell](result).zeroField = 1 # mark it as used
sysAssert(not isAllocatedPtr(allocator, result), "alloc") sysAssert(not isAllocatedPtr(allocator, result), "alloc")
result = cast[pointer](cast[TAddress](result) +% sizeof(TFreeCell)) result = cast[pointer](cast[ByteAddress](result) +% sizeof(TFreeCell))
proc alloc0(allocator: var TMemRegion, size: int): pointer = proc alloc0(allocator: var TMemRegion, size: int): pointer =
result = alloc(allocator, size) result = alloc(allocator, size)
@ -723,7 +723,7 @@ proc alloc0(allocator: var TMemRegion, size: int): pointer =
proc dealloc(allocator: var TMemRegion, p: pointer) = proc dealloc(allocator: var TMemRegion, p: pointer) =
sysAssert(p != nil, "dealloc 0") sysAssert(p != nil, "dealloc 0")
var x = cast[pointer](cast[TAddress](p) -% sizeof(TFreeCell)) var x = cast[pointer](cast[ByteAddress](p) -% sizeof(TFreeCell))
sysAssert(x != nil, "dealloc 1") sysAssert(x != nil, "dealloc 1")
sysAssert(isAccessible(allocator, x), "is not accessible") sysAssert(isAccessible(allocator, x), "is not accessible")
sysAssert(cast[ptr TFreeCell](x).zeroField == 1, "dealloc 2") sysAssert(cast[ptr TFreeCell](x).zeroField == 1, "dealloc 2")
@ -769,7 +769,7 @@ template instantiateForRegion(allocator: expr) =
result = interiorAllocatedPtr(allocator, p) result = interiorAllocatedPtr(allocator, p)
proc isAllocatedPtr*(p: pointer): bool = proc isAllocatedPtr*(p: pointer): bool =
let p = cast[pointer](cast[TAddress](p)-%TAddress(sizeof(TCell))) let p = cast[pointer](cast[ByteAddress](p)-%TAddress(sizeof(TCell)))
result = isAllocatedPtr(allocator, p) result = isAllocatedPtr(allocator, p)
proc deallocOsPages = deallocOsPages(allocator) proc deallocOsPages = deallocOsPages(allocator)
@ -784,7 +784,7 @@ template instantiateForRegion(allocator: expr) =
dealloc(allocator, p) dealloc(allocator, p)
proc realloc(p: pointer, newsize: int): pointer = proc realloc(p: pointer, newsize: int): pointer =
result = realloc(allocator, p, newsize) result = realloc(allocator, p, newSize)
when false: when false:
proc countFreeMem(): int = proc countFreeMem(): int =
@ -833,7 +833,7 @@ template instantiateForRegion(allocator: expr) =
result = realloc(sharedHeap, p, newsize) result = realloc(sharedHeap, p, newsize)
releaseSys(heapLock) releaseSys(heapLock)
else: else:
result = realloc(p, newsize) result = realloc(p, newSize)
when hasThreadSupport: when hasThreadSupport:

View file

@ -12,10 +12,10 @@
proc raiseOverflow {.compilerproc, noinline, noreturn.} = proc raiseOverflow {.compilerproc, noinline, noreturn.} =
# a single proc to reduce code size to a minimum # a single proc to reduce code size to a minimum
sysFatal(EOverflow, "over- or underflow") sysFatal(OverflowError, "over- or underflow")
proc raiseDivByZero {.compilerproc, noinline, noreturn.} = proc raiseDivByZero {.compilerproc, noinline, noreturn.} =
sysFatal(EDivByZero, "divison by zero") sysFatal(DivByZeroError, "divison by zero")
proc addInt64(a, b: int64): int64 {.compilerProc, inline.} = proc addInt64(a, b: int64): int64 {.compilerProc, inline.} =
result = a +% b result = a +% b
@ -328,16 +328,16 @@ when not declared(mulInt):
# written in other languages. # written in other languages.
proc raiseFloatInvalidOp {.noinline, noreturn.} = proc raiseFloatInvalidOp {.noinline, noreturn.} =
sysFatal(EFloatInvalidOp, "FPU operation caused a NaN result") sysFatal(FloatInvalidOpError, "FPU operation caused a NaN result")
proc nanCheck(x: float64) {.compilerProc, inline.} = proc nanCheck(x: float64) {.compilerProc, inline.} =
if x != x: raiseFloatInvalidOp() if x != x: raiseFloatInvalidOp()
proc raiseFloatOverflow(x: float64) {.noinline, noreturn.} = proc raiseFloatOverflow(x: float64) {.noinline, noreturn.} =
if x > 0.0: if x > 0.0:
sysFatal(EFloatOverflow, "FPU operation caused an overflow") sysFatal(FloatOverflowError, "FPU operation caused an overflow")
else: else:
sysFatal(EFloatUnderflow, "FPU operations caused an underflow") sysFatal(FloatUnderflowError, "FPU operations caused an underflow")
proc infCheck(x: float64) {.compilerProc, inline.} = proc infCheck(x: float64) {.compilerProc, inline.} =
if x != 0.0 and x*0.5 == x: raiseFloatOverflow(x) if x != 0.0 and x*0.5 == x: raiseFloatOverflow(x)

View file

@ -13,8 +13,8 @@ proc genericAssignAux(dest, src: pointer, mt: PNimType, shallow: bool) {.gcsafe.
proc genericAssignAux(dest, src: pointer, n: ptr TNimNode, proc genericAssignAux(dest, src: pointer, n: ptr TNimNode,
shallow: bool) {.gcsafe.} = shallow: bool) {.gcsafe.} =
var var
d = cast[TAddress](dest) d = cast[ByteAddress](dest)
s = cast[TAddress](src) s = cast[ByteAddress](src)
case n.kind case n.kind
of nkSlot: of nkSlot:
genericAssignAux(cast[pointer](d +% n.offset), genericAssignAux(cast[pointer](d +% n.offset),
@ -40,8 +40,8 @@ proc genericAssignAux(dest, src: pointer, n: ptr TNimNode,
proc genericAssignAux(dest, src: pointer, mt: PNimType, shallow: bool) = proc genericAssignAux(dest, src: pointer, mt: PNimType, shallow: bool) =
var var
d = cast[TAddress](dest) d = cast[ByteAddress](dest)
s = cast[TAddress](src) s = cast[ByteAddress](src)
sysAssert(mt != nil, "genericAssignAux 2") sysAssert(mt != nil, "genericAssignAux 2")
case mt.kind case mt.kind
of tyString: of tyString:
@ -62,11 +62,11 @@ proc genericAssignAux(dest, src: pointer, mt: PNimType, shallow: bool) =
return return
sysAssert(dest != nil, "genericAssignAux 3") sysAssert(dest != nil, "genericAssignAux 3")
unsureAsgnRef(x, newSeq(mt, seq.len)) unsureAsgnRef(x, newSeq(mt, seq.len))
var dst = cast[TAddress](cast[PPointer](dest)[]) var dst = cast[ByteAddress](cast[PPointer](dest)[])
for i in 0..seq.len-1: for i in 0..seq.len-1:
genericAssignAux( genericAssignAux(
cast[pointer](dst +% i*% mt.base.size +% GenericSeqSize), cast[pointer](dst +% i*% mt.base.size +% GenericSeqSize),
cast[pointer](cast[TAddress](s2) +% i *% mt.base.size +% cast[pointer](cast[ByteAddress](s2) +% i *% mt.base.size +%
GenericSeqSize), GenericSeqSize),
mt.base, shallow) mt.base, shallow)
of tyObject: of tyObject:
@ -130,15 +130,15 @@ proc genericSeqAssign(dest, src: pointer, mt: PNimType) {.compilerProc.} =
proc genericAssignOpenArray(dest, src: pointer, len: int, proc genericAssignOpenArray(dest, src: pointer, len: int,
mt: PNimType) {.compilerproc.} = mt: PNimType) {.compilerproc.} =
var var
d = cast[TAddress](dest) d = cast[ByteAddress](dest)
s = cast[TAddress](src) s = cast[ByteAddress](src)
for i in 0..len-1: for i in 0..len-1:
genericAssign(cast[pointer](d +% i*% mt.base.size), genericAssign(cast[pointer](d +% i*% mt.base.size),
cast[pointer](s +% i*% mt.base.size), mt.base) cast[pointer](s +% i*% mt.base.size), mt.base)
proc objectInit(dest: pointer, typ: PNimType) {.compilerProc, gcsafe.} proc objectInit(dest: pointer, typ: PNimType) {.compilerProc, gcsafe.}
proc objectInitAux(dest: pointer, n: ptr TNimNode) {.gcsafe.} = proc objectInitAux(dest: pointer, n: ptr TNimNode) {.gcsafe.} =
var d = cast[TAddress](dest) var d = cast[ByteAddress](dest)
case n.kind case n.kind
of nkNone: sysAssert(false, "objectInitAux") of nkNone: sysAssert(false, "objectInitAux")
of nkSlot: objectInit(cast[pointer](d +% n.offset), n.typ) of nkSlot: objectInit(cast[pointer](d +% n.offset), n.typ)
@ -152,7 +152,7 @@ proc objectInitAux(dest: pointer, n: ptr TNimNode) {.gcsafe.} =
proc objectInit(dest: pointer, typ: PNimType) = proc objectInit(dest: pointer, typ: PNimType) =
# the generic init proc that takes care of initialization of complex # the generic init proc that takes care of initialization of complex
# objects on the stack or heap # objects on the stack or heap
var d = cast[TAddress](dest) var d = cast[ByteAddress](dest)
case typ.kind case typ.kind
of tyObject: of tyObject:
# iterate over any structural type # iterate over any structural type
@ -184,7 +184,7 @@ else:
proc genericReset(dest: pointer, mt: PNimType) {.compilerProc, gcsafe.} proc genericReset(dest: pointer, mt: PNimType) {.compilerProc, gcsafe.}
proc genericResetAux(dest: pointer, n: ptr TNimNode) = proc genericResetAux(dest: pointer, n: ptr TNimNode) =
var d = cast[TAddress](dest) var d = cast[ByteAddress](dest)
case n.kind case n.kind
of nkNone: sysAssert(false, "genericResetAux") of nkNone: sysAssert(false, "genericResetAux")
of nkSlot: genericReset(cast[pointer](d +% n.offset), n.typ) of nkSlot: genericReset(cast[pointer](d +% n.offset), n.typ)
@ -196,7 +196,7 @@ proc genericResetAux(dest: pointer, n: ptr TNimNode) =
zeroMem(cast[pointer](d +% n.offset), n.typ.size) zeroMem(cast[pointer](d +% n.offset), n.typ.size)
proc genericReset(dest: pointer, mt: PNimType) = proc genericReset(dest: pointer, mt: PNimType) =
var d = cast[TAddress](dest) var d = cast[ByteAddress](dest)
sysAssert(mt != nil, "genericReset 2") sysAssert(mt != nil, "genericReset 2")
case mt.kind case mt.kind
of tyString, tyRef, tySequence: of tyString, tyRef, tySequence:
@ -223,4 +223,4 @@ proc FieldDiscriminantCheck(oldDiscVal, newDiscVal: int,
var oldBranch = selectBranch(oldDiscVal, L, a) var oldBranch = selectBranch(oldDiscVal, L, a)
var newBranch = selectBranch(newDiscVal, L, a) var newBranch = selectBranch(newDiscVal, L, a)
if newBranch != oldBranch and oldDiscVal != 0: if newBranch != oldBranch and oldDiscVal != 0:
sysFatal(EInvalidField, "assignment to discriminant changes object branch") sysFatal(FieldError, "assignment to discriminant changes object branch")

View file

@ -27,7 +27,7 @@ type
TBitIndex = range[0..UnitsPerPage-1] TBitIndex = range[0..UnitsPerPage-1]
TPageDesc {.final, pure.} = object TPageDesc {.final, pure.} = object
next: PPageDesc # all nodes are connected with this pointer next: PPageDesc # all nodes are connected with this pointer
key: TAddress # start address at bit 0 key: ByteAddress # start address at bit 0
bits: array[TBitIndex, int] # a bit vector bits: array[TBitIndex, int] # a bit vector
PPageDescArray = ptr array[0..1000_000, PPageDesc] PPageDescArray = ptr array[0..1000_000, PPageDesc]
@ -98,7 +98,7 @@ proc nextTry(h, maxHash: int): int {.inline.} =
# generates each int in range(maxHash) exactly once (see any text on # generates each int in range(maxHash) exactly once (see any text on
# random-number generation for proof). # random-number generation for proof).
proc cellSetGet(t: TCellSet, key: TAddress): PPageDesc = proc cellSetGet(t: TCellSet, key: ByteAddress): PPageDesc =
var h = cast[int](key) and t.max var h = cast[int](key) and t.max
while t.data[h] != nil: while t.data[h] != nil:
if t.data[h].key == key: return t.data[h] if t.data[h].key == key: return t.data[h]
@ -123,7 +123,7 @@ proc cellSetEnlarge(t: var TCellSet) =
dealloc(t.data) dealloc(t.data)
t.data = n t.data = n
proc cellSetPut(t: var TCellSet, key: TAddress): PPageDesc = proc cellSetPut(t: var TCellSet, key: ByteAddress): PPageDesc =
var h = cast[int](key) and t.max var h = cast[int](key) and t.max
while true: while true:
var x = t.data[h] var x = t.data[h]
@ -147,7 +147,7 @@ proc cellSetPut(t: var TCellSet, key: TAddress): PPageDesc =
# ---------- slightly higher level procs -------------------------------------- # ---------- slightly higher level procs --------------------------------------
proc contains(s: TCellSet, cell: PCell): bool = proc contains(s: TCellSet, cell: PCell): bool =
var u = cast[TAddress](cell) var u = cast[ByteAddress](cell)
var t = cellSetGet(s, u shr PageShift) var t = cellSetGet(s, u shr PageShift)
if t != nil: if t != nil:
u = (u %% PageSize) /% MemAlign u = (u %% PageSize) /% MemAlign
@ -156,13 +156,13 @@ proc contains(s: TCellSet, cell: PCell): bool =
result = false result = false
proc incl(s: var TCellSet, cell: PCell) {.noinline.} = proc incl(s: var TCellSet, cell: PCell) {.noinline.} =
var u = cast[TAddress](cell) var u = cast[ByteAddress](cell)
var t = cellSetPut(s, u shr PageShift) var t = cellSetPut(s, u shr PageShift)
u = (u %% PageSize) /% MemAlign u = (u %% PageSize) /% MemAlign
t.bits[u shr IntShift] = t.bits[u shr IntShift] or (1 shl (u and IntMask)) t.bits[u shr IntShift] = t.bits[u shr IntShift] or (1 shl (u and IntMask))
proc excl(s: var TCellSet, cell: PCell) = proc excl(s: var TCellSet, cell: PCell) =
var u = cast[TAddress](cell) var u = cast[ByteAddress](cell)
var t = cellSetGet(s, u shr PageShift) var t = cellSetGet(s, u shr PageShift)
if t != nil: if t != nil:
u = (u %% PageSize) /% MemAlign u = (u %% PageSize) /% MemAlign
@ -170,7 +170,7 @@ proc excl(s: var TCellSet, cell: PCell) =
not (1 shl (u and IntMask))) not (1 shl (u and IntMask)))
proc containsOrIncl(s: var TCellSet, cell: PCell): bool = proc containsOrIncl(s: var TCellSet, cell: PCell): bool =
var u = cast[TAddress](cell) var u = cast[ByteAddress](cell)
var t = cellSetGet(s, u shr PageShift) var t = cellSetGet(s, u shr PageShift)
if t != nil: if t != nil:
u = (u %% PageSize) /% MemAlign u = (u %% PageSize) /% MemAlign

View file

@ -13,13 +13,13 @@ proc raiseRangeError(val: BiggestInt) {.compilerproc, noreturn, noinline.} =
when hostOS == "standalone": when hostOS == "standalone":
sysFatal(EOutOfRange, "value out of range") sysFatal(EOutOfRange, "value out of range")
else: else:
sysFatal(EOutOfRange, "value out of range: ", $val) sysFatal(RangeError, "value out of range: ", $val)
proc raiseIndexError() {.compilerproc, noreturn, noinline.} = proc raiseIndexError() {.compilerproc, noreturn, noinline.} =
sysFatal(EInvalidIndex, "index out of bounds") sysFatal(IndexError, "index out of bounds")
proc raiseFieldError(f: string) {.compilerproc, noreturn, noinline.} = proc raiseFieldError(f: string) {.compilerproc, noreturn, noinline.} =
sysFatal(EInvalidField, f, " is not accessible") sysFatal(FieldError, f, " is not accessible")
proc chckIndx(i, a, b: int): int = proc chckIndx(i, a, b: int): int =
if i >= a and i <= b: if i >= a and i <= b:
@ -46,11 +46,11 @@ proc chckRangeF(x, a, b: float): float =
when hostOS == "standalone": when hostOS == "standalone":
sysFatal(EOutOfRange, "value out of range") sysFatal(EOutOfRange, "value out of range")
else: else:
sysFatal(EOutOfRange, "value out of range: ", $x) sysFatal(RangeError, "value out of range: ", $x)
proc chckNil(p: pointer) = proc chckNil(p: pointer) =
if p == nil: if p == nil:
sysFatal(EInvalidValue, "attempt to write to a nil address") sysFatal(ValueError, "attempt to write to a nil address")
#c_raise(SIGSEGV) #c_raise(SIGSEGV)
proc chckObj(obj, subclass: PNimType) {.compilerproc.} = proc chckObj(obj, subclass: PNimType) {.compilerproc.} =
@ -59,13 +59,13 @@ proc chckObj(obj, subclass: PNimType) {.compilerproc.} =
if x == subclass: return # optimized fast path if x == subclass: return # optimized fast path
while x != subclass: while x != subclass:
if x == nil: if x == nil:
sysFatal(EInvalidObjectConversion, "invalid object conversion") sysFatal(ObjectConversionError, "invalid object conversion")
break break
x = x.base x = x.base
proc chckObjAsgn(a, b: PNimType) {.compilerproc, inline.} = proc chckObjAsgn(a, b: PNimType) {.compilerproc, inline.} =
if a != b: if a != b:
sysFatal(EInvalidObjectAssignment, "invalid object assignment") sysFatal(ObjectAssignmentError, "invalid object assignment")
type ObjCheckCache = array[0..1, PNimType] type ObjCheckCache = array[0..1, PNimType]

View file

@ -10,8 +10,8 @@
proc genericDeepCopyAux(dest, src: pointer, mt: PNimType) {.gcsafe.} proc genericDeepCopyAux(dest, src: pointer, mt: PNimType) {.gcsafe.}
proc genericDeepCopyAux(dest, src: pointer, n: ptr TNimNode) {.gcsafe.} = proc genericDeepCopyAux(dest, src: pointer, n: ptr TNimNode) {.gcsafe.} =
var var
d = cast[TAddress](dest) d = cast[ByteAddress](dest)
s = cast[TAddress](src) s = cast[ByteAddress](src)
case n.kind case n.kind
of nkSlot: of nkSlot:
genericDeepCopyAux(cast[pointer](d +% n.offset), genericDeepCopyAux(cast[pointer](d +% n.offset),
@ -40,8 +40,8 @@ proc copyDeepString(src: NimString): NimString {.inline.} =
proc genericDeepCopyAux(dest, src: pointer, mt: PNimType) = proc genericDeepCopyAux(dest, src: pointer, mt: PNimType) =
var var
d = cast[TAddress](dest) d = cast[ByteAddress](dest)
s = cast[TAddress](src) s = cast[ByteAddress](src)
sysAssert(mt != nil, "genericDeepCopyAux 2") sysAssert(mt != nil, "genericDeepCopyAux 2")
case mt.kind case mt.kind
of tyString: of tyString:
@ -60,11 +60,11 @@ proc genericDeepCopyAux(dest, src: pointer, mt: PNimType) =
return return
sysAssert(dest != nil, "genericDeepCopyAux 3") sysAssert(dest != nil, "genericDeepCopyAux 3")
unsureAsgnRef(x, newSeq(mt, seq.len)) unsureAsgnRef(x, newSeq(mt, seq.len))
var dst = cast[TAddress](cast[PPointer](dest)[]) var dst = cast[ByteAddress](cast[PPointer](dest)[])
for i in 0..seq.len-1: for i in 0..seq.len-1:
genericDeepCopyAux( genericDeepCopyAux(
cast[pointer](dst +% i*% mt.base.size +% GenericSeqSize), cast[pointer](dst +% i*% mt.base.size +% GenericSeqSize),
cast[pointer](cast[TAddress](s2) +% i *% mt.base.size +% cast[pointer](cast[ByteAddress](s2) +% i *% mt.base.size +%
GenericSeqSize), GenericSeqSize),
mt.base) mt.base)
of tyObject: of tyObject:
@ -82,8 +82,8 @@ proc genericDeepCopyAux(dest, src: pointer, mt: PNimType) =
genericDeepCopyAux(cast[pointer](d +% i*% mt.base.size), genericDeepCopyAux(cast[pointer](d +% i*% mt.base.size),
cast[pointer](s +% i*% mt.base.size), mt.base) cast[pointer](s +% i*% mt.base.size), mt.base)
of tyRef: of tyRef:
if mt.base.deepCopy != nil: if mt.base.deepcopy != nil:
let z = mt.base.deepCopy(cast[PPointer](src)[]) let z = mt.base.deepcopy(cast[PPointer](src)[])
unsureAsgnRef(cast[PPointer](dest), z) unsureAsgnRef(cast[PPointer](dest), z)
else: else:
# we modify the header of the cell temporarily; instead of the type # we modify the header of the cell temporarily; instead of the type
@ -116,8 +116,8 @@ proc genericDeepCopyAux(dest, src: pointer, mt: PNimType) =
genericDeepCopyAux(z, s2, realType.base) genericDeepCopyAux(z, s2, realType.base)
of tyPtr: of tyPtr:
# no cycle check here, but also not really required # no cycle check here, but also not really required
if mt.base.deepCopy != nil: if mt.base.deepcopy != nil:
cast[PPointer](dest)[] = mt.base.deepCopy(cast[PPointer](s)[]) cast[PPointer](dest)[] = mt.base.deepcopy(cast[PPointer](s)[])
else: else:
cast[PPointer](dest)[] = cast[PPointer](s)[] cast[PPointer](dest)[] = cast[PPointer](s)[]
else: else:
@ -134,8 +134,8 @@ proc genericSeqDeepCopy(dest, src: pointer, mt: PNimType) {.compilerProc.} =
proc genericDeepCopyOpenArray(dest, src: pointer, len: int, proc genericDeepCopyOpenArray(dest, src: pointer, len: int,
mt: PNimType) {.compilerproc.} = mt: PNimType) {.compilerproc.} =
var var
d = cast[TAddress](dest) d = cast[ByteAddress](dest)
s = cast[TAddress](src) s = cast[ByteAddress](src)
for i in 0..len-1: for i in 0..len-1:
genericDeepCopy(cast[pointer](d +% i*% mt.base.size), genericDeepCopy(cast[pointer](d +% i*% mt.base.size),
cast[pointer](s +% i*% mt.base.size), mt.base) cast[pointer](s +% i*% mt.base.size), mt.base)

View file

@ -17,7 +17,7 @@
const const
NilLibHandle: TLibHandle = nil NilLibHandle: TLibHandle = nil
proc rawWrite(f: TFile, s: string) = proc rawWrite(f: File, s: string) =
# we cannot throw an exception here! # we cannot throw an exception here!
discard writeBuffer(f, cstring(s), s.len) discard writeBuffer(f, cstring(s), s.len)

View file

@ -11,7 +11,7 @@
# use the heap (and nor exceptions) do not include the GC or memory allocator. # use the heap (and nor exceptions) do not include the GC or memory allocator.
var var
errorMessageWriter*: (proc(msg: string) {.tags: [FWriteIO], gcsafe.}) errorMessageWriter*: (proc(msg: string) {.tags: [WriteIOEffect], gcsafe.})
## Function that will be called ## Function that will be called
## instead of stdmsg.write when printing stacktrace. ## instead of stdmsg.write when printing stacktrace.
## Unstable API. ## Unstable API.
@ -42,7 +42,7 @@ var
excHandler {.threadvar.}: PSafePoint excHandler {.threadvar.}: PSafePoint
# list of exception handlers # list of exception handlers
# a global variable for the root of all try blocks # a global variable for the root of all try blocks
currException {.threadvar.}: ref E_Base currException {.threadvar.}: ref Exception
proc popFrame {.compilerRtl, inl.} = proc popFrame {.compilerRtl, inl.} =
framePtr = framePtr.prev framePtr = framePtr.prev
@ -58,7 +58,7 @@ proc pushSafePoint(s: PSafePoint) {.compilerRtl, inl.} =
proc popSafePoint {.compilerRtl, inl.} = proc popSafePoint {.compilerRtl, inl.} =
excHandler = excHandler.prev excHandler = excHandler.prev
proc pushCurrentException(e: ref E_Base) {.compilerRtl, inl.} = proc pushCurrentException(e: ref Exception) {.compilerRtl, inl.} =
e.parent = currException e.parent = currException
currException = e currException = e
@ -195,7 +195,7 @@ proc quitOrDebug() {.inline.} =
else: else:
endbStep() # call the debugger endbStep() # call the debugger
proc raiseExceptionAux(e: ref E_Base) = proc raiseExceptionAux(e: ref Exception) =
if localRaiseHook != nil: if localRaiseHook != nil:
if not localRaiseHook(e): return if not localRaiseHook(e): return
if globalRaiseHook != nil: if globalRaiseHook != nil:
@ -204,7 +204,7 @@ proc raiseExceptionAux(e: ref E_Base) =
if not excHandler.hasRaiseAction or excHandler.raiseAction(e): if not excHandler.hasRaiseAction or excHandler.raiseAction(e):
pushCurrentException(e) pushCurrentException(e)
c_longjmp(excHandler.context, 1) c_longjmp(excHandler.context, 1)
elif e[] of EOutOfMemory: elif e[] of OutOfMemError:
showErrorMessage(e.name) showErrorMessage(e.name)
quitOrDebug() quitOrDebug()
else: else:
@ -236,7 +236,7 @@ proc raiseExceptionAux(e: ref E_Base) =
showErrorMessage(buf) showErrorMessage(buf)
quitOrDebug() quitOrDebug()
proc raiseException(e: ref E_Base, ename: cstring) {.compilerRtl.} = proc raiseException(e: ref Exception, ename: cstring) {.compilerRtl.} =
e.name = ename e.name = ename
when hasSomeStackTrace: when hasSomeStackTrace:
e.trace = "" e.trace = ""
@ -245,7 +245,7 @@ proc raiseException(e: ref E_Base, ename: cstring) {.compilerRtl.} =
proc reraiseException() {.compilerRtl.} = proc reraiseException() {.compilerRtl.} =
if currException == nil: if currException == nil:
sysFatal(ENoExceptionToReraise, "no exception to reraise") sysFatal(ReraiseError, "no exception to reraise")
else: else:
raiseExceptionAux(currException) raiseExceptionAux(currException)
@ -264,7 +264,7 @@ proc getStackTrace(): string =
else: else:
result = "No stack traceback available\n" result = "No stack traceback available\n"
proc getStackTrace(e: ref E_Base): string = proc getStackTrace(e: ref Exception): string =
if not isNil(e) and not isNil(e.trace): if not isNil(e) and not isNil(e.trace):
result = e.trace result = e.trace
else: else:

View file

@ -111,11 +111,11 @@ proc addZCT(s: var TCellSeq, c: PCell) {.noinline.} =
proc cellToUsr(cell: PCell): pointer {.inline.} = proc cellToUsr(cell: PCell): pointer {.inline.} =
# convert object (=pointer to refcount) to pointer to userdata # convert object (=pointer to refcount) to pointer to userdata
result = cast[pointer](cast[TAddress](cell)+%TAddress(sizeof(TCell))) result = cast[pointer](cast[ByteAddress](cell)+%TAddress(sizeof(TCell)))
proc usrToCell(usr: pointer): PCell {.inline.} = proc usrToCell(usr: pointer): PCell {.inline.} =
# convert pointer to userdata to object (=pointer to refcount) # convert pointer to userdata to object (=pointer to refcount)
result = cast[PCell](cast[TAddress](usr)-%TAddress(sizeof(TCell))) result = cast[PCell](cast[ByteAddress](usr)-%TAddress(sizeof(TCell)))
proc canbeCycleRoot(c: PCell): bool {.inline.} = proc canbeCycleRoot(c: PCell): bool {.inline.} =
result = ntfAcyclic notin c.typ.flags result = ntfAcyclic notin c.typ.flags
@ -312,7 +312,7 @@ proc cellsetReset(s: var TCellSet) =
init(s) init(s)
proc forAllSlotsAux(dest: pointer, n: ptr TNimNode, op: TWalkOp) {.gcsafe.} = proc forAllSlotsAux(dest: pointer, n: ptr TNimNode, op: TWalkOp) {.gcsafe.} =
var d = cast[TAddress](dest) var d = cast[ByteAddress](dest)
case n.kind case n.kind
of nkSlot: forAllChildrenAux(cast[pointer](d +% n.offset), n.typ, op) of nkSlot: forAllChildrenAux(cast[pointer](d +% n.offset), n.typ, op)
of nkList: of nkList:
@ -332,7 +332,7 @@ proc forAllSlotsAux(dest: pointer, n: ptr TNimNode, op: TWalkOp) {.gcsafe.} =
of nkNone: sysAssert(false, "forAllSlotsAux") of nkNone: sysAssert(false, "forAllSlotsAux")
proc forAllChildrenAux(dest: pointer, mt: PNimType, op: TWalkOp) = proc forAllChildrenAux(dest: pointer, mt: PNimType, op: TWalkOp) =
var d = cast[TAddress](dest) var d = cast[ByteAddress](dest)
if dest == nil: return # nothing to do if dest == nil: return # nothing to do
if ntfNoRefs notin mt.flags: if ntfNoRefs notin mt.flags:
case mt.kind case mt.kind
@ -358,7 +358,7 @@ proc forAllChildren(cell: PCell, op: TWalkOp) =
of tyRef: # common case of tyRef: # common case
forAllChildrenAux(cellToUsr(cell), cell.typ.base, op) forAllChildrenAux(cellToUsr(cell), cell.typ.base, op)
of tySequence: of tySequence:
var d = cast[TAddress](cellToUsr(cell)) var d = cast[ByteAddress](cellToUsr(cell))
var s = cast[PGenericSeq](d) var s = cast[PGenericSeq](d)
if s != nil: if s != nil:
for i in 0..s.len-1: for i in 0..s.len-1:
@ -424,7 +424,7 @@ proc rawNewObj(typ: PNimType, size: int, gch: var TGcHeap): pointer =
gcAssert(typ.kind in {tyRef, tyString, tySequence}, "newObj: 1") gcAssert(typ.kind in {tyRef, tyString, tySequence}, "newObj: 1")
collectCT(gch) collectCT(gch)
var res = cast[PCell](rawAlloc(gch.region, size + sizeof(TCell))) var res = cast[PCell](rawAlloc(gch.region, size + sizeof(TCell)))
gcAssert((cast[TAddress](res) and (MemAlign-1)) == 0, "newObj: 2") gcAssert((cast[ByteAddress](res) and (MemAlign-1)) == 0, "newObj: 2")
# now it is buffered in the ZCT # now it is buffered in the ZCT
res.typ = typ res.typ = typ
when leakDetector and not hasThreadSupport: when leakDetector and not hasThreadSupport:
@ -470,7 +470,7 @@ proc newObjRC1(typ: PNimType, size: int): pointer {.compilerRtl.} =
var res = cast[PCell](rawAlloc(gch.region, size + sizeof(TCell))) var res = cast[PCell](rawAlloc(gch.region, size + sizeof(TCell)))
sysAssert(allocInv(gch.region), "newObjRC1 after rawAlloc") sysAssert(allocInv(gch.region), "newObjRC1 after rawAlloc")
sysAssert((cast[TAddress](res) and (MemAlign-1)) == 0, "newObj: 2") sysAssert((cast[ByteAddress](res) and (MemAlign-1)) == 0, "newObj: 2")
# now it is buffered in the ZCT # now it is buffered in the ZCT
res.typ = typ res.typ = typ
when leakDetector and not hasThreadSupport: when leakDetector and not hasThreadSupport:
@ -511,9 +511,9 @@ proc growObj(old: pointer, newsize: int, gch: var TGcHeap): pointer =
var oldsize = cast[PGenericSeq](old).len*elemSize + GenericSeqSize var oldsize = cast[PGenericSeq](old).len*elemSize + GenericSeqSize
copyMem(res, ol, oldsize + sizeof(TCell)) copyMem(res, ol, oldsize + sizeof(TCell))
zeroMem(cast[pointer](cast[TAddress](res)+% oldsize +% sizeof(TCell)), zeroMem(cast[pointer](cast[ByteAddress](res)+% oldsize +% sizeof(TCell)),
newsize-oldsize) newsize-oldsize)
sysAssert((cast[TAddress](res) and (MemAlign-1)) == 0, "growObj: 3") sysAssert((cast[ByteAddress](res) and (MemAlign-1)) == 0, "growObj: 3")
sysAssert(res.refcount shr rcShift <=% 1, "growObj: 4") sysAssert(res.refcount shr rcShift <=% 1, "growObj: 4")
#if res.refcount <% rcIncrement: #if res.refcount <% rcIncrement:
# add(gch.zct, res) # add(gch.zct, res)
@ -728,7 +728,7 @@ proc gcMark(gch: var TGcHeap, p: pointer) {.inline.} =
# the addresses are not as cells on the stack, so turn them to cells: # the addresses are not as cells on the stack, so turn them to cells:
sysAssert(allocInv(gch.region), "gcMark begin") sysAssert(allocInv(gch.region), "gcMark begin")
var cell = usrToCell(p) var cell = usrToCell(p)
var c = cast[TAddress](cell) var c = cast[ByteAddress](cell)
if c >% PageSize: if c >% PageSize:
# fast check: does it look like a cell? # fast check: does it look like a cell?
var objStart = cast[PCell](interiorAllocatedPtr(gch.region, cell)) var objStart = cast[PCell](interiorAllocatedPtr(gch.region, cell))
@ -778,8 +778,8 @@ when not defined(useNimRtl):
# the first init must be the one that defines the stack bottom: # the first init must be the one that defines the stack bottom:
if gch.stackBottom == nil: gch.stackBottom = theStackBottom if gch.stackBottom == nil: gch.stackBottom = theStackBottom
else: else:
var a = cast[TAddress](theStackBottom) # and not PageMask - PageSize*2 var a = cast[ByteAddress](theStackBottom) # and not PageMask - PageSize*2
var b = cast[TAddress](gch.stackBottom) var b = cast[ByteAddress](gch.stackBottom)
#c_fprintf(c_stdout, "old: %p new: %p;\n",gch.stackBottom,theStackBottom) #c_fprintf(c_stdout, "old: %p new: %p;\n",gch.stackBottom,theStackBottom)
when stackIncreases: when stackIncreases:
gch.stackBottom = cast[pointer](min(a, b)) gch.stackBottom = cast[pointer](min(a, b))
@ -854,9 +854,9 @@ else:
proc isOnStack(p: pointer): bool = proc isOnStack(p: pointer): bool =
var stackTop {.volatile.}: pointer var stackTop {.volatile.}: pointer
stackTop = addr(stackTop) stackTop = addr(stackTop)
var b = cast[TAddress](gch.stackBottom) var b = cast[ByteAddress](gch.stackBottom)
var a = cast[TAddress](stackTop) var a = cast[ByteAddress](stackTop)
var x = cast[TAddress](p) var x = cast[ByteAddress](p)
result = a <=% x and x <=% b result = a <=% x and x <=% b
template forEachStackSlot(gch, gcMark: expr) {.immediate, dirty.} = template forEachStackSlot(gch, gcMark: expr) {.immediate, dirty.} =
@ -866,8 +866,8 @@ else:
type PStackSlice = ptr array [0..7, pointer] type PStackSlice = ptr array [0..7, pointer]
var registers {.noinit.}: C_JmpBuf var registers {.noinit.}: C_JmpBuf
if c_setjmp(registers) == 0'i32: # To fill the C stack with registers. if c_setjmp(registers) == 0'i32: # To fill the C stack with registers.
var max = cast[TAddress](gch.stackBottom) var max = cast[ByteAddress](gch.stackBottom)
var sp = cast[TAddress](addr(registers)) var sp = cast[ByteAddress](addr(registers))
# loop unrolled: # loop unrolled:
while sp <% max - 8*sizeof(pointer): while sp <% max - 8*sizeof(pointer):
gcMark(gch, cast[PStackSlice](sp)[0]) gcMark(gch, cast[PStackSlice](sp)[0])
@ -1040,7 +1040,7 @@ when not defined(useNimRtl):
else: else:
dec(gch.recGcLock) dec(gch.recGcLock)
proc GC_setStrategy(strategy: TGC_Strategy) = proc GC_setStrategy(strategy: GC_Strategy) =
discard discard
proc GC_enableMarkAndSweep() = proc GC_enableMarkAndSweep() =

View file

@ -155,7 +155,7 @@ when not defined(useNimRtl):
var bs = typ.base.size var bs = typ.base.size
for i in 0..typ.size div bs - 1: for i in 0..typ.size div bs - 1:
if i > 0: add result, ", " if i > 0: add result, ", "
reprAux(result, cast[pointer](cast[TAddress](p) + i*bs), typ.base, cl) reprAux(result, cast[pointer](cast[ByteAddress](p) + i*bs), typ.base, cl)
add result, "]" add result, "]"
proc reprSequence(result: var string, p: pointer, typ: PNimType, proc reprSequence(result: var string, p: pointer, typ: PNimType,
@ -167,7 +167,7 @@ when not defined(useNimRtl):
var bs = typ.base.size var bs = typ.base.size
for i in 0..cast[PGenericSeq](p).len-1: for i in 0..cast[PGenericSeq](p).len-1:
if i > 0: add result, ", " if i > 0: add result, ", "
reprAux(result, cast[pointer](cast[TAddress](p) + GenericSeqSize + i*bs), reprAux(result, cast[pointer](cast[ByteAddress](p) + GenericSeqSize + i*bs),
typ.base, cl) typ.base, cl)
add result, "]" add result, "]"
@ -178,14 +178,14 @@ when not defined(useNimRtl):
of nkSlot: of nkSlot:
add result, $n.name add result, $n.name
add result, " = " add result, " = "
reprAux(result, cast[pointer](cast[TAddress](p) + n.offset), n.typ, cl) reprAux(result, cast[pointer](cast[ByteAddress](p) + n.offset), n.typ, cl)
of nkList: of nkList:
for i in 0..n.len-1: for i in 0..n.len-1:
if i > 0: add result, ",\n" if i > 0: add result, ",\n"
reprRecordAux(result, p, n.sons[i], cl) reprRecordAux(result, p, n.sons[i], cl)
of nkCase: of nkCase:
var m = selectBranch(p, n) var m = selectBranch(p, n)
reprAux(result, cast[pointer](cast[TAddress](p) + n.offset), n.typ, cl) reprAux(result, cast[pointer](cast[ByteAddress](p) + n.offset), n.typ, cl)
if m != nil: reprRecordAux(result, p, m, cl) if m != nil: reprRecordAux(result, p, m, cl)
proc reprRecord(result: var string, p: pointer, typ: PNimType, proc reprRecord(result: var string, p: pointer, typ: PNimType,
@ -265,7 +265,7 @@ proc reprOpenArray(p: pointer, length: int, elemtyp: PNimType): string {.
var bs = elemtyp.size var bs = elemtyp.size
for i in 0..length - 1: for i in 0..length - 1:
if i > 0: add result, ", " if i > 0: add result, ", "
reprAux(result, cast[pointer](cast[TAddress](p) + i*bs), elemtyp, cl) reprAux(result, cast[pointer](cast[ByteAddress](p) + i*bs), elemtyp, cl)
add result, "]" add result, "]"
deinitReprClosure(cl) deinitReprClosure(cl)

View file

@ -1,6 +1,6 @@
# #
# #
# Nimrod's Runtime Library # Nim's Runtime Library
# (c) Copyright 2013 Andreas Rumpf # (c) Copyright 2013 Andreas Rumpf
# #
# See the file "copying.txt", included in this # See the file "copying.txt", included in this
@ -8,7 +8,7 @@
# #
# Nimrod's standard IO library. It contains high-performance # Nim's standard IO library. It contains high-performance
# routines for reading and writing data to (buffered) files or # routines for reading and writing data to (buffered) files or
# TTYs. # TTYs.
@ -16,33 +16,33 @@
# of the standard library! # of the standard library!
proc fputs(c: cstring, f: TFile) {.importc: "fputs", header: "<stdio.h>", proc fputs(c: cstring, f: File) {.importc: "fputs", header: "<stdio.h>",
tags: [FWriteIO].} tags: [WriteIOEffect].}
proc fgets(c: cstring, n: int, f: TFile): cstring {. proc fgets(c: cstring, n: int, f: File): cstring {.
importc: "fgets", header: "<stdio.h>", tags: [FReadIO].} importc: "fgets", header: "<stdio.h>", tags: [ReadIOEffect].}
proc fgetc(stream: TFile): cint {.importc: "fgetc", header: "<stdio.h>", proc fgetc(stream: File): cint {.importc: "fgetc", header: "<stdio.h>",
tags: [FReadIO].} tags: [ReadIOEffect].}
proc ungetc(c: cint, f: TFile) {.importc: "ungetc", header: "<stdio.h>", proc ungetc(c: cint, f: File) {.importc: "ungetc", header: "<stdio.h>",
tags: [].} tags: [].}
proc putc(c: char, stream: TFile) {.importc: "putc", header: "<stdio.h>", proc putc(c: char, stream: File) {.importc: "putc", header: "<stdio.h>",
tags: [FWriteIO].} tags: [WriteIOEffect].}
proc fprintf(f: TFile, frmt: cstring) {.importc: "fprintf", proc fprintf(f: File, frmt: cstring) {.importc: "fprintf",
header: "<stdio.h>", varargs, tags: [FWriteIO].} header: "<stdio.h>", varargs, tags: [WriteIOEffect].}
proc strlen(c: cstring): int {. proc strlen(c: cstring): int {.
importc: "strlen", header: "<string.h>", tags: [].} importc: "strlen", header: "<string.h>", tags: [].}
# C routine that is used here: # C routine that is used here:
proc fread(buf: pointer, size, n: int, f: TFile): int {. proc fread(buf: pointer, size, n: int, f: File): int {.
importc: "fread", header: "<stdio.h>", tags: [FReadIO].} importc: "fread", header: "<stdio.h>", tags: [ReadIOEffect].}
proc fseek(f: TFile, offset: clong, whence: int): int {. proc fseek(f: File, offset: clong, whence: int): int {.
importc: "fseek", header: "<stdio.h>", tags: [].} importc: "fseek", header: "<stdio.h>", tags: [].}
proc ftell(f: TFile): int {.importc: "ftell", header: "<stdio.h>", tags: [].} proc ftell(f: File): int {.importc: "ftell", header: "<stdio.h>", tags: [].}
proc setvbuf(stream: TFile, buf: pointer, typ, size: cint): cint {. proc setvbuf(stream: File, buf: pointer, typ, size: cint): cint {.
importc, header: "<stdio.h>", tags: [].} importc, header: "<stdio.h>", tags: [].}
{.push stackTrace:off, profiler:off.} {.push stackTrace:off, profiler:off.}
proc write(f: TFile, c: cstring) = fputs(c, f) proc write(f: File, c: cstring) = fputs(c, f)
{.pop.} {.pop.}
when NoFakeVars: when NoFakeVars:
@ -65,9 +65,9 @@ const
BufSize = 4000 BufSize = 4000
proc raiseEIO(msg: string) {.noinline, noreturn.} = proc raiseEIO(msg: string) {.noinline, noreturn.} =
sysFatal(EIO, msg) sysFatal(IOError, msg)
proc readLine(f: TFile, line: var TaintedString): bool = proc readLine(f: File, line: var TaintedString): bool =
# of course this could be optimized a bit; but IO is slow anyway... # of course this could be optimized a bit; but IO is slow anyway...
# and it was difficult to get this CORRECT with Ansi C's methods # and it was difficult to get this CORRECT with Ansi C's methods
setLen(line.string, 0) # reuse the buffer! setLen(line.string, 0) # reuse the buffer!
@ -84,34 +84,34 @@ proc readLine(f: TFile, line: var TaintedString): bool =
add line.string, chr(int(c)) add line.string, chr(int(c))
result = true result = true
proc readLine(f: TFile): TaintedString = proc readLine(f: File): TaintedString =
result = TaintedString(newStringOfCap(80)) result = TaintedString(newStringOfCap(80))
if not readLine(f, result): raiseEIO("EOF reached") if not readLine(f, result): raiseEIO("EOF reached")
proc write(f: TFile, i: int) = proc write(f: File, i: int) =
when sizeof(int) == 8: when sizeof(int) == 8:
fprintf(f, "%lld", i) fprintf(f, "%lld", i)
else: else:
fprintf(f, "%ld", i) fprintf(f, "%ld", i)
proc write(f: TFile, i: BiggestInt) = proc write(f: File, i: BiggestInt) =
when sizeof(BiggestInt) == 8: when sizeof(BiggestInt) == 8:
fprintf(f, "%lld", i) fprintf(f, "%lld", i)
else: else:
fprintf(f, "%ld", i) fprintf(f, "%ld", i)
proc write(f: TFile, b: bool) = proc write(f: File, b: bool) =
if b: write(f, "true") if b: write(f, "true")
else: write(f, "false") else: write(f, "false")
proc write(f: TFile, r: float32) = fprintf(f, "%g", r) proc write(f: File, r: float32) = fprintf(f, "%g", r)
proc write(f: TFile, r: BiggestFloat) = fprintf(f, "%g", r) proc write(f: File, r: BiggestFloat) = fprintf(f, "%g", r)
proc write(f: TFile, c: char) = putc(c, f) proc write(f: File, c: char) = putc(c, f)
proc write(f: TFile, a: varargs[string, `$`]) = proc write(f: File, a: varargs[string, `$`]) =
for x in items(a): write(f, x) for x in items(a): write(f, x)
proc readAllBuffer(file: TFile): string = proc readAllBuffer(file: File): string =
# This proc is for TFile we want to read but don't know how many # This proc is for File we want to read but don't know how many
# bytes we need to read before the buffer is empty. # bytes we need to read before the buffer is empty.
result = "" result = ""
var buffer = newString(BufSize) var buffer = newString(BufSize)
@ -124,27 +124,27 @@ proc readAllBuffer(file: TFile): string =
result.add(buffer) result.add(buffer)
break break
proc rawFileSize(file: TFile): int = proc rawFileSize(file: File): int =
# this does not raise an error opposed to `getFileSize` # this does not raise an error opposed to `getFileSize`
var oldPos = ftell(file) var oldPos = ftell(file)
discard fseek(file, 0, 2) # seek the end of the file discard fseek(file, 0, 2) # seek the end of the file
result = ftell(file) result = ftell(file)
discard fseek(file, clong(oldPos), 0) discard fseek(file, clong(oldPos), 0)
proc readAllFile(file: TFile, len: int): string = proc readAllFile(file: File, len: int): string =
# We aquire the filesize beforehand and hope it doesn't change. # We aquire the filesize beforehand and hope it doesn't change.
# Speeds things up. # Speeds things up.
result = newString(int(len)) result = newString(int(len))
if readBuffer(file, addr(result[0]), int(len)) != len: if readBuffer(file, addr(result[0]), int(len)) != len:
raiseEIO("error while reading from file") raiseEIO("error while reading from file")
proc readAllFile(file: TFile): string = proc readAllFile(file: File): string =
var len = rawFileSize(file) var len = rawFileSize(file)
result = readAllFile(file, len) result = readAllFile(file, len)
proc readAll(file: TFile): TaintedString = proc readAll(file: File): TaintedString =
# Separate handling needed because we need to buffer when we # Separate handling needed because we need to buffer when we
# don't know the overall length of the TFile. # don't know the overall length of the File.
var len = rawFileSize(file) var len = rawFileSize(file)
if len >= 0: if len >= 0:
result = readAllFile(file, len).TaintedString result = readAllFile(file, len).TaintedString
@ -165,13 +165,13 @@ proc writeFile(filename, content: string) =
finally: finally:
close(f) close(f)
proc endOfFile(f: TFile): bool = proc endOfFile(f: File): bool =
# do not blame me; blame the ANSI C standard this is so brain-damaged # do not blame me; blame the ANSI C standard this is so brain-damaged
var c = fgetc(f) var c = fgetc(f)
ungetc(c, f) ungetc(c, f)
return c < 0'i32 return c < 0'i32
proc writeln[Ty](f: TFile, x: varargs[Ty, `$`]) = proc writeln[Ty](f: File, x: varargs[Ty, `$`]) =
for i in items(x): write(f, i) for i in items(x): write(f, i)
write(f, "\n") write(f, "\n")
@ -186,7 +186,7 @@ when (defined(windows) and not defined(useWinAnsi)) or defined(nimdoc):
when defined(windows) and not defined(useWinAnsi): when defined(windows) and not defined(useWinAnsi):
proc wfopen(filename, mode: WideCString): pointer {. proc wfopen(filename, mode: WideCString): pointer {.
importc: "_wfopen", nodecl.} importc: "_wfopen", nodecl.}
proc wfreopen(filename, mode: WideCString, stream: TFile): TFile {. proc wfreopen(filename, mode: WideCString, stream: File): File {.
importc: "_wfreopen", nodecl.} importc: "_wfreopen", nodecl.}
proc fopen(filename, mode: cstring): pointer = proc fopen(filename, mode: cstring): pointer =
@ -194,82 +194,82 @@ when defined(windows) and not defined(useWinAnsi):
var m = newWideCString(mode) var m = newWideCString(mode)
result = wfopen(f, m) result = wfopen(f, m)
proc freopen(filename, mode: cstring, stream: TFile): TFile = proc freopen(filename, mode: cstring, stream: File): File =
var f = newWideCString(filename) var f = newWideCString(filename)
var m = newWideCString(mode) var m = newWideCString(mode)
result = wfreopen(f, m, stream) result = wfreopen(f, m, stream)
else: else:
proc fopen(filename, mode: cstring): pointer {.importc: "fopen", noDecl.} proc fopen(filename, mode: cstring): pointer {.importc: "fopen", noDecl.}
proc freopen(filename, mode: cstring, stream: TFile): TFile {. proc freopen(filename, mode: cstring, stream: File): File {.
importc: "freopen", nodecl.} importc: "freopen", nodecl.}
const const
FormatOpen: array [TFileMode, string] = ["rb", "wb", "w+b", "r+b", "ab"] FormatOpen: array [FileMode, string] = ["rb", "wb", "w+b", "r+b", "ab"]
#"rt", "wt", "w+t", "r+t", "at" #"rt", "wt", "w+t", "r+t", "at"
# we always use binary here as for Nimrod the OS line ending # we always use binary here as for Nim the OS line ending
# should not be translated. # should not be translated.
proc open(f: var TFile, filename: string, proc open(f: var File, filename: string,
mode: TFileMode = fmRead, mode: FileMode = fmRead,
bufSize: int = -1): bool = bufSize: int = -1): bool =
var p: pointer = fopen(filename, FormatOpen[mode]) var p: pointer = fopen(filename, FormatOpen[mode])
result = (p != nil) result = (p != nil)
f = cast[TFile](p) f = cast[File](p)
if bufSize > 0 and bufSize <= high(cint).int: if bufSize > 0 and bufSize <= high(cint).int:
if setvbuf(f, nil, IOFBF, bufSize.cint) != 0'i32: if setvbuf(f, nil, IOFBF, bufSize.cint) != 0'i32:
sysFatal(EOutOfMemory, "out of memory") sysFatal(OutOfMemError, "out of memory")
elif bufSize == 0: elif bufSize == 0:
discard setvbuf(f, nil, IONBF, 0) discard setvbuf(f, nil, IONBF, 0)
proc reopen(f: TFile, filename: string, mode: TFileMode = fmRead): bool = proc reopen(f: File, filename: string, mode: FileMode = fmRead): bool =
var p: pointer = freopen(filename, FormatOpen[mode], f) var p: pointer = freopen(filename, FormatOpen[mode], f)
result = p != nil result = p != nil
proc fdopen(filehandle: TFileHandle, mode: cstring): TFile {. proc fdopen(filehandle: FileHandle, mode: cstring): File {.
importc: pccHack & "fdopen", header: "<stdio.h>".} importc: pccHack & "fdopen", header: "<stdio.h>".}
proc open(f: var TFile, filehandle: TFileHandle, mode: TFileMode): bool = proc open(f: var File, filehandle: FileHandle, mode: FileMode): bool =
f = fdopen(filehandle, FormatOpen[mode]) f = fdopen(filehandle, FormatOpen[mode])
result = f != nil result = f != nil
proc fwrite(buf: pointer, size, n: int, f: TFile): int {. proc fwrite(buf: pointer, size, n: int, f: File): int {.
importc: "fwrite", noDecl.} importc: "fwrite", noDecl.}
proc readBuffer(f: TFile, buffer: pointer, len: int): int = proc readBuffer(f: File, buffer: pointer, len: int): int =
result = fread(buffer, 1, len, f) result = fread(buffer, 1, len, f)
proc readBytes(f: TFile, a: var openArray[int8], start, len: int): int = proc readBytes(f: File, a: var openArray[int8], start, len: int): int =
result = readBuffer(f, addr(a[start]), len) result = readBuffer(f, addr(a[start]), len)
proc readChars(f: TFile, a: var openArray[char], start, len: int): int = proc readChars(f: File, a: var openArray[char], start, len: int): int =
result = readBuffer(f, addr(a[start]), len) result = readBuffer(f, addr(a[start]), len)
{.push stackTrace:off, profiler:off.} {.push stackTrace:off, profiler:off.}
proc writeBytes(f: TFile, a: openArray[int8], start, len: int): int = proc writeBytes(f: File, a: openArray[int8], start, len: int): int =
var x = cast[ptr array[0..1000_000_000, int8]](a) var x = cast[ptr array[0..1000_000_000, int8]](a)
result = writeBuffer(f, addr(x[start]), len) result = writeBuffer(f, addr(x[start]), len)
proc writeChars(f: TFile, a: openArray[char], start, len: int): int = proc writeChars(f: File, a: openArray[char], start, len: int): int =
var x = cast[ptr array[0..1000_000_000, int8]](a) var x = cast[ptr array[0..1000_000_000, int8]](a)
result = writeBuffer(f, addr(x[start]), len) result = writeBuffer(f, addr(x[start]), len)
proc writeBuffer(f: TFile, buffer: pointer, len: int): int = proc writeBuffer(f: File, buffer: pointer, len: int): int =
result = fwrite(buffer, 1, len, f) result = fwrite(buffer, 1, len, f)
proc write(f: TFile, s: string) = proc write(f: File, s: string) =
if writeBuffer(f, cstring(s), s.len) != s.len: if writeBuffer(f, cstring(s), s.len) != s.len:
raiseEIO("cannot write string to file") raiseEIO("cannot write string to file")
{.pop.} {.pop.}
proc setFilePos(f: TFile, pos: int64) = proc setFilePos(f: File, pos: int64) =
if fseek(f, clong(pos), 0) != 0: if fseek(f, clong(pos), 0) != 0:
raiseEIO("cannot set file position") raiseEIO("cannot set file position")
proc getFilePos(f: TFile): int64 = proc getFilePos(f: File): int64 =
result = ftell(f) result = ftell(f)
if result < 0: raiseEIO("cannot retrieve file position") if result < 0: raiseEIO("cannot retrieve file position")
proc getFileSize(f: TFile): int64 = proc getFileSize(f: File): int64 =
var oldPos = getFilePos(f) var oldPos = getFilePos(f)
discard fseek(f, 0, 2) # seek the end of the file discard fseek(f, 0, 2) # seek the end of the file
result = getFilePos(f) result = getFilePos(f)

View file

@ -217,7 +217,7 @@ proc setLengthSeq(seq: PGenericSeq, elemSize, newLen: int): PGenericSeq {.
gch.tempStack.len = len0 gch.tempStack.len = len0
else: else:
for i in newLen..result.len-1: for i in newLen..result.len-1:
forAllChildrenAux(cast[pointer](cast[TAddress](result) +% forAllChildrenAux(cast[pointer](cast[ByteAddress](result) +%
GenericSeqSize +% (i*%elemSize)), GenericSeqSize +% (i*%elemSize)),
extGetCellType(result).base, waZctDecRef) extGetCellType(result).base, waZctDecRef)
@ -227,7 +227,7 @@ proc setLengthSeq(seq: PGenericSeq, elemSize, newLen: int): PGenericSeq {.
# presense of user defined destructors, the user will expect the cell to be # presense of user defined destructors, the user will expect the cell to be
# "destroyed" thus creating the same problem. We can destoy the cell in the # "destroyed" thus creating the same problem. We can destoy the cell in the
# finalizer of the sequence, but this makes destruction non-deterministic. # finalizer of the sequence, but this makes destruction non-deterministic.
zeroMem(cast[pointer](cast[TAddress](result) +% GenericSeqSize +% zeroMem(cast[pointer](cast[ByteAddress](result) +% GenericSeqSize +%
(newLen*%elemSize)), (result.len-%newLen) *% elemSize) (newLen*%elemSize)), (result.len-%newLen) *% elemSize)
result.len = newLen result.len = newLen