summary refs log tree commit diff stats
path: root/compiler/ccgmerge.nim
diff options
context:
space:
mode:
authorAraq <rumpf_a@web.de>2011-10-21 19:07:01 +0200
committerAraq <rumpf_a@web.de>2011-10-21 19:07:01 +0200
commit43eace163a03170475b70a12b24c374daef7e43a (patch)
treed75ab0feb17b0e831980c76d583c621847f16708 /compiler/ccgmerge.nim
parenta6f90d4cdd397017436d385fbf2b54d80e4c1a77 (diff)
downloadNim-43eace163a03170475b70a12b24c374daef7e43a.tar.gz
further steps for incremental C code generation
Diffstat (limited to 'compiler/ccgmerge.nim')
-rw-r--r--compiler/ccgmerge.nim47
1 files changed, 40 insertions, 7 deletions
diff --git a/compiler/ccgmerge.nim b/compiler/ccgmerge.nim
index 252b11079..3103edc63 100644
--- a/compiler/ccgmerge.nim
+++ b/compiler/ccgmerge.nim
@@ -220,8 +220,7 @@ proc processMergeInfo(L: var TBaseLexer, m: BModule) =
     of "labels":    m.labels = decodeVInt(L.buf, L.bufpos)
     else: InternalError("ccgmerge: unkown key: " & k)
   
-proc readMergeInfo*(cfilename: string, m: BModule) =
-  ## reads the merge information into `m`.
+template withCFile(cfilename: string, body: stmt) = 
   var s = LLStreamOpen(cfilename, fmRead)
   if s == nil: return
   var L: TBaseLexer
@@ -229,9 +228,28 @@ proc readMergeInfo*(cfilename: string, m: BModule) =
   while true:
     skipUntilCmd(L)
     if ^L.bufpos == '\0': break
+    body
+  closeBaseLexer(L)
+  
+proc readMergeInfo*(cfilename: string, m: BModule) =
+  ## reads the merge meta information into `m`.
+  withCFile(cfilename):
     var k = readKey(L)
-    if k == "NIM_merge_INFO":   
+    if k == "NIM_merge_INFO":
       processMergeInfo(L, m)
+      break
+
+type
+  TMergeSections = object {.pure.}
+    f: TCFileSections
+    p: TCProcSections
+
+proc readMergeSections(cfilename: string, m: var TMergeSections) =
+  ## reads the merge sections into `m`.
+  withCFile(cfilename):
+    var k = readKey(L)
+    if k == "NIM_merge_INFO":   
+      nil
     elif ^L.bufpos == '*' and ^(L.bufpos+1) == '/':
       inc(L.bufpos, 2)
       # read back into section
@@ -240,14 +258,29 @@ proc readMergeInfo*(cfilename: string, m: BModule) =
       skipWhite(L)
       var sectionA = CFileSectionNames.find(k)
       if sectionA > 0 and sectionA <= high(TCFileSection).int:
-        m.s[TCFileSection(sectionA)] = verbatim
+        m.f[TCFileSection(sectionA)] = verbatim
       else:
         var sectionB = CProcSectionNames.find(k)
         if sectionB >= 0 and sectionB <= high(TCProcSection).int:
-          m.initProc.s[TCProcSection(sectionB)] = verbatim
+          m.p[TCProcSection(sectionB)] = verbatim
         else:
           InternalError("ccgmerge: unknown section: " & k)
     else:
-      InternalError("ccgmerge: */ expected")
-  closeBaseLexer(L)
+      InternalError("ccgmerge: '*/' expected")
+
+proc mergeRequired*(m: BModule): bool =
+  for i in low(TCFileSection)..high(TCFileSection):
+    if m.s[i] != nil: return true
+  for i in low(TCProcSection)..high(TCProcSection):
+    if m.initProc.s[i] != nil: return true
+
+proc mergeFiles*(cfilename: string, m: BModule) =
+  ## merges the C file with the old version on hard disc.
+  var old: TMergeSections
+  readMergeSections(cfilename, old)  
+  # do the merge; old section before new section:    
+  for i in low(TCFileSection)..high(TCFileSection):
+    m.s[i] = con(old.f[i], m.s[i])
+  for i in low(TCProcSection)..high(TCProcSection):
+    m.initProc.s[i] = con(old.p[i], m.initProc.s[i])