Merge pull request #425 from visualfc/makeslice
ssa: makeSlice fit int size and check
This commit is contained in:
56
cl/_testgo/makeslice/in.go
Normal file
56
cl/_testgo/makeslice/in.go
Normal file
@@ -0,0 +1,56 @@
|
||||
package main
|
||||
|
||||
func main() {
|
||||
}
|
||||
|
||||
func init() {
|
||||
var n int = 2
|
||||
buf := make([]int, n, n*2)
|
||||
if len(buf) != 2 || cap(buf) != 4 {
|
||||
panic("error")
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
var n int32 = 2
|
||||
buf := make([]int, n, n*2)
|
||||
if len(buf) != 2 || cap(buf) != 4 {
|
||||
panic("error")
|
||||
}
|
||||
}
|
||||
|
||||
func init() {
|
||||
defer func() {
|
||||
r := recover()
|
||||
if r == nil {
|
||||
println("must error")
|
||||
}
|
||||
}()
|
||||
var n int = -1
|
||||
buf := make([]int, n)
|
||||
_ = buf
|
||||
}
|
||||
|
||||
func init() {
|
||||
defer func() {
|
||||
r := recover()
|
||||
if r == nil {
|
||||
println("must error")
|
||||
}
|
||||
}()
|
||||
var n int = 2
|
||||
buf := make([]int, n, n-1)
|
||||
_ = buf
|
||||
}
|
||||
|
||||
func init() {
|
||||
defer func() {
|
||||
r := recover()
|
||||
if r == nil {
|
||||
println("must error")
|
||||
}
|
||||
}()
|
||||
var n int64 = 1<<63 - 1
|
||||
buf := make([]int, n)
|
||||
_ = buf
|
||||
}
|
||||
1
cl/_testgo/makeslice/out.ll
Normal file
1
cl/_testgo/makeslice/out.ll
Normal file
@@ -0,0 +1 @@
|
||||
;
|
||||
@@ -108,67 +108,58 @@ declare void @"github.com/goplus/llgo/internal/runtime.Panic"(%"github.com/goplu
|
||||
|
||||
define i64 @"main.recur2[main.T]"(i64 %0) {
|
||||
_llgo_0:
|
||||
%1 = mul i64 %0, 8
|
||||
%2 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 %1)
|
||||
%3 = alloca %"github.com/goplus/llgo/internal/runtime.Slice", align 8
|
||||
%4 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %3, i32 0, i32 0
|
||||
store ptr %2, ptr %4, align 8
|
||||
%5 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %3, i32 0, i32 1
|
||||
store i64 %0, ptr %5, align 4
|
||||
%6 = getelementptr inbounds %"github.com/goplus/llgo/internal/runtime.Slice", ptr %3, i32 0, i32 2
|
||||
store i64 %0, ptr %6, align 4
|
||||
%7 = load %"github.com/goplus/llgo/internal/runtime.Slice", ptr %3, align 8
|
||||
%8 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %7, 1
|
||||
%1 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.MakeSlice"(i64 %0, i64 %0, i64 8)
|
||||
%2 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %1, 1
|
||||
br label %_llgo_1
|
||||
|
||||
_llgo_1: ; preds = %_llgo_2, %_llgo_0
|
||||
%9 = phi i64 [ -1, %_llgo_0 ], [ %10, %_llgo_2 ]
|
||||
%10 = add i64 %9, 1
|
||||
%11 = icmp slt i64 %10, %8
|
||||
br i1 %11, label %_llgo_2, label %_llgo_3
|
||||
%3 = phi i64 [ -1, %_llgo_0 ], [ %4, %_llgo_2 ]
|
||||
%4 = add i64 %3, 1
|
||||
%5 = icmp slt i64 %4, %2
|
||||
br i1 %5, label %_llgo_2, label %_llgo_3
|
||||
|
||||
_llgo_2: ; preds = %_llgo_1
|
||||
%12 = add i64 %10, 1
|
||||
%13 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %7, 0
|
||||
%14 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %7, 1
|
||||
%15 = icmp slt i64 %10, 0
|
||||
%16 = icmp sge i64 %10, %14
|
||||
%17 = or i1 %16, %15
|
||||
call void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1 %17)
|
||||
%18 = getelementptr inbounds i64, ptr %13, i64 %10
|
||||
store i64 %12, ptr %18, align 4
|
||||
%6 = add i64 %4, 1
|
||||
%7 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %1, 0
|
||||
%8 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %1, 1
|
||||
%9 = icmp slt i64 %4, 0
|
||||
%10 = icmp sge i64 %4, %8
|
||||
%11 = or i1 %10, %9
|
||||
call void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1 %11)
|
||||
%12 = getelementptr inbounds i64, ptr %7, i64 %4
|
||||
store i64 %6, ptr %12, align 4
|
||||
br label %_llgo_1
|
||||
|
||||
_llgo_3: ; preds = %_llgo_1
|
||||
%19 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %7, 1
|
||||
%13 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %1, 1
|
||||
br label %_llgo_4
|
||||
|
||||
_llgo_4: ; preds = %_llgo_5, %_llgo_3
|
||||
%20 = phi i64 [ 0, %_llgo_3 ], [ %31, %_llgo_5 ]
|
||||
%21 = phi i64 [ -1, %_llgo_3 ], [ %22, %_llgo_5 ]
|
||||
%22 = add i64 %21, 1
|
||||
%23 = icmp slt i64 %22, %19
|
||||
br i1 %23, label %_llgo_5, label %_llgo_6
|
||||
%14 = phi i64 [ 0, %_llgo_3 ], [ %25, %_llgo_5 ]
|
||||
%15 = phi i64 [ -1, %_llgo_3 ], [ %16, %_llgo_5 ]
|
||||
%16 = add i64 %15, 1
|
||||
%17 = icmp slt i64 %16, %13
|
||||
br i1 %17, label %_llgo_5, label %_llgo_6
|
||||
|
||||
_llgo_5: ; preds = %_llgo_4
|
||||
%24 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %7, 0
|
||||
%25 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %7, 1
|
||||
%26 = icmp slt i64 %22, 0
|
||||
%27 = icmp sge i64 %22, %25
|
||||
%28 = or i1 %27, %26
|
||||
call void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1 %28)
|
||||
%29 = getelementptr inbounds i64, ptr %24, i64 %22
|
||||
%30 = load i64, ptr %29, align 4
|
||||
%31 = add i64 %20, %30
|
||||
%18 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %1, 0
|
||||
%19 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %1, 1
|
||||
%20 = icmp slt i64 %16, 0
|
||||
%21 = icmp sge i64 %16, %19
|
||||
%22 = or i1 %21, %20
|
||||
call void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1 %22)
|
||||
%23 = getelementptr inbounds i64, ptr %18, i64 %16
|
||||
%24 = load i64, ptr %23, align 4
|
||||
%25 = add i64 %14, %24
|
||||
br label %_llgo_4
|
||||
|
||||
_llgo_6: ; preds = %_llgo_4
|
||||
%32 = sub i64 %0, 1
|
||||
%33 = call i64 @"main.recur1[main.T]"(i64 %32)
|
||||
%34 = add i64 %20, %33
|
||||
ret i64 %34
|
||||
%26 = sub i64 %0, 1
|
||||
%27 = call i64 @"main.recur1[main.T]"(i64 %26)
|
||||
%28 = add i64 %14, %27
|
||||
ret i64 %28
|
||||
}
|
||||
|
||||
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
|
||||
declare %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.MakeSlice"(i64, i64, i64)
|
||||
|
||||
declare void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1)
|
||||
|
||||
@@ -13,41 +13,32 @@ source_filename = "main"
|
||||
|
||||
define %"github.com/goplus/llgo/internal/runtime.Slice" @main.genInts(i64 %0, { ptr, ptr } %1) {
|
||||
_llgo_0:
|
||||
%2 = mul i64 %0, 4
|
||||
%3 = call ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64 %2)
|
||||
%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
|
||||
%2 = call %"github.com/goplus/llgo/internal/runtime.Slice" @"github.com/goplus/llgo/internal/runtime.MakeSlice"(i64 %0, i64 %0, i64 4)
|
||||
%3 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %2, 1
|
||||
br label %_llgo_1
|
||||
|
||||
_llgo_1: ; preds = %_llgo_2, %_llgo_0
|
||||
%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
|
||||
%4 = phi i64 [ -1, %_llgo_0 ], [ %5, %_llgo_2 ]
|
||||
%5 = add i64 %4, 1
|
||||
%6 = icmp slt i64 %5, %3
|
||||
br i1 %6, label %_llgo_2, label %_llgo_3
|
||||
|
||||
_llgo_2: ; preds = %_llgo_1
|
||||
%13 = extractvalue { ptr, ptr } %1, 1
|
||||
%14 = extractvalue { ptr, ptr } %1, 0
|
||||
%15 = call i32 %14(ptr %13)
|
||||
%16 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %8, 0
|
||||
%17 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %8, 1
|
||||
%18 = icmp slt i64 %11, 0
|
||||
%19 = icmp sge i64 %11, %17
|
||||
%20 = or i1 %19, %18
|
||||
call void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1 %20)
|
||||
%21 = getelementptr inbounds i32, ptr %16, i64 %11
|
||||
store i32 %15, ptr %21, align 4
|
||||
%7 = extractvalue { ptr, ptr } %1, 1
|
||||
%8 = extractvalue { ptr, ptr } %1, 0
|
||||
%9 = call i32 %8(ptr %7)
|
||||
%10 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %2, 0
|
||||
%11 = extractvalue %"github.com/goplus/llgo/internal/runtime.Slice" %2, 1
|
||||
%12 = icmp slt i64 %5, 0
|
||||
%13 = icmp sge i64 %5, %11
|
||||
%14 = or i1 %13, %12
|
||||
call void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1 %14)
|
||||
%15 = getelementptr inbounds i32, ptr %10, i64 %5
|
||||
store i32 %9, ptr %15, align 4
|
||||
br label %_llgo_1
|
||||
|
||||
_llgo_3: ; preds = %_llgo_1
|
||||
ret %"github.com/goplus/llgo/internal/runtime.Slice" %8
|
||||
ret %"github.com/goplus/llgo/internal/runtime.Slice" %2
|
||||
}
|
||||
|
||||
define i32 @"main.(*generator).next"(ptr %0) {
|
||||
@@ -195,7 +186,7 @@ _llgo_0:
|
||||
ret i32 %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.MakeSlice"(i64, i64, i64)
|
||||
|
||||
declare void @"github.com/goplus/llgo/internal/runtime.AssertIndexRange"(i1)
|
||||
|
||||
@@ -211,6 +202,8 @@ _llgo_0:
|
||||
|
||||
declare i32 @printf(ptr, ...)
|
||||
|
||||
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocZ"(i64)
|
||||
|
||||
declare ptr @"github.com/goplus/llgo/internal/runtime.AllocU"(i64)
|
||||
|
||||
define i32 @"main.next$bound"(ptr %0) {
|
||||
|
||||
@@ -543,12 +543,9 @@ func (p *context) compileInstrOrValue(b llssa.Builder, iv instrOrValue, asValue
|
||||
x := p.compileValue(b, v.X)
|
||||
ret = b.MakeInterface(t, x)
|
||||
case *ssa.MakeSlice:
|
||||
var nCap llssa.Expr
|
||||
t := p.prog.Type(v.Type(), llssa.InGo)
|
||||
nLen := p.compileValue(b, v.Len)
|
||||
if v.Cap != nil {
|
||||
nCap = p.compileValue(b, v.Cap)
|
||||
}
|
||||
nCap := p.compileValue(b, v.Cap)
|
||||
ret = b.MakeSlice(t, nLen, nCap)
|
||||
case *ssa.MakeMap:
|
||||
var nReserve llssa.Expr
|
||||
|
||||
3
internal/runtime/goarch/goarch.go
Normal file
3
internal/runtime/goarch/goarch.go
Normal file
@@ -0,0 +1,3 @@
|
||||
package goarch
|
||||
|
||||
const PtrSize = 4 << (^uintptr(0) >> 63)
|
||||
15
internal/runtime/math/math.go
Normal file
15
internal/runtime/math/math.go
Normal file
@@ -0,0 +1,15 @@
|
||||
package math
|
||||
|
||||
import "github.com/goplus/llgo/internal/runtime/goarch"
|
||||
|
||||
const MaxUintptr = ^uintptr(0)
|
||||
|
||||
// MulUintptr returns a * b and whether the multiplication overflowed.
|
||||
// On supported platforms this is an intrinsic lowered by the compiler.
|
||||
func MulUintptr(a, b uintptr) (uintptr, bool) {
|
||||
if a|b < 1<<(4*goarch.PtrSize) || a == 0 {
|
||||
return a * b, false
|
||||
}
|
||||
overflow := b > MaxUintptr/a
|
||||
return a * b, overflow
|
||||
}
|
||||
@@ -36,3 +36,10 @@ func fastrand() uint32 {
|
||||
return s0 + s1
|
||||
}
|
||||
*/
|
||||
|
||||
const (
|
||||
// _64bit = 1 on 64-bit systems, 0 on 32-bit systems
|
||||
_64bit = 1 << (^uintptr(0) >> 63) / 2
|
||||
heapAddrBits = (_64bit)*48 + (1-_64bit)*(32)
|
||||
maxAlloc = (1 << heapAddrBits) - (1-_64bit)*1
|
||||
)
|
||||
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
"unsafe"
|
||||
|
||||
"github.com/goplus/llgo/c"
|
||||
"github.com/goplus/llgo/internal/runtime/math"
|
||||
)
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
@@ -120,4 +121,24 @@ func SliceCopy(dst Slice, data unsafe.Pointer, num int, etSize int) int {
|
||||
return n
|
||||
}
|
||||
|
||||
func MakeSlice(len, cap int, etSize int) Slice {
|
||||
mem, overflow := math.MulUintptr(uintptr(etSize), uintptr(cap))
|
||||
if overflow || mem > maxAlloc || len < 0 || len > cap {
|
||||
mem, overflow := math.MulUintptr(uintptr(etSize), uintptr(len))
|
||||
if overflow || mem > maxAlloc || len < 0 {
|
||||
panicmakeslicelen()
|
||||
}
|
||||
panicmakeslicecap()
|
||||
}
|
||||
return Slice{AllocZ(mem), len, cap}
|
||||
}
|
||||
|
||||
func panicmakeslicelen() {
|
||||
panic(errorString("makeslice: len out of range"))
|
||||
}
|
||||
|
||||
func panicmakeslicecap() {
|
||||
panic(errorString("makeslice: cap out of range"))
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
@@ -416,16 +416,25 @@ func (b Builder) MakeSlice(t Type, len, cap Expr) (ret Expr) {
|
||||
log.Printf("MakeSlice %v, %v, %v\n", t.RawType(), len.impl, cap.impl)
|
||||
}
|
||||
prog := b.Prog
|
||||
if cap.IsNil() {
|
||||
cap = len
|
||||
}
|
||||
len = b.fitIntSize(len)
|
||||
cap = b.fitIntSize(cap)
|
||||
telem := prog.Index(t)
|
||||
ptr := b.ArrayAlloc(telem, cap)
|
||||
ret.impl = b.unsafeSlice(ptr, len.impl, cap.impl).impl
|
||||
ret = b.InlineCall(b.Pkg.rtFunc("MakeSlice"), len, cap, prog.IntVal(prog.SizeOf(telem), prog.Int()))
|
||||
ret.Type = t
|
||||
return
|
||||
}
|
||||
|
||||
// fit size to int
|
||||
func (b Builder) fitIntSize(n Expr) Expr {
|
||||
prog := b.Prog
|
||||
typ := prog.Int()
|
||||
if prog.SizeOf(n.Type) != prog.SizeOf(typ) {
|
||||
n.Type = typ
|
||||
n.impl = castInt(b, n.impl, typ)
|
||||
}
|
||||
return n
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// The MakeMap instruction creates a new hash-table-based map object
|
||||
|
||||
Reference in New Issue
Block a user