tiny C backend for a much faster REPL

This commit is contained in:
Andreas Rumpf 2010-04-02 19:34:57 +02:00
commit f530bbd631
12 changed files with 208 additions and 53 deletions

View file

@ -1,7 +1,7 @@
#
#
# The Nimrod Compiler
# (c) Copyright 2009 Andreas Rumpf
# (c) Copyright 2010 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
@ -15,6 +15,9 @@ import
nversion, nimsets, msgs, crc, bitsets, idents, lists, types, ccgutils, os,
times, ropes, math, passes, rodread, wordrecg, rnimsyn, treetab, cgmeth
when options.hasTinyCBackend:
import tccgen
proc cgenPass*(): TPass
# implementation
@ -518,7 +521,8 @@ proc genProcAux(m: BModule, prc: PSym) =
if p.beforeRetNeeded: app(generatedProc, "BeforeRet: ;" & tnl)
if optStackTrace in prc.options: app(generatedProc, deinitFrame(p))
if (optProfiler in prc.options) and (gCmd != cmdCompileToLLVM):
appf(generatedProc, "profileData[$1].total += elapsed(getticks(), NIM_profilingStart);$n",
appf(generatedProc,
"profileData[$1].total += elapsed(getticks(), NIM_profilingStart);$n",
[toRope(prc.loc.a)])
app(generatedProc, returnStmt)
app(generatedProc, '}' & tnl)
@ -600,13 +604,15 @@ proc genConstPrototype(m: BModule, sym: PSym) =
proc getFileHeader(cfilenoext: string): PRope =
if optCompileOnly in gGlobalOptions:
result = ropeff("/* Generated by Nimrod Compiler v$1 */$n" &
"/* (c) 2009 Andreas Rumpf */$n", "; Generated by Nimrod Compiler v$1$n" &
"; (c) 2009 Andreas Rumpf$n", [toRope(versionAsString)])
"/* (c) 2010 Andreas Rumpf */$n",
"; Generated by Nimrod Compiler v$1$n" &
"; (c) 2010 Andreas Rumpf$n", [toRope(versionAsString)])
else:
result = ropeff("/* Generated by Nimrod Compiler v$1 */$n" &
"/* (c) 2009 Andreas Rumpf */$n" & "/* Compiled for: $2, $3, $4 */$n" &
"/* Command for C compiler:$n $5 */$n", "; Generated by Nimrod Compiler v$1$n" &
"; (c) 2009 Andreas Rumpf$n" & "; Compiled for: $2, $3, $4$n" &
"/* (c) 2010 Andreas Rumpf */$n" & "/* Compiled for: $2, $3, $4 */$n" &
"/* Command for C compiler:$n $5 */$n",
"; Generated by Nimrod Compiler v$1$n" &
"; (c) 2010 Andreas Rumpf$n" & "; Compiled for: $2, $3, $4$n" &
"; Command for LLVM compiler:$n $5$n", [toRope(versionAsString),
toRope(platform.OS[targetOS].name),
toRope(platform.CPU[targetCPU].name),
@ -668,7 +674,8 @@ proc genMainProc(m: BModule) =
" LPVOID lpvReserved) {$n" & " NimMain();$n" &
" return 1;$n" & "}$n"
WinNimDllMainLLVM = WinNimMainLLVM
WinCDllMainLLVM = "define stdcall i32 @DllMain(i32 %hinstDLL, i32 %fwdreason, $n" &
WinCDllMainLLVM =
"define stdcall i32 @DllMain(i32 %hinstDLL, i32 %fwdreason, $n" &
" i8* %lpvReserved) {$n" &
" call void @NimMain()$n" & " ret i32 1$n" & "}$n"
var nimMain, otherMain: TFormatStr
@ -795,13 +802,13 @@ proc myOpen(module: PSym, filename: string): PPassContext =
if gNimDat == nil: registerTypeInfoModule()
result = newModule(module, filename)
proc myOpenCached(module: PSym, filename: string, rd: PRodReader): PPassContext =
var cfile, cfilenoext, objFile: string
proc myOpenCached(module: PSym, filename: string,
rd: PRodReader): PPassContext =
if gNimDat == nil:
registerTypeInfoModule()
#MessageOut('cgen.myOpenCached has been called ' + filename);
cfile = changeFileExt(completeCFilePath(filename), cExt)
cfilenoext = changeFileExt(cfile, "")
var cfile = changeFileExt(completeCFilePath(filename), cExt)
var cfilenoext = changeFileExt(cfile, "")
addFileToLink(cfilenoext)
registerModuleToMain(module)
# XXX: this cannot be right here, initalization has to be appended during
@ -838,18 +845,21 @@ proc finishModule(m: BModule) =
setlen(m.forwardedProcs, 0)
proc writeModule(m: BModule) =
var
cfile, cfilenoext: string
code: PRope
# generate code for the init statements of the module:
genInitCode(m)
finishTypeDescriptions(m)
cfile = completeCFilePath(m.cfilename)
cfilenoext = changeFileExt(cfile, "")
var cfile = completeCFilePath(m.cfilename)
var cfilenoext = changeFileExt(cfile, "")
if sfMainModule in m.module.flags:
# generate main file:
app(m.s[cfsProcHeaders], mainModProcs)
code = genModule(m, cfilenoext)
var code = genModule(m, cfilenoext)
when hasTinyCBackend:
if gCmd == cmdRun:
tccgen.compileCCode(ropeToStr(code))
return
if shouldRecompile(code, changeFileExt(cfile, cExt), cfilenoext):
addFileToCompile(cfilenoext)
addFileToLink(cfilenoext)
@ -867,7 +877,7 @@ proc myClose(b: PPassContext, n: PNode): PNode =
finishModule(m)
if sfMainModule in m.module.flags:
var disp = generateMethodDispatchers()
for i in countup(0, sonsLen(disp) - 1): genProcAux(gNimDat, disp.sons[i].sym)
for i in 0..sonsLen(disp)-1: genProcAux(gNimDat, disp.sons[i].sym)
genMainProc(m)
# we need to process the transitive closure because recursive module
# deps are allowed (and the system module is processed in the wrong