Browse Source

Use LLVM API for type size calculations

pull/6/head
Ayke van Laethem 7 years ago
parent
commit
02332080e6
  1. 28
      tgo.go

28
tgo.go

@ -286,26 +286,6 @@ func (c *Compiler) getLLVMType(goType types.Type) (llvm.Type, error) {
} }
} }
func (c *Compiler) getTypeWidth(typ types.Type) (int, error) {
switch typ := typ.(type) {
case *types.Basic:
if typ.Kind() == types.UnsafePointer {
return c.targetData.PointerSize(), nil
}
llvmType, err := c.getLLVMType(typ)
if err != nil {
return 0, err
}
return llvmType.IntTypeWidth(), nil
case *types.Named:
return c.getTypeWidth(typ.Underlying())
case *types.Pointer:
return c.targetData.PointerSize(), nil
default:
return 0, errors.New("todo: type width")
}
}
func (c *Compiler) getInterfaceType(typ types.Type) llvm.Value { func (c *Compiler) getInterfaceType(typ types.Type) llvm.Value {
if _, ok := c.itfTypeNumbers[typ]; !ok { if _, ok := c.itfTypeNumbers[typ]; !ok {
num := uint64(len(c.itfTypes)) num := uint64(len(c.itfTypes))
@ -831,18 +811,16 @@ func (c *Compiler) parseConvert(frame *Frame, expr *ssa.Convert) (llvm.Value, er
return value, nil return value, nil
} }
sizeFrom, err := c.getTypeWidth(expr.X.Type()) typeFrom, err := c.getLLVMType(expr.X.Type())
if err != nil { if err != nil {
return llvm.Value{}, err return llvm.Value{}, err
} }
sizeFrom := c.targetData.TypeAllocSize(typeFrom)
typeTo, err := c.getLLVMType(expr.Type()) typeTo, err := c.getLLVMType(expr.Type())
if err != nil { if err != nil {
return llvm.Value{}, err return llvm.Value{}, err
} }
sizeTo, err := c.getTypeWidth(expr.Type()) sizeTo := c.targetData.TypeAllocSize(typeTo)
if err != nil {
return llvm.Value{}, err
}
if sizeFrom > sizeTo { if sizeFrom > sizeTo {
return c.builder.CreateTrunc(value, typeTo, ""), nil return c.builder.CreateTrunc(value, typeTo, ""), nil

Loading…
Cancel
Save