Skip to content

Implement stack probe for x86 #9535

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
9 changes: 9 additions & 0 deletions src/compiler/crystal/codegen/fun.cr
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,15 @@ class Crystal::CodeGenVisitor
context.fun.add_attribute LLVM::Attribute::NoInline
context.fun.linkage = LLVM::Linkage::External
end

# TODO: crystal should support __chkstk/__chkstk_ms for windows.
if @program.has_flag?("unix")
if @program.has_flag?("i386") || @program.has_flag?("x86_64")
context.fun.add_target_dependent_attribute "probe-stack", "__crystal_probe_stack"
else
# currnetly LLVM does not support probe-stack for other architectures.
end
end
end

def setup_closure_vars(def_vars, closure_vars, context = self.context, closure_ptr = fun_literal_closure_ptr)
Expand Down
1 change: 1 addition & 0 deletions src/crystal/compiler_rt.cr
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
{% skip_file if flag?(:skip_crystal_compiler_rt) %}

require "./compiler_rt/mulodi4.cr"
require "./compiler_rt/probestack.cr"
47 changes: 47 additions & 0 deletions src/crystal/compiler_rt/probestack.cr
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{% skip_file unless flag?(:unix) %}

{% if flag?(:x86_64) %}
# :nodoc:
@[Naked]
fun __crystal_probe_stack
asm("
mov %rax, %r11
cmp $$0x1000, %r11 // check %r11 first, otherwise segmentation fault occurs.
jna 2f
1:
sub $$0x1000, %rsp
test %rsp, 8(%rsp)
sub $$0x1000, %r11
cmp $$0x1000, %r11
ja 1b
2:
sub %r11, %rsp
test %rsp, 8(%rsp)
add %rax, %rsp
ret
")
end
{% elsif flag?(:i386) %}
# :nodoc:
@[Naked]
fun __crystal_probe_stack
asm("
push %ecx
mov %eax, %ecx
cmp $$0x1000, %ecx
jna 2f
1:
sub $$0x1000, %esp
test %esp, 8(%esp)
sub $$0x1000, %ecx
cmp $$0x1000, %ecx
ja 1b
2:
sub %ecx, %esp
test %esp, 8(%esp)
add %eax, %esp
pop %ecx
ret
")
end
{% end %}