diff --git a/Makefile b/Makefile index b93e1719..dcf58112 100644 --- a/Makefile +++ b/Makefile @@ -12,7 +12,7 @@ build/tgo: *.go @mkdir -p build @go build -o build/tgo -i . -build/hello.o: build/tgo src/examples/hello/*.go +build/hello.o: build/tgo src/examples/hello/*.go src/runtime/*.go @./build/tgo -printir -o build/hello.o examples/hello build/runtime.o: src/runtime/*.c src/runtime/*.h diff --git a/src/runtime/runtime.c b/src/runtime/runtime.c index 93ff2024..ecad8e0f 100644 --- a/src/runtime/runtime.c +++ b/src/runtime/runtime.c @@ -1,43 +1,6 @@ -#include -#include -#include #include "runtime.h" -void __go_printstring(string_t str) { - for (int i = 0; i < str.len; i++) { - putchar(str.buf[i]); - } -} - -void __go_printint(intgo_t n) { - // Print integer in signed big-endian base-10 notation, for humans to - // read. - // TODO: don't recurse, but still be compact (and don't divide/mod - // more than necessary). - if (n < 0) { - putchar('-'); - n = -n; - } - intgo_t prevdigits = n / 10; - if (prevdigits != 0) { - __go_printint(prevdigits); - } - putchar((n % 10) + '0'); -} - -void __go_printbyte(uint8_t c) { - putchar(c); -} - -void __go_printspace() { - putchar(' '); -} - -void __go_printnl() { - putchar('\n'); -} - void go_main() __asm__("main.main"); void __go_runtime_main() { diff --git a/src/runtime/runtime.go b/src/runtime/runtime.go new file mode 100644 index 00000000..c891641c --- /dev/null +++ b/src/runtime/runtime.go @@ -0,0 +1,41 @@ + +package runtime + +// #include +import "C" + +const Compiler = "tgo" + +func printstring(s string) { + for i := 0; i < len(s); i++ { + C.putchar(C.int(s[i])) + } +} + +func printint(n int) { + // Print integer in signed big-endian base-10 notation, for humans to + // read. + // TODO: don't recurse, but still be compact (and don't divide/mod + // more than necessary). + if n < 0 { + C.putchar('-') + n = -n + } + prevdigits := n / 10 + if prevdigits != 0 { + printint(prevdigits) + } + C.putchar(C.int((n % 10) + '0')) +} + +func printbyte(c uint8) { + C.putchar(C.int(c)) +} + +func printspace() { + C.putchar(' ') +} + +func printnl() { + C.putchar('\n') +} diff --git a/tgo.go b/tgo.go index 2aa0df28..ef00c4df 100644 --- a/tgo.go +++ b/tgo.go @@ -97,15 +97,15 @@ func NewCompiler(pkgName, triple string) (*Compiler, error) { c.typeassertType = llvm.StructType([]llvm.Type{llvm.PointerType(llvm.Int8Type(), 0), llvm.Int1Type()}, false) printstringType := llvm.FunctionType(llvm.VoidType(), []llvm.Type{c.stringType}, false) - c.printstringFunc = llvm.AddFunction(c.mod, "__go_printstring", printstringType) + c.printstringFunc = llvm.AddFunction(c.mod, "runtime.printstring", printstringType) printintType := llvm.FunctionType(llvm.VoidType(), []llvm.Type{c.intType}, false) - c.printintFunc = llvm.AddFunction(c.mod, "__go_printint", printintType) + c.printintFunc = llvm.AddFunction(c.mod, "runtime.printint", printintType) printbyteType := llvm.FunctionType(llvm.VoidType(), []llvm.Type{llvm.Int8Type()}, false) - c.printbyteFunc = llvm.AddFunction(c.mod, "__go_printbyte", printbyteType) + c.printbyteFunc = llvm.AddFunction(c.mod, "runtime.printbyte", printbyteType) printspaceType := llvm.FunctionType(llvm.VoidType(), nil, false) - c.printspaceFunc = llvm.AddFunction(c.mod, "__go_printspace", printspaceType) + c.printspaceFunc = llvm.AddFunction(c.mod, "runtime.printspace", printspaceType) printnlType := llvm.FunctionType(llvm.VoidType(), nil, false) - c.printnlFunc = llvm.AddFunction(c.mod, "__go_printnl", printnlType) + c.printnlFunc = llvm.AddFunction(c.mod, "runtime.printnl", printnlType) return c, nil } @@ -126,6 +126,7 @@ func (c *Compiler) Parse(pkgName string) error { }, AllowErrors: true, } + config.Import("runtime") config.Import(pkgName) lprogram, err := config.Load() if err != nil {