summary refs log tree commit diff stats
path: root/compiler/ccgstmts.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2013-02-07 01:57:10 +0100
committerAraq <rumpf_a@web.de>2013-02-07 01:57:10 +0100
commitab6f793408c10935bad98071bdae4009f6873d5c (patch)
tree99cec4dd7cd520e040c45927d0eab16b27c4270c /compiler/ccgstmts.nim
parentf96d612e980af065217cddaf0a8521189977a37b (diff)
downloadNim-ab6f793408c10935bad98071bdae4009f6873d5c.tar.gz
first version of a simple mark&sweep GC; activate with --gc:markAndSweep
Diffstat (limited to 'compiler/ccgstmts.nim')
-rwxr-xr-xcompiler/ccgstmts.nim13
1 files changed, 11 insertions, 2 deletions
diff --git a/compiler/ccgstmts.nim b/compiler/ccgstmts.nim
index 2f07d24cb..7084864ae 100755
--- a/compiler/ccgstmts.nim
+++ b/compiler/ccgstmts.nim
@@ -1,7 +1,7 @@
 #
 #
 #           The Nimrod Compiler
-#        (c) Copyright 2012 Andreas Rumpf
+#        (c) Copyright 2013 Andreas Rumpf
 #
 #    See the file "copying.txt", included in this
 #    distribution, for details about the copyright.
@@ -15,6 +15,14 @@ const
   stringCaseThreshold = 8
     # above X strings a hash-switch for strings is generated
 
+proc registerGcRoot(p: BProc, v: PSym) =
+  if gSelectedGc == gcMarkAndSweep and containsGarbageCollectedRef(v.loc.t):
+    # we register a specialized marked proc here; this has the advantage
+    # that it works out of the box for thread local storage then :-)
+    let prc = genTraverseProcForGlobal(p.module, v)
+    linefmt(p.module.initProc, cpsStmts,
+      "#nimRegisterGlobalMarker((void*)$1);$n", prc)
+
 proc genVarTuple(p: BProc, n: PNode) = 
   var tup, field: TLoc
   if n.kind != nkVarTuple: InternalError(n.info, "genVarTuple")
@@ -28,6 +36,7 @@ proc genVarTuple(p: BProc, n: PNode) =
     if sfGlobal in v.flags:
       assignGlobalVar(p, v)
       genObjectInit(p, cpsInit, v.typ, v.loc, true)
+      registerGcRoot(p, v)
     else:
       assignLocalVar(p, v)
       initLocalVar(p, v, immediateAsgn=true)
@@ -143,7 +152,7 @@ proc genSingleVar(p: BProc, a: PNode) =
     # if sfImportc notin v.flags: constructLoc(p.module.preInitProc, v.loc)
     if sfExportc in v.flags and generatedHeader != nil:
       genVarPrototypeAux(generatedHeader, v)
-
+    registerGcRoot(p, v)
   else:
     assignLocalVar(p, v)
     initLocalVar(p, v, immediateAsgn)