Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions _demo/c/stacksave/stacksave_amd64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build amd64

package main

import (
"unsafe"
_ "unsafe"
)

//go:linkname getsp llgo.stackSave
func getsp() unsafe.Pointer

//go:linkname asmFull llgo.asm
func asmFull(instruction string, regs map[string]any) uintptr { return 0 }

func main() {
sp := asmFull("movq %rsp, {}", nil)

if sp != uintptr(getsp()) {
panic("invalid stack pointer")
}
}
22 changes: 22 additions & 0 deletions _demo/c/stacksave/stacksave_arm64.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//go:build arm64

package main

import (
"unsafe"
_ "unsafe"
)

//go:linkname getsp llgo.stackSave
func getsp() unsafe.Pointer

//go:linkname asmFull llgo.asm
func asmFull(instruction string, regs map[string]any) uintptr { return 0 }

func main() {
sp := asmFull("mov {}, sp", nil)

if sp != uintptr(getsp()) {
panic("invalid stack pointer")
}
}
14 changes: 14 additions & 0 deletions cl/_testrt/stacksave/in.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"unsafe"
_ "unsafe"
)

//go:linkname getsp llgo.stackSave
func getsp() unsafe.Pointer

func main() {
sp := getsp()
println(sp)
}
34 changes: 34 additions & 0 deletions cl/_testrt/stacksave/out.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
; ModuleID = 'github.com/goplus/llgo/cl/_testrt/stacksave'
source_filename = "github.com/goplus/llgo/cl/_testrt/stacksave"

@"github.com/goplus/llgo/cl/_testrt/stacksave.init$guard" = global i1 false, align 1

define void @"github.com/goplus/llgo/cl/_testrt/stacksave.init"() {
_llgo_0:
%0 = load i1, ptr @"github.com/goplus/llgo/cl/_testrt/stacksave.init$guard", align 1
br i1 %0, label %_llgo_2, label %_llgo_1

_llgo_1: ; preds = %_llgo_0
store i1 true, ptr @"github.com/goplus/llgo/cl/_testrt/stacksave.init$guard", align 1
br label %_llgo_2

_llgo_2: ; preds = %_llgo_1, %_llgo_0
ret void
}

define void @"github.com/goplus/llgo/cl/_testrt/stacksave.main"() {
_llgo_0:
%0 = call ptr @llvm.stacksave()
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintPointer"(ptr %0)
call void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8 10)
ret void
}

; Function Attrs: nocallback nofree nosync nounwind willreturn
declare ptr @llvm.stacksave() #0

declare void @"github.com/goplus/llgo/runtime/internal/runtime.PrintPointer"(ptr)

declare void @"github.com/goplus/llgo/runtime/internal/runtime.PrintByte"(i8)

attributes #0 = { nocallback nofree nosync nounwind willreturn }
3 changes: 2 additions & 1 deletion cl/import.go
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,8 @@ const (
llgoCgoCheckPointer = llgoCgoBase + 0x6
llgoCgoCgocall = llgoCgoBase + 0x7

llgoAsm = llgoInstrBase + 0x40
llgoAsm = llgoInstrBase + 0x40
llgoStackSave = llgoInstrBase + 0x41

llgoAtomicOpLast = llgoAtomicOpBase + int(llssa.OpUMin)
)
Expand Down
5 changes: 4 additions & 1 deletion cl/instr.go
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,8 @@ var llgoInstrs = map[string]int{
"_cgoCheckPointer": llgoCgoCheckPointer,
"_cgo_runtime_cgocall": llgoCgoCgocall,

"asm": llgoAsm,
"asm": llgoAsm,
"stackSave": llgoStackSave,
}

// funcOf returns a function by name and set ftype = goFunc, cFunc, etc.
Expand Down Expand Up @@ -601,6 +602,8 @@ func (p *context) call(b llssa.Builder, act llssa.DoAction, call *ssa.CallCommon
ret = p.sigsetjmp(b, args)
case llgoSiglongjmp:
p.siglongjmp(b, args)
case llgoStackSave:
ret = b.StackSave()
case llgoSigjmpbuf: // func sigjmpbuf()
ret = b.AllocaSigjmpBuf()
case llgoDeferData: // func deferData() *Defer
Expand Down
16 changes: 16 additions & 0 deletions ssa/eh.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,13 +91,29 @@ func (p Program) tySiglongjmp() *types.Signature {
return p.sigljmpTy
}

// func() unsafe.Pointer
func (p Program) tyStacksave() *types.Signature {
if p.stackSaveTy == nil {
paramPtr := types.NewParam(token.NoPos, nil, "", p.VoidPtr().raw.Type)
params := types.NewTuple(paramPtr)
p.stackSaveTy = types.NewSignatureType(nil, nil, nil, nil, params, false)
}
return p.stackSaveTy
}

func (b Builder) AllocaSigjmpBuf() Expr {
prog := b.Prog
n := unsafe.Sizeof(sigjmpbuf{})
size := prog.IntVal(uint64(n), prog.Uintptr())
return b.Alloca(size)
}

// declare ptr @llvm.stacksave.p0()
func (b Builder) StackSave() Expr {
fn := b.Pkg.cFunc("llvm.stacksave", b.Prog.tyStacksave())
return b.InlineCall(fn)
}

func (b Builder) Sigsetjmp(jb, savemask Expr) Expr {
if b.Prog.target.GOARCH == "wasm" {
return b.Setjmp(jb)
Expand Down
1 change: 1 addition & 0 deletions ssa/package.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ type aProgram struct {
mallocTy *types.Signature
freeTy *types.Signature
memsetInlineTy *types.Signature
stackSaveTy *types.Signature

createKeyTy *types.Signature
getSpecTy *types.Signature
Expand Down
Loading