ssa: Lookup

This commit is contained in:
xushiwei
2024-05-06 01:17:37 +08:00
parent 26b812a62a
commit df333fb144
4 changed files with 30 additions and 5 deletions

View File

@@ -1,13 +1,10 @@
package main package main
/*
import ( import (
"github.com/goplus/llgo/internal/runtime/c" "github.com/goplus/llgo/internal/runtime/c"
) )
*/
func main() { func main() {
a := map[int]int{23: 100, 7: 29} 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])
} }

View File

@@ -2,6 +2,7 @@
source_filename = "main" source_filename = "main"
@"main.init$guard" = global ptr null @"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() { define void @main.init() {
_llgo_0: _llgo_0:
@@ -21,9 +22,12 @@ _llgo_0:
call void @"github.com/goplus/llgo/internal/runtime.init"() call void @"github.com/goplus/llgo/internal/runtime.init"()
call void @main.init() call void @main.init()
%0 = call ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"() %0 = call ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"()
%1 = call i32 (ptr, ...) @printf(ptr @0, <null operand!>)
ret void ret void
} }
declare void @"github.com/goplus/llgo/internal/runtime.init"() declare void @"github.com/goplus/llgo/internal/runtime.init"()
declare ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"() declare ptr @"github.com/goplus/llgo/internal/runtime.MakeSmallMap"()
declare i32 @printf(ptr, ...)

View File

@@ -520,8 +520,12 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
return p.compileValue(b, n.X) 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: case *ssa.Slice:
vx := v.X vx := v.X
if _, ok := p.isVArgs(vx); ok { // varargs: this is a varargs slice if _, ok := p.isVArgs(vx); ok { // varargs: this is a varargs slice

View File

@@ -654,6 +654,26 @@ func (b Builder) Index(x, idx Expr, addr func(Expr) Expr) Expr {
return b.Load(buf) 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 // The Slice instruction yields a slice of an existing string, slice
// or *array X between optional integer bounds Low and High. // or *array X between optional integer bounds Low and High.
// //