|
92 | 92 | ret |
93 | 93 | #endif |
94 | 94 |
|
| 95 | +/* ---------------------------------------------- */ |
| 96 | +#elif defined __arm__ |
| 97 | + |
| 98 | +.globl _(__bound_alloca) |
| 99 | +_(__bound_alloca): |
| 100 | + mov r1, r0 |
| 101 | + add r0, r0, #1 |
| 102 | + rsb sp, r0, sp |
| 103 | + bic sp, sp, #7 |
| 104 | + mov r0, sp |
| 105 | + push { lr } |
| 106 | + bl _(__bound_new_region) |
| 107 | + pop { lr } |
| 108 | + mov r0, sp |
| 109 | + mov pc, lr |
| 110 | + |
| 111 | +/* ---------------------------------------------- */ |
| 112 | +#elif defined __aarch64__ || defined __arm64__ |
| 113 | + |
| 114 | +.globl _(__bound_alloca) |
| 115 | +_(__bound_alloca): |
| 116 | +#ifdef __TINYC__ |
| 117 | + .int 0xaa0003e1 |
| 118 | + .int 0x91004000 |
| 119 | + .int 0x927cec00 |
| 120 | +#ifdef _WIN32 |
| 121 | + .int 0xb4000160 |
| 122 | + .int 0xd2820002 |
| 123 | + .int 0xeb02001f |
| 124 | + .int 0x540000c3 |
| 125 | + .int 0xcb2263e3 |
| 126 | + .int 0xf940007f |
| 127 | + .int 0xcb2263ff |
| 128 | + .int 0xcb020000 |
| 129 | + .int 0x17fffffa |
| 130 | + .int 0xb4000040 |
| 131 | +#endif |
| 132 | + .int 0xcb2063ff |
| 133 | + .int 0x910003e0 |
| 134 | + .int 0xa9bf7bfd |
| 135 | + .reloc ., R_AARCH64_CALL26, _(__bound_new_region) |
| 136 | + .int 0x94000000 |
| 137 | + .int 0xa8c17bfd |
| 138 | + .int 0x910003e0 |
| 139 | + .int 0xd65f03c0 |
| 140 | +#else |
| 141 | + mov x1, x0 |
| 142 | + add x0, x0, #16 // Round up to 16-byte boundary |
| 143 | + and x0, x0, #-16 // Ensure 16-byte alignment |
| 144 | +#ifdef _WIN32 |
| 145 | + cbz x0, p100 // If size is 0, skip to return |
| 146 | + // Windows requires page-wise allocation with stack probing |
| 147 | + mov x2, #4096 // Page size = 4096 bytes |
| 148 | + |
| 149 | +p101: |
| 150 | + cmp x0, x2 // Compare remaining size with page size |
| 151 | + b.lo p102 // If less than page, jump to remainder |
| 152 | + |
| 153 | + // Probe first, then allocate |
| 154 | + sub x3, sp, x2 // Calculate guard page address (sp - 4096) |
| 155 | + ldr xzr, [x3] // Touch guard page FIRST |
| 156 | + sub sp, sp, x2 // THEN allocate the page |
| 157 | + |
| 158 | + sub x0, x0, x2 // Decrement remaining size |
| 159 | + b p101 // Continue loop |
| 160 | + |
| 161 | +p102: |
| 162 | + // Allocate remaining bytes (less than one page) |
| 163 | + cbz x0, p100 // If no remaining bytes, skip |
| 164 | + sub sp, sp, x0 // Allocate remaining space |
| 165 | +#else |
| 166 | + // Non-Windows: simple one-time allocation |
| 167 | + sub sp, sp, x0 // Allocate space on stack |
| 168 | +#endif |
| 169 | + |
| 170 | +p100: |
| 171 | + mov x0, sp // Return allocated address |
| 172 | + stp x29, x30, [sp, #-16]! |
| 173 | + bl _(__bound_new_region) |
| 174 | + ldp x29, x30, [sp], #16 |
| 175 | + mov x0, sp // Return allocated address |
| 176 | + ret // Return to caller |
| 177 | +#endif |
| 178 | + |
| 179 | +/* ---------------------------------------------- */ |
| 180 | +#elif defined __riscv |
| 181 | + |
| 182 | +.globl _(__bound_alloca) |
| 183 | +_(__bound_alloca): |
| 184 | + mv a1, a0 |
| 185 | + sub sp, sp, a0 |
| 186 | + addi sp, sp, -16 |
| 187 | + andi sp, sp, -16 |
| 188 | + add a0, sp, zero |
| 189 | + addi sp,sp,-16 |
| 190 | + sd s0,0(sp) |
| 191 | + sd ra,8(sp) |
| 192 | + jal _(__bound_new_region) |
| 193 | + ld s0,0(sp) |
| 194 | + ld ra,8(sp) |
| 195 | + addi sp,sp,16 |
| 196 | + add a0, sp, zero |
| 197 | + ret |
| 198 | + |
95 | 199 | /* ---------------------------------------------- */ |
96 | 200 | #endif |
0 commit comments