ssa: builtin print/println

This commit is contained in:
visualfc
2024-05-15 21:26:53 +08:00
parent 6278718a24
commit 022965b9c7
6 changed files with 507 additions and 172 deletions

View File

@@ -1386,6 +1386,50 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) {
}
}
}
case "print", "println":
ln := fn == "println"
ret.Type = b.Prog.Void()
for i, arg := range args {
if ln && i > 0 {
b.InlineCall(b.Func.Pkg.rtFunc("PrintString"), b.Str(" "))
}
var fn string
var typ types.Type
switch arg.kind {
case vkBool:
fn = "PrintBool"
case vkSigned:
fn = "PrintInt"
typ = types.Typ[types.Int64]
case vkUnsigned:
fn = "PrintUint"
typ = types.Typ[types.Uint64]
case vkFloat:
fn = "PrintFloat"
typ = types.Typ[types.Float64]
case vkSlice:
fn = "PrintSlice"
case vkPtr, vkFuncPtr, vkFuncDecl, vkClosure, vkPyVarRef, vkPyFuncRef:
fn = "PrintPointer"
typ = types.Typ[types.UnsafePointer]
case vkString:
fn = "PrintString"
case vkInterface:
fn = "PrintIface"
// case vkComplex:
// fn = "PrintComplex"
default:
panic(fmt.Errorf("illegal types for operand: print %v", arg.RawType()))
}
if typ != nil && typ != arg.raw.Type {
arg = b.Convert(b.Prog.Type(typ, InGo), arg)
}
b.InlineCall(b.Func.Pkg.rtFunc(fn), arg)
}
if ln {
b.InlineCall(b.Func.Pkg.rtFunc("PrintString"), b.Str("\n"))
}
return
}
panic("todo")
}

View File

@@ -47,6 +47,9 @@ const (
vkPyVarRef
vkTuple
vkSlice
vkArray
vkMap
vkInterface
vkPhisExpr = -1
)
@@ -240,11 +243,11 @@ func (p Program) toType(raw types.Type) Type {
elem := p.rawType(t.Elem())
return &aType{llvm.PointerType(elem.ll, 0), typ, vkPtr}
case *types.Interface:
return &aType{p.rtIface(), typ, vkInvalid}
return &aType{p.rtIface(), typ, vkInterface}
case *types.Slice:
return &aType{p.rtSlice(), typ, vkSlice}
case *types.Map:
return &aType{p.rtMap(), typ, vkInvalid}
return &aType{p.rtMap(), typ, vkMap}
case *types.Struct:
ll, kind := p.toLLVMStruct(t)
return &aType{ll, typ, kind}
@@ -254,7 +257,7 @@ func (p Program) toType(raw types.Type) Type {
return &aType{p.toLLVMFuncPtr(t), typ, vkFuncPtr}
case *types.Array:
elem := p.rawType(t.Elem())
return &aType{llvm.ArrayType(elem.ll, int(t.Len())), typ, vkInvalid}
return &aType{llvm.ArrayType(elem.ll, int(t.Len())), typ, vkArray}
case *types.Chan:
}
panic(fmt.Sprintf("toLLVMType: todo - %T\n", typ))