aboutsummaryrefslogtreecommitdiff
path: root/dynasm/dasm_arm64.h
diff options
context:
space:
mode:
Diffstat (limited to 'dynasm/dasm_arm64.h')
-rw-r--r--dynasm/dasm_arm64.h558
1 files changed, 558 insertions, 0 deletions
diff --git a/dynasm/dasm_arm64.h b/dynasm/dasm_arm64.h
new file mode 100644
index 00000000..a922a4ae
--- /dev/null
+++ b/dynasm/dasm_arm64.h
@@ -0,0 +1,558 @@
1/*
2** DynASM ARM64 encoding engine.
3** Copyright (C) 2005-2025 Mike Pall. All rights reserved.
4** Released under the MIT license. See dynasm.lua for full copyright notice.
5*/
6
7#include <stddef.h>
8#include <stdarg.h>
9#include <string.h>
10#include <stdlib.h>
11
12#define DASM_ARCH "arm64"
13
14#ifndef DASM_EXTERN
15#define DASM_EXTERN(a,b,c,d) 0
16#endif
17
18/* Action definitions. */
19enum {
20 DASM_STOP, DASM_SECTION, DASM_ESC, DASM_REL_EXT,
21 /* The following actions need a buffer position. */
22 DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG,
23 /* The following actions also have an argument. */
24 DASM_REL_PC, DASM_LABEL_PC, DASM_REL_A,
25 DASM_IMM, DASM_IMM6, DASM_IMM12, DASM_IMM13W, DASM_IMM13X, DASM_IMML,
26 DASM_IMMV, DASM_VREG,
27 DASM__MAX
28};
29
30/* Maximum number of section buffer positions for a single dasm_put() call. */
31#define DASM_MAXSECPOS 25
32
33/* DynASM encoder status codes. Action list offset or number are or'ed in. */
34#define DASM_S_OK 0x00000000
35#define DASM_S_NOMEM 0x01000000
36#define DASM_S_PHASE 0x02000000
37#define DASM_S_MATCH_SEC 0x03000000
38#define DASM_S_RANGE_I 0x11000000
39#define DASM_S_RANGE_SEC 0x12000000
40#define DASM_S_RANGE_LG 0x13000000
41#define DASM_S_RANGE_PC 0x14000000
42#define DASM_S_RANGE_REL 0x15000000
43#define DASM_S_RANGE_VREG 0x16000000
44#define DASM_S_UNDEF_LG 0x21000000
45#define DASM_S_UNDEF_PC 0x22000000
46
47/* Macros to convert positions (8 bit section + 24 bit index). */
48#define DASM_POS2IDX(pos) ((pos)&0x00ffffff)
49#define DASM_POS2BIAS(pos) ((pos)&0xff000000)
50#define DASM_SEC2POS(sec) ((sec)<<24)
51#define DASM_POS2SEC(pos) ((pos)>>24)
52#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos))
53
54/* Action list type. */
55typedef const unsigned int *dasm_ActList;
56
57/* Per-section structure. */
58typedef struct dasm_Section {
59 int *rbuf; /* Biased buffer pointer (negative section bias). */
60 int *buf; /* True buffer pointer. */
61 size_t bsize; /* Buffer size in bytes. */
62 int pos; /* Biased buffer position. */
63 int epos; /* End of biased buffer position - max single put. */
64 int ofs; /* Byte offset into section. */
65} dasm_Section;
66
67/* Core structure holding the DynASM encoding state. */
68struct dasm_State {
69 size_t psize; /* Allocated size of this structure. */
70 dasm_ActList actionlist; /* Current actionlist pointer. */
71 int *lglabels; /* Local/global chain/pos ptrs. */
72 size_t lgsize;
73 int *pclabels; /* PC label chains/pos ptrs. */
74 size_t pcsize;
75 void **globals; /* Array of globals. */
76 dasm_Section *section; /* Pointer to active section. */
77 size_t codesize; /* Total size of all code sections. */
78 int maxsection; /* 0 <= sectionidx < maxsection. */
79 int status; /* Status code. */
80 dasm_Section sections[1]; /* All sections. Alloc-extended. */
81};
82
83/* The size of the core structure depends on the max. number of sections. */
84#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section))
85
86
87/* Initialize DynASM state. */
88void dasm_init(Dst_DECL, int maxsection)
89{
90 dasm_State *D;
91 size_t psz = 0;
92 Dst_REF = NULL;
93 DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection));
94 D = Dst_REF;
95 D->psize = psz;
96 D->lglabels = NULL;
97 D->lgsize = 0;
98 D->pclabels = NULL;
99 D->pcsize = 0;
100 D->globals = NULL;
101 D->maxsection = maxsection;
102 memset((void *)D->sections, 0, maxsection * sizeof(dasm_Section));
103}
104
105/* Free DynASM state. */
106void dasm_free(Dst_DECL)
107{
108 dasm_State *D = Dst_REF;
109 int i;
110 for (i = 0; i < D->maxsection; i++)
111 if (D->sections[i].buf)
112 DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize);
113 if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize);
114 if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize);
115 DASM_M_FREE(Dst, D, D->psize);
116}
117
118/* Setup global label array. Must be called before dasm_setup(). */
119void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
120{
121 dasm_State *D = Dst_REF;
122 D->globals = gl;
123 DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int));
124}
125
126/* Grow PC label array. Can be called after dasm_setup(), too. */
127void dasm_growpc(Dst_DECL, unsigned int maxpc)
128{
129 dasm_State *D = Dst_REF;
130 size_t osz = D->pcsize;
131 DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int));
132 memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz);
133}
134
135/* Setup encoder. */
136void dasm_setup(Dst_DECL, const void *actionlist)
137{
138 dasm_State *D = Dst_REF;
139 int i;
140 D->actionlist = (dasm_ActList)actionlist;
141 D->status = DASM_S_OK;
142 D->section = &D->sections[0];
143 memset((void *)D->lglabels, 0, D->lgsize);
144 if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
145 for (i = 0; i < D->maxsection; i++) {
146 D->sections[i].pos = DASM_SEC2POS(i);
147 D->sections[i].rbuf = D->sections[i].buf - D->sections[i].pos;
148 D->sections[i].ofs = 0;
149 }
150}
151
152
153#ifdef DASM_CHECKS
154#define CK(x, st) \
155 do { if (!(x)) { \
156 D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0)
157#define CKPL(kind, st) \
158 do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
159 D->status = DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0)
160#else
161#define CK(x, st) ((void)0)
162#define CKPL(kind, st) ((void)0)
163#endif
164
165static int dasm_imm12(unsigned int n)
166{
167 if ((n >> 12) == 0)
168 return n;
169 else if ((n & 0xff000fff) == 0)
170 return (n >> 12) | 0x1000;
171 else
172 return -1;
173}
174
175static int dasm_ffs(unsigned long long x)
176{
177 int n = -1;
178 while (x) { x >>= 1; n++; }
179 return n;
180}
181
182static int dasm_imm13(int lo, int hi)
183{
184 int inv = 0, w = 64, s = 0xfff, xa, xb;
185 unsigned long long n = (((unsigned long long)hi) << 32) | (unsigned int)lo;
186 unsigned long long m = 1ULL, a, b, c;
187 if (n & 1) { n = ~n; inv = 1; }
188 a = n & (unsigned long long)-(long long)n;
189 b = (n+a)&(unsigned long long)-(long long)(n+a);
190 c = (n+a-b)&(unsigned long long)-(long long)(n+a-b);
191 xa = dasm_ffs(a); xb = dasm_ffs(b);
192 if (c) {
193 w = dasm_ffs(c) - xa;
194 if (w == 32) m = 0x0000000100000001UL;
195 else if (w == 16) m = 0x0001000100010001UL;
196 else if (w == 8) m = 0x0101010101010101UL;
197 else if (w == 4) m = 0x1111111111111111UL;
198 else if (w == 2) m = 0x5555555555555555UL;
199 else return -1;
200 s = (-2*w & 0x3f) - 1;
201 } else if (!a) {
202 return -1;
203 } else if (xb == -1) {
204 xb = 64;
205 }
206 if ((b-a) * m != n) return -1;
207 if (inv) {
208 return ((w - xb) << 6) | (s+w+xa-xb);
209 } else {
210 return ((w - xa) << 6) | (s+xb-xa);
211 }
212 return -1;
213}
214
215/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
216void dasm_put(Dst_DECL, int start, ...)
217{
218 va_list ap;
219 dasm_State *D = Dst_REF;
220 dasm_ActList p = D->actionlist + start;
221 dasm_Section *sec = D->section;
222 int pos = sec->pos, ofs = sec->ofs;
223 int *b;
224
225 if (pos >= sec->epos) {
226 DASM_M_GROW(Dst, int, sec->buf, sec->bsize,
227 sec->bsize + 2*DASM_MAXSECPOS*sizeof(int));
228 sec->rbuf = sec->buf - DASM_POS2BIAS(pos);
229 sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos);
230 }
231
232 b = sec->rbuf;
233 b[pos++] = start;
234
235 va_start(ap, start);
236 while (1) {
237 unsigned int ins = *p++;
238 unsigned int action = (ins >> 16);
239 if (action >= DASM__MAX) {
240 ofs += 4;
241 } else {
242 int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0;
243 switch (action) {
244 case DASM_STOP: goto stop;
245 case DASM_SECTION:
246 n = (ins & 255); CK(n < D->maxsection, RANGE_SEC);
247 D->section = &D->sections[n]; goto stop;
248 case DASM_ESC: p++; ofs += 4; break;
249 case DASM_REL_EXT: if ((ins & 0x8000)) ofs += 8; break;
250 case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break;
251 case DASM_REL_LG:
252 n = (ins & 2047) - 10; pl = D->lglabels + n;
253 /* Bkwd rel or global. */
254 if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; }
255 pl += 10; n = *pl;
256 if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */
257 goto linkrel;
258 case DASM_REL_PC:
259 pl = D->pclabels + n; CKPL(pc, PC);
260 putrel:
261 n = *pl;
262 if (n < 0) { /* Label exists. Get label pos and store it. */
263 b[pos] = -n;
264 } else {
265 linkrel:
266 b[pos] = n; /* Else link to rel chain, anchored at label. */
267 *pl = pos;
268 }
269 pos++;
270 if ((ins & 0x8000)) ofs += 8;
271 break;
272 case DASM_REL_A:
273 b[pos++] = n;
274 b[pos++] = va_arg(ap, int);
275 break;
276 case DASM_LABEL_LG:
277 pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel;
278 case DASM_LABEL_PC:
279 pl = D->pclabels + n; CKPL(pc, PC);
280 putlabel:
281 n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */
282 while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos;
283 }
284 *pl = -pos; /* Label exists now. */
285 b[pos++] = ofs; /* Store pass1 offset estimate. */
286 break;
287 case DASM_IMM:
288 CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I);
289 n >>= ((ins>>10)&31);
290#ifdef DASM_CHECKS
291 if ((ins & 0x8000))
292 CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I);
293 else
294 CK((n>>((ins>>5)&31)) == 0, RANGE_I);
295#endif
296 b[pos++] = n;
297 break;
298 case DASM_IMM6:
299 CK((n >> 6) == 0, RANGE_I);
300 b[pos++] = n;
301 break;
302 case DASM_IMM12:
303 CK(dasm_imm12((unsigned int)n) != -1, RANGE_I);
304 b[pos++] = n;
305 break;
306 case DASM_IMM13W:
307 CK(dasm_imm13(n, n) != -1, RANGE_I);
308 b[pos++] = n;
309 break;
310 case DASM_IMM13X: {
311 int m = va_arg(ap, int);
312 CK(dasm_imm13(n, m) != -1, RANGE_I);
313 b[pos++] = n;
314 b[pos++] = m;
315 break;
316 }
317 case DASM_IMML: {
318#ifdef DASM_CHECKS
319 int scale = (ins & 3);
320 CK((!(n & ((1<<scale)-1)) && (unsigned int)(n>>scale) < 4096) ||
321 (unsigned int)(n+256) < 512, RANGE_I);
322#endif
323 b[pos++] = n;
324 break;
325 }
326 case DASM_IMMV:
327 ofs += 4;
328 b[pos++] = n;
329 break;
330 case DASM_VREG:
331 CK(n < 32, RANGE_VREG);
332 b[pos++] = n;
333 break;
334 }
335 }
336 }
337stop:
338 va_end(ap);
339 sec->pos = pos;
340 sec->ofs = ofs;
341}
342#undef CK
343
344/* Pass 2: Link sections, shrink aligns, fix label offsets. */
345int dasm_link(Dst_DECL, size_t *szp)
346{
347 dasm_State *D = Dst_REF;
348 int secnum;
349 int ofs = 0;
350
351#ifdef DASM_CHECKS
352 *szp = 0;
353 if (D->status != DASM_S_OK) return D->status;
354 {
355 int pc;
356 for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
357 if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
358 }
359#endif
360
361 { /* Handle globals not defined in this translation unit. */
362 int idx;
363 for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) {
364 int n = D->lglabels[idx];
365 /* Undefined label: Collapse rel chain and replace with marker (< 0). */
366 while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
367 }
368 }
369
370 /* Combine all code sections. No support for data sections (yet). */
371 for (secnum = 0; secnum < D->maxsection; secnum++) {
372 dasm_Section *sec = D->sections + secnum;
373 int *b = sec->rbuf;
374 int pos = DASM_SEC2POS(secnum);
375 int lastpos = sec->pos;
376
377 while (pos != lastpos) {
378 dasm_ActList p = D->actionlist + b[pos++];
379 while (1) {
380 unsigned int ins = *p++;
381 unsigned int action = (ins >> 16);
382 switch (action) {
383 case DASM_STOP: case DASM_SECTION: goto stop;
384 case DASM_ESC: p++; break;
385 case DASM_REL_EXT: break;
386 case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break;
387 case DASM_REL_LG: case DASM_REL_PC: pos++; break;
388 case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break;
389 case DASM_IMM: case DASM_IMM6: case DASM_IMM12: case DASM_IMM13W:
390 case DASM_IMML: case DASM_IMMV: case DASM_VREG: pos++; break;
391 case DASM_IMM13X: case DASM_REL_A: pos += 2; break;
392 }
393 }
394 stop: (void)0;
395 }
396 ofs += sec->ofs; /* Next section starts right after current section. */
397 }
398
399 D->codesize = ofs; /* Total size of all code sections */
400 *szp = ofs;
401 return DASM_S_OK;
402}
403
404#ifdef DASM_CHECKS
405#define CK(x, st) \
406 do { if (!(x)) return DASM_S_##st|(int)(p-D->actionlist-1); } while (0)
407#else
408#define CK(x, st) ((void)0)
409#endif
410
411/* Pass 3: Encode sections. */
412int dasm_encode(Dst_DECL, void *buffer)
413{
414 dasm_State *D = Dst_REF;
415 char *base = (char *)buffer;
416 unsigned int *cp = (unsigned int *)buffer;
417 int secnum;
418
419 /* Encode all code sections. No support for data sections (yet). */
420 for (secnum = 0; secnum < D->maxsection; secnum++) {
421 dasm_Section *sec = D->sections + secnum;
422 int *b = sec->buf;
423 int *endb = sec->rbuf + sec->pos;
424
425 while (b != endb) {
426 dasm_ActList p = D->actionlist + *b++;
427 while (1) {
428 unsigned int ins = *p++;
429 unsigned int action = (ins >> 16);
430 int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0;
431 switch (action) {
432 case DASM_STOP: case DASM_SECTION: goto stop;
433 case DASM_ESC: *cp++ = *p++; break;
434 case DASM_REL_EXT:
435 n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins&2047), !(ins&2048));
436 goto patchrel;
437 case DASM_ALIGN:
438 ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0xd503201f;
439 break;
440 case DASM_REL_LG:
441 if (n < 0) {
442 ptrdiff_t na = (ptrdiff_t)D->globals[-n-10] - (ptrdiff_t)cp + 4;
443 n = (int)na;
444 CK((ptrdiff_t)n == na, RANGE_REL);
445 goto patchrel;
446 }
447 /* fallthrough */
448 case DASM_REL_PC:
449 CK(n >= 0, UNDEF_PC);
450 n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base) + 4;
451 patchrel:
452 if (!(ins & 0xf800)) { /* B, BL */
453 CK((n & 3) == 0 && ((n+0x08000000) >> 28) == 0, RANGE_REL);
454 cp[-1] |= ((n >> 2) & 0x03ffffff);
455 } else if ((ins & 0x800)) { /* B.cond, CBZ, CBNZ, LDR* literal */
456 CK((n & 3) == 0 && ((n+0x00100000) >> 21) == 0, RANGE_REL);
457 cp[-1] |= ((n << 3) & 0x00ffffe0);
458 } else if ((ins & 0x3000) == 0x2000) { /* ADR */
459 CK(((n+0x00100000) >> 21) == 0, RANGE_REL);
460 cp[-1] |= ((n << 3) & 0x00ffffe0) | ((n & 3) << 29);
461 } else if ((ins & 0x3000) == 0x3000) { /* ADRP */
462 cp[-1] |= ((n >> 9) & 0x00ffffe0) | (((n >> 12) & 3) << 29);
463 } else if ((ins & 0x1000)) { /* TBZ, TBNZ */
464 CK((n & 3) == 0 && ((n+0x00008000) >> 16) == 0, RANGE_REL);
465 cp[-1] |= ((n << 3) & 0x0007ffe0);
466 } else if ((ins & 0x8000)) { /* absolute */
467 cp[0] = (unsigned int)((ptrdiff_t)cp - 4 + n);
468 cp[1] = (unsigned int)(((ptrdiff_t)cp - 4 + n) >> 32);
469 cp += 2;
470 }
471 break;
472 case DASM_REL_A: {
473 ptrdiff_t na = (((ptrdiff_t)(*b++) << 32) | (unsigned int)n);
474 if ((ins & 0x3000) == 0x3000) { /* ADRP */
475 ins &= ~0x1000;
476 na = (na >> 12) - (((ptrdiff_t)cp - 4) >> 12);
477 } else {
478 na = na - (ptrdiff_t)cp + 4;
479 }
480 n = (int)na;
481 CK((ptrdiff_t)n == na, RANGE_REL);
482 goto patchrel;
483 }
484 case DASM_LABEL_LG:
485 ins &= 2047; if (ins >= 20) D->globals[ins-20] = (void *)(base + n);
486 break;
487 case DASM_LABEL_PC: break;
488 case DASM_IMM:
489 cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31);
490 break;
491 case DASM_IMM6:
492 cp[-1] |= ((n&31) << 19) | ((n&32) << 26);
493 break;
494 case DASM_IMM12:
495 cp[-1] |= (dasm_imm12((unsigned int)n) << 10);
496 break;
497 case DASM_IMM13W:
498 cp[-1] |= (dasm_imm13(n, n) << 10);
499 break;
500 case DASM_IMM13X:
501 cp[-1] |= (dasm_imm13(n, *b++) << 10);
502 break;
503 case DASM_IMML: {
504 int scale = (ins & 3);
505 cp[-1] |= (!(n & ((1<<scale)-1)) && (unsigned int)(n>>scale) < 4096) ?
506 ((n << (10-scale)) | 0x01000000) : ((n & 511) << 12);
507 break;
508 }
509 case DASM_IMMV:
510 *cp++ = n;
511 break;
512 case DASM_VREG:
513 cp[-1] |= (n & 0x1f) << (ins & 0x1f);
514 break;
515 default: *cp++ = ins; break;
516 }
517 }
518 stop: (void)0;
519 }
520 }
521
522 if (base + D->codesize != (char *)cp) /* Check for phase errors. */
523 return DASM_S_PHASE;
524 return DASM_S_OK;
525}
526#undef CK
527
528/* Get PC label offset. */
529int dasm_getpclabel(Dst_DECL, unsigned int pc)
530{
531 dasm_State *D = Dst_REF;
532 if (pc*sizeof(int) < D->pcsize) {
533 int pos = D->pclabels[pc];
534 if (pos < 0) return *DASM_POS2PTR(D, -pos);
535 if (pos > 0) return -1; /* Undefined. */
536 }
537 return -2; /* Unused or out of range. */
538}
539
540#ifdef DASM_CHECKS
541/* Optional sanity checker to call between isolated encoding steps. */
542int dasm_checkstep(Dst_DECL, int secmatch)
543{
544 dasm_State *D = Dst_REF;
545 if (D->status == DASM_S_OK) {
546 int i;
547 for (i = 1; i <= 9; i++) {
548 if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; }
549 D->lglabels[i] = 0;
550 }
551 }
552 if (D->status == DASM_S_OK && secmatch >= 0 &&
553 D->section != &D->sections[secmatch])
554 D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections);
555 return D->status;
556}
557#endif
558