diff --git a/cl/_testgo/makeslice/in.go b/cl/_testgo/makeslice/in.go new file mode 100644 index 00000000..d6602a8c --- /dev/null +++ b/cl/_testgo/makeslice/in.go @@ -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 +} diff --git a/cl/_testgo/makeslice/out.ll b/cl/_testgo/makeslice/out.ll new file mode 100644 index 00000000..1c8a0e79 --- /dev/null +++ b/cl/_testgo/makeslice/out.ll @@ -0,0 +1 @@ +; \ No newline at end of file diff --git a/cl/_testgo/tprecur/out.ll b/cl/_testgo/tprecur/out.ll index a1e869f6..6cd1f48f 100644 --- a/cl/_testgo/tprecur/out.ll +++ b/cl/_testgo/tprecur/out.ll @@ -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) diff --git a/cl/_testrt/intgen/out.ll b/cl/_testrt/intgen/out.ll index 5908ac5b..1ffc9428 100644 --- a/cl/_testrt/intgen/out.ll +++ b/cl/_testrt/intgen/out.ll @@ -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) { diff --git a/cl/compile.go b/cl/compile.go index 4517303e..419618cf 100644 --- a/cl/compile.go +++ b/cl/compile.go @@ -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 diff --git a/internal/runtime/goarch/goarch.go b/internal/runtime/goarch/goarch.go new file mode 100644 index 00000000..24c0de95 --- /dev/null +++ b/internal/runtime/goarch/goarch.go @@ -0,0 +1,3 @@ +package goarch + +const PtrSize = 4 << (^uintptr(0) >> 63) diff --git a/internal/runtime/math/math.go b/internal/runtime/math/math.go new file mode 100644 index 00000000..8d38eab8 --- /dev/null +++ b/internal/runtime/math/math.go @@ -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 +} diff --git a/internal/runtime/stubs.go b/internal/runtime/stubs.go index ed8b9a8d..0b81dc67 100644 --- a/internal/runtime/stubs.go +++ b/internal/runtime/stubs.go @@ -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 +) diff --git a/internal/runtime/z_slice.go b/internal/runtime/z_slice.go index 3c31710f..6eed5167 100644 --- a/internal/runtime/z_slice.go +++ b/internal/runtime/z_slice.go @@ -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")) +} + // ----------------------------------------------------------------------------- diff --git a/ssa/datastruct.go b/ssa/datastruct.go index 2f45c7b6..9ce447fa 100644 --- a/ssa/datastruct.go +++ b/ssa/datastruct.go @@ -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