llgo/ssa: builder.Alloc sizeof(t) bugfix

This commit is contained in:
xushiwei
2024-05-01 16:56:26 +08:00
parent e112d8277f
commit 62f6542ca9
8 changed files with 56 additions and 107 deletions

View File

@@ -23,7 +23,6 @@ import (
"go/token"
"go/types"
"log"
"unsafe"
"github.com/goplus/llvm"
)
@@ -531,8 +530,8 @@ func (b Builder) Alloc(t *types.Pointer, heap bool) (ret Expr) {
telem := t.Elem()
if heap {
pkg := b.fn.pkg
size := unsafe.Sizeof(telem)
ret = b.Call(pkg.rtFunc("Alloc"), prog.Val(size))
size := prog.sizs.Sizeof(telem)
ret = b.Call(pkg.rtFunc("Alloc"), prog.Val(uintptr(size)))
} else {
ret.impl = llvm.CreateAlloca(b.impl, prog.Type(telem).ll)
}

View File

@@ -19,6 +19,7 @@ package ssa
import (
"go/constant"
"go/types"
"runtime"
"github.com/goplus/llvm"
"golang.org/x/tools/go/types/typeutil"
@@ -97,6 +98,7 @@ func Initialize(flags InitFlags) {
type aProgram struct {
ctx llvm.Context
typs typeutil.Map
sizs types.Sizes
rt *types.Package
rtget func() *types.Package
@@ -139,11 +141,16 @@ func NewProgram(target *Target) Program {
if target == nil {
target = &Target{}
}
arch := target.GOARCH
if arch == "" {
arch = runtime.GOARCH
}
ctx := llvm.NewContext()
sizes := types.SizesFor("gc", arch)
// TODO(xsw): Finalize may cause panic, so comment it.
// ctx.Finalize()
td := llvm.NewTargetData("") // TODO(xsw): target config
return &aProgram{ctx: ctx, target: target, td: td}
return &aProgram{ctx: ctx, sizs: sizes, target: target, td: td}
}
// SetRuntime sets the runtime.