comparison druntime/src/compiler/ldc/alloca.d @ 1458:e0b2d67cfe7c

Added druntime (this should be removed once it works).
author Robert Clipsham <robert@octarineparrot.com>
date Tue, 02 Jun 2009 17:43:06 +0100
parents
children
comparison
equal deleted inserted replaced
1456:7b218ec1044f 1458:e0b2d67cfe7c
1 /**
2 * Implementation of alloca() standard C routine.
3 *
4 * Copyright: Copyright Digital Mars 1990 - 2009.
5 * License: <a href="http://www.boost.org/LICENSE_1_0.txt>Boost License 1.0</a>.
6 * Authors: Walter Bright
7 *
8 * Copyright Digital Mars 1990 - 2009.
9 * Distributed under the Boost Software License, Version 1.0.
10 * (See accompanying file LICENSE_1_0.txt or copy at
11 * http://www.boost.org/LICENSE_1_0.txt)
12 */
13 module rt.alloca;
14
15 /+
16 #if DOS386
17 extern size_t _x386_break;
18 #else
19 extern size_t _pastdata;
20 #endif
21 +/
22
23 /*******************************************
24 * Allocate data from the caller's stack frame.
25 * This is a 'magic' function that needs help from the compiler to
26 * work right, do not change its name, do not call it from other compilers.
27 * Input:
28 * nbytes number of bytes to allocate
29 * ECX address of variable with # of bytes in locals
30 * This is adjusted upon return to reflect the additional
31 * size of the stack frame.
32 * Returns:
33 * EAX allocated data, null if stack overflows
34 */
35
36 extern (C) void* __alloca(int nbytes)
37 {
38 asm
39 {
40 naked ;
41 mov EDX,ECX ;
42 mov EAX,4[ESP] ; // get nbytes
43 push EBX ;
44 push EDI ;
45 push ESI ;
46 }
47
48 version (OSX)
49 {
50 asm
51 {
52 add EAX,15 ;
53 and EAX,0xFFFFFFF0 ; // round up to 16 byte boundary
54 }
55 }
56 else
57 {
58 asm
59 {
60 add EAX,3 ;
61 and EAX,0xFFFFFFFC ; // round up to dword
62 }
63 }
64
65 asm
66 {
67 jnz Abegin ;
68 mov EAX,4 ; // allow zero bytes allocation, 0 rounded to dword is 4..
69 Abegin:
70 mov ESI,EAX ; // ESI = nbytes
71 neg EAX ;
72 add EAX,ESP ; // EAX is now what the new ESP will be.
73 jae Aoverflow ;
74 }
75 version (Windows)
76 {
77 asm
78 {
79 // We need to be careful about the guard page
80 // Thus, for every 4k page, touch it to cause the OS to load it in.
81 mov ECX,EAX ; // ECX is new location for stack
82 mov EBX,ESI ; // EBX is size to "grow" stack
83 L1:
84 test [ECX+EBX],EBX ; // bring in page
85 sub EBX,0x1000 ; // next 4K page down
86 jae L1 ; // if more pages
87 test [ECX],EBX ; // bring in last page
88 }
89 }
90 version (DOS386)
91 {
92 asm
93 {
94 // is ESP off bottom?
95 cmp EAX,_x386_break ;
96 jbe Aoverflow ;
97 }
98 }
99 version (Unix)
100 {
101 asm
102 {
103 cmp EAX,_pastdata ;
104 jbe Aoverflow ; // Unlikely - ~2 Gbytes under UNIX
105 }
106 }
107 asm
108 {
109 // Copy down to [ESP] the temps on the stack.
110 // The number of temps is (EBP - ESP - locals).
111 mov ECX,EBP ;
112 sub ECX,ESP ;
113 sub ECX,[EDX] ; // ECX = number of temps (bytes) to move.
114 add [EDX],ESI ; // adjust locals by nbytes for next call to alloca()
115 mov ESP,EAX ; // Set up new stack pointer.
116 add EAX,ECX ; // Return value = ESP + temps.
117 mov EDI,ESP ; // Destination of copy of temps.
118 add ESI,ESP ; // Source of copy.
119 shr ECX,2 ; // ECX to count of dwords in temps
120 // Always at least 4 (nbytes, EIP, ESI,and EDI).
121 rep ;
122 movsd ;
123 jmp done ;
124
125 Aoverflow:
126 // Overflowed the stack. Return null
127 xor EAX,EAX ;
128
129 done:
130 pop ESI ;
131 pop EDI ;
132 pop EBX ;
133 ret ;
134 }
135 }