llgo/ssa: Builtin

This commit is contained in:
xushiwei
2024-06-01 17:18:17 +08:00
parent 881574ed39
commit 45734c0b5c
3 changed files with 25 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 if fn == "ssa:wrapnilchk" { // TODO(xsw): check nil ptr
arg := args[0] arg := args[0]
ret = p.compileValue(b, arg) ret = p.compileValue(b, arg)
// log.Println("wrapnilchk:", ret.TypeOf())
} else { } else {
args := p.compileValues(b, args, kind) args := p.compileValues(b, args, kind)
ret = b.BuiltinDo(act, fn, args...) ret = b.Do(act, llssa.Builtin(fn), args...)
} }
case *ssa.Function: case *ssa.Function:
aFn, pyFn, ftype := p.compileFunction(cv) 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 { type pyVarTy struct {
mod Expr mod Expr
name string name string
@@ -742,6 +762,9 @@ func (b Builder) Call(fn Expr, args ...Expr) (ret Expr) {
case vkFuncDecl: case vkFuncDecl:
sig = raw.(*types.Signature) sig = raw.(*types.Signature)
ll = fn.ll ll = fn.ll
case vkBuiltin:
bi := raw.(*builtinTy)
return b.BuiltinCall(bi.name, args...)
default: default:
log.Panicf("unreachable: %d(%T)\n", kind, raw) log.Panicf("unreachable: %d(%T)\n", kind, raw)
} }
@@ -791,11 +814,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 // `fn` indicates the function: one of the built-in functions from the
// Go spec (excluding "make" and "new"). // Go spec (excluding "make" and "new").
func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) { 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 { switch fn {
case "len": case "len":
if len(args) == 1 { if len(args) == 1 {

View File

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