ssa: fix castInt

This commit is contained in:
visualfc
2024-05-16 20:14:04 +08:00
parent 00f74b7f0a
commit feb28ecace
8 changed files with 39 additions and 34 deletions

View File

@@ -322,7 +322,7 @@ _llgo_15: ; preds = %_llgo_13
br i1 %40, label %_llgo_16, label %_llgo_17
_llgo_16: ; preds = %_llgo_15
%41 = sext i8 %39 to i64
%41 = zext i8 %39 to i64
call void @main.printuint(i64 %41)
br label %_llgo_1
@@ -335,7 +335,7 @@ _llgo_17: ; preds = %_llgo_15
br i1 %46, label %_llgo_18, label %_llgo_19
_llgo_18: ; preds = %_llgo_17
%47 = sext i16 %45 to i64
%47 = zext i16 %45 to i64
call void @main.printuint(i64 %47)
br label %_llgo_1
@@ -348,7 +348,7 @@ _llgo_19: ; preds = %_llgo_17
br i1 %52, label %_llgo_20, label %_llgo_21
_llgo_20: ; preds = %_llgo_19
%53 = sext i32 %51 to i64
%53 = zext i32 %51 to i64
call void @main.printuint(i64 %53)
br label %_llgo_1

View File

@@ -25,5 +25,5 @@ func main() {
data = append(data, "def"...)
println(data)
var i any = 100
println(true, 100, -100, uint(100), int32(-100), 100.5, i, &i, uintptr(unsafe.Pointer(&i)))
println(true, 100, -100, uint(255), int32(-100), 100.5, i, &i, uintptr(unsafe.Pointer(&i)))
}

View File

@@ -292,7 +292,7 @@ _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 -100)
%129 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @37, i64 1)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %129)
call void @"github.com/goplus/llgo/internal/runtime.PrintUint"(i64 100)
call void @"github.com/goplus/llgo/internal/runtime.PrintUint"(i64 255)
%130 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @38, i64 1)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %130)
call void @"github.com/goplus/llgo/internal/runtime.PrintInt"(i64 -100)

View File

@@ -199,7 +199,7 @@ _llgo_2: ; preds = %_llgo_0
define void @main.cvtUinptr(i32 %0, i64 %1) {
_llgo_0:
%2 = sext i32 %0 to i64
%2 = zext i32 %0 to i64
%3 = icmp ne i64 %2, %1
br i1 %3, label %_llgo_1, label %_llgo_2

View File

@@ -57,7 +57,7 @@ _llgo_0:
%9 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @2, i64 1)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %9)
%10 = call i8 @main.mask_shl8u(i8 127, i64 5)
%11 = sext i8 %10 to i64
%11 = zext i8 %10 to i64
call void @"github.com/goplus/llgo/internal/runtime.PrintUint"(i64 %11)
%12 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @3, i64 1)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %12)
@@ -67,7 +67,7 @@ _llgo_0:
%15 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @4, i64 1)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %15)
%16 = call i8 @main.mask_shl8u(i8 127, i64 16)
%17 = sext i8 %16 to i64
%17 = zext i8 %16 to i64
call void @"github.com/goplus/llgo/internal/runtime.PrintUint"(i64 %17)
%18 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @5, i64 1)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %18)
@@ -81,7 +81,7 @@ _llgo_0:
%23 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @7, i64 1)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %23)
%24 = call i8 @main.mask_shr8u(i8 127, i64 5)
%25 = sext i8 %24 to i64
%25 = zext i8 %24 to i64
call void @"github.com/goplus/llgo/internal/runtime.PrintUint"(i64 %25)
%26 = call %"github.com/goplus/llgo/internal/runtime.String" @"github.com/goplus/llgo/internal/runtime.NewString"(ptr @8, i64 1)
call void @"github.com/goplus/llgo/internal/runtime.PrintString"(%"github.com/goplus/llgo/internal/runtime.String" %26)

Binary file not shown.

Binary file not shown.

View File

