This repository has been archived by the owner on Oct 12, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 422
/
alloca.d
281 lines (262 loc) · 8.19 KB
/
alloca.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
/**
* Implementation of alloca() standard C routine.
*
* Copyright: Copyright Digital Mars 2000 - 2012.
* License: Distributed under the
* $(LINK2 http://www.boost.org/LICENSE_1_0.txt, Boost Software License 1.0).
* (See accompanying file LICENSE)
* Authors: Walter Bright
* Source: $(DRUNTIMESRC rt/_alloca.d)
*/
module rt.alloca;
version (Posix)
{
version = alloca;
version (OSX)
version = Darwin;
else version (iOS)
version = Darwin;
else version (TVOS)
version = Darwin;
else version (WatchOS)
version = Darwin;
}
else version (CRuntime_Microsoft)
{
version = alloca;
}
// Use DMC++'s alloca() for Win32
version (alloca)
{
/+
#if DOS386
extern size_t _x386_break;
#else
extern size_t _pastdata;
#endif
+/
/*******************************************
* Allocate data from the caller's stack frame.
* This is a 'magic' function that needs help from the compiler to
* work right, do not change its name, do not call it from other compilers.
* Input:
* nbytes number of bytes to allocate
* ECX address of variable with # of bytes in locals
* This is adjusted upon return to reflect the additional
* size of the stack frame.
* Returns:
* EAX allocated data, null if stack overflows
*/
extern (C) void* __alloca(int nbytes)
{
version (D_InlineAsm_X86)
{
asm
{
naked ;
mov EDX,ECX ;
mov EAX,4[ESP] ; // get nbytes
push EBX ;
push EDI ;
push ESI ;
}
version (Darwin)
{
asm
{
add EAX,15 ;
and EAX,0xFFFFFFF0 ; // round up to 16 byte boundary
}
}
else
{
asm
{
add EAX,3 ;
and EAX,0xFFFFFFFC ; // round up to dword
}
}
asm
{
jnz Abegin ;
mov EAX,4 ; // allow zero bytes allocation, 0 rounded to dword is 4..
Abegin:
mov ESI,EAX ; // ESI = nbytes
neg EAX ;
add EAX,ESP ; // EAX is now what the new ESP will be.
jae Aoverflow ;
}
version (Win32)
{
asm
{
// We need to be careful about the guard page
// Thus, for every 4k page, touch it to cause the OS to load it in.
mov ECX,EAX ; // ECX is new location for stack
mov EBX,ESI ; // EBX is size to "grow" stack
L1:
test [ECX+EBX],EBX ; // bring in page
sub EBX,0x1000 ; // next 4K page down
jae L1 ; // if more pages
test [ECX],EBX ; // bring in last page
}
}
version (DOS386)
{
asm
{
// is ESP off bottom?
cmp EAX,_x386_break ;
jbe Aoverflow ;
}
}
version (Unix)
{
asm
{
cmp EAX,_pastdata ;
jbe Aoverflow ; // Unlikely - ~2 Gbytes under UNIX
}
}
asm
{
// Copy down to [ESP] the temps on the stack.
// The number of temps is (EBP - ESP - locals).
mov ECX,EBP ;
sub ECX,ESP ;
sub ECX,[EDX] ; // ECX = number of temps (bytes) to move.
add [EDX],ESI ; // adjust locals by nbytes for next call to alloca()
mov ESP,EAX ; // Set up new stack pointer.
add EAX,ECX ; // Return value = ESP + temps.
mov EDI,ESP ; // Destination of copy of temps.
add ESI,ESP ; // Source of copy.
shr ECX,2 ; // ECX to count of dwords in temps
// Always at least 4 (nbytes, EIP, ESI,and EDI).
rep ;
movsd ;
jmp done ;
Aoverflow:
// Overflowed the stack. Return null
xor EAX,EAX ;
done:
pop ESI ;
pop EDI ;
pop EBX ;
ret ;
}
}
else version (D_InlineAsm_X86_64)
{
version (Win64)
{
asm
{
/* RCX nbytes
* RDX address of variable with # of bytes in locals
* Must save registers RBX,RDI,RSI,R12..R15
*/
naked ;
push RBX ;
push RDI ;
push RSI ;
mov RAX,RCX ; // get nbytes
add RAX,15 ;
and AL,0xF0 ; // round up to 16 byte boundary
test RAX,RAX ;
jnz Abegin ;
mov RAX,16 ; // allow zero bytes allocation
Abegin:
mov RSI,RAX ; // RSI = nbytes
neg RAX ;
add RAX,RSP ; // RAX is now what the new RSP will be.
jae Aoverflow ;
// We need to be careful about the guard page
// Thus, for every 4k page, touch it to cause the OS to load it in.
mov RCX,RAX ; // RCX is new location for stack
mov RBX,RSI ; // RBX is size to "grow" stack
L1:
test [RCX+RBX],RBX ; // bring in page
sub RBX,0x1000 ; // next 4K page down
jae L1 ; // if more pages
test [RCX],RBX ; // bring in last page
// Copy down to [RSP] the temps on the stack.
// The number of temps is (RBP - RSP - locals).
mov RCX,RBP ;
sub RCX,RSP ;
sub RCX,[RDX] ; // RCX = number of temps (bytes) to move.
add [RDX],RSI ; // adjust locals by nbytes for next call to alloca()
mov RSP,RAX ; // Set up new stack pointer.
add RAX,RCX ; // Return value = RSP + temps.
mov RDI,RSP ; // Destination of copy of temps.
add RSI,RSP ; // Source of copy.
shr RCX,3 ; // RCX to count of qwords in temps
rep ;
movsq ;
jmp done ;
Aoverflow:
// Overflowed the stack. Return null
xor RAX,RAX ;
done:
pop RSI ;
pop RDI ;
pop RBX ;
ret ;
}
}
else
{
asm
{
/* Parameter is passed in RDI
* Must save registers RBX,R12..R15
*/
naked ;
mov RDX,RCX ;
mov RAX,RDI ; // get nbytes
add RAX,15 ;
and AL,0xF0 ; // round up to 16 byte boundary
test RAX,RAX ;
jnz Abegin ;
mov RAX,16 ; // allow zero bytes allocation
Abegin:
mov RSI,RAX ; // RSI = nbytes
neg RAX ;
add RAX,RSP ; // RAX is now what the new RSP will be.
jae Aoverflow ;
}
version (Unix)
{
asm
{
cmp RAX,_pastdata ;
jbe Aoverflow ; // Unlikely - ~2 Gbytes under UNIX
}
}
asm
{
// Copy down to [RSP] the temps on the stack.
// The number of temps is (RBP - RSP - locals).
mov RCX,RBP ;
sub RCX,RSP ;
sub RCX,[RDX] ; // RCX = number of temps (bytes) to move.
add [RDX],RSI ; // adjust locals by nbytes for next call to alloca()
mov RSP,RAX ; // Set up new stack pointer.
add RAX,RCX ; // Return value = RSP + temps.
mov RDI,RSP ; // Destination of copy of temps.
add RSI,RSP ; // Source of copy.
shr RCX,3 ; // RCX to count of qwords in temps
rep ;
movsq ;
jmp done ;
Aoverflow:
// Overflowed the stack. Return null
xor RAX,RAX ;
done:
ret ;
}
}
}
else
static assert(0);
}
}