cl: compileInstrOrValue bugfix

This commit is contained in:
xushiwei
2024-04-28 22:20:46 +08:00
parent 70623dd554
commit ba94d6f04e
4 changed files with 23 additions and 23 deletions

View File

@@ -15,10 +15,9 @@ _llgo_0:
br i1 %3, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0
%4 = alloca %main.Foo, align 8
%5 = getelementptr inbounds %main.Foo, ptr %4, i32 0, i32 0
%6 = load i32, ptr %5, align 4
call void (ptr, ...) @printf(ptr @main.format, i32 %6)
%4 = getelementptr inbounds %main.Foo, ptr %1, i32 0, i32 0
%5 = load i32, ptr %4, align 4
call void (ptr, ...) @printf(ptr @main.format, i32 %5)
br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0

View File

@@ -115,7 +115,7 @@ func inPkg(name, pkg string) bool {
type none = struct{}
type instrAndValue interface {
type instrOrValue interface {
ssa.Instruction
ssa.Value
}
@@ -202,6 +202,7 @@ func (p *context) compileFunc(pkg llssa.Package, pkgTypes *types.Package, f *ssa
}
fn.MakeBlocks(nblk)
b := fn.NewBuilder()
p.bvals = make(map[ssa.Value]llssa.Expr)
for i, block := range f.Blocks {
p.compileBlock(b, block, i == 0 && name == "main")
}
@@ -211,7 +212,6 @@ func (p *context) compileFunc(pkg llssa.Package, pkgTypes *types.Package, f *ssa
func (p *context) compileBlock(b llssa.Builder, block *ssa.BasicBlock, doInit bool) llssa.BasicBlock {
ret := p.fn.Block(block.Index)
b.SetBlock(ret)
p.bvals = make(map[ssa.Value]llssa.Expr)
if doInit {
fn := p.pkg.FuncOf("main.init")
b.Call(fn.Expr)
@@ -257,9 +257,12 @@ func (p *context) checkVArgs(v *ssa.Alloc, t *types.Pointer) bool {
return false
}
func (p *context) compileInstrAndValue(b llssa.Builder, iv instrAndValue) (ret llssa.Expr) {
if v, ok := p.bvals[iv]; ok {
return v
func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue bool) (ret llssa.Expr) {
if asValue {
if v, ok := p.bvals[iv]; ok {
return v
}
log.Panicln("unreachable:", iv)
}
switch v := iv.(type) {
case *ssa.Call:
@@ -343,8 +346,8 @@ func (p *context) compileInstrAndValue(b llssa.Builder, iv instrAndValue) (ret l
}
func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) {
if iv, ok := instr.(instrAndValue); ok {
p.compileInstrAndValue(b, iv)
if iv, ok := instr.(instrOrValue); ok {
p.compileInstrOrValue(b, iv, false)
return
}
switch v := instr.(type) {
@@ -394,8 +397,8 @@ func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) {
}
func (p *context) compileValue(b llssa.Builder, v ssa.Value) llssa.Expr {
if iv, ok := v.(instrAndValue); ok {
return p.compileInstrAndValue(b, iv)
if iv, ok := v.(instrOrValue); ok {
return p.compileInstrOrValue(b, iv, true)
}
switch v := v.(type) {
case *ssa.Parameter:

View File

@@ -29,7 +29,7 @@ func testCompile(t *testing.T, src, expected string) {
}
func TestFromTestcgo(t *testing.T) {
cltest.FromDir(t, "", "./_testcgo", true)
cltest.FromDir(t, "struct", "./_testcgo", true)
}
func TestFromTestdata(t *testing.T) {

View File

@@ -39,10 +39,9 @@ _llgo_0:
br i1 %7, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0
%8 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8
%9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %8, i32 0, i32 1
%10 = load ptr, ptr %9, align 8
%castInt = sext ptr %10 to i64
%8 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, i32 0, i32 1
%9 = load ptr, ptr %8, align 8
%castInt = sext ptr %9 to i64
%mrv = insertvalue { i64, i1 } poison, i64 %castInt, 0
%mrv1 = insertvalue { i64, i1 } %mrv, i1 true, 1
ret { i64, i1 } %mrv1
@@ -74,14 +73,13 @@ _llgo_0:
br i1 %7, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0
%8 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8
%9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %8, i32 0, i32 1
%10 = load ptr, ptr %9, align 8
%castInt = sext ptr %10 to i64
%8 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, i32 0, i32 1
%9 = load ptr, ptr %8, align 8
%castInt = sext ptr %9 to i64
ret i64 %castInt
_llgo_2: ; preds = %_llgo_0
%11 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"([21 x i8] c"I2Int: type mismatch\00")
%10 = call %"github.com/goplus/llgo/internal/runtime.iface" @"github.com/goplus/llgo/internal/runtime.MakeAnyString"([21 x i8] c"I2Int: type mismatch\00")
call void @abort()
}