Merge pull request #257 from xushiwei/q

llgo/ssa: Builtin
This commit is contained in:
xushiwei
2024-06-01 17:33:01 +08:00
committed by GitHub
4 changed files with 31 additions and 7 deletions

View File

@@ -571,10 +571,9 @@ func (p *context) call(b llssa.Builder, act llssa.DoAction, call *ssa.CallCommon
if fn == "ssa:wrapnilchk" { // TODO(xsw): check nil ptr
arg := args[0]
ret = p.compileValue(b, arg)
// log.Println("wrapnilchk:", ret.TypeOf())
} else {
args := p.compileValues(b, args, kind)
ret = b.BuiltinDo(act, fn, args...)
ret = b.Do(act, llssa.Builtin(fn), args...)
}
case *ssa.Function:
aFn, pyFn, ftype := p.compileFunction(cv)

View File

@@ -43,6 +43,26 @@ func (v Expr) IsNil() bool {
// -----------------------------------------------------------------------------
type builtinTy struct {
name string
}
func (p builtinTy) Underlying() types.Type {
panic("don't call")
}
func (p builtinTy) String() string {
return "builtinTy"
}
// Builtin returns a builtin function expression.
func Builtin(name string) Expr {
tbi := &aType{raw: rawType{&builtinTy{name}}, kind: vkBuiltin}
return Expr{Type: tbi}
}
// -----------------------------------------------------------------------------
type pyVarTy struct {
mod Expr
name string
@@ -742,6 +762,9 @@ func (b Builder) Call(fn Expr, args ...Expr) (ret Expr) {
case vkFuncDecl:
sig = raw.(*types.Signature)
ll = fn.ll
case vkBuiltin:
bi := raw.(*builtinTy)
return b.BuiltinCall(bi.name, args...)
default:
log.Panicf("unreachable: %d(%T)\n", kind, raw)
}
@@ -751,6 +774,9 @@ func (b Builder) Call(fn Expr, args ...Expr) (ret Expr) {
}
func logCall(da string, fn Expr, args []Expr) {
if fn.kind == vkBuiltin {
return
}
var b bytes.Buffer
name := fn.impl.Name()
if name == "" {
@@ -791,11 +817,6 @@ func (b Builder) Do(da DoAction, fn Expr, args ...Expr) (ret Expr) {
// `fn` indicates the function: one of the built-in functions from the
// Go spec (excluding "make" and "new").
func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) {
return b.BuiltinDo(Call, fn, args...)
}
// BuiltinDo call a builtin function with an action.
func (b Builder) BuiltinDo(da DoAction, fn string, args ...Expr) (ret Expr) {
switch fn {
case "len":
if len(args) == 1 {

View File

@@ -126,7 +126,9 @@ func TestCvtType(t *testing.T) {
func TestUserdefExpr(t *testing.T) {
c := &pyVarTy{}
b := &builtinTy{}
_ = c.String()
_ = b.String()
test := func(a types.Type) {
defer func() {
if r := recover(); r == nil {
@@ -136,6 +138,7 @@ func TestUserdefExpr(t *testing.T) {
a.Underlying()
}
test(c)
test(b)
}
func TestAny(t *testing.T) {

View File

@@ -44,6 +44,7 @@ const (
vkFuncDecl
vkFuncPtr
vkClosure
vkBuiltin
vkPyFuncRef
vkPyVarRef
vkTuple