instead runtime.Zeroinit of memset to compatible build.NaiveForm

This commit is contained in:
Li Jie
2024-09-14 10:54:03 +08:00
parent 9f8b9ea806
commit 1e58c365ed
3 changed files with 28 additions and 8 deletions

View File

@@ -95,11 +95,6 @@ func stringTracef(fp c.FilePtr, format *c.Char, s String) {
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// Zeroinit initializes memory to zero.
func Zeroinit(p unsafe.Pointer, size uintptr) unsafe.Pointer {
return c.Memset(p, 0, size)
}
// New allocates memory and initializes it to zero. // New allocates memory and initializes it to zero.
func New(t *Type) unsafe.Pointer { func New(t *Type) unsafe.Pointer {
return AllocZ(t.Size_) return AllocZ(t.Size_)

View File

@@ -110,7 +110,7 @@ func (b Builder) Alloc(elem Type, heap bool) (ret Expr) {
ret = b.InlineCall(pkg.rtFunc("AllocZ"), size) ret = b.InlineCall(pkg.rtFunc("AllocZ"), size)
} else { } else {
ret = Expr{llvm.CreateAlloca(b.impl, elem.ll), prog.VoidPtr()} ret = Expr{llvm.CreateAlloca(b.impl, elem.ll), prog.VoidPtr()}
ret.impl = b.InlineCall(pkg.rtFunc("Zeroinit"), ret, size).impl ret.impl = b.zeroinit(ret, size).impl
} }
ret.Type = prog.Pointer(elem) ret.Type = prog.Pointer(elem)
return return
@@ -210,6 +210,18 @@ func (p Program) tyFree() *types.Signature {
return p.freeTy return p.freeTy
} }
func (p Program) tyMemsetInline() *types.Signature {
if p.memsetInlineTy == nil {
paramPtr := types.NewParam(token.NoPos, nil, "", p.VoidPtr().raw.Type)
paramInt := types.NewParam(token.NoPos, nil, "", p.Byte().raw.Type)
paramSize := types.NewParam(token.NoPos, nil, "", p.Uintptr().raw.Type)
paramBool := types.NewParam(token.NoPos, nil, "", p.Bool().raw.Type)
params := types.NewTuple(paramPtr, paramInt, paramSize, paramBool)
p.memsetInlineTy = types.NewSignatureType(nil, nil, nil, params, nil, false)
}
return p.memsetInlineTy
}
func (b Builder) malloc(size Expr) Expr { func (b Builder) malloc(size Expr) Expr {
fn := b.Pkg.cFunc("malloc", b.Prog.tyMalloc()) fn := b.Pkg.cFunc("malloc", b.Prog.tyMalloc())
return b.Call(fn, size) return b.Call(fn, size)
@@ -220,6 +232,18 @@ func (b Builder) free(ptr Expr) Expr {
return b.Call(fn, ptr) return b.Call(fn, ptr)
} }
// declare void @llvm.memset.inline.p0.p0i8.i32(ptr <dest>, i8 <val>, i32 <len>, i1 <isvolatile>)
// declare void @llvm.memset.inline.p0.p0.i64(ptr <dest>, i8 <val>, i64 <len>, i1 <isvolatile>)
func (b Builder) memset(ptr, val, len, isvolatile Expr) Expr {
fn := b.Pkg.cFunc("llvm.memset", b.Prog.tyMemsetInline())
b.Call(fn, ptr, val, len, isvolatile)
return ptr
}
func (b Builder) zeroinit(ptr, size Expr) Expr {
return b.memset(ptr, b.Prog.IntVal(0, b.Prog.Byte()), size, b.Prog.Val(false))
}
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
// ArrayAlloca reserves space for an array of n elements of type telem. // ArrayAlloca reserves space for an array of n elements of type telem.

View File

@@ -181,6 +181,7 @@ type aProgram struct {
mallocTy *types.Signature mallocTy *types.Signature
freeTy *types.Signature freeTy *types.Signature
memsetInlineTy *types.Signature
createKeyTy *types.Signature createKeyTy *types.Signature
getSpecTy *types.Signature getSpecTy *types.Signature