Skip to content

Commit 40eb83b

Browse files
committed
Support for 32-bit mingw-w64 in compiler-rt.
Add chkstk/alloca for gcc objects. Replace or instructions with test, the latter should be marginally more efficent, as it does not write to memory. Differential Revision: http://reviews.llvm.org/D14044 Patch by vadimcn llvm-svn: 251928
1 parent 9eb3e57 commit 40eb83b

File tree

5 files changed

+88
-4
lines changed

5 files changed

+88
-4
lines changed

compiler-rt/lib/builtins/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@ if (NOT MSVC)
177177
set(x86_64_SOURCES
178178
${x86_64_SOURCES}
179179
x86_64/chkstk.S)
180+
x86_64/chkstk2.S)
180181
endif()
181182

182183
set(i386_SOURCES
@@ -200,6 +201,7 @@ if (NOT MSVC)
200201
set(i386_SOURCES
201202
${i386_SOURCES}
202203
i386/chkstk.S)
204+
i386/chkstk2.S)
203205
endif()
204206

205207
set(i686_SOURCES

compiler-rt/lib/builtins/i386/chkstk.S

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,13 +19,13 @@ DEFINE_COMPILERRT_FUNCTION(__chkstk_ms)
1919
jb 1f
2020
2:
2121
sub $0x1000,%ecx
22-
orl $0,(%ecx)
22+
test %ecx,(%ecx)
2323
sub $0x1000,%eax
2424
cmp $0x1000,%eax
2525
ja 2b
2626
1:
2727
sub %eax,%ecx
28-
orl $0,(%ecx)
28+
test %ecx,(%ecx)
2929
pop %eax
3030
pop %ecx
3131
ret
+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// This file is dual licensed under the MIT and the University of Illinois Open
2+
// Source Licenses. See LICENSE.TXT for details.
3+
4+
#include "../assembly.h"
5+
6+
#ifdef __i386__
7+
8+
// _chkstk (_alloca) routine - probe stack between %esp and (%esp-%eax) in 4k increments,
9+
// then decrement %esp by %eax. Preserves all registers except %esp and flags.
10+
// This routine is windows specific
11+
// http://msdn.microsoft.com/en-us/library/ms648426.aspx
12+
13+
.text
14+
.balign 4
15+
DEFINE_COMPILERRT_FUNCTION(_alloca) // _chkstk and _alloca are the same function
16+
DEFINE_COMPILERRT_FUNCTION(__chkstk)
17+
push %ecx
18+
cmp $0x1000,%eax
19+
lea 8(%esp),%ecx // esp before calling this routine -> ecx
20+
jb 1f
21+
2:
22+
sub $0x1000,%ecx
23+
test %ecx,(%ecx)
24+
sub $0x1000,%eax
25+
cmp $0x1000,%eax
26+
ja 2b
27+
1:
28+
sub %eax,%ecx
29+
test %ecx,(%ecx)
30+
31+
lea 4(%esp),%eax // load pointer to the return address into eax
32+
mov %ecx,%esp // install the new top of stack pointer into esp
33+
mov -4(%eax),%ecx // restore ecx
34+
push (%eax) // push return address onto the stack
35+
sub %esp,%eax // restore the original value in eax
36+
ret
37+
END_COMPILERRT_FUNCTION(__chkstk)
38+
END_COMPILERRT_FUNCTION(_alloca)
39+
40+
#endif // __i386__

compiler-rt/lib/builtins/x86_64/chkstk.S

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,13 @@ DEFINE_COMPILERRT_FUNCTION(___chkstk_ms)
2424
jb 1f
2525
2:
2626
sub $0x1000,%rcx
27-
orl $0,(%rcx)
27+
test %rcx,(%rcx)
2828
sub $0x1000,%rax
2929
cmp $0x1000,%rax
3030
ja 2b
3131
1:
3232
sub %rax,%rcx
33-
orl $0,(%rcx)
33+
test %rcx,(%rcx)
3434
pop %rax
3535
pop %rcx
3636
ret
+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// This file is dual licensed under the MIT and the University of Illinois Open
2+
// Source Licenses. See LICENSE.TXT for details.
3+
4+
#include "../assembly.h"
5+
6+
#ifdef __x86_64__
7+
8+
// _chkstk (_alloca) routine - probe stack between %rsp and (%rsp-%rax) in 4k increments,
9+
// then decrement %rsp by %rax. Preserves all registers except %rsp and flags.
10+
// This routine is windows specific
11+
// http://msdn.microsoft.com/en-us/library/ms648426.aspx
12+
13+
.text
14+
.balign 4
15+
DEFINE_COMPILERRT_FUNCTION(__alloca)
16+
mov %rcx,%rax // x64 _alloca is a normal function with parameter in rcx
17+
// fallthrough
18+
DEFINE_COMPILERRT_FUNCTION(___chkstk)
19+
push %rcx
20+
cmp $0x1000,%rax
21+
lea 16(%rsp),%rcx // rsp before calling this routine -> rcx
22+
jb 1f
23+
2:
24+
sub $0x1000,%rcx
25+
test %rcx,(%rcx)
26+
sub $0x1000,%rax
27+
cmp $0x1000,%rax
28+
ja 2b
29+
1:
30+
sub %rax,%rcx
31+
test %rcx,(%rcx)
32+
33+
lea 8(%rsp),%rax // load pointer to the return address into rax
34+
mov %rcx,%rsp // install the new top of stack pointer into rsp
35+
mov -8(%rax),%rcx // restore rcx
36+
push (%rax) // push return address onto the stack
37+
sub %rsp,%rax // restore the original value in rax
38+
ret
39+
END_COMPILERRT_FUNCTION(___chkstk)
40+
END_COMPILERRT_FUNCTION(__alloca)
41+
42+
#endif // __x86_64__

0 commit comments

Comments
 (0)