runtime: rm NewSlice (use b.unsafeSlice); llgo/ssa: Println
This commit is contained in:
@@ -18,7 +18,6 @@ package ssa
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"go/token"
|
||||
"go/types"
|
||||
"log"
|
||||
|
||||
@@ -198,26 +197,6 @@ func (b Builder) Index(x, idx Expr, addr func(Expr) Expr) Expr {
|
||||
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
|
||||
// or *array X between optional integer bounds Low and High.
|
||||
//
|
||||
@@ -297,6 +276,35 @@ func (b Builder) SliceLit(t Type, elts ...Expr) Expr {
|
||||
return b.unsafeSlice(ptr, size, size)
|
||||
}
|
||||
|
||||
// The MakeSlice instruction yields a slice of length Len backed by a
|
||||
// newly allocated array of length Cap.
|
||||
//
|
||||
// Both Len and Cap must be non-nil Values of integer type.
|
||||
//
|
||||
// (Alloc(types.Array) followed by Slice will not suffice because
|
||||
// Alloc can only create arrays of constant length.)
|
||||
//
|
||||
// Type() returns a (possibly named) *types.Slice.
|
||||
//
|
||||
// Example printed form:
|
||||
//
|
||||
// t1 = make []string 1:int t0
|
||||
// t1 = make StringSlice 1:int t0
|
||||
func (b Builder) MakeSlice(t Type, len, cap Expr) (ret Expr) {
|
||||
if debugInstr {
|
||||
log.Printf("MakeSlice %v, %v, %v\n", t.RawType(), len.impl, cap.impl)
|
||||
}
|
||||
prog := b.Prog
|
||||
if cap.IsNil() {
|
||||
cap = len
|
||||
}
|
||||
telem := prog.Index(t)
|
||||
ptr := b.ArrayAlloc(telem, cap)
|
||||
ret.impl = b.unsafeSlice(ptr, len.impl, cap.impl).impl
|
||||
ret.Type = t
|
||||
return
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// The MakeMap instruction creates a new hash-table-based map object
|
||||
@@ -318,35 +326,41 @@ func (b Builder) MakeMap(t Type, nReserve Expr) (ret Expr) {
|
||||
return
|
||||
}
|
||||
|
||||
// The MakeSlice instruction yields a slice of length Len backed by a
|
||||
// newly allocated array of length Cap.
|
||||
// The Lookup instruction yields element Index of collection map X.
|
||||
// Index is the appropriate key type.
|
||||
//
|
||||
// Both Len and Cap must be non-nil Values of integer type.
|
||||
//
|
||||
// (Alloc(types.Array) followed by Slice will not suffice because
|
||||
// Alloc can only create arrays of constant length.)
|
||||
//
|
||||
// Type() returns a (possibly named) *types.Slice.
|
||||
// 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:
|
||||
//
|
||||
// t1 = make []string 1:int t0
|
||||
// t1 = make StringSlice 1:int t0
|
||||
func (b Builder) MakeSlice(t Type, len, cap Expr) (ret Expr) {
|
||||
// t2 = t0[t1]
|
||||
// t5 = t3[t4],ok
|
||||
func (b Builder) Lookup(x, key Expr, commaOk bool) (ret Expr) {
|
||||
if debugInstr {
|
||||
log.Printf("MakeSlice %v, %v, %v\n", t.RawType(), len.impl, cap.impl)
|
||||
log.Printf("Lookup %v, %v, %v\n", x.impl, key.impl, commaOk)
|
||||
}
|
||||
pkg := b.Pkg
|
||||
prog := b.Prog
|
||||
if cap.IsNil() {
|
||||
cap = len
|
||||
}
|
||||
elemSize := SizeOf(prog, prog.Index(t))
|
||||
size := b.BinOp(token.MUL, cap, elemSize)
|
||||
ptr := b.InlineCall(pkg.rtFunc("AllocZ"), size)
|
||||
ret.impl = b.InlineCall(pkg.rtFunc("NewSlice"), ptr, len, cap).impl
|
||||
ret.Type = t
|
||||
// TODO(xsw)
|
||||
// panic("todo")
|
||||
return
|
||||
}
|
||||
|
||||
// The MapUpdate instruction updates the association of Map[Key] to
|
||||
// Value.
|
||||
//
|
||||
// Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack,
|
||||
// if explicit in the source.
|
||||
//
|
||||
// Example printed form:
|
||||
//
|
||||
// t0[t1] = t2
|
||||
func (b Builder) MapUpdate(m, k, v Expr) {
|
||||
if debugInstr {
|
||||
log.Printf("MapUpdate %v[%v] = %v\n", m.impl, k.impl, v.impl)
|
||||
}
|
||||
// TODO(xsw)
|
||||
// panic("todo")
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
174
ssa/expr.go
174
ssa/expr.go
@@ -500,37 +500,6 @@ func llvmFields(vals []Expr, t *types.Struct, b Builder) (ret []llvm.Value) {
|
||||
return
|
||||
}
|
||||
|
||||
func llvmPredBlocks(preds []BasicBlock) []llvm.BasicBlock {
|
||||
ret := make([]llvm.BasicBlock, len(preds))
|
||||
for i, v := range preds {
|
||||
ret[i] = v.last
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Phi represents a phi node.
|
||||
type Phi struct {
|
||||
Expr
|
||||
}
|
||||
|
||||
// AddIncoming adds incoming values to a phi node.
|
||||
func (p Phi) AddIncoming(b Builder, preds []BasicBlock, f func(i int, blk BasicBlock) Expr) {
|
||||
bs := llvmPredBlocks(preds)
|
||||
vals := make([]llvm.Value, len(preds))
|
||||
for iblk, blk := range preds {
|
||||
vals[iblk] = f(iblk, blk).impl
|
||||
}
|
||||
p.impl.AddIncoming(vals, bs)
|
||||
}
|
||||
|
||||
// Phi returns a phi node.
|
||||
func (b Builder) Phi(t Type) Phi {
|
||||
phi := llvm.CreatePHI(b.impl, t.ll)
|
||||
return Phi{Expr{phi, t}}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Advance returns the pointer ptr advanced by offset.
|
||||
@@ -660,9 +629,6 @@ func (b Builder) Alloc(elem Type, heap bool) (ret Expr) {
|
||||
|
||||
// AllocU allocates uninitialized space for n*sizeof(elem) bytes.
|
||||
func (b Builder) AllocU(elem Type, n ...int64) (ret Expr) {
|
||||
if debugInstr {
|
||||
log.Printf("AllocU %v, %v\n", elem.raw.Type, n)
|
||||
}
|
||||
prog := b.Prog
|
||||
size := SizeOf(prog, elem, n...)
|
||||
ret = b.InlineCall(b.Pkg.rtFunc("AllocU"), size)
|
||||
@@ -670,6 +636,11 @@ func (b Builder) AllocU(elem Type, n ...int64) (ret Expr) {
|
||||
return
|
||||
}
|
||||
|
||||
// AllocZ allocates zero initialized space for n bytes.
|
||||
func (b Builder) AllocZ(n Expr) (ret Expr) {
|
||||
return b.InlineCall(b.Pkg.rtFunc("AllocZ"), n)
|
||||
}
|
||||
|
||||
// Alloca allocates uninitialized space for n bytes.
|
||||
func (b Builder) Alloca(n Expr) (ret Expr) {
|
||||
if debugInstr {
|
||||
@@ -682,6 +653,17 @@ func (b Builder) Alloca(n Expr) (ret Expr) {
|
||||
return
|
||||
}
|
||||
|
||||
// AllocaCStr allocates space for copy it from a Go string.
|
||||
func (b Builder) AllocaCStr(gostr Expr) (ret Expr) {
|
||||
if debugInstr {
|
||||
log.Printf("AllocaCStr %v\n", gostr.impl)
|
||||
}
|
||||
n := b.StringLen(gostr)
|
||||
n1 := b.BinOp(token.ADD, n, b.Prog.Val(1))
|
||||
cstr := b.Alloca(n1)
|
||||
return b.InlineCall(b.Pkg.rtFunc("CStrCopy"), cstr, gostr)
|
||||
}
|
||||
|
||||
/*
|
||||
// ArrayAlloca reserves space for an array of n elements of type telem.
|
||||
func (b Builder) ArrayAlloca(telem Type, n Expr) (ret Expr) {
|
||||
@@ -694,15 +676,14 @@ func (b Builder) ArrayAlloca(telem Type, n Expr) (ret Expr) {
|
||||
}
|
||||
*/
|
||||
|
||||
// AllocaCStr allocates space for copy it from a Go string.
|
||||
func (b Builder) AllocaCStr(gostr Expr) (ret Expr) {
|
||||
if debugInstr {
|
||||
log.Printf("AllocaCStr %v\n", gostr.impl)
|
||||
}
|
||||
n := b.StringLen(gostr)
|
||||
n1 := b.BinOp(token.ADD, n, b.Prog.Val(1))
|
||||
cstr := b.Alloca(n1)
|
||||
return b.InlineCall(b.Pkg.rtFunc("CStrCopy"), cstr, gostr)
|
||||
// ArrayAlloc allocates zero initialized space for an array of n elements of type telem.
|
||||
func (b Builder) ArrayAlloc(telem Type, n Expr) (ret Expr) {
|
||||
prog := b.Prog
|
||||
elemSize := SizeOf(prog, telem)
|
||||
size := b.BinOp(token.MUL, n, elemSize)
|
||||
ret.impl = b.AllocZ(size).impl
|
||||
ret.Type = prog.Pointer(telem)
|
||||
return
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -1047,55 +1028,7 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) {
|
||||
}
|
||||
}
|
||||
case "print", "println":
|
||||
ln := fn == "println"
|
||||
prog := b.Prog
|
||||
ret.Type = prog.Void()
|
||||
for i, arg := range args {
|
||||
if ln && i > 0 {
|
||||
b.InlineCall(b.Pkg.rtFunc("PrintByte"), prog.IntVal(' ', prog.Byte()))
|
||||
}
|
||||
var fn string
|
||||
typ := arg.Type
|
||||
switch arg.kind {
|
||||
case vkBool:
|
||||
fn = "PrintBool"
|
||||
case vkSigned:
|
||||
fn = "PrintInt"
|
||||
typ = prog.Int64()
|
||||
case vkUnsigned:
|
||||
fn = "PrintUint"
|
||||
typ = prog.Uint64()
|
||||
case vkFloat:
|
||||
fn = "PrintFloat"
|
||||
typ = prog.Float64()
|
||||
case vkSlice:
|
||||
fn = "PrintSlice"
|
||||
case vkClosure:
|
||||
arg = b.Field(arg, 0)
|
||||
fallthrough
|
||||
case vkPtr, vkFuncPtr, vkFuncDecl:
|
||||
fn = "PrintPointer"
|
||||
typ = prog.VoidPtr()
|
||||
case vkString:
|
||||
fn = "PrintString"
|
||||
case vkEface:
|
||||
fn = "PrintEface"
|
||||
case vkIface:
|
||||
fn = "PrintIface"
|
||||
// case vkComplex:
|
||||
// fn = "PrintComplex"
|
||||
default:
|
||||
panic(fmt.Errorf("illegal types for operand: print %v", arg.RawType()))
|
||||
}
|
||||
if typ != arg.Type {
|
||||
arg = b.Convert(typ, arg)
|
||||
}
|
||||
b.InlineCall(b.Pkg.rtFunc(fn), arg)
|
||||
}
|
||||
if ln {
|
||||
b.InlineCall(b.Pkg.rtFunc("PrintByte"), prog.IntVal('\n', prog.Byte()))
|
||||
}
|
||||
return
|
||||
return b.PrintEx(fn == "println", args...)
|
||||
case "copy":
|
||||
if len(args) == 2 {
|
||||
dst := args[0]
|
||||
@@ -1120,4 +1053,61 @@ func (b Builder) BuiltinCall(fn string, args ...Expr) (ret Expr) {
|
||||
panic("todo: " + fn)
|
||||
}
|
||||
|
||||
// Println prints the arguments to stderr, followed by a newline.
|
||||
func (b Builder) Println(args ...Expr) (ret Expr) {
|
||||
return b.PrintEx(true, args...)
|
||||
}
|
||||
|
||||
// PrintEx prints the arguments to stderr.
|
||||
func (b Builder) PrintEx(ln bool, args ...Expr) (ret Expr) {
|
||||
prog := b.Prog
|
||||
ret.Type = prog.Void()
|
||||
for i, arg := range args {
|
||||
if ln && i > 0 {
|
||||
b.InlineCall(b.Pkg.rtFunc("PrintByte"), prog.IntVal(' ', prog.Byte()))
|
||||
}
|
||||
var fn string
|
||||
typ := arg.Type
|
||||
switch arg.kind {
|
||||
case vkBool:
|
||||
fn = "PrintBool"
|
||||
case vkSigned:
|
||||
fn = "PrintInt"
|
||||
typ = prog.Int64()
|
||||
case vkUnsigned:
|
||||
fn = "PrintUint"
|
||||
typ = prog.Uint64()
|
||||
case vkFloat:
|
||||
fn = "PrintFloat"
|
||||
typ = prog.Float64()
|
||||
case vkSlice:
|
||||
fn = "PrintSlice"
|
||||
case vkClosure:
|
||||
arg = b.Field(arg, 0)
|
||||
fallthrough
|
||||
case vkPtr, vkFuncPtr, vkFuncDecl:
|
||||
fn = "PrintPointer"
|
||||
typ = prog.VoidPtr()
|
||||
case vkString:
|
||||
fn = "PrintString"
|
||||
case vkEface:
|
||||
fn = "PrintEface"
|
||||
case vkIface:
|
||||
fn = "PrintIface"
|
||||
// case vkComplex:
|
||||
// fn = "PrintComplex"
|
||||
default:
|
||||
panic(fmt.Errorf("illegal types for operand: print %v", arg.RawType()))
|
||||
}
|
||||
if typ != arg.Type {
|
||||
arg = b.Convert(typ, arg)
|
||||
}
|
||||
b.InlineCall(b.Pkg.rtFunc(fn), arg)
|
||||
}
|
||||
if ln {
|
||||
b.InlineCall(b.Pkg.rtFunc("PrintByte"), prog.IntVal('\n', prog.Byte()))
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -287,7 +287,8 @@ func (b Builder) Imethod(intf Expr, method *types.Func) Expr {
|
||||
impl := intf.impl
|
||||
itab := Expr{b.faceItab(impl), prog.VoidPtrPtr()}
|
||||
pfn := b.Advance(itab, prog.IntVal(uint64(i+3), prog.Int()))
|
||||
return b.aggregateValue(tclosure, b.Load(pfn).impl, b.faceData(impl))
|
||||
fn := b.Load(pfn)
|
||||
return b.aggregateValue(tclosure, fn.impl, b.faceData(impl))
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -206,21 +206,35 @@ func (b Builder) If(cond Expr, thenb, elseb BasicBlock) {
|
||||
b.impl.CreateCondBr(cond.impl, thenb.first, elseb.first)
|
||||
}
|
||||
|
||||
// The MapUpdate instruction updates the association of Map[Key] to
|
||||
// Value.
|
||||
//
|
||||
// Pos() returns the ast.KeyValueExpr.Colon or ast.IndexExpr.Lbrack,
|
||||
// if explicit in the source.
|
||||
//
|
||||
// Example printed form:
|
||||
//
|
||||
// t0[t1] = t2
|
||||
func (b Builder) MapUpdate(m, k, v Expr) {
|
||||
if debugInstr {
|
||||
log.Printf("MapUpdate %v[%v] = %v\n", m.impl, k.impl, v.impl)
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Phi represents a phi node.
|
||||
type Phi struct {
|
||||
Expr
|
||||
}
|
||||
|
||||
// AddIncoming adds incoming values to a phi node.
|
||||
func (p Phi) AddIncoming(b Builder, preds []BasicBlock, f func(i int, blk BasicBlock) Expr) {
|
||||
bs := llvmPredBlocks(preds)
|
||||
vals := make([]llvm.Value, len(preds))
|
||||
for iblk, blk := range preds {
|
||||
vals[iblk] = f(iblk, blk).impl
|
||||
}
|
||||
// TODO(xsw)
|
||||
// panic("todo")
|
||||
p.impl.AddIncoming(vals, bs)
|
||||
}
|
||||
|
||||
func llvmPredBlocks(preds []BasicBlock) []llvm.BasicBlock {
|
||||
ret := make([]llvm.BasicBlock, len(preds))
|
||||
for i, v := range preds {
|
||||
ret[i] = v.last
|
||||
}
|
||||
return ret
|
||||
}
|
||||
|
||||
// Phi returns a phi node.
|
||||
func (b Builder) Phi(t Type) Phi {
|
||||
phi := llvm.CreatePHI(b.impl, t.ll)
|
||||
return Phi{Expr{phi, t}}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user