|
70 | 70 | /* ---------------------------------------------- */ |
71 | 71 | #elif defined __arm__ |
72 | 72 |
|
73 | | - .text |
74 | | - .align 2 |
75 | | - .global alloca |
76 | | - .type alloca, %function |
| 73 | + .text |
| 74 | + .align |
| 75 | + .global alloca |
| 76 | + .type alloca, %function |
77 | 77 | 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 |
83 | 83 |
|
84 | 84 | /* ---------------------------------------------- */ |
| 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 | +/* ---------------------------------------------- */ |
85 | 145 | #endif |
0 commit comments