Implement strings the way Go itself does
This makes string slicing cheap.
Этот коммит содержится в:
родитель
7cc2301621
коммит
60a01a43a3
3 изменённых файлов: 10 добавлений и 15 удалений
|
@ -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) {
|
||||
|
|
|
@ -4,8 +4,8 @@
|
|||
#include <stdint.h>
|
||||
|
||||
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
|
||||
|
|
17
tgo.go
17
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
|
||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче