Skip to content

Commit 264229a

Browse files
committed
Add arm64 alloca() support.
1 parent fa6a6bf commit 264229a

2 files changed

Lines changed: 70 additions & 10 deletions

File tree

lib/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ XFLAGS += -I$(TOP)
3838
I386_O = libtcc1.o alloca.o alloca-bt.o $(COMMON_O)
3939
X86_64_O = libtcc1.o alloca.o alloca-bt.o $(COMMON_O)
4040
ARM_O = libtcc1.o armeabi.o alloca.o armflush.o $(COMMON_O)
41-
ARM64_O = lib-arm64.o $(COMMON_O)
41+
ARM64_O = lib-arm64.o alloca.o $(COMMON_O)
4242
RISCV64_O = lib-arm64.o $(COMMON_O)
4343
COMMON_O = stdatomic.o atomic.o builtin.o
4444
WIN_O = crt1.o crt1w.o wincrt1.o wincrt1w.o dllcrt1.o dllmain.o

lib/alloca.S

Lines changed: 69 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,16 +70,76 @@ p3:
7070
/* ---------------------------------------------- */
7171
#elif defined __arm__
7272

73-
.text
74-
.align 2
75-
.global alloca
76-
.type alloca, %function
73+
.text
74+
.align
75+
.global alloca
76+
.type alloca, %function
7777
alloca:
78-
rsb sp, r0, sp
79-
bic sp, sp, #7
80-
mov r0, sp
81-
mov pc, lr
82-
.size alloca, .-alloca
78+
rsb sp, r0, sp
79+
bic sp, sp, #7
80+
mov r0, sp
81+
mov pc, lr
82+
.size alloca, .-alloca
8383

8484
/* ---------------------------------------------- */
85+
#elif defined __aarch64__ || defined __arm64__
86+
87+
.text
88+
.align 2
89+
.global alloca
90+
.type alloca, %function
91+
alloca:
92+
#ifdef __TINYC__
93+
.int 0x91003c00
94+
.int 0x927cec00
95+
#ifdef _WIN32
96+
.int 0xb4000160
97+
.int 0xd2820001
98+
.int 0xeb01001f
99+
.int 0x540000c3
100+
.int 0xcb2163e2
101+
.int 0xf940005f
102+
.int 0xcb2163ff
103+
.int 0xcb010000
104+
.int 0x17fffffa
105+
#endif
106+
.int 0xb4000040
107+
.int 0xcb2063ff
108+
.int 0x910003e0
109+
.int 0xd65f03c0
110+
#else
111+
add x0, x0, #15 // Round up to 16-byte boundary
112+
and x0, x0, #-16 // Ensure 16-byte alignment
113+
cbz x0, p100 // If size is 0, skip to return
114+
#ifdef _WIN32
115+
// Windows requires page-wise allocation with stack probing
116+
mov x1, #4096 // Page size = 4096 bytes
117+
118+
p101:
119+
cmp x0, x1 // Compare remaining size with page size
120+
b.lo p102 // If less than page, jump to remainder
121+
122+
// Probe first, then allocate
123+
sub x2, sp, x1 // Calculate guard page address (sp - 4096)
124+
ldr xzr, [x2] // Touch guard page FIRST
125+
sub sp, sp, x1 // THEN allocate the page
126+
127+
sub x0, x0, x1 // Decrement remaining size
128+
b p101 // Continue loop
129+
130+
p102:
131+
// Allocate remaining bytes (less than one page)
132+
cbz x0, p100 // If no remaining bytes, skip
133+
sub sp, sp, x0 // Allocate remaining space
134+
#else
135+
// Non-Windows: simple one-time allocation
136+
sub sp, sp, x0 // Allocate space on stack
137+
#endif
138+
139+
p100:
140+
mov x0, sp // Return allocated address
141+
ret // Return to caller
142+
#endif
143+
.size alloca, .-alloca
144+
/* ---------------------------------------------- */
85145
#endif

0 commit comments

Comments
 (0)