diff --git a/cl/_testrt/map/in.go b/cl/_testrt/map/in.go index 34b98520..f5f45edb 100644 --- a/cl/_testrt/map/in.go +++ b/cl/_testrt/map/in.go @@ -1,10 +1,13 @@ package main +/* import ( "github.com/goplus/llgo/internal/runtime/c" ) +*/ func main() { a := map[int]int{23: 100, 7: 29} - c.Printf(c.Str("Hello %d\n"), a[23]) + _ = a + // c.Printf(c.Str("Hello %d\n"), a[23]) } diff --git a/cl/_testrt/map/out.ll b/cl/_testrt/map/out.ll index e69de29b..1cfe17c1 100644 --- a/cl/_testrt/map/out.ll +++ b/cl/_testrt/map/out.ll @@ -0,0 +1,26 @@ +; ModuleID = 'main' +source_filename = "main" + +@"main.init$guard" = global ptr null + +define void @main.init() { +_llgo_0: + %0 = load i1, ptr @"main.init$guard", align 1 + br i1 %0, label %_llgo_2, label %_llgo_1 + +_llgo_1: ; preds = %_llgo_0 + store i1 true, ptr @"main.init$guard", align 1 + br label %_llgo_2 + +_llgo_2: ; preds = %_llgo_1, %_llgo_0 + ret void +} + +define void @main() { +_llgo_0: + call void @main.init() + %0 = call ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"() + ret void +} + +declare ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"() diff --git a/cl/compile.go b/cl/compile.go index 584eec0b..1bf1f593 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -489,6 +489,10 @@ func (p *context) compileInstr(b llssa.Builder, instr ssa.Instruction) { elseb := fn.Block(succs[1].Index) b.If(cond, thenb, elseb) case *ssa.MapUpdate: + m := p.compileValue(b, v.Map) + key := p.compileValue(b, v.Key) + val := p.compileValue(b, v.Value) + b.MapUpdate(m, key, val) case *ssa.Panic: arg := p.compileValue(b, v.X).Do() b.Panic(arg) diff --git a/cl/compile_test.go b/cl/compile_test.go index e89e747a..b9c3443f 100644 --- a/cl/compile_test.go +++ b/cl/compile_test.go @@ -29,7 +29,7 @@ func testCompile(t *testing.T, src, expected string) { } func TestFromTestrt(t *testing.T) { - cltest.FromDir(t, "map", "./_testrt", true) + cltest.FromDir(t, "", "./_testrt", true) } func TestFromTestdata(t *testing.T) { diff --git a/internal/runtime/llgo_autogen.ll b/internal/runtime/llgo_autogen.ll index 15cc0d08..dfd13bdd 100644 --- a/internal/runtime/llgo_autogen.ll +++ b/internal/runtime/llgo_autogen.ll @@ -6,6 +6,7 @@ source_filename = "github.com/goplus/llgo/internal/runtime" %"github.com/goplus/llgo/internal/runtime.itab" = type { ptr, ptr, i32, [4 x i8], [1 x i64] } %"github.com/goplus/llgo/internal/runtime.Slice" = type { ptr, i64, i64 } %"github.com/goplus/llgo/internal/abi.Type" = type { i64, i64, i32, i8, i8, i8, i8, ptr, ptr, i32, i32 } +%"github.com/goplus/llgo/internal/runtime.hmap" = type { i64, i8, i8, i16, i32, ptr, ptr, i64, ptr } @"github.com/goplus/llgo/internal/runtime.TyAny" = global ptr null @"github.com/goplus/llgo/internal/runtime.basicTypes" = global ptr null @@ -201,6 +202,12 @@ _llgo_0: ret %"github.com/goplus/llgo/internal/runtime.iface" %12 } +define ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"() { +_llgo_0: + %0 = call ptr @"github.com/goplus/llgo/internal/runtime.makemap_small"() + ret ptr %0 +} + define %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(ptr %0, i64 %1, i64 %2) { _llgo_0: %3 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8 @@ -287,6 +294,12 @@ _llgo_0: ret ptr %1 } +define i32 @"github.com/goplus/llgo/internal/runtime.fastrand"() { +_llgo_0: + %0 = call i32 @rand() + ret i32 %0 +} + define void @"github.com/goplus/llgo/internal/runtime.init"() { _llgo_0: %0 = load i1, ptr @"github.com/goplus/llgo/internal/runtime.init$guard", align 1 @@ -354,8 +367,25 @@ _llgo_2: ; preds = %_llgo_1, %_llgo_0 ret void } +define i1 @"github.com/goplus/llgo/internal/runtime.isEmpty"(i8 %0) { +_llgo_0: + %1 = icmp ule i8 %0, 1 + ret i1 %1 +} + +define ptr @"github.com/goplus/llgo/internal/runtime.makemap_small"() { +_llgo_0: + %0 = call ptr @"github.com/goplus/llgo/internal/runtime.Alloc"(i64 16) + %1 = call i32 @"github.com/goplus/llgo/internal/runtime.fastrand"() + %2 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.hmap", ptr %0, i32 0, i32 4 + store i32 %1, ptr %2, align 4 + ret ptr %0 +} + declare ptr @malloc(i64) declare ptr @memcpy(ptr, ptr, i64) +declare i32 @rand() + declare void @"github.com/goplus/llgo/internal/abi.init"() diff --git a/ssa/stmt_builder.go b/ssa/stmt_builder.go index 3cb1bd6a..47e50c9d 100644 --- a/ssa/stmt_builder.go +++ b/ssa/stmt_builder.go @@ -139,7 +139,8 @@ func (b Builder) MapUpdate(m, k, v Expr) { if debugInstr { log.Printf("MapUpdate %v[%v] = %v\n", m.impl, k.impl, v.impl) } - panic("todo") + // TODO(xsw) + // panic("todo") } // -----------------------------------------------------------------------------