aboutsummaryrefslogtreecommitdiff
path: root/dynasm
diff options
context:
space:
mode:
authorMike Pall <mike>2014-12-03 14:12:02 +0100
committerMike Pall <mike>2014-12-03 14:12:02 +0100
commitf49c61a2776ae9abeb2297dbc3b53ea2962ad750 (patch)
tree0b9095fb733688230d5aa07b4488da31a2358623 /dynasm
parent1fc9cd07c3ba21d54b3cec7cec3ea8778ef4919f (diff)
downloadluajit-f49c61a2776ae9abeb2297dbc3b53ea2962ad750.tar.gz
luajit-f49c61a2776ae9abeb2297dbc3b53ea2962ad750.tar.bz2
luajit-f49c61a2776ae9abeb2297dbc3b53ea2962ad750.zip
DynASM/ARM64: Initial commit of ARM64 module.
Diffstat (limited to 'dynasm')
-rw-r--r--dynasm/dasm_arm64.h510
-rw-r--r--dynasm/dasm_arm64.lua1140
2 files changed, 1650 insertions, 0 deletions
diff --git a/dynasm/dasm_arm64.h b/dynasm/dasm_arm64.h
new file mode 100644
index 00000000..30bc3f95
--- /dev/null
+++ b/dynasm/dasm_arm64.h
@@ -0,0 +1,510 @@
1/*
2** DynASM ARM64 encoding engine.
3** Copyright (C) 2005-2014 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,
25 DASM_IMM, DASM_IMM12, DASM_IMM13W, DASM_IMM13X, DASM_IMML,
26 DASM__MAX
27};
28
29/* Maximum number of section buffer positions for a single dasm_put() call. */
30#define DASM_MAXSECPOS 25
31
32/* DynASM encoder status codes. Action list offset or number are or'ed in. */
33#define DASM_S_OK 0x00000000
34#define DASM_S_NOMEM 0x01000000
35#define DASM_S_PHASE 0x02000000
36#define DASM_S_MATCH_SEC 0x03000000
37#define DASM_S_RANGE_I 0x11000000
38#define DASM_S_RANGE_SEC 0x12000000
39#define DASM_S_RANGE_LG 0x13000000
40#define DASM_S_RANGE_PC 0x14000000
41#define DASM_S_RANGE_REL 0x15000000
42#define DASM_S_UNDEF_LG 0x21000000
43#define DASM_S_UNDEF_PC 0x22000000
44
45/* Macros to convert positions (8 bit section + 24 bit index). */
46#define DASM_POS2IDX(pos) ((pos)&0x00ffffff)
47#define DASM_POS2BIAS(pos) ((pos)&0xff000000)
48#define DASM_SEC2POS(sec) ((sec)<<24)
49#define DASM_POS2SEC(pos) ((pos)>>24)
50#define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos))
51
52/* Action list type. */
53typedef const unsigned int *dasm_ActList;
54
55/* Per-section structure. */
56typedef struct dasm_Section {
57 int *rbuf; /* Biased buffer pointer (negative section bias). */
58 int *buf; /* True buffer pointer. */
59 size_t bsize; /* Buffer size in bytes. */
60 int pos; /* Biased buffer position. */
61 int epos; /* End of biased buffer position - max single put. */
62 int ofs; /* Byte offset into section. */
63} dasm_Section;
64
65/* Core structure holding the DynASM encoding state. */
66struct dasm_State {
67 size_t psize; /* Allocated size of this structure. */
68 dasm_ActList actionlist; /* Current actionlist pointer. */
69 int *lglabels; /* Local/global chain/pos ptrs. */
70 size_t lgsize;
71 int *pclabels; /* PC label chains/pos ptrs. */
72 size_t pcsize;
73 void **globals; /* Array of globals (bias -10). */
74 dasm_Section *section; /* Pointer to active section. */
75 size_t codesize; /* Total size of all code sections. */
76 int maxsection; /* 0 <= sectionidx < maxsection. */
77 int status; /* Status code. */
78 dasm_Section sections[1]; /* All sections. Alloc-extended. */
79};
80
81/* The size of the core structure depends on the max. number of sections. */
82#define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section))
83
84
85/* Initialize DynASM state. */
86void dasm_init(Dst_DECL, int maxsection)
87{
88 dasm_State *D;
89 size_t psz = 0;
90 int i;
91 Dst_REF = NULL;
92 DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection));
93 D = Dst_REF;
94 D->psize = psz;
95 D->lglabels = NULL;
96 D->lgsize = 0;
97 D->pclabels = NULL;
98 D->pcsize = 0;
99 D->globals = NULL;
100 D->maxsection = maxsection;
101 for (i = 0; i < maxsection; i++) {
102 D->sections[i].buf = NULL; /* Need this for pass3. */
103 D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i);
104 D->sections[i].bsize = 0;
105 D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */
106 }
107}
108
109/* Free DynASM state. */
110void dasm_free(Dst_DECL)
111{
112 dasm_State *D = Dst_REF;
113 int i;
114 for (i = 0; i < D->maxsection; i++)
115 if (D->sections[i].buf)
116 DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize);
117 if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize);
118 if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize);
119 DASM_M_FREE(Dst, D, D->psize);
120}
121
122/* Setup global label array. Must be called before dasm_setup(). */
123void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
124{
125 dasm_State *D = Dst_REF;
126 D->globals = gl - 10; /* Negative bias to compensate for locals. */
127 DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int));
128}
129
130/* Grow PC label array. Can be called after dasm_setup(), too. */
131void dasm_growpc(Dst_DECL, unsigned int maxpc)
132{
133 dasm_State *D = Dst_REF;
134 size_t osz = D->pcsize;
135 DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int));
136 memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz);
137}
138
139/* Setup encoder. */
140void dasm_setup(Dst_DECL, const void *actionlist)
141{
142 dasm_State *D = Dst_REF;
143 int i;
144 D->actionlist = (dasm_ActList)actionlist;
145 D->status = DASM_S_OK;
146 D->section = &D->sections[0];
147 memset((void *)D->lglabels, 0, D->lgsize);
148 if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
149 for (i = 0; i < D->maxsection; i++) {
150 D->sections[i].pos = DASM_SEC2POS(i);
151 D->sections[i].ofs = 0;
152 }
153}
154
155
156#ifdef DASM_CHECKS
157#define CK(x, st) \
158 do { if (!(x)) { \
159 D->status = DASM_S_##st|(p-D->actionlist-1); return; } } while (0)
160#define CKPL(kind, st) \
161 do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
162 D->status = DASM_S_RANGE_##st|(p-D->actionlist-1); return; } } while (0)
163#else
164#define CK(x, st) ((void)0)
165#define CKPL(kind, st) ((void)0)
166#endif
167
168static int dasm_imm12(unsigned int n)
169{
170 if ((n >> 12) == 0)
171 return n;
172 else if ((n & 0xff000fff) == 0)
173 return (n >> 12) | 0x1000;
174 else
175 return -1;
176}
177
178static int dasm_ffs(unsigned long long x)
179{
180 int n = -1;
181 while (x) { x >>= 1; n++; }
182 return n;
183}
184
185static int dasm_imm13(int lo, int hi)
186{
187 int inv = 0, w = 64, s = 0xfff, xa, xb;
188 unsigned long long n = (((unsigned long long)hi) << 32) | (unsigned int)lo;
189 unsigned long long m = 1ULL, a, b, c;
190 if (n & 1) { n = ~n; inv = 1; }
191 a = n & -n; b = (n+a)&-(n+a); c = (n+a-b)&-(n+a-b);
192 xa = dasm_ffs(a); xb = dasm_ffs(b);
193 if (c) {
194 w = dasm_ffs(c) - xa;
195 if (w == 32) m = 0x0000000100000001UL;
196 else if (w == 16) m = 0x0001000100010001UL;
197 else if (w == 8) m = 0x0101010101010101UL;
198 else if (w == 4) m = 0x1111111111111111UL;
199 else if (w == 2) m = 0x5555555555555555UL;
200 else return -1;
201 s = (-2*w & 0x3f) - 1;
202 } else if (!a) {
203 return -1;
204 } else if (xb == -1) {
205 xb = 64;
206 }
207 if ((b-a) * m != n) return -1;
208 if (inv) {
209 return ((w - xb) << 6) | (s+w+xa-xb);
210 } else {
211 return ((w - xa) << 6) | (s+xb-xa);
212 }
213 return -1;
214}
215
216/* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
217void dasm_put(Dst_DECL, int start, ...)
218{
219 va_list ap;
220 dasm_State *D = Dst_REF;
221 dasm_ActList p = D->actionlist + start;
222 dasm_Section *sec = D->section;
223 int pos = sec->pos, ofs = sec->ofs;
224 int *b;
225
226 if (pos >= sec->epos) {
227 DASM_M_GROW(Dst, int, sec->buf, sec->bsize,
228 sec->bsize + 2*DASM_MAXSECPOS*sizeof(int));
229 sec->rbuf = sec->buf - DASM_POS2BIAS(pos);
230 sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos);
231 }
232
233 b = sec->rbuf;
234 b[pos++] = start;
235
236 va_start(ap, start);
237 while (1) {
238 unsigned int ins = *p++;
239 unsigned int action = (ins >> 16);
240 if (action >= DASM__MAX) {
241 ofs += 4;
242 } else {
243 int *pl, n = action >= DASM_REL_PC ? va_arg(ap, int) : 0;
244 switch (action) {
245 case DASM_STOP: goto stop;
246 case DASM_SECTION:
247 n = (ins & 255); CK(n < D->maxsection, RANGE_SEC);
248 D->section = &D->sections[n]; goto stop;
249 case DASM_ESC: p++; ofs += 4; break;
250 case DASM_REL_EXT: break;
251 case DASM_ALIGN: ofs += (ins & 255); b[pos++] = ofs; break;
252 case DASM_REL_LG:
253 n = (ins & 2047) - 10; pl = D->lglabels + n;
254 /* Bkwd rel or global. */
255 if (n >= 0) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; }
256 pl += 10; n = *pl;
257 if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */
258 goto linkrel;
259 case DASM_REL_PC:
260 pl = D->pclabels + n; CKPL(pc, PC);
261 putrel:
262 n = *pl;
263 if (n < 0) { /* Label exists. Get label pos and store it. */
264 b[pos] = -n;
265 } else {
266 linkrel:
267 b[pos] = n; /* Else link to rel chain, anchored at label. */
268 *pl = pos;
269 }
270 pos++;
271 break;
272 case DASM_LABEL_LG:
273 pl = D->lglabels + (ins & 2047) - 10; CKPL(lg, LG); goto putlabel;
274 case DASM_LABEL_PC:
275 pl = D->pclabels + n; CKPL(pc, PC);
276 putlabel:
277 n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */
278 while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos;
279 }
280 *pl = -pos; /* Label exists now. */
281 b[pos++] = ofs; /* Store pass1 offset estimate. */
282 break;
283 case DASM_IMM:
284#ifdef DASM_CHECKS
285 CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I);
286 if ((ins & 0x8000))
287 CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I);
288 else
289 CK((n>>((ins>>5)&31)) == 0, RANGE_I);
290#endif
291 b[pos++] = n;
292 break;
293 case DASM_IMM12:
294 CK(dasm_imm12((unsigned int)n) != -1, RANGE_I);
295 b[pos++] = n;
296 break;
297 case DASM_IMM13W:
298 CK(dasm_imm13(n, n) != -1, RANGE_I);
299 b[pos++] = n;
300 break;
301 case DASM_IMM13X: {
302 int m = va_arg(ap, int);
303 CK(dasm_imm13(n, m) != -1, RANGE_I);
304 b[pos++] = n;
305 b[pos++] = m;
306 break;
307 }
308 case DASM_IMML: {
309#ifdef DASM_CHECKS
310 int scale = (p[-2] >> 30);
311 CK((!(n & ((1<<scale)-1)) && (unsigned int)(n>>scale) < 4096) ||
312 (unsigned int)(n+256) < 512, RANGE_I);
313#endif
314 b[pos++] = n;
315 break;
316 }
317 }
318 }
319 }
320stop:
321 va_end(ap);
322 sec->pos = pos;
323 sec->ofs = ofs;
324}
325#undef CK
326
327/* Pass 2: Link sections, shrink aligns, fix label offsets. */
328int dasm_link(Dst_DECL, size_t *szp)
329{
330 dasm_State *D = Dst_REF;
331 int secnum;
332 int ofs = 0;
333
334#ifdef DASM_CHECKS
335 *szp = 0;
336 if (D->status != DASM_S_OK) return D->status;
337 {
338 int pc;
339 for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
340 if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
341 }
342#endif
343
344 { /* Handle globals not defined in this translation unit. */
345 int idx;
346 for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) {
347 int n = D->lglabels[idx];
348 /* Undefined label: Collapse rel chain and replace with marker (< 0). */
349 while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
350 }
351 }
352
353 /* Combine all code sections. No support for data sections (yet). */
354 for (secnum = 0; secnum < D->maxsection; secnum++) {
355 dasm_Section *sec = D->sections + secnum;
356 int *b = sec->rbuf;
357 int pos = DASM_SEC2POS(secnum);
358 int lastpos = sec->pos;
359
360 while (pos != lastpos) {
361 dasm_ActList p = D->actionlist + b[pos++];
362 while (1) {
363 unsigned int ins = *p++;
364 unsigned int action = (ins >> 16);
365 switch (action) {
366 case DASM_STOP: case DASM_SECTION: goto stop;
367 case DASM_ESC: p++; break;
368 case DASM_REL_EXT: break;
369 case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break;
370 case DASM_REL_LG: case DASM_REL_PC: pos++; break;
371 case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break;
372 case DASM_IMM: case DASM_IMM12: case DASM_IMM13W:
373 case DASM_IMML: pos++; break;
374 case DASM_IMM13X: pos += 2; break;
375 }
376 }
377 stop: (void)0;
378 }
379 ofs += sec->ofs; /* Next section starts right after current section. */
380 }
381
382 D->codesize = ofs; /* Total size of all code sections */
383 *szp = ofs;
384 return DASM_S_OK;
385}
386
387#ifdef DASM_CHECKS
388#define CK(x, st) \
389 do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0)
390#else
391#define CK(x, st) ((void)0)
392#endif
393
394/* Pass 3: Encode sections. */
395int dasm_encode(Dst_DECL, void *buffer)
396{
397 dasm_State *D = Dst_REF;
398 char *base = (char *)buffer;
399 unsigned int *cp = (unsigned int *)buffer;
400 int secnum;
401
402 /* Encode all code sections. No support for data sections (yet). */
403 for (secnum = 0; secnum < D->maxsection; secnum++) {
404 dasm_Section *sec = D->sections + secnum;
405 int *b = sec->buf;
406 int *endb = sec->rbuf + sec->pos;
407
408 while (b != endb) {
409 dasm_ActList p = D->actionlist + *b++;
410 while (1) {
411 unsigned int ins = *p++;
412 unsigned int action = (ins >> 16);
413 int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0;
414 switch (action) {
415 case DASM_STOP: case DASM_SECTION: goto stop;
416 case DASM_ESC: *cp++ = *p++; break;
417 case DASM_REL_EXT:
418 n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins&2047), !(ins&2048));
419 goto patchrel;
420 case DASM_ALIGN:
421 ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0xe1a00000;
422 break;
423 case DASM_REL_LG:
424 CK(n >= 0, UNDEF_LG);
425 case DASM_REL_PC:
426 CK(n >= 0, UNDEF_PC);
427 n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base) + 4;
428 patchrel:
429 if (!(ins & 0xf800)) { /* B, BL */
430 CK((n & 3) == 0 && ((n+0x08000000) >> 28) == 0, RANGE_REL);
431 cp[-1] |= ((n >> 2) & 0x03ffffff);
432 } else if ((ins & 0x800)) { /* B.cond, CBZ, CBNZ, LDR* literal */
433 CK((n & 3) == 0 && ((n+0x00100000) >> 21) == 0, RANGE_REL);
434 cp[-1] |= ((n << 3) & 0x00ffffe0);
435 } else if ((ins & 0x3000) == 0x2000) { /* ADR */
436 CK(((n+0x00100000) >> 21) == 0, RANGE_REL);
437 cp[-1] |= ((n << 3) & 0x00ffffe0) | ((n & 3) << 29);
438 } else if ((ins & 0x3000) == 0x3000) { /* ADRP */
439 cp[-1] |= ((n >> 9) & 0x00ffffe0) | (((n >> 12) & 3) << 29);
440 } else if ((ins & 0x1000)) { /* TBZ, TBNZ */
441 CK((n & 3) == 0 && ((n+0x00008000) >> 16) == 0, RANGE_REL);
442 cp[-1] |= ((n << 3) & 0x0007ffe0);
443 }
444 break;
445 case DASM_LABEL_LG:
446 ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n);
447 break;
448 case DASM_LABEL_PC: break;
449 case DASM_IMM:
450 cp[-1] |= ((n>>((ins>>10)&31)) & ((1<<((ins>>5)&31))-1)) << (ins&31);
451 break;
452 case DASM_IMM12:
453 cp[-1] |= (dasm_imm12((unsigned int)n) << 10);
454 break;
455 case DASM_IMM13W:
456 cp[-1] |= (dasm_imm13(n, n) << 10);
457 break;
458 case DASM_IMM13X:
459 cp[-1] |= (dasm_imm13(n, *b++) << 10);
460 break;
461 case DASM_IMML: {
462 int scale = (p[-2] >> 30);
463 cp[-1] |= (!(n & ((1<<scale)-1)) && (unsigned int)(n>>scale) < 4096) ?
464 ((n << (10-scale)) | 0x01000000) : ((n & 511) << 12);
465 break;
466 }
467 default: *cp++ = ins; break;
468 }
469 }
470 stop: (void)0;
471 }
472 }
473
474 if (base + D->codesize != (char *)cp) /* Check for phase errors. */
475 return DASM_S_PHASE;
476 return DASM_S_OK;
477}
478#undef CK
479
480/* Get PC label offset. */
481int dasm_getpclabel(Dst_DECL, unsigned int pc)
482{
483 dasm_State *D = Dst_REF;
484 if (pc*sizeof(int) < D->pcsize) {
485 int pos = D->pclabels[pc];
486 if (pos < 0) return *DASM_POS2PTR(D, -pos);
487 if (pos > 0) return -1; /* Undefined. */
488 }
489 return -2; /* Unused or out of range. */
490}
491
492#ifdef DASM_CHECKS
493/* Optional sanity checker to call between isolated encoding steps. */
494int dasm_checkstep(Dst_DECL, int secmatch)
495{
496 dasm_State *D = Dst_REF;
497 if (D->status == DASM_S_OK) {
498 int i;
499 for (i = 1; i <= 9; i++) {
500 if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; }
501 D->lglabels[i] = 0;
502 }
503 }
504 if (D->status == DASM_S_OK && secmatch >= 0 &&
505 D->section != &D->sections[secmatch])
506 D->status = DASM_S_MATCH_SEC|(D->section-D->sections);
507 return D->status;
508}
509#endif
510
diff --git a/dynasm/dasm_arm64.lua b/dynasm/dasm_arm64.lua
new file mode 100644
index 00000000..33328606
--- /dev/null
+++ b/dynasm/dasm_arm64.lua
@@ -0,0 +1,1140 @@
1------------------------------------------------------------------------------
2-- DynASM ARM64 module.
3--
4-- Copyright (C) 2005-2014 Mike Pall. All rights reserved.
5-- See dynasm.lua for full copyright notice.
6------------------------------------------------------------------------------
7
8-- Module information:
9local _info = {
10 arch = "arm",
11 description = "DynASM ARM64 module",
12 version = "1.3.0",
13 vernum = 10300,
14 release = "2014-12-03",
15 author = "Mike Pall",
16 license = "MIT",
17}
18
19-- Exported glue functions for the arch-specific module.
20local _M = { _info = _info }
21
22-- Cache library functions.
23local type, tonumber, pairs, ipairs = type, tonumber, pairs, ipairs
24local assert, setmetatable, rawget = assert, setmetatable, rawget
25local _s = string
26local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char
27local match, gmatch, gsub = _s.match, _s.gmatch, _s.gsub
28local concat, sort, insert = table.concat, table.sort, table.insert
29local bit = bit or require("bit")
30local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift
31local ror, tohex = bit.ror, bit.tohex
32
33-- Inherited tables and callbacks.
34local g_opt, g_arch
35local wline, werror, wfatal, wwarn
36
37-- Action name list.
38-- CHECK: Keep this in sync with the C code!
39local action_names = {
40 "STOP", "SECTION", "ESC", "REL_EXT",
41 "ALIGN", "REL_LG", "LABEL_LG",
42 "REL_PC", "LABEL_PC", "IMM", "IMM12", "IMM13W", "IMM13X", "IMML",
43}
44
45-- Maximum number of section buffer positions for dasm_put().
46-- CHECK: Keep this in sync with the C code!
47local maxsecpos = 25 -- Keep this low, to avoid excessively long C lines.
48
49-- Action name -> action number.
50local map_action = {}
51for n,name in ipairs(action_names) do
52 map_action[name] = n-1
53end
54
55-- Action list buffer.
56local actlist = {}
57
58-- Argument list for next dasm_put(). Start with offset 0 into action list.
59local actargs = { 0 }
60
61-- Current number of section buffer positions for dasm_put().
62local secpos = 1
63
64------------------------------------------------------------------------------
65
66-- Dump action names and numbers.
67local function dumpactions(out)
68 out:write("DynASM encoding engine action codes:\n")
69 for n,name in ipairs(action_names) do
70 local num = map_action[name]
71 out:write(format(" %-10s %02X %d\n", name, num, num))
72 end
73 out:write("\n")
74end
75
76-- Write action list buffer as a huge static C array.
77local function writeactions(out, name)
78 local nn = #actlist
79 if nn == 0 then nn = 1; actlist[0] = map_action.STOP end
80 out:write("static const unsigned int ", name, "[", nn, "] = {\n")
81 for i = 1,nn-1 do
82 assert(out:write("0x", tohex(actlist[i]), ",\n"))
83 end
84 assert(out:write("0x", tohex(actlist[nn]), "\n};\n\n"))
85end
86
87------------------------------------------------------------------------------
88
89-- Add word to action list.
90local function wputxw(n)
91 assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range")
92 actlist[#actlist+1] = n
93end
94
95-- Add action to list with optional arg. Advance buffer pos, too.
96local function waction(action, val, a, num)
97 local w = assert(map_action[action], "bad action name `"..action.."'")
98 wputxw(w * 0x10000 + (val or 0))
99 if a then actargs[#actargs+1] = a end
100 if a or num then secpos = secpos + (num or 1) end
101end
102
103-- Flush action list (intervening C code or buffer pos overflow).
104local function wflush(term)
105 if #actlist == actargs[1] then return end -- Nothing to flush.
106 if not term then waction("STOP") end -- Terminate action list.
107 wline(format("dasm_put(Dst, %s);", concat(actargs, ", ")), true)
108 actargs = { #actlist } -- Actionlist offset is 1st arg to next dasm_put().
109 secpos = 1 -- The actionlist offset occupies a buffer position, too.
110end
111
112-- Put escaped word.
113local function wputw(n)
114 if n <= 0x000fffff then waction("ESC") end
115 wputxw(n)
116end
117
118-- Reserve position for word.
119local function wpos()
120 local pos = #actlist+1
121 actlist[pos] = ""
122 return pos
123end
124
125-- Store word to reserved position.
126local function wputpos(pos, n)
127 assert(n >= 0 and n <= 0xffffffff and n % 1 == 0, "word out of range")
128 if n <= 0x000fffff then
129 insert(actlist, pos+1, n)
130 n = map_action.ESC * 0x10000
131 end
132 actlist[pos] = n
133end
134
135------------------------------------------------------------------------------
136
137-- Global label name -> global label number. With auto assignment on 1st use.
138local next_global = 20
139local map_global = setmetatable({}, { __index = function(t, name)
140 if not match(name, "^[%a_][%w_]*$") then werror("bad global label") end
141 local n = next_global
142 if n > 2047 then werror("too many global labels") end
143 next_global = n + 1
144 t[name] = n
145 return n
146end})
147
148-- Dump global labels.
149local function dumpglobals(out, lvl)
150 local t = {}
151 for name, n in pairs(map_global) do t[n] = name end
152 out:write("Global labels:\n")
153 for i=20,next_global-1 do
154 out:write(format(" %s\n", t[i]))
155 end
156 out:write("\n")
157end
158
159-- Write global label enum.
160local function writeglobals(out, prefix)
161 local t = {}
162 for name, n in pairs(map_global) do t[n] = name end
163 out:write("enum {\n")
164 for i=20,next_global-1 do
165 out:write(" ", prefix, t[i], ",\n")
166 end
167 out:write(" ", prefix, "_MAX\n};\n")
168end
169
170-- Write global label names.
171local function writeglobalnames(out, name)
172 local t = {}
173 for name, n in pairs(map_global) do t[n] = name end
174 out:write("static const char *const ", name, "[] = {\n")
175 for i=20,next_global-1 do
176 out:write(" \"", t[i], "\",\n")
177 end
178 out:write(" (const char *)0\n};\n")
179end
180
181------------------------------------------------------------------------------
182
183-- Extern label name -> extern label number. With auto assignment on 1st use.
184local next_extern = 0
185local map_extern_ = {}
186local map_extern = setmetatable({}, { __index = function(t, name)
187 -- No restrictions on the name for now.
188 local n = next_extern
189 if n > 2047 then werror("too many extern labels") end
190 next_extern = n + 1
191 t[name] = n
192 map_extern_[n] = name
193 return n
194end})
195
196-- Dump extern labels.
197local function dumpexterns(out, lvl)
198 out:write("Extern labels:\n")
199 for i=0,next_extern-1 do
200 out:write(format(" %s\n", map_extern_[i]))
201 end
202 out:write("\n")
203end
204
205-- Write extern label names.
206local function writeexternnames(out, name)
207 out:write("static const char *const ", name, "[] = {\n")
208 for i=0,next_extern-1 do
209 out:write(" \"", map_extern_[i], "\",\n")
210 end
211 out:write(" (const char *)0\n};\n")
212end
213
214------------------------------------------------------------------------------
215
216-- Arch-specific maps.
217
218-- Ext. register name -> int. name.
219local map_archdef = { xzr = "@x31", wzr = "@w31", lr = "x30", }
220
221-- Int. register name -> ext. name.
222local map_reg_rev = { ["@x31"] = "xzr", ["@w31"] = "wzr", x30 = "lr", }
223
224local map_type = {} -- Type name -> { ctype, reg }
225local ctypenum = 0 -- Type number (for Dt... macros).
226
227-- Reverse defines for registers.
228function _M.revdef(s)
229 return map_reg_rev[s] or s
230end
231
232local map_shift = { lsl = 0, lsr = 1, asr = 2, }
233
234local map_extend = {
235 uxtb = 0, uxth = 1, uxtw = 2, uxtx = 3,
236 sxtb = 4, sxth = 5, sxtw = 6, sxtx = 7,
237}
238
239local map_cond = {
240 eq = 0, ne = 1, cs = 2, cc = 3, mi = 4, pl = 5, vs = 6, vc = 7,
241 hi = 8, ls = 9, ge = 10, lt = 11, gt = 12, le = 13, al = 14,
242 hs = 2, lo = 3,
243}
244
245------------------------------------------------------------------------------
246
247local parse_reg_type
248
249local function parse_reg(expr)
250 if not expr then werror("expected register name") end
251 local tname, ovreg = match(expr, "^([%w_]+):(@?%l%d+)$")
252 local tp = map_type[tname or expr]
253 if tp then
254 local reg = ovreg or tp.reg
255 if not reg then
256 werror("type `"..(tname or expr).."' needs a register override")
257 end
258 expr = reg
259 end
260 local ok31, rt, r = match(expr, "^(@?)([xwqdshb])([123]?[0-9])$")
261 if r then
262 r = tonumber(r)
263 if r <= 30 or (r == 31 and ok31 ~= "" or (rt ~= "w" and rt ~= "x")) then
264 if not parse_reg_type then
265 parse_reg_type = rt
266 elseif parse_reg_type ~= rt then
267 werror("register size mismatch")
268 end
269 return r, tp
270 end
271 end
272 werror("bad register name `"..expr.."'")
273end
274
275local function parse_reg_base(expr)
276 if expr == "sp" then return 0x3e0 end
277 local base = parse_reg(expr)
278 if parse_reg_type ~= "x" then werror("bad register type") end
279 parse_reg_type = false
280 return shl(base, 5)
281end
282
283local parse_ctx = {}
284
285local loadenv = setfenv and function(s)
286 local code = loadstring(s, "")
287 if code then setfenv(code, parse_ctx) end
288 return code
289end or function(s)
290 return load(s, "", nil, parse_ctx)
291end
292
293-- Try to parse simple arithmetic, too, since some basic ops are aliases.
294local function parse_number(n)
295 local x = tonumber(n)
296 if x then return x end
297 local code = loadenv("return "..n)
298 if code then
299 local ok, y = pcall(code)
300 if ok then return y end
301 end
302 return nil
303end
304
305local function parse_imm(imm, bits, shift, scale, signed)
306 imm = match(imm, "^#(.*)$")
307 if not imm then werror("expected immediate operand") end
308 local n = parse_number(imm)
309 if n then
310 local m = sar(n, scale)
311 if shl(m, scale) == n then
312 if signed then
313 local s = sar(m, bits-1)
314 if s == 0 then return shl(m, shift)
315 elseif s == -1 then return shl(m + shl(1, bits), shift) end
316 else
317 if sar(m, bits) == 0 then return shl(m, shift) end
318 end
319 end
320 werror("out of range immediate `"..imm.."'")
321 else
322 waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm)
323 return 0
324 end
325end
326
327local function parse_imm12(imm)
328 imm = match(imm, "^#(.*)$")
329 if not imm then werror("expected immediate operand") end
330 local n = parse_number(imm)
331 if n then
332 if shr(n, 12) == 0 then
333 return shl(n, 10)
334 elseif band(n, 0xff000fff) == 0 then
335 return shr(n, 2) + 0x00400000
336 end
337 werror("out of range immediate `"..imm.."'")
338 else
339 waction("IMM12", 0, imm)
340 return 0
341 end
342end
343
344local function parse_imm13(imm)
345 imm = match(imm, "^#(.*)$")
346 if not imm then werror("expected immediate operand") end
347 local n = parse_number(imm)
348 local r64 = parse_reg_type == "x"
349 if n and n % 1 == 0 and n >= 0 and n <= 0xffffffff then
350 local inv = false
351 if band(n, 1) == 1 then n = bit.bnot(n); inv = true end
352 local t = {}
353 for i=1,32 do t[i] = band(n, 1); n = shr(n, 1) end
354 local b = table.concat(t)
355 b = b..(r64 and (inv and "1" or "0"):rep(32) or b)
356 local p0, p1, p0a, p1a = b:match("^(0+)(1+)(0*)(1*)")
357 if p0 then
358 local w = p1a == "" and (r64 and 64 or 32) or #p1+#p0a
359 if band(w, w-1) == 0 and b == b:sub(1, w):rep(64/w) then
360 local s = band(-2*w, 0x3f) - 1
361 if w == 64 then s = s + 0x1000 end
362 if inv then
363 return shl(w-#p1-#p0, 16) + shl(s+w-#p1, 10)
364 else
365 return shl(w-#p0, 16) + shl(s+#p1, 10)
366 end
367 end
368 end
369 werror("out of range immediate `"..imm.."'")
370 elseif r64 then
371 waction("IMM13X", 0, format("(unsigned int)(%s)", imm))
372 actargs[#actargs+1] = format("(unsigned int)((unsigned long long)(%s)>>32)", imm)
373 return 0
374 else
375 waction("IMM13W", 0, imm)
376 return 0
377 end
378end
379
380local function parse_imm6(imm)
381 imm = match(imm, "^#(.*)$")
382 if not imm then werror("expected immediate operand") end
383 local n = parse_number(imm)
384 if n then
385 if n >= 0 and n <= 63 then
386 return shl(band(n, 0x1f), 19) + (n >= 32 and 0x80000000 or 0)
387 end
388 werror("out of range immediate `"..imm.."'")
389 else
390 werror("NYI imm6 action")
391 end
392end
393
394local function parse_imm_load(imm, scale)
395 local n = parse_number(imm)
396 if n then
397 local m = sar(n, scale)
398 if shl(m, scale) == n and m >= 0 and m < 0x1000 then
399 return shl(m, 10) + 0x01000000 -- Scaled, unsigned 12 bit offset.
400 elseif n >= -256 and n < 256 then
401 return shl(band(n, 511), 12) -- Unscaled, signed 9 bit offset.
402 end
403 werror("out of range immediate `"..imm.."'")
404 else
405 waction("IMML", 0, imm)
406 return 0
407 end
408end
409
410local function parse_fpimm(imm)
411 imm = match(imm, "^#(.*)$")
412 if not imm then werror("expected immediate operand") end
413 local n = parse_number(imm)
414 if n then
415 local m, e = math.frexp(n)
416 local s, e2 = 0, band(e-2, 7)
417 if m < 0 then m = -m; s = 0x00100000 end
418 m = m*32-16
419 if m % 1 == 0 and m >= 0 and m <= 15 and sar(shl(e2, 29), 29)+2 == e then
420 return s + shl(e2, 17) + shl(m, 13)
421 end
422 werror("out of range immediate `"..imm.."'")
423 else
424 werror("NYI fpimm action")
425 end
426end
427
428local function parse_shift(expr)
429 local s, s2 = match(expr, "^(%S+)%s*(.*)$")
430 s = map_shift[s]
431 if not s then werror("expected shift operand") end
432 return parse_imm(s2, 6, 10, 0, false) + shl(s, 22)
433end
434
435local function parse_lslx16(expr)
436 local n = match(expr, "^lsl%s*#(%d+)$")
437 n = tonumber(n)
438 if not n then werror("expected shift operand") end
439 if band(n, parse_reg_type == "x" and 0xffffffcf or 0xffffffef) ~= 0 then
440 werror("bad shift amount")
441 end
442 return shl(n, 17)
443end
444
445local function parse_extend(expr)
446 local s, s2 = match(expr, "^(%S+)%s*(.*)$")
447 if s == "lsl" then
448 s = parse_reg_type == "x" and 3 or 2
449 else
450 s = map_extend[s]
451 end
452 if not s then werror("expected extend operand") end
453 return (s2 == "" and 0 or parse_imm(s2, 3, 10, 0, false)) + shl(s, 13)
454end
455
456local function parse_cond(expr, inv)
457 local c = map_cond[expr]
458 if not c then werror("expected condition operand") end
459 return shl(bit.bxor(c, inv), 12)
460end
461
462local function parse_load(params, nparams, n, op)
463 local pn = params[n]
464 local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$")
465 if not p1 then werror("expected address operand") end
466 if params[n+2] then werror("too many operands") end
467 local scale = shr(op, 30)
468 local p2 = params[n+1]
469 if p2 then
470 if wb == "!" then werror("bad use of '!'") end
471 op = op + parse_reg_base(p1) + parse_imm(p2, 9, 12, 0, true) + 0x400
472 elseif wb == "!" then
473 local p1a, p2a = match(p1, "^([^,%s]*)%s*,%s*(.*)$")
474 if not p1a then werror("bad use of '!'") end
475 op = op + parse_reg_base(p1a) + parse_imm(p2a, 9, 12, 0, true) + 0xc00
476 else
477 local p1a, p2a = match(p1, "^([^,%s]*)%s*(.*)$")
478 op = op + parse_reg_base(p1a)
479 if p2a ~= "" then
480 local imm = match(p2a, "^,%s*#(.*)$")
481 if imm then
482 op = op + parse_imm_load(imm, scale)
483 else
484 local p2b, p3b, p3s = match(p2a, "^,%s*([^,%s]*)%s*,?%s*(%S*)%s*(.*)$")
485 op = op + shl(parse_reg(p2b), 16) + 0x00200800
486 if parse_reg_type ~= "x" and parse_reg_type ~= "w" then
487 werror("bad index register type")
488 end
489 if p3b == "" then
490 if parse_reg_type ~= "x" then werror("bad index register type") end
491 op = op + 0x6000
492 else
493 if p3s == "" or p3s == "#0" then
494 elseif p3s == "#"..scale then
495 op = op + 0x1000
496 else
497 werror("bad scale")
498 end
499 if parse_reg_type == "x" then
500 if p3b == "lsl" and p3s ~= "" then op = op + 0x6000
501 elseif p3b == "sxtx" then op = op + 0xe000
502 else
503 werror("bad extend/shift specifier")
504 end
505 else
506 if p3b == "uxtw" then op = op + 0x4000
507 elseif p3b == "sxtw" then op = op + 0xc000
508 else
509 werror("bad extend/shift specifier")
510 end
511 end
512 end
513 end
514 else
515 if wb == "!" then werror("bad use of '!'") end
516 op = op + 0x01000000
517 end
518 end
519 return op
520end
521
522local function parse_load_pair(params, nparams, n, op)
523 local pn = params[n]
524 local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$")
525 if not p1 then werror("expected address operand") end
526 if params[n+2] then werror("too many operands") end
527 local scale = shr(op, 30) == 0 and 2 or 3
528 local p2 = params[n+1]
529 if p2 then
530 if wb == "!" then werror("bad use of '!'") end
531 op = op + 0x00800000
532 else
533 local p1a, p2a = match(p1, "^([^,%s]*)%s*,%s*(.*)$")
534 if p1a then p1, p2 = p1a, p2a else p2 = "#0" end
535 op = op + (wb == "!" and 0x01800000 or 0x01000000)
536 end
537 return op + parse_reg_base(p1) + parse_imm(p2, 7, 15, scale, true)
538end
539
540local function parse_label(label, def)
541 local prefix = sub(label, 1, 2)
542 -- =>label (pc label reference)
543 if prefix == "=>" then
544 return "PC", 0, sub(label, 3)
545 end
546 -- ->name (global label reference)
547 if prefix == "->" then
548 return "LG", map_global[sub(label, 3)]
549 end
550 if def then
551 -- [1-9] (local label definition)
552 if match(label, "^[1-9]$") then
553 return "LG", 10+tonumber(label)
554 end
555 else
556 -- [<>][1-9] (local label reference)
557 local dir, lnum = match(label, "^([<>])([1-9])$")
558 if dir then -- Fwd: 1-9, Bkwd: 11-19.
559 return "LG", lnum + (dir == ">" and 0 or 10)
560 end
561 -- extern label (extern label reference)
562 local extname = match(label, "^extern%s+(%S+)$")
563 if extname then
564 return "EXT", map_extern[extname]
565 end
566 end
567 werror("bad label `"..label.."'")
568end
569
570local function branch_type(op)
571 if band(op, 0x7c000000) == 0x14000000 then return 0 -- B, BL
572 elseif shr(op, 24) == 0x54 or band(op, 0x7e000000) == 0x34000000 or
573 band(op, 0x3b000000) == 0x18000000 then
574 return 0x800 -- B.cond, CBZ, CBNZ, LDR* literal
575 elseif band(op, 0x7e000000) == 0x36000000 then return 0x1000 -- TBZ, TBNZ
576 elseif band(op, 0x9f000000) == 0x10000000 then return 0x2000 -- ADR
577 elseif band(op, 0x9f000000) == band(0x90000000) then return 0x3000 -- ADRP
578 else
579 assert(false, "unknown branch type")
580 end
581end
582
583------------------------------------------------------------------------------
584
585local map_op, op_template
586
587local function op_alias(opname, f)
588 return function(params, nparams)
589 if not params then return "-> "..opname:sub(1, -3) end
590 f(params, nparams)
591 op_template(params, map_op[opname], nparams)
592 end
593end
594
595local function alias_bfx(p)
596 p[4] = "#("..p[3]:sub(2)..")+("..p[4]:sub(2)..")-1"
597end
598
599local function alias_bfiz(p)
600 parse_reg(p[1])
601 if parse_reg_type == "w" then
602 p[3] = "#-("..p[3]:sub(2)..")%32"
603 p[4] = "#("..p[4]:sub(2)..")-1"
604 else
605 p[3] = "#-("..p[3]:sub(2)..")%64"
606 p[4] = "#("..p[4]:sub(2)..")-1"
607 end
608end
609
610local alias_lslimm = op_alias("ubfm_4", function(p)
611 parse_reg(p[1])
612 local sh = p[3]:sub(2)
613 if parse_reg_type == "w" then
614 p[3] = "#-("..sh..")%32"
615 p[4] = "#31-("..sh..")"
616 else
617 p[3] = "#-("..sh..")%64"
618 p[4] = "#63-("..sh..")"
619 end
620end)
621
622-- Template strings for ARM instructions.
623map_op = {
624 -- Basic data processing instructions.
625 add_3 = "0b000000DNMg|11000000pDpNIg|8b206000pDpNMx",
626 add_4 = "0b000000DNMSg|0b200000DNMXg|8b200000pDpNMXx|8b200000pDpNxMwX",
627 adds_3 = "2b000000DNMg|31000000DpNIg|ab206000DpNMx",
628 adds_4 = "2b000000DNMSg|2b200000DNMXg|ab200000DpNMXx|ab200000DpNxMwX",
629 cmn_2 = "2b00001fNMg|3100001fpNIg|ab20601fpNMx",
630 cmn_3 = "2b00001fNMSg|2b20001fNMXg|ab20001fpNMXx|ab20001fpNxMwX",
631
632 sub_3 = "4b000000DNMg|51000000pDpNIg|cb206000pDpNMx",
633 sub_4 = "4b000000DNMSg|4b200000DNMXg|cb200000pDpNMXx|cb200000pDpNxMwX",
634 subs_3 = "6b000000DNMg|71000000DpNIg|eb206000DpNMx",
635 subs_4 = "6b000000DNMSg|6b200000DNMXg|eb200000DpNMXx|eb200000DpNxMwX",
636 cmp_2 = "6b00001fNMg|7100001fpNIg|eb20601fpNMx",
637 cmp_3 = "6b00001fNMSg|6b20001fNMXg|eb20001fpNMXx|eb20001fpNxMwX",
638
639 neg_2 = "4b0003e0DMg",
640 neg_3 = "4b0003e0DMSg",
641 negs_2 = "6b0003e0DMg",
642 negs_3 = "6b0003e0DMSg",
643
644 adc_3 = "1a000000DNMg",
645 adcs_3 = "3a000000DNMg",
646 sbc_3 = "5a000000DNMg",
647 sbcs_3 = "7a000000DNMg",
648 ngc_2 = "5a0003e0DMg",
649 ngcs_2 = "7a0003e0DMg",
650
651 and_3 = "0a000000DNMg|12000000pDNig",
652 and_4 = "0a000000DNMSg",
653 orr_3 = "2a000000DNMg|32000000pDNig",
654 orr_4 = "2a000000DNMSg",
655 eor_3 = "4a000000DNMg|52000000pDNig",
656 eor_4 = "4a000000DNMSg",
657 ands_3 = "6a000000DNMg|72000000DNig",
658 ands_4 = "6a000000DNMSg",
659 tst_2 = "6a00001fNMg|7200001fNig",
660 tst_3 = "6a00001fNMSg",
661
662 bic_3 = "0a200000DNMg",
663 bic_4 = "0a200000DNMSg",
664 orn_3 = "2a200000DNMg",
665 orn_4 = "2a200000DNMSg",
666 eon_3 = "4a200000DNMg",
667 eon_4 = "4a200000DNMSg",
668 bics_3 = "6a200000DNMg",
669 bics_4 = "6a200000DNMSg",
670
671 movn_2 = "12800000DWg",
672 movn_3 = "12800000DWRg",
673 movz_2 = "52800000DWg",
674 movz_3 = "52800000DWRg",
675 movk_2 = "72800000DWg",
676 movk_3 = "72800000DWRg",
677
678 -- TODO: this doesn't cover all valid immediates for mov reg, #imm.
679 mov_2 = "2a0003e0DMg|52800000DW|320003e0pDig|11000000pDpNg",
680 mov_3 = "2a0003e0DMSg",
681 mvn_2 = "2a2003e0DMg",
682 mvn_3 = "2a2003e0DMSg",
683
684 adr_2 = "10000000DBx",
685 adrp_2 = "90000000DBx",
686
687 csel_4 = "1a800000DNMCg",
688 csinc_4 = "1a800400DNMCg",
689 csinv_4 = "5a800000DNMCg",
690 csneg_4 = "5a800400DNMCg",
691 cset_2 = "1a9f07e0Dcg",
692 csetm_2 = "5a9f03e0Dcg",
693 cinc_3 = "1a800400DNmcg",
694 cinv_3 = "5a800000DNmcg",
695 cneg_3 = "5a800400DNmcg",
696
697 ccmn_4 = "3a400000NMVCg|3a400800N5VCg",
698 ccmp_4 = "7a400000NMVCg|7a400800N5VCg",
699
700 madd_4 = "1b000000DNMAg",
701 msub_4 = "1b008000DNMAg",
702 mul_3 = "1b007c00DNMg",
703 mneg_3 = "1b00fc00DNMg",
704
705 smaddl_4 = "9b200000DxNMwAx",
706 smsubl_4 = "9b208000DxNMwAx",
707 smull_3 = "9b207c00DxNMw",
708 smnegl_3 = "9b20fc00DxNMw",
709 smulh_3 = "9b407c00DNMx",
710 umaddl_4 = "9ba00000DxNMwAx",
711 umsubl_4 = "9ba08000DxNMwAx",
712 umull_3 = "9ba07c00DxNMw",
713 umnegl_3 = "9ba0fc00DxNMw",
714 umulh_3 = "9bc07c00DNMx",
715
716 udiv_3 = "1ac00800DNMg",
717 sdiv_3 = "1ac00c00DNMg",
718
719 -- Bit operations.
720 sbfm_4 = "13000000DN12w|93400000DN12x",
721 bfm_4 = "33000000DN12w|b3400000DN12x",
722 ubfm_4 = "53000000DN12w|d3400000DN12x",
723 extr_4 = "13800000DNM2w|93c00000DNM2x",
724
725 sxtb_2 = "13001c00DNw|93401c00DNx",
726 sxth_2 = "13003c00DNw|93403c00DNx",
727 sxtw_2 = "93407c00DNx",
728 uxtb_2 = "53001c00DNw",
729 uxth_2 = "53003c00DNw",
730
731 sbfx_4 = op_alias("sbfm_4", alias_bfx),
732 bfxil_4 = op_alias("bfm_4", alias_bfx),
733 ubfx_4 = op_alias("ubfm_4", alias_bfx),
734 sbfiz_4 = op_alias("sbfm_4", alias_bfiz),
735 bfi_4 = op_alias("bfm_4", alias_bfiz),
736 ubfiz_4 = op_alias("ubfm_4", alias_bfiz),
737
738 lsl_3 = function(params, nparams)
739 if params and params[3]:byte() == 35 then
740 return alias_lslimm(params, nparams)
741 else
742 return op_template(params, "1ac02000DNMg", nparams)
743 end
744 end,
745 lsr_3 = "1ac02400DNMg|53007c00DN1w|d340fc00DN1x",
746 asr_3 = "1ac02800DNMg|13007c00DN1w|9340fc00DN1x",
747 ror_3 = "1ac02c00DNMg|13800000DNm2w|93c00000DNm2x",
748
749 clz_2 = "5ac01000DNg",
750 cls_2 = "5ac01400DNg",
751 rbit_2 = "5ac00000DNg",
752 rev_2 = "5ac00800DNw|dac00c00DNx",
753 rev16_2 = "5ac00400DNg",
754 rev32_2 = "dac00800DNx",
755
756 -- Loads and stores.
757 ["strb_*"] = "38000000DwL",
758 ["ldrb_*"] = "38400000DwL",
759 ["ldrsb_*"] = "38c00000DwL|38800000DxL",
760 ["strh_*"] = "78000000DwL",
761 ["ldrh_*"] = "78400000DwL",
762 ["ldrsh_*"] = "78c00000DwL|78800000DxL",
763 ["str_*"] = "b8000000DwL|f8000000DxL|bc000000DsL|fc000000DdL",
764 ["ldr_*"] = "18000000DwB|58000000DxB|1c000000DsB|5c000000DdB|b8400000DwL|f8400000DxL|bc400000DsL|fc400000DdL",
765 ["ldrsw_*"] = "98000000DxB|b8800000DxL",
766 -- NOTE: ldur etc. are handled by ldr et al.
767
768 ["stp_*"] = "28000000DAwP|a8000000DAxP|2c000000DAsP|6c000000DAdP",
769 ["ldp_*"] = "28400000DAwP|a8400000DAxP|2c400000DAsP|6c400000DAdP",
770 ["ldpsw_*"] = "68400000DAxP",
771
772 -- Branches.
773 b_1 = "14000000B",
774 bl_1 = "94000000B",
775 blr_1 = "d63f0000Nx",
776 br_1 = "d61f0000Nx",
777 ret_0 = "d65f03c0",
778 ret_1 = "d65f0000Nx",
779 -- b.cond is added below.
780 cbz_2 = "34000000DBg",
781 cbnz_2 = "35000000DBg",
782 tbz_3 = "36000000DTBw|36000000DTBx",
783 tbnz_3 = "37000000DTBw|37000000DTBx",
784
785 -- Miscellaneous instructions.
786 -- TODO: hlt, hvc, smc, svc, eret, dcps[123], drps, mrs, msr
787 -- TODO: sys, sysl, ic, dc, at, tlbi
788 -- TODO: hint, yield, wfe, wfi, sev, sevl
789 -- TODO: clrex, dsb, dmb, isb
790 nop_0 = "d503201f",
791 brk_0 = "d4200000",
792 brk_1 = "d4200000W",
793
794 -- Floating point instructions.
795 fmov_2 = "1e204000DNf|1e260000DwNs|1e270000DsNw|9e660000DxNd|9e670000DdNx|1e201000DFf",
796 fabs_2 = "1e20c000DNf",
797 fneg_2 = "1e214000DNf",
798 fsqrt_2 = "1e21c000DNf",
799
800 fcvt_2 = "1e22c000DdNs|1e624000DsNd",
801
802 -- TODO: half-precision and fixed-point conversions.
803 fcvtas_2 = "1e240000DwNs|9e240000DxNs|1e640000DwNd|9e640000DxNd",
804 fcvtau_2 = "1e250000DwNs|9e250000DxNs|1e650000DwNd|9e650000DxNd",
805 fcvtms_2 = "1e300000DwNs|9e300000DxNs|1e700000DwNd|9e700000DxNd",
806 fcvtmu_2 = "1e310000DwNs|9e310000DxNs|1e710000DwNd|9e710000DxNd",
807 fcvtns_2 = "1e200000DwNs|9e200000DxNs|1e600000DwNd|9e600000DxNd",
808 fcvtnu_2 = "1e210000DwNs|9e210000DxNs|1e610000DwNd|9e610000DxNd",
809 fcvtps_2 = "1e280000DwNs|9e280000DxNs|1e680000DwNd|9e680000DxNd",
810 fcvtpu_2 = "1e290000DwNs|9e290000DxNs|1e690000DwNd|9e690000DxNd",
811 fcvtzs_2 = "1e380000DwNs|9e380000DxNs|1e780000DwNd|9e780000DxNd",
812 fcvtzu_2 = "1e390000DwNs|9e390000DxNs|1e790000DwNd|9e790000DxNd",
813
814 scvtf_2 = "1e220000DsNw|9e220000DsNx|1e620000DdNw|9e620000DdNx",
815 ucvtf_2 = "1e230000DsNw|9e230000DsNx|1e630000DdNw|9e630000DdNx",
816
817 frintn_2 = "1e244000DNf",
818 frintp_2 = "1e24c000DNf",
819 frintm_2 = "1e254000DNf",
820 frintz_2 = "1e25c000DNf",
821 frinta_2 = "1e264000DNf",
822 frintx_2 = "1e274000DNf",
823 frinti_2 = "1e27c000DNf",
824
825 fadd_3 = "1e202800DNMf",
826 fsub_3 = "1e203800DNMf",
827 fmul_3 = "1e200800DNMf",
828 fnmul_3 = "1e208800DNMf",
829 fdiv_3 = "1e201800DNMf",
830
831 fmadd_4 = "1f000000DNMAf",
832 fmsub_4 = "1f008000DNMAf",
833 fnmadd_4 = "1f200000DNMAf",
834 fnmsub_4 = "1f208000DNMAf",
835
836 fmax_3 = "1e204800DNMf",
837 fmaxnm_3 = "1e206800DNMf",
838 fmin_3 = "1e205800DNMf",
839 fminnm_3 = "1e207800DNMf",
840
841 fcmp_2 = "1e202000NMf|1e202008NZf",
842 fcmpe_2 = "1e202010NMf|1e202018NZf",
843
844 fccmp_4 = "1e200400NMVCf",
845 fccmpe_4 = "1e200410NMVCf",
846
847 fcsel_4 = "1e200c00DNMCf",
848
849 -- TODO: crc32*, aes*, sha*, pmull
850 -- TODO: SIMD instructions.
851}
852
853for cond,c in pairs(map_cond) do
854 map_op["b"..cond.."_1"] = tohex(0x54000000+c)
855end
856
857------------------------------------------------------------------------------
858
859-- Handle opcodes defined with template strings.
860local function parse_template(params, template, nparams, pos)
861 local op = tonumber(sub(template, 1, 8), 16)
862 local n = 1
863 local rtt = {}
864
865 parse_reg_type = false
866
867 -- Process each character.
868 for p in gmatch(sub(template, 9), ".") do
869 local q = params[n]
870 if p == "D" then
871 op = op + parse_reg(q); n = n + 1
872 elseif p == "N" then
873 op = op + shl(parse_reg(q), 5); n = n + 1
874 elseif p == "M" then
875 op = op + shl(parse_reg(q), 16); n = n + 1
876 elseif p == "A" then
877 op = op + shl(parse_reg(q), 10); n = n + 1
878 elseif p == "m" then
879 op = op + shl(parse_reg(params[n-1]), 16)
880
881 elseif p == "p" then
882 if q == "sp" then params[n] = "@x31" end
883 elseif p == "g" then
884 if parse_reg_type == "x" then
885 op = op + 0x80000000
886 elseif parse_reg_type ~= "w" then
887 werror("bad register type")
888 end
889 parse_reg_type = false
890 elseif p == "f" then
891 if parse_reg_type == "d" then
892 op = op + 0x00400000
893 elseif parse_reg_type ~= "s" then
894 werror("bad register type")
895 end
896 parse_reg_type = false
897 elseif p == "x" or p == "w" or p == "d" or p == "s" then
898 if parse_reg_type ~= p then
899 werror("register size mismatch")
900 end
901 parse_reg_type = false
902
903 elseif p == "L" then
904 op = parse_load(params, nparams, n, op)
905 elseif p == "P" then
906 op = parse_load_pair(params, nparams, n, op)
907
908 elseif p == "B" then
909 local mode, v, s = parse_label(q, false); n = n + 1
910 local m = branch_type(op)
911 waction("REL_"..mode, v+m, s, 1)
912
913 elseif p == "I" then
914 op = op + parse_imm12(q); n = n + 1
915 elseif p == "i" then
916 op = op + parse_imm13(q); n = n + 1
917 elseif p == "W" then
918 op = op + parse_imm(q, 16, 5, 0, false); n = n + 1
919 elseif p == "T" then
920 op = op + parse_imm6(q); n = n + 1
921 elseif p == "1" then
922 op = op + parse_imm(q, 6, 16, 0, false); n = n + 1
923 elseif p == "2" then
924 op = op + parse_imm(q, 6, 10, 0, false); n = n + 1
925 elseif p == "5" then
926 op = op + parse_imm(q, 5, 16, 0, false); n = n + 1
927 elseif p == "V" then
928 op = op + parse_imm(q, 4, 0, 0, false); n = n + 1
929 elseif p == "F" then
930 op = op + parse_fpimm(q); n = n + 1
931 elseif p == "Z" then
932 if q ~= "#0" and q ~= "#0.0" then werror("expected zero immediate") end
933 n = n + 1
934
935 elseif p == "S" then
936 op = op + parse_shift(q); n = n + 1
937 elseif p == "X" then
938 op = op + parse_extend(q); n = n + 1
939 elseif p == "R" then
940 op = op + parse_lslx16(q); n = n + 1
941 elseif p == "C" then
942 op = op + parse_cond(q, 0); n = n + 1
943 elseif p == "c" then
944 op = op + parse_cond(q, 1); n = n + 1
945
946 else
947 assert(false)
948 end
949 end
950 wputpos(pos, op)
951end
952
953function op_template(params, template, nparams)
954 if not params then return template:gsub("%x%x%x%x%x%x%x%x", "") end
955
956 -- Limit number of section buffer positions used by a single dasm_put().
957 -- A single opcode needs a maximum of 3 positions.
958 if secpos+3 > maxsecpos then wflush() end
959 local pos = wpos()
960 local apos, spos = #actargs, secpos
961
962 local ok, err
963 for t in gmatch(template, "[^|]+") do
964 ok, err = pcall(parse_template, params, t, nparams, pos)
965 if ok then return end
966 secpos = spos
967 actargs[apos+1] = nil
968 actargs[apos+2] = nil
969 actargs[apos+3] = nil
970 end
971 error(err, 0)
972end
973
974map_op[".template__"] = op_template
975
976------------------------------------------------------------------------------
977
978-- Pseudo-opcode to mark the position where the action list is to be emitted.
979map_op[".actionlist_1"] = function(params)
980 if not params then return "cvar" end
981 local name = params[1] -- No syntax check. You get to keep the pieces.
982 wline(function(out) writeactions(out, name) end)
983end
984
985-- Pseudo-opcode to mark the position where the global enum is to be emitted.
986map_op[".globals_1"] = function(params)
987 if not params then return "prefix" end
988 local prefix = params[1] -- No syntax check. You get to keep the pieces.
989 wline(function(out) writeglobals(out, prefix) end)
990end
991
992-- Pseudo-opcode to mark the position where the global names are to be emitted.
993map_op[".globalnames_1"] = function(params)
994 if not params then return "cvar" end
995 local name = params[1] -- No syntax check. You get to keep the pieces.
996 wline(function(out) writeglobalnames(out, name) end)
997end
998
999-- Pseudo-opcode to mark the position where the extern names are to be emitted.
1000map_op[".externnames_1"] = function(params)
1001 if not params then return "cvar" end
1002 local name = params[1] -- No syntax check. You get to keep the pieces.
1003 wline(function(out) writeexternnames(out, name) end)
1004end
1005
1006------------------------------------------------------------------------------
1007
1008-- Label pseudo-opcode (converted from trailing colon form).
1009map_op[".label_1"] = function(params)
1010 if not params then return "[1-9] | ->global | =>pcexpr" end
1011 if secpos+1 > maxsecpos then wflush() end
1012 local mode, n, s = parse_label(params[1], true)
1013 if mode == "EXT" then werror("bad label definition") end
1014 waction("LABEL_"..mode, n, s, 1)
1015end
1016
1017------------------------------------------------------------------------------
1018
1019-- Pseudo-opcodes for data storage.
1020map_op[".long_*"] = function(params)
1021 if not params then return "imm..." end
1022 for _,p in ipairs(params) do
1023 local n = tonumber(p)
1024 if not n then werror("bad immediate `"..p.."'") end
1025 if n < 0 then n = n + 2^32 end
1026 wputw(n)
1027 if secpos+2 > maxsecpos then wflush() end
1028 end
1029end
1030
1031-- Alignment pseudo-opcode.
1032map_op[".align_1"] = function(params)
1033 if not params then return "numpow2" end
1034 if secpos+1 > maxsecpos then wflush() end
1035 local align = tonumber(params[1])
1036 if align then
1037 local x = align
1038 -- Must be a power of 2 in the range (2 ... 256).
1039 for i=1,8 do
1040 x = x / 2
1041 if x == 1 then
1042 waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1.
1043 return
1044 end
1045 end
1046 end
1047 werror("bad alignment")
1048end
1049
1050------------------------------------------------------------------------------
1051
1052-- Pseudo-opcode for (primitive) type definitions (map to C types).
1053map_op[".type_3"] = function(params, nparams)
1054 if not params then
1055 return nparams == 2 and "name, ctype" or "name, ctype, reg"
1056 end
1057 local name, ctype, reg = params[1], params[2], params[3]
1058 if not match(name, "^[%a_][%w_]*$") then
1059 werror("bad type name `"..name.."'")
1060 end
1061 local tp = map_type[name]
1062 if tp then
1063 werror("duplicate type `"..name.."'")
1064 end
1065 -- Add #type to defines. A bit unclean to put it in map_archdef.
1066 map_archdef["#"..name] = "sizeof("..ctype..")"
1067 -- Add new type and emit shortcut define.
1068 local num = ctypenum + 1
1069 map_type[name] = {
1070 ctype = ctype,
1071 ctypefmt = format("Dt%X(%%s)", num),
1072 reg = reg,
1073 }
1074 wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype))
1075 ctypenum = num
1076end
1077map_op[".type_2"] = map_op[".type_3"]
1078
1079-- Dump type definitions.
1080local function dumptypes(out, lvl)
1081 local t = {}
1082 for name in pairs(map_type) do t[#t+1] = name end
1083 sort(t)
1084 out:write("Type definitions:\n")
1085 for _,name in ipairs(t) do
1086 local tp = map_type[name]
1087 local reg = tp.reg or ""
1088 out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg))
1089 end
1090 out:write("\n")
1091end
1092
1093------------------------------------------------------------------------------
1094
1095-- Set the current section.
1096function _M.section(num)
1097 waction("SECTION", num)
1098 wflush(true) -- SECTION is a terminal action.
1099end
1100
1101------------------------------------------------------------------------------
1102
1103-- Dump architecture description.
1104function _M.dumparch(out)
1105 out:write(format("DynASM %s version %s, released %s\n\n",
1106 _info.arch, _info.version, _info.release))
1107 dumpactions(out)
1108end
1109
1110-- Dump all user defined elements.
1111function _M.dumpdef(out, lvl)
1112 dumptypes(out, lvl)
1113 dumpglobals(out, lvl)
1114 dumpexterns(out, lvl)
1115end
1116
1117------------------------------------------------------------------------------
1118
1119-- Pass callbacks from/to the DynASM core.
1120function _M.passcb(wl, we, wf, ww)
1121 wline, werror, wfatal, wwarn = wl, we, wf, ww
1122 return wflush
1123end
1124
1125-- Setup the arch-specific module.
1126function _M.setup(arch, opt)
1127 g_arch, g_opt = arch, opt
1128end
1129
1130-- Merge the core maps and the arch-specific maps.
1131function _M.mergemaps(map_coreop, map_def)
1132 setmetatable(map_op, { __index = map_coreop })
1133 setmetatable(map_def, { __index = map_archdef })
1134 return map_op, map_def
1135end
1136
1137return _M
1138
1139------------------------------------------------------------------------------
1140