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 br i1 %3, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0 _llgo_1: ; preds = %_llgo_0
%4 = alloca %main.Foo, align 8 %4 = getelementptr inbounds %main.Foo, ptr %1, i32 0, i32 0
%5 = getelementptr inbounds %main.Foo, ptr %4, i32 0, i32 0 %5 = load i32, ptr %4, align 4
%6 = load i32, ptr %5, align 4 call void (ptr, ...) @printf(ptr @main.format, i32 %5)
call void (ptr, ...) @printf(ptr @main.format, i32 %6)
br label %_llgo_2 br label %_llgo_2
_llgo_2: ; preds = %_llgo_1, %_llgo_0 _llgo_2: ; preds = %_llgo_1, %_llgo_0

View File

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

View File

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

View File

@@ -39,10 +39,9 @@ _llgo_0:
br i1 %7, label %_llgo_1, label %_llgo_2 br i1 %7, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0 _llgo_1: ; preds = %_llgo_0
%8 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8 %8 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, i32 0, i32 1
%9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %8, i32 0, i32 1 %9 = load ptr, ptr %8, align 8
%10 = load ptr, ptr %9, align 8 %castInt = sext ptr %9 to i64
%castInt = sext ptr %10 to i64
%mrv = insertvalue { i64, i1 } poison, i64 %castInt, 0 %mrv = insertvalue { i64, i1 } poison, i64 %castInt, 0
%mrv1 = insertvalue { i64, i1 } %mrv, i1 true, 1 %mrv1 = insertvalue { i64, i1 } %mrv, i1 true, 1
ret { i64, i1 } %mrv1 ret { i64, i1 } %mrv1
@@ -74,14 +73,13 @@ _llgo_0:
br i1 %7, label %_llgo_1, label %_llgo_2 br i1 %7, label %_llgo_1, label %_llgo_2
_llgo_1: ; preds = %_llgo_0 _llgo_1: ; preds = %_llgo_0
%8 = alloca %"github.com/goplus/llgo/internal/runtime.iface", align 8 %8 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %2, i32 0, i32 1
%9 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.iface", ptr %8, i32 0, i32 1 %9 = load ptr, ptr %8, align 8
%10 = load ptr, ptr %9, align 8 %castInt = sext ptr %9 to i64
%castInt = sext ptr %10 to i64
ret i64 %castInt ret i64 %castInt
_llgo_2: ; preds = %_llgo_0 _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() call void @abort()
} }