1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#
#
# The Nimrod Compiler
# (c) Copyright 2012 Andreas Rumpf
#
# See the file "copying.txt", included in this
# distribution, for details about the copyright.
#
import
os, strutils, options, msgs, tinyc
{.compile: "../tinyc/libtcc.c".}
proc tinyCErrorHandler(closure: pointer, msg: cstring) {.cdecl.} =
rawMessage(errGenerated, $msg)
proc initTinyCState: PccState =
result = openCCState()
setErrorFunc(result, nil, tinyCErrorHandler)
var
gTinyC = initTinyCState()
libIncluded = false
proc addFile(filename: string) =
if addFile(gTinyC, filename) != 0'i32:
rawMessage(errCannotOpenFile, filename)
proc setupEnvironment =
when defined(amd64):
defineSymbol(gTinyC, "__x86_64__", nil)
elif defined(i386):
defineSymbol(gTinyC, "__i386__", nil)
when defined(linux):
defineSymbol(gTinyC, "__linux__", nil)
defineSymbol(gTinyC, "__linux", nil)
var nimrodDir = getPrefixDir()
addIncludePath(gTinyC, libpath)
when defined(windows):
addSysincludePath(gTinyC, nimrodDir / "tinyc/win32/include")
addSysincludePath(gTinyC, nimrodDir / "tinyc/include")
when defined(windows):
defineSymbol(gTinyC, "_WIN32", nil)
# we need Mingw's headers too:
var gccbin = getConfigVar("gcc.path") % ["nimrod", nimrodDir]
addSysincludePath(gTinyC, gccbin /../ "include")
#addFile(nimrodDir / r"tinyc\win32\wincrt1.o")
addFile(nimrodDir / r"tinyc\win32\alloca86.o")
addFile(nimrodDir / r"tinyc\win32\chkstk.o")
#addFile(nimrodDir / r"tinyc\win32\crt1.o")
#addFile(nimrodDir / r"tinyc\win32\dllcrt1.o")
#addFile(nimrodDir / r"tinyc\win32\dllmain.o")
addFile(nimrodDir / r"tinyc\win32\libtcc1.o")
#addFile(nimrodDir / r"tinyc\win32\lib\crt1.c")
#addFile(nimrodDir / r"tinyc\lib\libtcc1.c")
else:
addSysincludePath(gTinyC, "/usr/include")
proc compileCCode*(ccode: string) =
if not libIncluded:
libIncluded = true
setupEnvironment()
discard compileString(gTinyC, ccode)
proc run*() =
var a: array[0..1, cstring]
a[0] = ""
a[1] = ""
var err = tinyc.run(gTinyC, 0'i32, addr(a)) != 0'i32
closeCCState(gTinyC)
if err: rawMessage(errExecutionOfProgramFailed, "")
|