code gen bugfixes; marshal.nim implemented

This commit is contained in:
Araq
2011-06-26 17:21:52 +02:00
parent db0a4a9f86
commit 990dc2d715
21 changed files with 725 additions and 422 deletions

View File

@@ -198,18 +198,18 @@ proc prepareDealloc(cell: PCell) =
proc rtlAddCycleRoot(c: PCell) {.rtl, inl.} =
# we MUST access gch as a global here, because this crosses DLL boundaries!
when hasThreadSupport:
when hasThreadSupport and hasSharedHeap:
AcquireSys(HeapLock)
incl(gch.cycleRoots, c)
when hasThreadSupport:
when hasThreadSupport and hasSharedHeap:
ReleaseSys(HeapLock)
proc rtlAddZCT(c: PCell) {.rtl, inl.} =
# we MUST access gch as a global here, because this crosses DLL boundaries!
when hasThreadSupport:
when hasThreadSupport and hasSharedHeap:
AcquireSys(HeapLock)
addZCT(gch.zct, c)
when hasThreadSupport:
when hasThreadSupport and hasSharedHeap:
ReleaseSys(HeapLock)
proc decRef(c: PCell) {.inline.} =

View File

@@ -9,9 +9,6 @@
# The generic ``repr`` procedure. It is an invaluable debugging tool.
#proc cstrToNimStrDummy(s: cstring): string {.inline.} =
# result = cast[string](cstrToNimStr(s))
proc reprInt(x: int64): string {.compilerproc.} = return $x
proc reprFloat(x: float): string {.compilerproc.} = return $x

View File

@@ -36,13 +36,16 @@ var
IOFBF {.importc: "_IOFBF", nodecl.}: cint
IONBF {.importc: "_IONBF", nodecl.}: cint
proc raiseEIO(msg: string) {.noinline, noreturn.} =
raise newException(EIO, msg)
proc rawReadLine(f: TFile, result: var string) =
# 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
setLen(result, 0) # reuse the buffer!
while True:
var c = fgetc(f)
if c < 0'i32: break # EOF
if c < 0'i32: raiseEIO("EOF reached")
if c == 10'i32: break # LF
if c == 13'i32: # CR
c = fgetc(f) # is the next char LF?
@@ -76,11 +79,6 @@ proc write(f: TFile, c: Char) = putc(c, f)
proc write(f: TFile, a: openArray[string]) =
for x in items(a): write(f, x)
#{.error: "for debugging.".}
proc raiseEIO(msg: string) {.noinline, noreturn.} =
raise newException(EIO, msg)
proc readFile(filename: string): string =
var f = open(filename)
try:

View File

@@ -15,12 +15,10 @@
# we don't use refcounts because that's a behaviour
# the programmer may not want
# implementation:
proc resize(old: int): int {.inline.} =
if old <= 0: return 4
elif old < 65536: return old * 2
else: return old * 3 div 2 # for large arrays * 3/2 is better
if old <= 0: result = 4
elif old < 65536: result = old * 2
else: result = old * 3 div 2 # for large arrays * 3/2 is better
proc cmpStrings(a, b: NimString): int {.inline, compilerProc.} =
if a == b: return 0
@@ -53,13 +51,13 @@ proc toNimStr(str: CString, len: int): NimString {.compilerProc.} =
result.data[len] = '\0' # readline relies on this!
proc cstrToNimstr(str: CString): NimString {.compilerProc.} =
return toNimstr(str, c_strlen(str))
result = toNimstr(str, c_strlen(str))
proc copyString(src: NimString): NimString {.compilerProc.} =
if src == nil: return nil
result = rawNewString(src.space)
result.len = src.len
c_memcpy(result.data, src.data, (src.len + 1) * sizeof(Char))
if src != nil:
result = rawNewString(src.space)
result.len = src.len
c_memcpy(result.data, src.data, (src.len + 1) * sizeof(Char))
proc hashString(s: string): int {.compilerproc.} =
# the compiler needs exactly the same hash function!
@@ -86,7 +84,7 @@ proc copyStrLast(s: NimString, start, last: int): NimString {.exportc.} =
result = mnewString(0)
proc copyStr(s: NimString, start: int): NimString {.exportc.} =
return copyStrLast(s, start, s.len-1)
result = copyStrLast(s, start, s.len-1)
proc addChar(s: NimString, c: char): NimString =
# is compilerproc!
@@ -135,7 +133,7 @@ proc addChar(s: NimString, c: char): NimString =
# s = rawNewString(0);
proc resizeString(dest: NimString, addlen: int): NimString {.compilerproc.} =
if dest.len + addLen + 1 <= dest.space: # BUGFIX: this is horrible!
if dest.len + addLen + 1 <= dest.space:
result = dest
else: # slow path:
var sp = max(resize(dest.space), dest.len + addLen + 1)
@@ -279,12 +277,12 @@ proc binaryStrSearch(x: openarray[string], y: string): int {.compilerproc.} =
a = 0
b = len(x)
while a < b:
var mid = (a + b) div 2
if x[mid] < y:
a = mid + 1
else:
b = mid
if (a < len(x)) and (x[a] == y):
return a
var mid = (a + b) div 2
if x[mid] < y:
a = mid + 1
else:
b = mid
if a < len(x) and x[a] == y:
result = a
else:
return -1
result = -1

View File

@@ -339,14 +339,16 @@ proc joinThreads*[TParam](t: openArray[TThread[TParam]]) =
else:
for i in 0..t.high: joinThread(t[i])
proc destroyThread*[TParam](t: var TThread[TParam]) {.inline.} =
## forces the thread `t` to terminate. This is potentially dangerous if
## you don't have full control over `t` and its acquired resources.
when hostOS == "windows":
discard TerminateThread(t.sys, 1'i32)
else:
discard pthread_cancel(t.sys)
unregisterThread(addr(t.gcInfo))
when false:
# XXX a thread should really release its heap here somehow:
proc destroyThread*[TParam](t: var TThread[TParam]) {.inline.} =
## forces the thread `t` to terminate. This is potentially dangerous if
## you don't have full control over `t` and its acquired resources.
when hostOS == "windows":
discard TerminateThread(t.sys, 1'i32)
else:
discard pthread_cancel(t.sys)
unregisterThread(addr(t))
proc createThread*[TParam](t: var TThread[TParam],
tp: proc (param: TParam),