runtime: rm NewSlice (use b.unsafeSlice); llgo/ssa: Println

This commit is contained in:
xushiwei
2024-05-28 07:47:07 +08:00
parent 963d7958ea
commit 1c1da6433a
9 changed files with 224 additions and 180 deletions

View File

@@ -15,29 +15,36 @@ define %"github.com/goplus/llgo/internal/runtime.Slice" @main.genInts(i64 %0, {
_llgo_0:
%2 = mul i64 %0, 4
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 %2)
%4 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(ptr %3, i64 %0, i64 %0)
%5 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %4, 1
%4 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8
%5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %4, i32 0, i32 0
store ptr %3, ptr %5, align 8
%6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %4, i32 0, i32 1
store i64 %0, ptr %6, align 4
%7 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %4, i32 0, i32 2
store i64 %0, ptr %7, align 4
%8 = load %"github.com/goplus/llgo/internal/runtime.Slice", ptr %4, align 8
%9 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %8, 1
br label %_llgo_1
_llgo_1: ; preds = %_llgo_2, %_llgo_0
%6 = phi i64 [ -1, %_llgo_0 ], [ %7, %_llgo_2 ]
%7 = add i64 %6, 1
%8 = icmp slt i64 %7, %5
br i1 %8, label %_llgo_2, label %_llgo_3
%10 = phi i64 [ -1, %_llgo_0 ], [ %11, %_llgo_2 ]
%11 = add i64 %10, 1
%12 = icmp slt i64 %11, %9
br i1 %12, label %_llgo_2, label %_llgo_3
_llgo_2: ; preds = %_llgo_1
%9 = extractvalue { ptr, ptr } %1, 1
%10 = extractvalue { ptr, ptr } %1, 0
%11 = call i32 %10(ptr %9)
%12 = icmp slt i64 %7, 0
call void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1 %12)
%13 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %4, 0
%14 = getelementptr inbounds i32, ptr %13, i64 %7
store i32 %11, ptr %14, align 4
%13 = extractvalue { ptr, ptr } %1, 1
%14 = extractvalue { ptr, ptr } %1, 0
%15 = call i32 %14(ptr %13)
%16 = icmp slt i64 %11, 0
call void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1 %16)
%17 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %8, 0
%18 = getelementptr inbounds i32, ptr %17, i64 %11
store i32 %15, ptr %18, align 4
br label %_llgo_1
_llgo_3: ; preds = %_llgo_1
ret %"github.com/goplus/llgo/internal/runtime.Slice" %4
ret %"github.com/goplus/llgo/internal/runtime.Slice" %8
}
define i32 @"(*main.generator).next"(ptr %0) {
@@ -165,8 +172,6 @@ _llgo_9: ; preds = %_llgo_7
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.NewSlice"(ptr, i64, i64)
declare void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1)
declare void @"github.com/goplus/llgo/internal/runtime.init"()

Binary file not shown.

View File

@@ -1,6 +1,18 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package runtime
@@ -91,7 +103,7 @@ func hdrSizeOf(kind abi.Kind) uintptr {
}
// Named returns a named type.
func Named(pkgPath, name string, underlying *Type, methods []abi.Method) *Type {
func Named(pkgPath, name string, underlying *Type, methods []Method) *Type {
tflag := underlying.TFlag
if tflag&abi.TFlagUncommon != 0 {
panic("runtime: underlying type is already named")
@@ -179,7 +191,7 @@ func Func(in, out []*Type, variadic bool) *FuncType {
}
// Interface returns an interface type.
func Interface(pkgPath string, methods []abi.Imethod) *InterfaceType {
func Interface(pkgPath string, methods []Imethod) *InterfaceType {
ret := &abi.InterfaceType{
Type: Type{
Size_: unsafe.Sizeof(eface{}),
@@ -227,6 +239,7 @@ func findMethod(mthds []abi.Method, im abi.Imethod) abi.Text {
mName := m.Name_
if mName >= imName {
if mName == imName && m.Mtyp_ == im.Typ_ {
println("==> findMethod", mName, m.Ifn_)
return m.Ifn_
}
break

View File

@@ -1,6 +1,18 @@
// Copyright 2009 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
/*
* Copyright (c) 2024 The GoPlus Authors (goplus.org). All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package runtime

View File

@@ -31,11 +31,6 @@ type Slice struct {
cap int
}
// NewSlice creates a new slice.
func NewSlice(data unsafe.Pointer, len, cap int) Slice {
return Slice{data, len, cap}
}
func NewSlice3(base unsafe.Pointer, eltSize, cap, i, j, k int) (s Slice) {
if i < 0 || j < i || k < j || k > cap {
panic("slice index out of bounds")

View File

@@ -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")
}
// -----------------------------------------------------------------------------

View File

@@ -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
}
// -----------------------------------------------------------------------------

View File

@@ -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))
}
// -----------------------------------------------------------------------------

View File

@@ -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}}
}
// -----------------------------------------------------------------------------