compiler: Give the interface type a name

Этот коммит содержится в:
Ayke van Laethem 2018-06-07 17:54:41 +02:00
родитель bb60912fa1
коммит 2e5b313d54
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: E97FF5335DFDFDED

14
tgo.go
Просмотреть файл

@ -38,8 +38,6 @@ type Compiler struct {
i8ptrType llvm.Type // for convenience i8ptrType llvm.Type // for convenience
uintptrType llvm.Type uintptrType llvm.Type
stringLenType llvm.Type stringLenType llvm.Type
interfaceType llvm.Type
typeassertType llvm.Type
taskDataType llvm.Type taskDataType llvm.Type
allocFunc llvm.Value allocFunc llvm.Value
freeFunc llvm.Value freeFunc llvm.Value
@ -110,10 +108,8 @@ func NewCompiler(pkgName, triple string) (*Compiler, error) {
t.StructSetBody([]llvm.Type{c.stringLenType, c.i8ptrType}, false) t.StructSetBody([]llvm.Type{c.stringLenType, c.i8ptrType}, false)
// Go interface: tuple of (type, ptr) // Go interface: tuple of (type, ptr)
c.interfaceType = llvm.StructType([]llvm.Type{llvm.Int32Type(), c.i8ptrType}, false) t = c.ctx.StructCreateNamed("interface")
t.StructSetBody([]llvm.Type{llvm.Int32Type(), c.i8ptrType}, false)
// Go typeassert result: tuple of (ptr, bool)
c.typeassertType = llvm.StructType([]llvm.Type{c.i8ptrType, llvm.Int1Type()}, false)
// Goroutine / task data: {i8 state, i32 data, i8* next} // Goroutine / task data: {i8 state, i32 data, i8* next}
c.taskDataType = llvm.StructType([]llvm.Type{llvm.Int8Type(), llvm.Int32Type(), c.i8ptrType}, false) c.taskDataType = llvm.StructType([]llvm.Type{llvm.Int8Type(), llvm.Int32Type(), c.i8ptrType}, false)
@ -306,7 +302,7 @@ func (c *Compiler) getLLVMType(goType types.Type) (llvm.Type, error) {
return llvm.Type{}, errors.New("todo: unknown basic type: " + fmt.Sprintf("%#v", typ)) return llvm.Type{}, errors.New("todo: unknown basic type: " + fmt.Sprintf("%#v", typ))
} }
case *types.Interface: case *types.Interface:
return c.interfaceType, nil return c.mod.GetTypeByName("interface"), nil
case *types.Named: case *types.Named:
return c.getLLVMType(typ.Underlying()) return c.getLLVMType(typ.Underlying())
case *types.Pointer: case *types.Pointer:
@ -383,7 +379,7 @@ func (c *Compiler) getZeroValue(typ llvm.Type) (llvm.Value, error) {
} }
vals[i] = val vals[i] = val
} }
if typ.StructName() == "string" { if typ.StructName() != "" {
return llvm.ConstNamedStruct(typ, vals), nil return llvm.ConstNamedStruct(typ, vals), nil
} else { } else {
return llvm.ConstStruct(vals, false), nil return llvm.ConstStruct(vals, false), nil
@ -1237,7 +1233,7 @@ func (c *Compiler) parseExpr(frame *Frame, expr ssa.Value) (llvm.Value, error) {
itfValue = c.builder.CreateIntToPtr(val, c.i8ptrType, "") itfValue = c.builder.CreateIntToPtr(val, c.i8ptrType, "")
} }
itfTypeNum := c.getInterfaceType(expr.X.Type()) itfTypeNum := c.getInterfaceType(expr.X.Type())
itf := c.ctx.ConstStruct([]llvm.Value{itfTypeNum, llvm.Undef(c.i8ptrType)}, false) itf := llvm.ConstNamedStruct(c.mod.GetTypeByName("interface"), []llvm.Value{itfTypeNum, llvm.Undef(c.i8ptrType)})
itf = c.builder.CreateInsertValue(itf, itfValue, 1, "") itf = c.builder.CreateInsertValue(itf, itfValue, 1, "")
return itf, nil return itf, nil
case *ssa.Phi: case *ssa.Phi: