summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rwxr-xr-xcompiler/ccgexprs.nim10
-rwxr-xr-xcompiler/cgen.nim24
-rw-r--r--compiler/cgendata.nim5
-rwxr-xr-xcompiler/lexer.nim6
-rwxr-xr-xcompiler/main.nim27
-rwxr-xr-xlib/system/gc.nim99
-rwxr-xr-xlib/system/mmdisp.nim6
-rwxr-xr-xlib/system/sysstr.nim1
8 files changed, 67 insertions, 111 deletions
diff --git a/compiler/ccgexprs.nim b/compiler/ccgexprs.nim
index 67d7b1a38..1ac3dad7d 100755
--- a/compiler/ccgexprs.nim
+++ b/compiler/ccgexprs.nim
@@ -188,19 +188,9 @@ proc genRefAssign(p: BProc, dest, src: TLoc, flags: TAssignmentFlags) =
       linefmt(p, cpsStmts, "#asgnRef((void**) $1, $2);$n",
               addrLoc(dest), rdLoc(src))
     else:
-      if cnimdbg:
-        echo "ASSIGN REF ", dest.k, " ", dest.s, " ", dest.r.ropeToStr
-        if dest.heapRoot != nil:
-          echo "ROOTED AT ", dest.heapRoot.ropeToStr
-
       linefmt(p, cpsStmts, "#asgnRefNoCycle((void**) $1, $2);$n",
               addrLoc(dest), rdLoc(src))
   else:
-    if cnimdbg:
-      echo "ASSIGN REF ", dest.k, " ", dest.s, " ", dest.r.ropeToStr
-      if dest.heapRoot != nil:
-        echo "ROOTED AT ", dest.heapRoot.ropeToStr
-        
     linefmt(p, cpsStmts, "#unsureAsgnRef((void**) $1, $2);$n",
             addrLoc(dest), rdLoc(src))
     if needToKeepAlive in flags: keepAlive(p, dest)
diff --git a/compiler/cgen.nim b/compiler/cgen.nim
index fafa08ef4..71d55c879 100755
--- a/compiler/cgen.nim
+++ b/compiler/cgen.nim
@@ -1133,18 +1133,16 @@ proc rawNewModule(module: PSym): BModule =
   result = rawNewModule(module, module.filename)
 
 proc newModule(module: PSym): BModule =
-  result = getCgenModule(module)
-  if result == nil:
-    result = rawNewModule(module)
-    growCache gModules, module.position
-    gModules[module.position] = result
-
-    if (optDeadCodeElim in gGlobalOptions): 
-      if (sfDeadCodeElim in module.flags): 
-        InternalError("added pending module twice: " & module.filename)
-  else:
-    echo "CGEN CACHED MODULE: ", result.filename
-    assert optCaasEnabled in gGlobalOptions
+  # we should create only one cgen module for each module sym
+  InternalAssert getCgenModule(module) == nil
+
+  result = rawNewModule(module)
+  growCache gModules, module.position
+  gModules[module.position] = result
+
+  if (optDeadCodeElim in gGlobalOptions): 
+    if (sfDeadCodeElim in module.flags): 
+      InternalError("added pending module twice: " & module.filename)
 
 proc myOpen(module: PSym): PPassContext = 
   result = newModule(module)
@@ -1260,7 +1258,6 @@ proc updateCachedModule(m: BModule) =
   let cfilenoext = changeFileExt(cfile, "")
   
   if mergeRequired(m):
-    echo "MERGE REQUIRED FOR ", m.filename
     mergeFiles(cfile, m)
     genInitCode(m)
     finishTypeDescriptions(m)
@@ -1271,6 +1268,7 @@ proc updateCachedModule(m: BModule) =
   addFileToLink(cfilenoext)
 
 proc cgenCaasUpdate* =
+  # XXX(zah): clean-up the fromCache mess
   for m in cgenModules():
     if m.fromCache: m.updateCachedModule
 
diff --git a/compiler/cgendata.nim b/compiler/cgendata.nim
index 846acbd2c..32e9fb4ce 100644
--- a/compiler/cgendata.nim
+++ b/compiler/cgendata.nim
@@ -143,7 +143,8 @@ proc newProc*(prc: PSym, module: BModule): BProc =
 
 iterator cgenModules*: var BModule =
   for i in 0..high(gModules):
-    # some modules (like stdin) may exist only in memory.
-    # they won't have a cgen BModule for them and we must skip them.
+    # ultimately, we are iterating over the file ids here.
+    # some "files" won't have an associated cgen module (like stdin)
+    # and we must skip over them.
     if gModules[i] != nil: yield gModules[i]
 
diff --git a/compiler/lexer.nim b/compiler/lexer.nim
index 89a9497df..02f63aee8 100755
--- a/compiler/lexer.nim
+++ b/compiler/lexer.nim
@@ -509,10 +509,10 @@ proc getEscapedChar(L: var TLexer, tok: var TToken) =
     else: lexMessage(L, errInvalidCharacterConstant)
   else: lexMessage(L, errInvalidCharacterConstant)
 
-proc newString(s: cstring, l: int): string =
+proc newString(s: cstring, len: int): string =
   ## XXX, how come there is no support for this?
-  result = newString(l)
-  for i in 0 .. <l:
+  result = newString(len)
+  for i in 0 .. <len:
     result[i] = s[i]
 
 proc HandleCRLF(L: var TLexer, pos: int): int =
diff --git a/compiler/main.nim b/compiler/main.nim
index 1307fa6b7..7bb277812 100755
--- a/compiler/main.nim
+++ b/compiler/main.nim
@@ -442,9 +442,14 @@ proc resetMemory =
   GC_fullCollect()
   echo GC_getStatistics()
 
+const
+  SimiluateCaasMemReset = false
+  PrintRopeCacheStats = false
+
 proc MainCommand =
-  gGlobalOptions.incl(optCaasEnabled)
-    
+  when SimiluateCaasMemReset:
+    gGlobalOptions.incl(optCaasEnabled)
+      
   # In "nimrod serve" scenario, each command must reset the registered passes
   clearPasses()
   gLastCmdTime = epochTime()
@@ -561,19 +566,21 @@ proc MainCommand =
     gGlobalOptions.incl(optCaasEnabled)
     msgs.gErrorMax = high(int)  # do not stop after first error     
     serve(MainCommand)
-    
-  else: rawMessage(errInvalidCommandX, command)
+  else:
+    rawMessage(errInvalidCommandX, command)
   
   if msgs.gErrorCounter == 0 and gCmd notin {cmdInterpret, cmdRun}:
     rawMessage(hintSuccessX, [$gLinesCompiled,
                formatFloat(epochTime() - gLastCmdTime, ffDecimal, 3),
                formatSize(getTotalMem())])
 
-  echo "rope cache stats: "
-  echo "  tries : ", gCacheTries
-  echo "  misses: ", gCacheMisses
-  echo "  int tries: ", gCacheIntTries
-  echo "  efficiency: ", formatFloat(1-(gCacheMisses.float/gCacheTries.float), ffDecimal, 3)
+  when PrintRopeCacheStats:
+    echo "rope cache stats: "
+    echo "  tries : ", gCacheTries
+    echo "  misses: ", gCacheMisses
+    echo "  int tries: ", gCacheIntTries
+    echo "  efficiency: ", formatFloat(1-(gCacheMisses.float/gCacheTries.float), ffDecimal, 3)
 
-  # resetMemory()
+  when SimiluateCaasMemReset:
+    resetMemory()
 
diff --git a/lib/system/gc.nim b/lib/system/gc.nim
index b870e9f8e..a5cd9a2b0 100755
--- a/lib/system/gc.nim
+++ b/lib/system/gc.nim
@@ -96,6 +96,8 @@ const
     # XXX: still has problems in release mode in the compiler itself.
     # investigate how it affects growObj
 
+  CollectCyclesStats = false
+
 type
   TWalkOp = enum
     waPush
@@ -203,8 +205,6 @@ proc addZCT(zct: var TCellSeq, c: PCell) {.noinline.} =
   if c.isBitDown(rcZct):
     c.setBit rcZct
     zct.add c
-    # writecell("adding to ZCT 1", c)
-    # cprintf ("called from %d\n", framePtr.prev.line)
 
 template setStackTop(gch) =
   # This must be called immediately after we enter the GC code
@@ -257,7 +257,6 @@ proc forAllChildrenAux(dest: Pointer, mt: PNimType, op: TWalkOp)
 # we need the prototype here for debugging purposes
 
 proc prepareDealloc(cell: PCell) =
