cl: _testdata/method

This commit is contained in:
xushiwei
2024-04-25 21:44:23 +08:00
parent 0f00add402
commit 87b7ecd1d6
7 changed files with 115 additions and 15 deletions

View File

@@ -258,7 +258,7 @@ func (b Builder) UnOp(op token.Token, x Expr) Expr {
// Load returns the value at the pointer ptr.
func (b Builder) Load(ptr Expr) Expr {
if debugInstr {
log.Printf("Load %v\n", ptr.impl.Name())
log.Printf("Load %v\n", ptr.impl)
}
telem := b.prog.Elem(ptr.Type)
return Expr{llvm.CreateLoad(b.impl, telem.ll, ptr.impl), telem}
@@ -267,7 +267,7 @@ func (b Builder) Load(ptr Expr) Expr {
// Store stores val at the pointer ptr.
func (b Builder) Store(ptr, val Expr) Builder {
if debugInstr {
log.Printf("Store %v, %v\n", ptr.impl.Name(), val.impl)
log.Printf("Store %v, %v\n", ptr.impl, val.impl)
}
b.impl.CreateStore(val.impl, ptr.impl)
return b
@@ -346,7 +346,7 @@ func (b Builder) Alloc(t Type, heap bool) (ret Expr) {
func (b Builder) Call(fn Expr, args ...Expr) (ret Expr) {
if debugInstr {
var b bytes.Buffer
fmt.Fprint(&b, "Call @", fn.impl.Name())
fmt.Fprint(&b, "Call ", fn.impl.Name())
for _, arg := range args {
fmt.Fprint(&b, ", ", arg.impl)
}

View File

@@ -187,6 +187,7 @@ type aPackage struct {
type Package = *aPackage
// NewConst creates a new named constant.
func (p Package) NewConst(name string, val constant.Value) NamedConst {
return &aNamedConst{}
}

View File

@@ -17,8 +17,8 @@
package ssa
import (
"fmt"
"go/types"
"log"
"github.com/goplus/llvm"
)
@@ -108,8 +108,21 @@ func (p Program) llvmSignature(sig *types.Signature) Type {
if v := p.typs.At(sig); v != nil {
return v.(Type)
}
sigOrg := sig
if recv := sig.Recv(); recv != nil {
// convert method to func
tParams := sig.Params()
nParams := tParams.Len()
params := make([]*types.Var, nParams+1)
params[0] = recv
for i := 0; i < nParams; i++ {
params[i+1] = tParams.At(i)
}
sig = types.NewSignatureType(
nil, nil, nil, types.NewTuple(params...), sig.Results(), sig.Variadic())
}
ret := p.toLLVMFunc(sig)
p.typs.Set(sig, ret)
p.typs.Set(sigOrg, ret)
return ret
}
@@ -228,8 +241,7 @@ func (p Program) toLLVMType(typ types.Type) Type {
return &aType{llvm.ArrayType(elem.ll, int(t.Len())), typ, vkInvalid}
case *types.Chan:
}
log.Println("toLLVMType: todo -", typ)
panic("todo")
panic(fmt.Sprintf("toLLVMType: todo - %T\n", typ))
}
func (p Program) toLLVMNamedStruct(name string, typ *types.Struct) llvm.Type {
@@ -304,12 +316,13 @@ func (p Program) retType(sig *types.Signature) Type {
}
func (p Program) toLLVMNamed(typ *types.Named) Type {
name := typ.Obj().Name()
switch typ := typ.Underlying().(type) {
switch t := typ.Underlying().(type) {
case *types.Struct:
return &aType{p.toLLVMNamedStruct(name, typ), typ, vkInvalid}
name := typ.Obj().Name()
return &aType{p.toLLVMNamedStruct(name, t), typ, vkInvalid}
default:
return p.Type(t)
}
panic("todo")
}
// -----------------------------------------------------------------------------