diff --git a/cl/_testrt/map/in.go b/cl/_testrt/map/in.go index f5f45edb..34b98520 100644 --- a/cl/_testrt/map/in.go +++ b/cl/_testrt/map/in.go @@ -1,13 +1,10 @@ package main -/* import ( "github.com/goplus/llgo/internal/runtime/c" ) -*/ func main() { a := map[int]int{23: 100, 7: 29} - _ = a - // c.Printf(c.Str("Hello %d\n"), a[23]) + c.Printf(c.Str("Hello %d\n"), a[23]) } diff --git a/cl/_testrt/map/out.ll b/cl/_testrt/map/out.ll index 7f1f90f0..7ad91eed 100644 --- a/cl/_testrt/map/out.ll +++ b/cl/_testrt/map/out.ll @@ -2,6 +2,7 @@ source_filename = "main" @"main.init$guard" = global ptr null +@0 = private unnamed_addr constant [10 x i8] c"Hello %d\0A\00", align 1 define void @main.init() { _llgo_0: @@ -21,9 +22,12 @@ _llgo_0: call void @"github.com/goplus/llgo/internal/runtime.init"() call void @main.init() %0 = call ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"() + %1 = call i32 (ptr, ...) @printf(ptr @0, ) ret void } declare void @"github.com/goplus/llgo/internal/runtime.init"() declare ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"() + +declare i32 @printf(ptr, ...) diff --git a/cl/compile.go b/cl/compile.go index e923d3b6..3c53c299 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -520,8 +520,12 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue return p.compileValue(b, n.X) } } - panic(fmt.Errorf("todo addr of %v", e)) + panic(fmt.Errorf("todo: addr of %v", e)) }) + case *ssa.Lookup: + x := p.compileValue(b, v.X) + idx := p.compileValue(b, v.Index) + ret = b.Lookup(x, idx, v.CommaOk) case *ssa.Slice: vx := v.X if _, ok := p.isVArgs(vx); ok { // varargs: this is a varargs slice diff --git a/ssa/expr.go b/ssa/expr.go index a4214a62..09657449 100644 --- a/ssa/expr.go +++ b/ssa/expr.go @@ -654,6 +654,26 @@ func (b Builder) Index(x, idx Expr, addr func(Expr) Expr) Expr { return b.Load(buf) } +// The Lookup instruction yields element Index of collection map X. +// Index is the appropriate key type. +// +// If CommaOk, the result is a 2-tuple of the value above and a +// boolean indicating the result of a map membership test for the key. +// The components of the tuple are accessed using Extract. +// +// Example printed form: +// +// t2 = t0[t1] +// t5 = t3[t4],ok +func (b Builder) Lookup(x, key Expr, commaOk bool) (ret Expr) { + if debugInstr { + log.Printf("Lookup %v, %v, %v\n", x.impl, key.impl, commaOk) + } + // TODO(xsw) + // panic("todo") + return +} + // The Slice instruction yields a slice of an existing string, slice // or *array X between optional integer bounds Low and High. //