-  # writecell("finalizers", cell)
   if cell.typ.finalizer != nil:
     # the finalizer could invoke something that
     # allocates memory; this could trigger a garbage
@@ -397,7 +396,6 @@ template doDecRef(cc: PCell,
     # this is the last reference from the heap
     # add to a zero-count-table that will be matched against stack pointers
     rtlAddZCT(c)
-    # writeCell("decref to 0", c)
   else:
     when cycleFlag != Acyclic:
       if cycleFlag == Cyclic or canBeCycleRoot(c):
@@ -559,7 +557,6 @@ proc addNewObjToZCT(res: PCell, gch: var TGcHeap) {.inline.} =
   # all slots             68%
   var L = gch.zct.len
   var d = gch.zct.d
-  #writecell("ZCT ADDING 2", res)
   when true:
     # loop unrolled for performance:
     template replaceZctEntry(i: expr) =
@@ -622,7 +619,6 @@ proc rawNewObj(typ: PNimType, size: int, gch: var TGcHeap, rc1: bool): pointer =
     addNewObjToZCT(res, gch)
 
     if NewObjectsAreCycleRoots and canBeCycleRoot(res):
-      # writeCell("cyclic allocation", res)
       res.setBit(rcInCycleRoots)
       res.setColor rcCycleCandidate
       gch.cycleRoots.add res
@@ -647,7 +643,6 @@ proc freeCell(gch: var TGcHeap, c: PCell) =
   else:
     sysAssert(c.typ != nil, "collectCycles")
     zeroMem(c, sizeof(TCell))
-    # writecell("nuked cell", c)
 
 template eraseAt(cells: var TCellSeq, at: int): stmt =
   cells.d[at] = cells.d[cells.len - 1]
@@ -706,16 +701,13 @@ proc growObj(old: pointer, newsize: int, gch: var TGcHeap): pointer =
       var d = gch.zct.d
       while j >= 0: 
         if d[j] == ol:
-          #writecell("replaced old", ol)
           d[j] = res
-          #writecell("replaced new", res)
           break
         dec(j)
     
     if ol.isBitUp(rcInCycleRoots):
       for i in 0 .. <gch.cycleRoots.len:
         if gch.cycleRoots.d[i] == ol:
-          #writecell("evicted cycleroot", ol)
           eraseAt(gch.cycleRoots, i)
 
     freeCell(gch, ol)
@@ -785,11 +777,9 @@ proc collectCycles(gch: var TGcHeap) =
   if gch.cycleRoots.len == 0: return
   gch.stat.cycleTableSize = max(gch.stat.cycleTableSize, gch.cycleRoots.len)
 
-  #c_printf "collect cycles table:\n"
-  #for i in 0 .. <gch.cycleRoots.len:
-  #  writecell("CROOT ", gch.cycleRoots.d[i])
-  let l0 = gch.cycleRoots.len
-  let tStart = getTicks()
+  when CollectCyclesStats:
+    let l0 = gch.cycleRoots.len
+    let tStart = getTicks()
 
   var
     decrefs = 0
@@ -826,7 +816,9 @@ proc collectCycles(gch: var TGcHeap) =
       earlyMarkAliveRec(c)
 
   earlyMarkAlive(gch.decStack)
-  let tAfterEarlyMarkAlive = getTicks()
+  
+  when CollectCyclesStats:
+    let tAfterEarlyMarkAlive = getTicks()
 
   template recursiveDecRef(cell) =
     let startLen = gch.tempStack.len
@@ -843,7 +835,6 @@ proc collectCycles(gch: var TGcHeap) =
       inc decrefs
       if c.color != rcDecRefApplied:
         c.setColor rcDecRefApplied
-        # writeCell("decref", c)
         c.forAllChildren waPush
  
   template markRoots(roots) =
@@ -862,9 +853,10 @@ proc collectCycles(gch: var TGcHeap) =
           freeCell(gch, c)
   
   markRoots(gch.cycleRoots)
-  let tAfterMark = getTicks()
-
-  c_printf "COLLECT CYCLES %d: %d/%d\n", gcCollectionIdx, gch.cycleRoots.len, l0
+  
+  when CollectCyclesStats:
+    let tAfterMark = getTicks()
+    c_printf "COLLECT CYCLES %d: %d/%d\n", gcCollectionIdx, gch.cycleRoots.len, l0
   
   template recursiveMarkAlive(cell) =
     let startLen = gch.tempStack.len
@@ -877,7 +869,6 @@ proc collectCycles(gch: var TGcHeap) =
       if ignoreObject(c): continue
       inc c.refcount, rcIncrement
       inc increfs
-      # writeCell("mark alive", c)
       
       if c.color != rcAlive:
         c.setColor rcAlive
@@ -905,7 +896,9 @@ proc collectCycles(gch: var TGcHeap) =
             c.forAllChildren waPush
   
   scanRoots(gch.cycleRoots)
-  let tAfterScan = getTicks()
+  
+  when CollectCyclesStats:
+    let tAfterScan = getTicks()
 
   template collectDead(roots) =
     for i in 0 .. <roots.len:
@@ -938,17 +931,18 @@ proc collectCycles(gch: var TGcHeap) =
       freeCell(gch, gch.freeStack.d[i])
 
   collectDead(gch.cycleRoots)
-  let tFinal = getTicks()
   
-  cprintf "times:\n  early mark alive: %d ms\n  mark: %d ms\n  scan: %d ms\n  collect: %d ms\n  decrefs: %d\n  increfs: %d\n  marked dead: %d\n  collected: %d\n",
-    (tAfterEarlyMarkAlive - tStart)  div 1_000_000,
-    (tAfterMark - tAfterEarlyMarkAlive) div 1_000_000,
-    (tAfterScan - tAfterMark) div 1_000_000,
-    (tFinal - tAfterScan) div 1_000_000,
-    decrefs,
-    increfs,
-    maybedeads,
-    collected
+  when CollectCyclesStats:
+    let tFinal = getTicks()
+    cprintf "times:\n  early mark alive: %d ms\n  mark: %d ms\n  scan: %d ms\n  collect: %d ms\n  decrefs: %d\n  increfs: %d\n  marked dead: %d\n  collected: %d\n",
+      (tAfterEarlyMarkAlive - tStart)  div 1_000_000,
+      (tAfterMark - tAfterEarlyMarkAlive) div 1_000_000,
+      (tAfterScan - tAfterMark) div 1_000_000,
+      (tFinal - tAfterScan) div 1_000_000,
+      decrefs,
+      increfs,
+      maybedeads,
+      collected
 
   Deinit(gch.cycleRoots)
   Init(gch.cycleRoots)
@@ -963,20 +957,7 @@ proc collectCycles(gch: var TGcHeap) =
     # CollectZCT may add new cycle candidates and we may decide to loop here
     # if gch.cycleRoots.len > 0: repeat
 
-  # quit 1
-
 var gcDebugging* = false
-var vis*: proc (a: pointer, b: PNimType)
-
-proc debugNode(n: ptr TNimNode) =
-  c_fprintf(c_stdout, "node %s\n", n.name)
-  for i in 0..n.len-1:
-    debugNode(n.sons[i])
-
-proc debugTyp(x: PNimType) =
-  c_fprintf(c_stdout, "type %d\n", x.kind)
-  if x.node != nil:
-    debugNode(x.node)
 
 var seqdbg* : proc (s: PGenericSeq) {.cdecl.}
 
@@ -992,7 +973,7 @@ proc gcMark(gch: var TGcHeap, p: pointer) {.inline.} =
       # mark the cell:
       if objStart.isBitDown(rcReallyDead):
         if gcDebugging:
-          writeCell("marking ", objStart)
+          # writeCell("marking ", objStart)
         else:
           inc objStart.refcount, rcIncrement
           gch.decStack.add objStart
@@ -1174,18 +1155,14 @@ proc releaseCell(gch: var TGcHeap, cell: PCell) =
     prepareDealloc(cell)
     cell.setColor rcReallyDead
 
-    #writecell("RELEASING ", cell)
-
     let l1 = gch.tempStack.len
     cell.forAllChildren waPush
     let l2 = gch.tempStack.len
     for i in l1 .. <l2:
       var cc = gch.tempStack.d[i]
-      #writecell("SON ", cc)
       if cc.refcount--(LocalHeap):
         releaseCell(gch, cc)
       else:
-        #writecell("crashy", cc)
         if canbeCycleRoot(cc):
           addCycleRoot(gch.cycleRoots, cc)
 
@@ -1203,10 +1180,6 @@ proc releaseCell(gch: var TGcHeap, cell: PCell) =
   # We can ignore it now as the ZCT cleaner will reach it soon.
 
 proc CollectZCT(gch: var TGcHeap): bool =
-  #cprintf "ZCT TABLE START:\n"
-  #for i in 0 .. <gch.zct.len:
-  #  writecell("ZCT CELL", gch.zct.d[i])
-  #cprintf "ZCT TABLE END\n"
   const workPackage = 100
   var L = addr(gch.zct.len)
   
@@ -1217,14 +1190,10 @@ proc CollectZCT(gch: var TGcHeap): bool =
   
   while L[] > 0:
     var c = gch.zct.d[0]
-    if c.isBitDown(rcZct):
-      writecell("BAD ZCT", c)
-      quit 1
-    # writecell("ZCT PROCESS", c)
+    sysAssert c.isBitUp(rcZct), "CollectZCT: rcZct missing!"
     sysAssert(isAllocatedPtr(gch.region, c), "CollectZCT: isAllocatedPtr")
-    # remove from ZCT:
-    sysAssert((c.refcount and rcZct) == rcZct, "collectZCT")
     
+    # remove from ZCT:    
     c.clearBit(rcZct)
     gch.zct.d[0] = gch.zct.d[L[] - 1]
     dec(L[])
@@ -1238,13 +1207,9 @@ proc CollectZCT(gch: var TGcHeap): bool =
       # freed. **KEEP THIS IN MIND WHEN MAKING THIS INCREMENTAL!**
       if c.color == rcRetiredBuffer:
         if c.isBitDown(rcInCycleRoots):
-          # writecell("retired buffer", c)
           freeCell(gch, c)
       else:
-        if c.color == rcReallyDead:
-          # writeCell("ReallyDead in ZCT?", c)
-        
-        # writecell("bad cell in zct", c)
+        # if c.color == rcReallyDead: writeCell("ReallyDead in ZCT?", c)
         releaseCell(gch, c)
     when withRealtime:
       if steps == 0:
@@ -1270,7 +1235,6 @@ proc unmarkStackAndRegisters(gch: var TGcHeap) =
     sysAssert c.typ != nil, "unmarkStackAndRegisters 2"
     
     if c.color == rcRetiredBuffer:
-      # writecell("unmark retired", c)
       continue
 
     # XXX no need for an atomic dec here:
@@ -1284,11 +1248,8 @@ proc unmarkStackAndRegisters(gch: var TGcHeap) =
       # a leak if it's orphaned through the stack reference
       # that's because the write-barrier won't be executed for stack
       # locations
-      #writeCell("restoring balance cycle roots", c)
       addCycleRoot(gch.cycleRoots, c)
 
-    #writecell("unmark stack cell", c)
-
   gch.decStack.len = 0
 
 proc collectCTBody(gch: var TGcHeap) =
diff --git a/lib/system/mmdisp.nim b/lib/system/mmdisp.nim
index 28ef67781..9bf9c1e67 100755
--- a/lib/system/mmdisp.nim
+++ b/lib/system/mmdisp.nim
@@ -14,14 +14,14 @@
 {.push checks:off.}
 
 const
-  debugGC = true # we wish to debug the GC...
+  debugGC = false # we wish to debug the GC...
   logGC = false
-  traceGC = true # extensive debugging
+  traceGC = false # extensive debugging
   alwaysCycleGC = false
   alwaysGC = false # collect after every memory allocation (for debugging)
   leakDetector = false
   overwriteFree = false
-  trackAllocationSource = true or leakDetector
+  trackAllocationSource = leakDetector
   
   cycleGC = true # (de)activate the cycle GC
   reallyDealloc = true # for debugging purposes this can be set to false
diff --git a/lib/system/sysstr.nim b/lib/system/sysstr.nim
index 8314362f3..bd830d213 100755
--- a/lib/system/sysstr.nim
+++ b/lib/system/sysstr.nim
@@ -213,7 +213,6 @@ proc setLengthSeq(seq: PGenericSeq, elemSize, newLen: int): PGenericSeq {.
           doDecRef(gch.tempStack.d[i], LocalHeap, MaybeCyclic)
         gch.tempStack.len = len0
                           
-        # XXX add a proper addCycleRoot barrier here!
     # and set the memory to nil:
     zeroMem(cast[pointer](cast[TAddress](result) +% GenericSeqSize +%
            (newLen*%elemSize)), (result.len-%newLen) *% elemSize)