@@ -1059,7 +1059,7 @@ func (b Builder) Convert(t Type, x Expr) (ret Expr) {
case *types.Basic:
switch typ.Kind() {
case types.Uintptr:
ret.impl = castInt(b.impl, x.impl, t.ll)
ret.impl = castUintptr(b, x.impl, t)
return
case types.UnsafePointer:
ret.impl = castPtr(b.impl, x.impl, t.ll)
@@ -1067,19 +1067,10 @@ func (b Builder) Convert(t Type, x Expr) (ret Expr) {
}
switch xtyp := x.RawType().Underlying().(type) {
case *types.Basic:
size := b.Prog.SizeOf(t)
xsize := b.Prog.SizeOf(x.Type)
if typ.Info()&types.IsInteger != 0 {
// int <- int/float
if xtyp.Info()&types.IsInteger != 0 {
// if xsize > size {
// ret.impl = b.impl.CreateTrunc(x.impl, t.ll, "")
// } else if typ.Info()&types.IsUnsigned != 0 {
// ret.impl = b.impl.CreateZExt(x.impl, t.ll, "")
// } else {
// ret.impl = b.impl.CreateSExt(x.impl, t.ll, "")
// }
ret.impl = castInt(b.impl, x.impl, t.ll)
ret.impl = castInt(b, x.impl, t)
return
} else if xtyp.Info()&types.IsFloat != 0 {
if typ.Info()&types.IsUnsigned != 0 {
@@ -1099,11 +1090,7 @@ func (b Builder) Convert(t Type, x Expr) (ret Expr) {
}
return
} else if xtyp.Info()&types.IsFloat != 0 {
if xsize > size {
ret.impl = b.impl.CreateFPTrunc(x.impl, t.ll, "")
} else {
ret.impl = b.impl.CreateFPExt(x.impl, t.ll, "")
}
ret.impl = castFloat(b, x.impl, t)
return
}
}
@@ -1115,15 +1102,33 @@ func (b Builder) Convert(t Type, x Expr) (ret Expr) {
panic("todo")
}
func castInt(b llvm.Builder, x llvm.Value, t llvm.Type) llvm.Value {
xt := x.Type()
if xt.TypeKind() == llvm.PointerTypeKind {
return llvm.CreatePtrToInt(b, x, t)
func castUintptr(b Builder, x llvm.Value, typ Type) llvm.Value {
if x.Type().TypeKind() == llvm.PointerTypeKind {
return llvm.CreatePtrToInt(b.impl, x, typ.ll)
}
if xt.IntTypeWidth() <= t.IntTypeWidth() {
return llvm.CreateIntCast(b, x, t)
return castInt(b, x, typ)
}
func castInt(b Builder, x llvm.Value, typ Type) llvm.Value {
xsize := b.Prog.td.TypeAllocSize(x.Type())
size := b.Prog.td.TypeAllocSize(typ.ll)
if xsize > size {
return b.impl.CreateTrunc(x, typ.ll, "")
} else if typ.kind == vkUnsigned {
return b.impl.CreateZExt(x, typ.ll, "")
} else {
return b.impl.CreateSExt(x, typ.ll, "")
}
}
func castFloat(b Builder, x llvm.Value, typ Type) llvm.Value {
xsize := b.Prog.td.TypeAllocSize(x.Type())
size := b.Prog.td.TypeAllocSize(typ.ll)
if xsize > size {
return b.impl.CreateFPTrunc(x, typ.ll, "")
} else {
return b.impl.CreateFPExt(x, typ.ll, "")
}
return llvm.CreateTrunc(b, x, t)
}
func castPtr(b llvm.Builder, x llvm.Value, t llvm.Type) llvm.Value {
@@ -1245,12 +1250,12 @@ func (b Builder) TypeAssert(x Expr, assertedTyp Type, commaOk bool) (ret Expr) {
conv := func(v llvm.Value) llvm.Value {
switch kind {
case types.Float32:
v = castInt(b.impl, v, b.Prog.tyInt32())
v = castInt(b, v, b.Prog.Type(types.Typ[types.Int32], InC))
v = b.impl.CreateBitCast(v, assertedTyp.ll, "")
case types.Float64:
v = b.impl.CreateBitCast(v, assertedTyp.ll, "")
default:
v = castInt(b.impl, v, assertedTyp.ll)
v = castInt(b, v, assertedTyp)
}
return v
}