From 60a01a43a39dd9b4e8c87fd31402c01d4d7544d6 Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Sat, 14 Apr 2018 19:24:21 +0200 Subject: [PATCH] Implement strings the way Go itself does This makes string slicing cheap. --- runtime/runtime.c | 4 ++-- runtime/runtime.h | 4 ++-- tgo.go | 17 ++++++----------- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/runtime/runtime.c b/runtime/runtime.c index 37c03c42..7a623501 100644 --- a/runtime/runtime.c +++ b/runtime/runtime.c @@ -6,8 +6,8 @@ #define print(buf, len) write(STDOUT_FILENO, buf, len) -void __go_printstring(string_t *str) { - write(STDOUT_FILENO, str->buf, str->len); +void __go_printstring(string_t str) { + write(STDOUT_FILENO, str.buf, str.len); } void __go_printint(intgo_t n) { diff --git a/runtime/runtime.h b/runtime/runtime.h index 44640175..1f70d6ae 100644 --- a/runtime/runtime.h +++ b/runtime/runtime.h @@ -4,8 +4,8 @@ #include typedef struct { - uint32_t len; // TODO: size_t (or, let max string size depend on target/flag) - uint8_t buf[]; // variable size + uint32_t len; // TODO: size_t (or, let max string size depend on target/flag) + uint8_t *buf; // points to string buffer itself } string_t; typedef int32_t intgo_t; // may be 64-bit diff --git a/tgo.go b/tgo.go index 17c29048..0a17ad4a 100644 --- a/tgo.go +++ b/tgo.go @@ -34,7 +34,6 @@ type Compiler struct { intType llvm.Type stringLenType llvm.Type stringType llvm.Type - stringPtrType llvm.Type printstringFunc llvm.Value printintFunc llvm.Value printspaceFunc llvm.Value @@ -73,10 +72,9 @@ func NewCompiler(path, triple string) (*Compiler, error) { c.stringLenType = llvm.Int32Type() // Length-prefixed string. - c.stringType = llvm.StructType([]llvm.Type{c.stringLenType, llvm.ArrayType(llvm.Int8Type(), 0)}, false) - c.stringPtrType = llvm.PointerType(c.stringType, 0) + c.stringType = llvm.StructType([]llvm.Type{c.stringLenType, llvm.PointerType(llvm.Int8Type(), 0)}, false) - printstringType := llvm.FunctionType(llvm.VoidType(), []llvm.Type{c.stringPtrType}, false) + printstringType := llvm.FunctionType(llvm.VoidType(), []llvm.Type{c.stringType}, false) c.printstringFunc = llvm.AddFunction(c.mod, "__go_printstring", printstringType) printintType := llvm.FunctionType(llvm.VoidType(), []llvm.Type{c.intType}, false) c.printintFunc = llvm.AddFunction(c.mod, "__go_printint", printintType) @@ -306,7 +304,7 @@ func (c *Compiler) parseBuiltin(frame *Frame, instr *ssa.CallCommon, call *ssa.B return llvm.Value{}, err } switch expr.Type() { - case c.stringPtrType: + case c.stringType: c.builder.CreateCall(c.printstringFunc, []llvm.Value{expr}, "") case c.intType: c.builder.CreateCall(c.printintFunc, []llvm.Value{expr}, "") @@ -426,13 +424,10 @@ func (c *Compiler) parseExpr(frame *Frame, expr ssa.Value) (llvm.Value, error) { switch expr.Value.Kind() { case constant.String: str := constant.StringVal(expr.Value) - strVal := c.ctx.ConstString(str, false) strLen := llvm.ConstInt(c.stringLenType, uint64(len(str)), false) - strObj := llvm.ConstStruct([]llvm.Value{strLen, strVal}, false) - ptr := llvm.AddGlobal(c.mod, strObj.Type(), ".str") - ptr.SetInitializer(strObj) - ptr.SetLinkage(llvm.InternalLinkage) - return llvm.ConstPointerCast(ptr, c.stringPtrType), nil + strPtr := c.builder.CreateGlobalStringPtr(str, ".str") + strObj := llvm.ConstStruct([]llvm.Value{strLen, strPtr}, false) + return strObj, nil case constant.Int: n, _ := constant.Int64Val(expr.Value) // TODO: do something with the 'exact' return value? return llvm.ConstInt(c.intType, uint64(n), true), nil