aboutsummaryrefslogtreecommitdiff
path: root/dynasm
diff options
context:
space:
mode:
Diffstat (limited to 'dynasm')
-rw-r--r--dynasm/dasm_arm.lua6
-rw-r--r--dynasm/dasm_arm64.h519
-rw-r--r--dynasm/dasm_arm64.lua1166
-rw-r--r--dynasm/dasm_mips.h22
-rw-r--r--dynasm/dasm_mips.lua684
-rw-r--r--dynasm/dasm_mips64.lua12
-rw-r--r--dynasm/dasm_ppc.h13
-rw-r--r--dynasm/dasm_ppc.lua702
-rw-r--r--dynasm/dasm_proto.h4
-rw-r--r--dynasm/dasm_x86.h38
-rw-r--r--dynasm/dasm_x86.lua606
-rw-r--r--dynasm/dynasm.lua7
12 files changed, 3413 insertions, 366 deletions
diff --git a/dynasm/dasm_arm.lua b/dynasm/dasm_arm.lua
index 21fb5022..164980a1 100644
--- a/dynasm/dasm_arm.lua
+++ b/dynasm/dasm_arm.lua
@@ -9,9 +9,9 @@
9local _info = { 9local _info = {
10 arch = "arm", 10 arch = "arm",
11 description = "DynASM ARM module", 11 description = "DynASM ARM module",
12 version = "1.3.0", 12 version = "1.4.0",
13 vernum = 10300, 13 vernum = 10400,
14 release = "2011-05-05", 14 release = "2015-10-18",
15 author = "Mike Pall", 15 author = "Mike Pall",
16 license = "MIT", 16 license = "MIT",
17} 17}
diff --git a/dynasm/dasm_arm64.h b/dynasm/dasm_arm64.h
new file mode 100644
index 00000000..3455981f
--- /dev/null
+++ b/dynasm/dasm_arm64.h
@@ -0,0 +1,519 @@
1/*
2** DynASM ARM64 encoding engine.
3** Copyright (C) 2005-2020 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_IMM6, 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 CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I);
285 n >>= ((ins>>10)&31);
286#ifdef DASM_CHECKS
287 if ((ins & 0x8000))
288 CK(((n + (1<<(((ins>>5)&31)-1)))>>((ins>>5)&31)) == 0, RANGE_I);
289 else
290 CK((n>>((ins>>5)&31)) == 0, RANGE_I);
291#endif
292 b[pos++] = n;
293 break;
294 case DASM_IMM6:
295 CK((n >> 6) == 0, RANGE_I);
296 b[pos++] = n;
297 break;
298 case DASM_IMM12:
299 CK(dasm_imm12((unsigned int)n) != -1, RANGE_I);
300 b[pos++] = n;
301 break;
302 case DASM_IMM13W:
303 CK(dasm_imm13(n, n) != -1, RANGE_I);
304 b[pos++] = n;
305 break;
306 case DASM_IMM13X: {
307 int m = va_arg(ap, int);
308 CK(dasm_imm13(n, m) != -1, RANGE_I);
309 b[pos++] = n;
310 b[pos++] = m;
311 break;
312 }
313 case DASM_IMML: {
314#ifdef DASM_CHECKS
315 int scale = (p[-2] >> 30);
316 CK((!(n & ((1<<scale)-1)) && (unsigned int)(n>>scale) < 4096) ||
317 (unsigned int)(n+256) < 512, RANGE_I);
318#endif
319 b[pos++] = n;
320 break;
321 }
322 }
323 }
324 }
325stop:
326 va_end(ap);
327 sec->pos = pos;
328 sec->ofs = ofs;
329}
330#undef CK
331
332/* Pass 2: Link sections, shrink aligns, fix label offsets. */
333int dasm_link(Dst_DECL, size_t *szp)
334{
335 dasm_State *D = Dst_REF;
336 int secnum;
337 int ofs = 0;
338
339#ifdef DASM_CHECKS
340 *szp = 0;
341 if (D->status != DASM_S_OK) return D->status;
342 {
343 int pc;
344 for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
345 if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
346 }
347#endif
348
349 { /* Handle globals not defined in this translation unit. */
350 int idx;
351 for (idx = 20; idx*sizeof(int) < D->lgsize; idx++) {
352 int n = D->lglabels[idx];
353 /* Undefined label: Collapse rel chain and replace with marker (< 0). */
354 while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
355 }
356 }
357
358 /* Combine all code sections. No support for data sections (yet). */
359 for (secnum = 0; secnum < D->maxsection; secnum++) {
360 dasm_Section *sec = D->sections + secnum;
361 int *b = sec->rbuf;
362 int pos = DASM_SEC2POS(secnum);
363 int lastpos = sec->pos;
364
365 while (pos != lastpos) {
366 dasm_ActList p = D->actionlist + b[pos++];
367 while (1) {
368 unsigned int ins = *p++;
369 unsigned int action = (ins >> 16);
370 switch (action) {
371 case DASM_STOP: case DASM_SECTION: goto stop;
372 case DASM_ESC: p++; break;
373 case DASM_REL_EXT: break;
374 case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break;
375 case DASM_REL_LG: case DASM_REL_PC: pos++; break;
376 case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break;
377 case DASM_IMM: case DASM_IMM6: case DASM_IMM12: case DASM_IMM13W:
378 case DASM_IMML: pos++; break;
379 case DASM_IMM13X: pos += 2; break;
380 }
381 }
382 stop: (void)0;
383 }
384 ofs += sec->ofs; /* Next section starts right after current section. */
385 }
386
387 D->codesize = ofs; /* Total size of all code sections */
388 *szp = ofs;
389 return DASM_S_OK;
390}
391
392#ifdef DASM_CHECKS
393#define CK(x, st) \
394 do { if (!(x)) return DASM_S_##st|(p-D->actionlist-1); } while (0)
395#else
396#define CK(x, st) ((void)0)
397#endif
398
399/* Pass 3: Encode sections. */
400int dasm_encode(Dst_DECL, void *buffer)
401{
402 dasm_State *D = Dst_REF;
403 char *base = (char *)buffer;
404 unsigned int *cp = (unsigned int *)buffer;
405 int secnum;
406
407 /* Encode all code sections. No support for data sections (yet). */
408 for (secnum = 0; secnum < D->maxsection; secnum++) {
409 dasm_Section *sec = D->sections + secnum;
410 int *b = sec->buf;
411 int *endb = sec->rbuf + sec->pos;
412
413 while (b != endb) {
414 dasm_ActList p = D->actionlist + *b++;
415 while (1) {
416 unsigned int ins = *p++;
417 unsigned int action = (ins >> 16);
418 int n = (action >= DASM_ALIGN && action < DASM__MAX) ? *b++ : 0;
419 switch (action) {
420 case DASM_STOP: case DASM_SECTION: goto stop;
421 case DASM_ESC: *cp++ = *p++; break;
422 case DASM_REL_EXT:
423 n = DASM_EXTERN(Dst, (unsigned char *)cp, (ins&2047), !(ins&2048));
424 goto patchrel;
425 case DASM_ALIGN:
426 ins &= 255; while ((((char *)cp - base) & ins)) *cp++ = 0xe1a00000;
427 break;
428 case DASM_REL_LG:
429 CK(n >= 0, UNDEF_LG);
430 /* fallthrough */
431 case DASM_REL_PC:
432 CK(n >= 0, UNDEF_PC);
433 n = *DASM_POS2PTR(D, n) - (int)((char *)cp - base) + 4;
434 patchrel:
435 if (!(ins & 0xf800)) { /* B, BL */
436 CK((n & 3) == 0 && ((n+0x08000000) >> 28) == 0, RANGE_REL);
437 cp[-1] |= ((n >> 2) & 0x03ffffff);
438 } else if ((ins & 0x800)) { /* B.cond, CBZ, CBNZ, LDR* literal */
439 CK((n & 3) == 0 && ((n+0x00100000) >> 21) == 0, RANGE_REL);
440 cp[-1] |= ((n << 3) & 0x00ffffe0);
441 } else if ((ins & 0x3000) == 0x2000) { /* ADR */
442 CK(((n+0x00100000) >> 21) == 0, RANGE_REL);
443 cp[-1] |= ((n << 3) & 0x00ffffe0) | ((n & 3) << 29);
444 } else if ((ins & 0x3000) == 0x3000) { /* ADRP */
445 cp[-1] |= ((n >> 9) & 0x00ffffe0) | (((n >> 12) & 3) << 29);
446 } else if ((ins & 0x1000)) { /* TBZ, TBNZ */
447 CK((n & 3) == 0 && ((n+0x00008000) >> 16) == 0, RANGE_REL);
448 cp[-1] |= ((n << 3) & 0x0007ffe0);
449 }
450 break;
451 case DASM_LABEL_LG:
452 ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n);
453 break;
454 case DASM_LABEL_PC: break;
455 case DASM_IMM:
456 cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31);
457 break;
458 case DASM_IMM6:
459 cp[-1] |= ((n&31) << 19) | ((n&32) << 26);
460 break;
461 case DASM_IMM12:
462 cp[-1] |= (dasm_imm12((unsigned int)n) << 10);
463 break;
464 case DASM_IMM13W:
465 cp[-1] |= (dasm_imm13(n, n) << 10);
466 break;
467 case DASM_IMM13X:
468 cp[-1] |= (dasm_imm13(n, *b++) << 10);
469 break;
470 case DASM_IMML: {
471 int scale = (p[-2] >> 30);
472 cp[-1] |= (!(n & ((1<<scale)-1)) && (unsigned int)(n>>scale) < 4096) ?
473 ((n << (10-scale)) | 0x01000000) : ((n & 511) << 12);
474 break;
475 }
476 default: *cp++ = ins; break;
477 }
478 }
479 stop: (void)0;
480 }
481 }
482
483 if (base + D->codesize != (char *)cp) /* Check for phase errors. */
484 return DASM_S_PHASE;
485 return DASM_S_OK;
486}
487#undef CK
488
489/* Get PC label offset. */
490int dasm_getpclabel(Dst_DECL, unsigned int pc)
491{
492 dasm_State *D = Dst_REF;
493 if (pc*sizeof(int) < D->pcsize) {
494 int pos = D->pclabels[pc];
495 if (pos < 0) return *DASM_POS2PTR(D, -pos);
496 if (pos > 0) return -1; /* Undefined. */
497 }
498 return -2; /* Unused or out of range. */
499}
500
501#ifdef DASM_CHECKS
502/* Optional sanity checker to call between isolated encoding steps. */
503int dasm_checkstep(Dst_DECL, int secmatch)
504{
505 dasm_State *D = Dst_REF;
506 if (D->status == DASM_S_OK) {
507 int i;
508 for (i = 1; i <= 9; i++) {
509 if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_LG|i; break; }
510 D->lglabels[i] = 0;
511 }
512 }
513 if (D->status == DASM_S_OK && secmatch >= 0 &&
514 D->section != &D->sections[secmatch])
515 D->status = DASM_S_MATCH_SEC|(D->section-D->sections);
516 return D->status;
517}
518#endif
519
diff --git a/dynasm/dasm_arm64.lua b/dynasm/dasm_arm64.lua
new file mode 100644
index 00000000..d5640842
--- /dev/null
+++ b/dynasm/dasm_arm64.lua
@@ -0,0 +1,1166 @@
1------------------------------------------------------------------------------
2-- DynASM ARM64 module.
3--
4-- Copyright (C) 2005-2020 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.4.0",
13 vernum = 10400,
14 release = "2015-10-18",
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", "IMM6", "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, tp = 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), tp
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 waction("IMM6", 0, imm)
391 return 0
392 end
393end
394
395local function parse_imm_load(imm, scale)
396 local n = parse_number(imm)
397 if n then
398 local m = sar(n, scale)
399 if shl(m, scale) == n and m >= 0 and m < 0x1000 then
400 return shl(m, 10) + 0x01000000 -- Scaled, unsigned 12 bit offset.
401 elseif n >= -256 and n < 256 then
402 return shl(band(n, 511), 12) -- Unscaled, signed 9 bit offset.
403 end
404 werror("out of range immediate `"..imm.."'")
405 else
406 waction("IMML", 0, imm)
407 return 0
408 end
409end
410
411local function parse_fpimm(imm)
412 imm = match(imm, "^#(.*)$")
413 if not imm then werror("expected immediate operand") end
414 local n = parse_number(imm)
415 if n then
416 local m, e = math.frexp(n)
417 local s, e2 = 0, band(e-2, 7)
418 if m < 0 then m = -m; s = 0x00100000 end
419 m = m*32-16
420 if m % 1 == 0 and m >= 0 and m <= 15 and sar(shl(e2, 29), 29)+2 == e then
421 return s + shl(e2, 17) + shl(m, 13)
422 end
423 werror("out of range immediate `"..imm.."'")
424 else
425 werror("NYI fpimm action")
426 end
427end
428
429local function parse_shift(expr)
430 local s, s2 = match(expr, "^(%S+)%s*(.*)$")
431 s = map_shift[s]
432 if not s then werror("expected shift operand") end
433 return parse_imm(s2, 6, 10, 0, false) + shl(s, 22)
434end
435
436local function parse_lslx16(expr)
437 local n = match(expr, "^lsl%s*#(%d+)$")
438 n = tonumber(n)
439 if not n then werror("expected shift operand") end
440 if band(n, parse_reg_type == "x" and 0xffffffcf or 0xffffffef) ~= 0 then
441 werror("bad shift amount")
442 end
443 return shl(n, 17)
444end
445
446local function parse_extend(expr)
447 local s, s2 = match(expr, "^(%S+)%s*(.*)$")
448 if s == "lsl" then
449 s = parse_reg_type == "x" and 3 or 2
450 else
451 s = map_extend[s]
452 end
453 if not s then werror("expected extend operand") end
454 return (s2 == "" and 0 or parse_imm(s2, 3, 10, 0, false)) + shl(s, 13)
455end
456
457local function parse_cond(expr, inv)
458 local c = map_cond[expr]
459 if not c then werror("expected condition operand") end
460 return shl(bit.bxor(c, inv), 12)
461end
462
463local function parse_load(params, nparams, n, op)
464 if params[n+2] then werror("too many operands") end
465 local pn, p2 = params[n], params[n+1]
466 local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$")
467 if not p1 then
468 if not p2 then
469 local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$")
470 if reg and tailr ~= "" then
471 local base, tp = parse_reg_base(reg)
472 if tp then
473 waction("IMML", 0, format(tp.ctypefmt, tailr))
474 return op + base
475 end
476 end
477 end
478 werror("expected address operand")
479 end
480 local scale = shr(op, 30)
481 if p2 then
482 if wb == "!" then werror("bad use of '!'") end
483 op = op + parse_reg_base(p1) + parse_imm(p2, 9, 12, 0, true) + 0x400
484 elseif wb == "!" then
485 local p1a, p2a = match(p1, "^([^,%s]*)%s*,%s*(.*)$")
486 if not p1a then werror("bad use of '!'") end
487 op = op + parse_reg_base(p1a) + parse_imm(p2a, 9, 12, 0, true) + 0xc00
488 else
489 local p1a, p2a = match(p1, "^([^,%s]*)%s*(.*)$")
490 op = op + parse_reg_base(p1a)
491 if p2a ~= "" then
492 local imm = match(p2a, "^,%s*#(.*)$")
493 if imm then
494 op = op + parse_imm_load(imm, scale)
495 else
496 local p2b, p3b, p3s = match(p2a, "^,%s*([^,%s]*)%s*,?%s*(%S*)%s*(.*)$")
497 op = op + shl(parse_reg(p2b), 16) + 0x00200800
498 if parse_reg_type ~= "x" and parse_reg_type ~= "w" then
499 werror("bad index register type")
500 end
501 if p3b == "" then
502 if parse_reg_type ~= "x" then werror("bad index register type") end
503 op = op + 0x6000
504 else
505 if p3s == "" or p3s == "#0" then
506 elseif p3s == "#"..scale then
507 op = op + 0x1000
508 else
509 werror("bad scale")
510 end
511 if parse_reg_type == "x" then
512 if p3b == "lsl" and p3s ~= "" then op = op + 0x6000
513 elseif p3b == "sxtx" then op = op + 0xe000
514 else
515 werror("bad extend/shift specifier")
516 end
517 else
518 if p3b == "uxtw" then op = op + 0x4000
519 elseif p3b == "sxtw" then op = op + 0xc000
520 else
521 werror("bad extend/shift specifier")
522 end
523 end
524 end
525 end
526 else
527 if wb == "!" then werror("bad use of '!'") end
528 op = op + 0x01000000
529 end
530 end
531 return op
532end
533
534local function parse_load_pair(params, nparams, n, op)
535 if params[n+2] then werror("too many operands") end
536 local pn, p2 = params[n], params[n+1]
537 local scale = shr(op, 30) == 0 and 2 or 3
538 local p1, wb = match(pn, "^%[%s*(.-)%s*%](!?)$")
539 if not p1 then
540 if not p2 then
541 local reg, tailr = match(pn, "^([%w_:]+)%s*(.*)$")
542 if reg and tailr ~= "" then
543 local base, tp = parse_reg_base(reg)
544 if tp then
545 waction("IMM", 32768+7*32+15+scale*1024, format(tp.ctypefmt, tailr))
546 return op + base + 0x01000000
547 end
548 end
549 end
550 werror("expected address operand")
551 end
552 if p2 then
553 if wb == "!" then werror("bad use of '!'") end
554 op = op + 0x00800000
555 else
556 local p1a, p2a = match(p1, "^([^,%s]*)%s*,%s*(.*)$")
557 if p1a then p1, p2 = p1a, p2a else p2 = "#0" end
558 op = op + (wb == "!" and 0x01800000 or 0x01000000)
559 end
560 return op + parse_reg_base(p1) + parse_imm(p2, 7, 15, scale, true)
561end
562
563local function parse_label(label, def)
564 local prefix = sub(label, 1, 2)
565 -- =>label (pc label reference)
566 if prefix == "=>" then
567 return "PC", 0, sub(label, 3)
568 end
569 -- ->name (global label reference)
570 if prefix == "->" then
571 return "LG", map_global[sub(label, 3)]
572 end
573 if def then
574 -- [1-9] (local label definition)
575 if match(label, "^[1-9]$") then
576 return "LG", 10+tonumber(label)
577 end
578 else
579 -- [<>][1-9] (local label reference)
580 local dir, lnum = match(label, "^([<>])([1-9])$")
581 if dir then -- Fwd: 1-9, Bkwd: 11-19.
582 return "LG", lnum + (dir == ">" and 0 or 10)
583 end
584 -- extern label (extern label reference)
585 local extname = match(label, "^extern%s+(%S+)$")
586 if extname then
587 return "EXT", map_extern[extname]
588 end
589 end
590 werror("bad label `"..label.."'")
591end
592
593local function branch_type(op)
594 if band(op, 0x7c000000) == 0x14000000 then return 0 -- B, BL
595 elseif shr(op, 24) == 0x54 or band(op, 0x7e000000) == 0x34000000 or
596 band(op, 0x3b000000) == 0x18000000 then
597 return 0x800 -- B.cond, CBZ, CBNZ, LDR* literal
598 elseif band(op, 0x7e000000) == 0x36000000 then return 0x1000 -- TBZ, TBNZ
599 elseif band(op, 0x9f000000) == 0x10000000 then return 0x2000 -- ADR
600 elseif band(op, 0x9f000000) == band(0x90000000) then return 0x3000 -- ADRP
601 else
602 assert(false, "unknown branch type")
603 end
604end
605
606------------------------------------------------------------------------------
607
608local map_op, op_template
609
610local function op_alias(opname, f)
611 return function(params, nparams)
612 if not params then return "-> "..opname:sub(1, -3) end
613 f(params, nparams)
614 op_template(params, map_op[opname], nparams)
615 end
616end
617
618local function alias_bfx(p)
619 p[4] = "#("..p[3]:sub(2)..")+("..p[4]:sub(2)..")-1"
620end
621
622local function alias_bfiz(p)
623 parse_reg(p[1])
624 if parse_reg_type == "w" then
625 p[3] = "#-("..p[3]:sub(2)..")%32"
626 p[4] = "#("..p[4]:sub(2)..")-1"
627 else
628 p[3] = "#-("..p[3]:sub(2)..")%64"
629 p[4] = "#("..p[4]:sub(2)..")-1"
630 end
631end
632
633local alias_lslimm = op_alias("ubfm_4", function(p)
634 parse_reg(p[1])
635 local sh = p[3]:sub(2)
636 if parse_reg_type == "w" then
637 p[3] = "#-("..sh..")%32"
638 p[4] = "#31-("..sh..")"
639 else
640 p[3] = "#-("..sh..")%64"
641 p[4] = "#63-("..sh..")"
642 end
643end)
644
645-- Template strings for ARM instructions.
646map_op = {
647 -- Basic data processing instructions.
648 add_3 = "0b000000DNMg|11000000pDpNIg|8b206000pDpNMx",
649 add_4 = "0b000000DNMSg|0b200000DNMXg|8b200000pDpNMXx|8b200000pDpNxMwX",
650 adds_3 = "2b000000DNMg|31000000DpNIg|ab206000DpNMx",
651 adds_4 = "2b000000DNMSg|2b200000DNMXg|ab200000DpNMXx|ab200000DpNxMwX",
652 cmn_2 = "2b00001fNMg|3100001fpNIg|ab20601fpNMx",
653 cmn_3 = "2b00001fNMSg|2b20001fNMXg|ab20001fpNMXx|ab20001fpNxMwX",
654
655 sub_3 = "4b000000DNMg|51000000pDpNIg|cb206000pDpNMx",
656 sub_4 = "4b000000DNMSg|4b200000DNMXg|cb200000pDpNMXx|cb200000pDpNxMwX",
657 subs_3 = "6b000000DNMg|71000000DpNIg|eb206000DpNMx",
658 subs_4 = "6b000000DNMSg|6b200000DNMXg|eb200000DpNMXx|eb200000DpNxMwX",
659 cmp_2 = "6b00001fNMg|7100001fpNIg|eb20601fpNMx",
660 cmp_3 = "6b00001fNMSg|6b20001fNMXg|eb20001fpNMXx|eb20001fpNxMwX",
661
662 neg_2 = "4b0003e0DMg",
663 neg_3 = "4b0003e0DMSg",
664 negs_2 = "6b0003e0DMg",
665 negs_3 = "6b0003e0DMSg",
666
667 adc_3 = "1a000000DNMg",
668 adcs_3 = "3a000000DNMg",
669 sbc_3 = "5a000000DNMg",
670 sbcs_3 = "7a000000DNMg",
671 ngc_2 = "5a0003e0DMg",
672 ngcs_2 = "7a0003e0DMg",
673
674 and_3 = "0a000000DNMg|12000000pDNig",
675 and_4 = "0a000000DNMSg",
676 orr_3 = "2a000000DNMg|32000000pDNig",
677 orr_4 = "2a000000DNMSg",
678 eor_3 = "4a000000DNMg|52000000pDNig",
679 eor_4 = "4a000000DNMSg",
680 ands_3 = "6a000000DNMg|72000000DNig",
681 ands_4 = "6a000000DNMSg",
682 tst_2 = "6a00001fNMg|7200001fNig",
683 tst_3 = "6a00001fNMSg",
684
685 bic_3 = "0a200000DNMg",
686 bic_4 = "0a200000DNMSg",
687 orn_3 = "2a200000DNMg",
688 orn_4 = "2a200000DNMSg",
689 eon_3 = "4a200000DNMg",
690 eon_4 = "4a200000DNMSg",
691 bics_3 = "6a200000DNMg",
692 bics_4 = "6a200000DNMSg",
693
694 movn_2 = "12800000DWg",
695 movn_3 = "12800000DWRg",
696 movz_2 = "52800000DWg",
697 movz_3 = "52800000DWRg",
698 movk_2 = "72800000DWg",
699 movk_3 = "72800000DWRg",
700
701 -- TODO: this doesn't cover all valid immediates for mov reg, #imm.
702 mov_2 = "2a0003e0DMg|52800000DW|320003e0pDig|11000000pDpNg",
703 mov_3 = "2a0003e0DMSg",
704 mvn_2 = "2a2003e0DMg",
705 mvn_3 = "2a2003e0DMSg",
706
707 adr_2 = "10000000DBx",
708 adrp_2 = "90000000DBx",
709
710 csel_4 = "1a800000DNMCg",
711 csinc_4 = "1a800400DNMCg",
712 csinv_4 = "5a800000DNMCg",
713 csneg_4 = "5a800400DNMCg",
714 cset_2 = "1a9f07e0Dcg",
715 csetm_2 = "5a9f03e0Dcg",
716 cinc_3 = "1a800400DNmcg",
717 cinv_3 = "5a800000DNmcg",
718 cneg_3 = "5a800400DNmcg",
719
720 ccmn_4 = "3a400000NMVCg|3a400800N5VCg",
721 ccmp_4 = "7a400000NMVCg|7a400800N5VCg",
722
723 madd_4 = "1b000000DNMAg",
724 msub_4 = "1b008000DNMAg",
725 mul_3 = "1b007c00DNMg",
726 mneg_3 = "1b00fc00DNMg",
727
728 smaddl_4 = "9b200000DxNMwAx",
729 smsubl_4 = "9b208000DxNMwAx",
730 smull_3 = "9b207c00DxNMw",
731 smnegl_3 = "9b20fc00DxNMw",
732 smulh_3 = "9b407c00DNMx",
733 umaddl_4 = "9ba00000DxNMwAx",
734 umsubl_4 = "9ba08000DxNMwAx",
735 umull_3 = "9ba07c00DxNMw",
736 umnegl_3 = "9ba0fc00DxNMw",
737 umulh_3 = "9bc07c00DNMx",
738
739 udiv_3 = "1ac00800DNMg",
740 sdiv_3 = "1ac00c00DNMg",
741
742 -- Bit operations.
743 sbfm_4 = "13000000DN12w|93400000DN12x",
744 bfm_4 = "33000000DN12w|b3400000DN12x",
745 ubfm_4 = "53000000DN12w|d3400000DN12x",
746 extr_4 = "13800000DNM2w|93c00000DNM2x",
747
748 sxtb_2 = "13001c00DNw|93401c00DNx",
749 sxth_2 = "13003c00DNw|93403c00DNx",
750 sxtw_2 = "93407c00DxNw",
751 uxtb_2 = "53001c00DNw",
752 uxth_2 = "53003c00DNw",
753
754 sbfx_4 = op_alias("sbfm_4", alias_bfx),
755 bfxil_4 = op_alias("bfm_4", alias_bfx),
756 ubfx_4 = op_alias("ubfm_4", alias_bfx),
757 sbfiz_4 = op_alias("sbfm_4", alias_bfiz),
758 bfi_4 = op_alias("bfm_4", alias_bfiz),
759 ubfiz_4 = op_alias("ubfm_4", alias_bfiz),
760
761 lsl_3 = function(params, nparams)
762 if params and params[3]:byte() == 35 then
763 return alias_lslimm(params, nparams)
764 else
765 return op_template(params, "1ac02000DNMg", nparams)
766 end
767 end,
768 lsr_3 = "1ac02400DNMg|53007c00DN1w|d340fc00DN1x",
769 asr_3 = "1ac02800DNMg|13007c00DN1w|9340fc00DN1x",
770 ror_3 = "1ac02c00DNMg|13800000DNm2w|93c00000DNm2x",
771
772 clz_2 = "5ac01000DNg",
773 cls_2 = "5ac01400DNg",
774 rbit_2 = "5ac00000DNg",
775 rev_2 = "5ac00800DNw|dac00c00DNx",
776 rev16_2 = "5ac00400DNg",
777 rev32_2 = "dac00800DNx",
778
779 -- Loads and stores.
780 ["strb_*"] = "38000000DwL",
781 ["ldrb_*"] = "38400000DwL",
782 ["ldrsb_*"] = "38c00000DwL|38800000DxL",
783 ["strh_*"] = "78000000DwL",
784 ["ldrh_*"] = "78400000DwL",
785 ["ldrsh_*"] = "78c00000DwL|78800000DxL",
786 ["str_*"] = "b8000000DwL|f8000000DxL|bc000000DsL|fc000000DdL",
787 ["ldr_*"] = "18000000DwB|58000000DxB|1c000000DsB|5c000000DdB|b8400000DwL|f8400000DxL|bc400000DsL|fc400000DdL",
788 ["ldrsw_*"] = "98000000DxB|b8800000DxL",
789 -- NOTE: ldur etc. are handled by ldr et al.
790
791 ["stp_*"] = "28000000DAwP|a8000000DAxP|2c000000DAsP|6c000000DAdP",
792 ["ldp_*"] = "28400000DAwP|a8400000DAxP|2c400000DAsP|6c400000DAdP",
793 ["ldpsw_*"] = "68400000DAxP",
794
795 -- Branches.
796 b_1 = "14000000B",
797 bl_1 = "94000000B",
798 blr_1 = "d63f0000Nx",
799 br_1 = "d61f0000Nx",
800 ret_0 = "d65f03c0",
801 ret_1 = "d65f0000Nx",
802 -- b.cond is added below.
803 cbz_2 = "34000000DBg",
804 cbnz_2 = "35000000DBg",
805 tbz_3 = "36000000DTBw|36000000DTBx",
806 tbnz_3 = "37000000DTBw|37000000DTBx",
807
808 -- Miscellaneous instructions.
809 -- TODO: hlt, hvc, smc, svc, eret, dcps[123], drps, mrs, msr
810 -- TODO: sys, sysl, ic, dc, at, tlbi
811 -- TODO: hint, yield, wfe, wfi, sev, sevl
812 -- TODO: clrex, dsb, dmb, isb
813 nop_0 = "d503201f",
814 brk_0 = "d4200000",
815 brk_1 = "d4200000W",
816
817 -- Floating point instructions.
818 fmov_2 = "1e204000DNf|1e260000DwNs|1e270000DsNw|9e660000DxNd|9e670000DdNx|1e201000DFf",
819 fabs_2 = "1e20c000DNf",
820 fneg_2 = "1e214000DNf",
821 fsqrt_2 = "1e21c000DNf",
822
823 fcvt_2 = "1e22c000DdNs|1e624000DsNd",
824
825 -- TODO: half-precision and fixed-point conversions.
826 fcvtas_2 = "1e240000DwNs|9e240000DxNs|1e640000DwNd|9e640000DxNd",
827 fcvtau_2 = "1e250000DwNs|9e250000DxNs|1e650000DwNd|9e650000DxNd",
828 fcvtms_2 = "1e300000DwNs|9e300000DxNs|1e700000DwNd|9e700000DxNd",
829 fcvtmu_2 = "1e310000DwNs|9e310000DxNs|1e710000DwNd|9e710000DxNd",
830 fcvtns_2 = "1e200000DwNs|9e200000DxNs|1e600000DwNd|9e600000DxNd",
831 fcvtnu_2 = "1e210000DwNs|9e210000DxNs|1e610000DwNd|9e610000DxNd",
832 fcvtps_2 = "1e280000DwNs|9e280000DxNs|1e680000DwNd|9e680000DxNd",
833 fcvtpu_2 = "1e290000DwNs|9e290000DxNs|1e690000DwNd|9e690000DxNd",
834 fcvtzs_2 = "1e380000DwNs|9e380000DxNs|1e780000DwNd|9e780000DxNd",
835 fcvtzu_2 = "1e390000DwNs|9e390000DxNs|1e790000DwNd|9e790000DxNd",
836
837 scvtf_2 = "1e220000DsNw|9e220000DsNx|1e620000DdNw|9e620000DdNx",
838 ucvtf_2 = "1e230000DsNw|9e230000DsNx|1e630000DdNw|9e630000DdNx",
839
840 frintn_2 = "1e244000DNf",
841 frintp_2 = "1e24c000DNf",
842 frintm_2 = "1e254000DNf",
843 frintz_2 = "1e25c000DNf",
844 frinta_2 = "1e264000DNf",
845 frintx_2 = "1e274000DNf",
846 frinti_2 = "1e27c000DNf",
847
848 fadd_3 = "1e202800DNMf",
849 fsub_3 = "1e203800DNMf",
850 fmul_3 = "1e200800DNMf",
851 fnmul_3 = "1e208800DNMf",
852 fdiv_3 = "1e201800DNMf",
853
854 fmadd_4 = "1f000000DNMAf",
855 fmsub_4 = "1f008000DNMAf",
856 fnmadd_4 = "1f200000DNMAf",
857 fnmsub_4 = "1f208000DNMAf",
858
859 fmax_3 = "1e204800DNMf",
860 fmaxnm_3 = "1e206800DNMf",
861 fmin_3 = "1e205800DNMf",
862 fminnm_3 = "1e207800DNMf",
863
864 fcmp_2 = "1e202000NMf|1e202008NZf",
865 fcmpe_2 = "1e202010NMf|1e202018NZf",
866
867 fccmp_4 = "1e200400NMVCf",
868 fccmpe_4 = "1e200410NMVCf",
869
870 fcsel_4 = "1e200c00DNMCf",
871
872 -- TODO: crc32*, aes*, sha*, pmull
873 -- TODO: SIMD instructions.
874}
875
876for cond,c in pairs(map_cond) do
877 map_op["b"..cond.."_1"] = tohex(0x54000000+c).."B"
878end
879
880------------------------------------------------------------------------------
881
882-- Handle opcodes defined with template strings.
883local function parse_template(params, template, nparams, pos)
884 local op = tonumber(sub(template, 1, 8), 16)
885 local n = 1
886 local rtt = {}
887
888 parse_reg_type = false
889
890 -- Process each character.
891 for p in gmatch(sub(template, 9), ".") do
892 local q = params[n]
893 if p == "D" then
894 op = op + parse_reg(q); n = n + 1
895 elseif p == "N" then
896 op = op + shl(parse_reg(q), 5); n = n + 1
897 elseif p == "M" then
898 op = op + shl(parse_reg(q), 16); n = n + 1
899 elseif p == "A" then
900 op = op + shl(parse_reg(q), 10); n = n + 1
901 elseif p == "m" then
902 op = op + shl(parse_reg(params[n-1]), 16)
903
904 elseif p == "p" then
905 if q == "sp" then params[n] = "@x31" end
906 elseif p == "g" then
907 if parse_reg_type == "x" then
908 op = op + 0x80000000
909 elseif parse_reg_type ~= "w" then
910 werror("bad register type")
911 end
912 parse_reg_type = false
913 elseif p == "f" then
914 if parse_reg_type == "d" then
915 op = op + 0x00400000
916 elseif parse_reg_type ~= "s" then
917 werror("bad register type")
918 end
919 parse_reg_type = false
920 elseif p == "x" or p == "w" or p == "d" or p == "s" then
921 if parse_reg_type ~= p then
922 werror("register size mismatch")
923 end
924 parse_reg_type = false
925
926 elseif p == "L" then
927 op = parse_load(params, nparams, n, op)
928 elseif p == "P" then
929 op = parse_load_pair(params, nparams, n, op)
930
931 elseif p == "B" then
932 local mode, v, s = parse_label(q, false); n = n + 1
933 local m = branch_type(op)
934 waction("REL_"..mode, v+m, s, 1)
935
936 elseif p == "I" then
937 op = op + parse_imm12(q); n = n + 1
938 elseif p == "i" then
939 op = op + parse_imm13(q); n = n + 1
940 elseif p == "W" then
941 op = op + parse_imm(q, 16, 5, 0, false); n = n + 1
942 elseif p == "T" then
943 op = op + parse_imm6(q); n = n + 1
944 elseif p == "1" then
945 op = op + parse_imm(q, 6, 16, 0, false); n = n + 1
946 elseif p == "2" then
947 op = op + parse_imm(q, 6, 10, 0, false); n = n + 1
948 elseif p == "5" then
949 op = op + parse_imm(q, 5, 16, 0, false); n = n + 1
950 elseif p == "V" then
951 op = op + parse_imm(q, 4, 0, 0, false); n = n + 1
952 elseif p == "F" then
953 op = op + parse_fpimm(q); n = n + 1
954 elseif p == "Z" then
955 if q ~= "#0" and q ~= "#0.0" then werror("expected zero immediate") end
956 n = n + 1
957
958 elseif p == "S" then
959 op = op + parse_shift(q); n = n + 1
960 elseif p == "X" then
961 op = op + parse_extend(q); n = n + 1
962 elseif p == "R" then
963 op = op + parse_lslx16(q); n = n + 1
964 elseif p == "C" then
965 op = op + parse_cond(q, 0); n = n + 1
966 elseif p == "c" then
967 op = op + parse_cond(q, 1); n = n + 1
968
969 else
970 assert(false)
971 end
972 end
973 wputpos(pos, op)
974end
975
976function op_template(params, template, nparams)
977 if not params then return template:gsub("%x%x%x%x%x%x%x%x", "") end
978
979 -- Limit number of section buffer positions used by a single dasm_put().
980 -- A single opcode needs a maximum of 3 positions.
981 if secpos+3 > maxsecpos then wflush() end
982 local pos = wpos()
983 local lpos, apos, spos = #actlist, #actargs, secpos
984
985 local ok, err
986 for t in gmatch(template, "[^|]+") do
987 ok, err = pcall(parse_template, params, t, nparams, pos)
988 if ok then return end
989 secpos = spos
990 actlist[lpos+1] = nil
991 actlist[lpos+2] = nil
992 actlist[lpos+3] = nil
993 actargs[apos+1] = nil
994 actargs[apos+2] = nil
995 actargs[apos+3] = nil
996 end
997 error(err, 0)
998end
999
1000map_op[".template__"] = op_template
1001
1002------------------------------------------------------------------------------
1003
1004-- Pseudo-opcode to mark the position where the action list is to be emitted.
1005map_op[".actionlist_1"] = function(params)
1006 if not params then return "cvar" end
1007 local name = params[1] -- No syntax check. You get to keep the pieces.
1008 wline(function(out) writeactions(out, name) end)
1009end
1010
1011-- Pseudo-opcode to mark the position where the global enum is to be emitted.
1012map_op[".globals_1"] = function(params)
1013 if not params then return "prefix" end
1014 local prefix = params[1] -- No syntax check. You get to keep the pieces.
1015 wline(function(out) writeglobals(out, prefix) end)
1016end
1017
1018-- Pseudo-opcode to mark the position where the global names are to be emitted.
1019map_op[".globalnames_1"] = function(params)
1020 if not params then return "cvar" end
1021 local name = params[1] -- No syntax check. You get to keep the pieces.
1022 wline(function(out) writeglobalnames(out, name) end)
1023end
1024
1025-- Pseudo-opcode to mark the position where the extern names are to be emitted.
1026map_op[".externnames_1"] = function(params)
1027 if not params then return "cvar" end
1028 local name = params[1] -- No syntax check. You get to keep the pieces.
1029 wline(function(out) writeexternnames(out, name) end)
1030end
1031
1032------------------------------------------------------------------------------
1033
1034-- Label pseudo-opcode (converted from trailing colon form).
1035map_op[".label_1"] = function(params)
1036 if not params then return "[1-9] | ->global | =>pcexpr" end
1037 if secpos+1 > maxsecpos then wflush() end
1038 local mode, n, s = parse_label(params[1], true)
1039 if mode == "EXT" then werror("bad label definition") end
1040 waction("LABEL_"..mode, n, s, 1)
1041end
1042
1043------------------------------------------------------------------------------
1044
1045-- Pseudo-opcodes for data storage.
1046map_op[".long_*"] = function(params)
1047 if not params then return "imm..." end
1048 for _,p in ipairs(params) do
1049 local n = tonumber(p)
1050 if not n then werror("bad immediate `"..p.."'") end
1051 if n < 0 then n = n + 2^32 end
1052 wputw(n)
1053 if secpos+2 > maxsecpos then wflush() end
1054 end
1055end
1056
1057-- Alignment pseudo-opcode.
1058map_op[".align_1"] = function(params)
1059 if not params then return "numpow2" end
1060 if secpos+1 > maxsecpos then wflush() end
1061 local align = tonumber(params[1])
1062 if align then
1063 local x = align
1064 -- Must be a power of 2 in the range (2 ... 256).
1065 for i=1,8 do
1066 x = x / 2
1067 if x == 1 then
1068 waction("ALIGN", align-1, nil, 1) -- Action byte is 2**n-1.
1069 return
1070 end
1071 end
1072 end
1073 werror("bad alignment")
1074end
1075
1076------------------------------------------------------------------------------
1077
1078-- Pseudo-opcode for (primitive) type definitions (map to C types).
1079map_op[".type_3"] = function(params, nparams)
1080 if not params then
1081 return nparams == 2 and "name, ctype" or "name, ctype, reg"
1082 end
1083 local name, ctype, reg = params[1], params[2], params[3]
1084 if not match(name, "^[%a_][%w_]*$") then
1085 werror("bad type name `"..name.."'")
1086 end
1087 local tp = map_type[name]
1088 if tp then
1089 werror("duplicate type `"..name.."'")
1090 end
1091 -- Add #type to defines. A bit unclean to put it in map_archdef.
1092 map_archdef["#"..name] = "sizeof("..ctype..")"
1093 -- Add new type and emit shortcut define.
1094 local num = ctypenum + 1
1095 map_type[name] = {
1096 ctype = ctype,
1097 ctypefmt = format("Dt%X(%%s)", num),
1098 reg = reg,
1099 }
1100 wline(format("#define Dt%X(_V) (int)(ptrdiff_t)&(((%s *)0)_V)", num, ctype))
1101 ctypenum = num
1102end
1103map_op[".type_2"] = map_op[".type_3"]
1104
1105-- Dump type definitions.
1106local function dumptypes(out, lvl)
1107 local t = {}
1108 for name in pairs(map_type) do t[#t+1] = name end
1109 sort(t)
1110 out:write("Type definitions:\n")
1111 for _,name in ipairs(t) do
1112 local tp = map_type[name]
1113 local reg = tp.reg or ""
1114 out:write(format(" %-20s %-20s %s\n", name, tp.ctype, reg))
1115 end
1116 out:write("\n")
1117end
1118
1119------------------------------------------------------------------------------
1120
1121-- Set the current section.
1122function _M.section(num)
1123 waction("SECTION", num)
1124 wflush(true) -- SECTION is a terminal action.
1125end
1126
1127------------------------------------------------------------------------------
1128
1129-- Dump architecture description.
1130function _M.dumparch(out)
1131 out:write(format("DynASM %s version %s, released %s\n\n",
1132 _info.arch, _info.version, _info.release))
1133 dumpactions(out)
1134end
1135
1136-- Dump all user defined elements.
1137function _M.dumpdef(out, lvl)
1138 dumptypes(out, lvl)
1139 dumpglobals(out, lvl)
1140 dumpexterns(out, lvl)
1141end
1142
1143------------------------------------------------------------------------------
1144
1145-- Pass callbacks from/to the DynASM core.
1146function _M.passcb(wl, we, wf, ww)
1147 wline, werror, wfatal, wwarn = wl, we, wf, ww
1148 return wflush
1149end
1150
1151-- Setup the arch-specific module.
1152function _M.setup(arch, opt)
1153 g_arch, g_opt = arch, opt
1154end
1155
1156-- Merge the core maps and the arch-specific maps.
1157function _M.mergemaps(map_coreop, map_def)
1158 setmetatable(map_op, { __index = map_coreop })
1159 setmetatable(map_def, { __index = map_archdef })
1160 return map_op, map_def
1161end
1162
1163return _M
1164
1165------------------------------------------------------------------------------
1166
diff --git a/dynasm/dasm_mips.h b/dynasm/dasm_mips.h
index 1b309edd..143c3cbe 100644
--- a/dynasm/dasm_mips.h
+++ b/dynasm/dasm_mips.h
@@ -21,7 +21,7 @@ enum {
21 /* The following actions need a buffer position. */ 21 /* The following actions need a buffer position. */
22 DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, 22 DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG,
23 /* The following actions also have an argument. */ 23 /* The following actions also have an argument. */
24 DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, 24 DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, DASM_IMMS,
25 DASM__MAX 25 DASM__MAX
26}; 26};
27 27
@@ -231,7 +231,7 @@ void dasm_put(Dst_DECL, int start, ...)
231 *pl = -pos; /* Label exists now. */ 231 *pl = -pos; /* Label exists now. */
232 b[pos++] = ofs; /* Store pass1 offset estimate. */ 232 b[pos++] = ofs; /* Store pass1 offset estimate. */
233 break; 233 break;
234 case DASM_IMM: 234 case DASM_IMM: case DASM_IMMS:
235#ifdef DASM_CHECKS 235#ifdef DASM_CHECKS
236 CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I); 236 CK((n & ((1<<((ins>>10)&31))-1)) == 0, RANGE_I);
237#endif 237#endif
@@ -299,7 +299,7 @@ int dasm_link(Dst_DECL, size_t *szp)
299 case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; 299 case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break;
300 case DASM_REL_LG: case DASM_REL_PC: pos++; break; 300 case DASM_REL_LG: case DASM_REL_PC: pos++; break;
301 case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; 301 case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break;
302 case DASM_IMM: pos++; break; 302 case DASM_IMM: case DASM_IMMS: pos++; break;
303 } 303 }
304 } 304 }
305 stop: (void)0; 305 stop: (void)0;
@@ -355,19 +355,23 @@ int dasm_encode(Dst_DECL, void *buffer)
355 CK(n >= 0, UNDEF_PC); 355 CK(n >= 0, UNDEF_PC);
356 n = *DASM_POS2PTR(D, n); 356 n = *DASM_POS2PTR(D, n);
357 if (ins & 2048) 357 if (ins & 2048)
358 n = n - (int)((char *)cp - base); 358 n = (n + (int)(size_t)base) & 0x0fffffff;
359 else 359 else
360 n = (n + (int)base) & 0x0fffffff; 360 n = n - (int)((char *)cp - base);
361 patchrel: 361 patchrel: {
362 unsigned int e = 16 + ((ins >> 12) & 15);
362 CK((n & 3) == 0 && 363 CK((n & 3) == 0 &&
363 ((n + ((ins & 2048) ? 0x00020000 : 0)) >> 364 ((n + ((ins & 2048) ? 0 : (1<<(e+1)))) >> (e+2)) == 0, RANGE_REL);
364 ((ins & 2048) ? 18 : 28)) == 0, RANGE_REL); 365 cp[-1] |= ((n>>2) & ((1<<e)-1));
365 cp[-1] |= ((n>>2) & ((ins & 2048) ? 0x0000ffff: 0x03ffffff)); 366 }
366 break; 367 break;
367 case DASM_LABEL_LG: 368 case DASM_LABEL_LG:
368 ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n); 369 ins &= 2047; if (ins >= 20) D->globals[ins-10] = (void *)(base + n);
369 break; 370 break;
370 case DASM_LABEL_PC: break; 371 case DASM_LABEL_PC: break;
372 case DASM_IMMS:
373 cp[-1] |= ((n>>3) & 4); n &= 0x1f;
374 /* fallthrough */
371 case DASM_IMM: 375 case DASM_IMM:
372 cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); 376 cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31);
373 break; 377 break;
diff --git a/dynasm/dasm_mips.lua b/dynasm/dasm_mips.lua
index 9ef280e3..3e41df52 100644
--- a/dynasm/dasm_mips.lua
+++ b/dynasm/dasm_mips.lua
@@ -1,17 +1,20 @@
1------------------------------------------------------------------------------ 1------------------------------------------------------------------------------
2-- DynASM MIPS module. 2-- DynASM MIPS32/MIPS64 module.
3-- 3--
4-- Copyright (C) 2005-2020 Mike Pall. All rights reserved. 4-- Copyright (C) 2005-2020 Mike Pall. All rights reserved.
5-- See dynasm.lua for full copyright notice. 5-- See dynasm.lua for full copyright notice.
6------------------------------------------------------------------------------ 6------------------------------------------------------------------------------
7 7
8local mips64 = mips64
9local mipsr6 = _map_def.MIPSR6
10
8-- Module information: 11-- Module information:
9local _info = { 12local _info = {
10 arch = "mips", 13 arch = mips64 and "mips64" or "mips",
11 description = "DynASM MIPS module", 14 description = "DynASM MIPS32/MIPS64 module",
12 version = "1.3.0", 15 version = "1.4.0",
13 vernum = 10300, 16 vernum = 10400,
14 release = "2012-01-23", 17 release = "2020-01-20",
15 author = "Mike Pall", 18 author = "Mike Pall",
16 license = "MIT", 19 license = "MIT",
17} 20}
@@ -27,7 +30,8 @@ local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char
27local match, gmatch = _s.match, _s.gmatch 30local match, gmatch = _s.match, _s.gmatch
28local concat, sort = table.concat, table.sort 31local concat, sort = table.concat, table.sort
29local bit = bit or require("bit") 32local bit = bit or require("bit")
30local band, shl, sar, tohex = bit.band, bit.lshift, bit.arshift, bit.tohex 33local band, shl, shr, sar = bit.band, bit.lshift, bit.rshift, bit.arshift
34local tohex = bit.tohex
31 35
32-- Inherited tables and callbacks. 36-- Inherited tables and callbacks.
33local g_opt, g_arch 37local g_opt, g_arch
@@ -38,7 +42,7 @@ local wline, werror, wfatal, wwarn
38local action_names = { 42local action_names = {
39 "STOP", "SECTION", "ESC", "REL_EXT", 43 "STOP", "SECTION", "ESC", "REL_EXT",
40 "ALIGN", "REL_LG", "LABEL_LG", 44 "ALIGN", "REL_LG", "LABEL_LG",
41 "REL_PC", "LABEL_PC", "IMM", 45 "REL_PC", "LABEL_PC", "IMM", "IMMS",
42} 46}
43 47
44-- Maximum number of section buffer positions for dasm_put(). 48-- Maximum number of section buffer positions for dasm_put().
@@ -235,7 +239,6 @@ local map_op = {
235 bne_3 = "14000000STB", 239 bne_3 = "14000000STB",
236 blez_2 = "18000000SB", 240 blez_2 = "18000000SB",
237 bgtz_2 = "1c000000SB", 241 bgtz_2 = "1c000000SB",
238 addi_3 = "20000000TSI",
239 li_2 = "24000000TI", 242 li_2 = "24000000TI",
240 addiu_3 = "24000000TSI", 243 addiu_3 = "24000000TSI",
241 slti_3 = "28000000TSI", 244 slti_3 = "28000000TSI",
@@ -245,70 +248,52 @@ local map_op = {
245 ori_3 = "34000000TSU", 248 ori_3 = "34000000TSU",
246 xori_3 = "38000000TSU", 249 xori_3 = "38000000TSU",
247 lui_2 = "3c000000TU", 250 lui_2 = "3c000000TU",
248 beqzl_2 = "50000000SB", 251 daddiu_3 = mips64 and "64000000TSI",
249 beql_3 = "50000000STB", 252 ldl_2 = mips64 and "68000000TO",
250 bnezl_2 = "54000000SB", 253 ldr_2 = mips64 and "6c000000TO",
251 bnel_3 = "54000000STB",
252 blezl_2 = "58000000SB",
253 bgtzl_2 = "5c000000SB",
254 lb_2 = "80000000TO", 254 lb_2 = "80000000TO",
255 lh_2 = "84000000TO", 255 lh_2 = "84000000TO",
256 lwl_2 = "88000000TO",
257 lw_2 = "8c000000TO", 256 lw_2 = "8c000000TO",
258 lbu_2 = "90000000TO", 257 lbu_2 = "90000000TO",
259 lhu_2 = "94000000TO", 258 lhu_2 = "94000000TO",
260 lwr_2 = "98000000TO", 259 lwu_2 = mips64 and "9c000000TO",
261 sb_2 = "a0000000TO", 260 sb_2 = "a0000000TO",
262 sh_2 = "a4000000TO", 261 sh_2 = "a4000000TO",
263 swl_2 = "a8000000TO",
264 sw_2 = "ac000000TO", 262 sw_2 = "ac000000TO",
265 swr_2 = "b8000000TO",
266 cache_2 = "bc000000NO",
267 ll_2 = "c0000000TO",
268 lwc1_2 = "c4000000HO", 263 lwc1_2 = "c4000000HO",
269 pref_2 = "cc000000NO",
270 ldc1_2 = "d4000000HO", 264 ldc1_2 = "d4000000HO",
271 sc_2 = "e0000000TO", 265 ld_2 = mips64 and "dc000000TO",
272 swc1_2 = "e4000000HO", 266 swc1_2 = "e4000000HO",
273 sdc1_2 = "f4000000HO", 267 sdc1_2 = "f4000000HO",
268 sd_2 = mips64 and "fc000000TO",
274 269
275 -- Opcode SPECIAL. 270 -- Opcode SPECIAL.
276 nop_0 = "00000000", 271 nop_0 = "00000000",
277 sll_3 = "00000000DTA", 272 sll_3 = "00000000DTA",
278 movf_2 = "00000001DS", 273 sextw_2 = "00000000DT",
279 movf_3 = "00000001DSC",
280 movt_2 = "00010001DS",
281 movt_3 = "00010001DSC",
282 srl_3 = "00000002DTA", 274 srl_3 = "00000002DTA",
283 rotr_3 = "00200002DTA", 275 rotr_3 = "00200002DTA",
284 sra_3 = "00000003DTA", 276 sra_3 = "00000003DTA",
285 sllv_3 = "00000004DTS", 277 sllv_3 = "00000004DTS",
286 srlv_3 = "00000006DTS", 278 srlv_3 = "00000006DTS",
287 rotrv_3 = "00000046DTS", 279 rotrv_3 = "00000046DTS",
280 drotrv_3 = mips64 and "00000056DTS",
288 srav_3 = "00000007DTS", 281 srav_3 = "00000007DTS",
289 jr_1 = "00000008S",
290 jalr_1 = "0000f809S", 282 jalr_1 = "0000f809S",
291 jalr_2 = "00000009DS", 283 jalr_2 = "00000009DS",
292 movz_3 = "0000000aDST",
293 movn_3 = "0000000bDST",
294 syscall_0 = "0000000c", 284 syscall_0 = "0000000c",
295 syscall_1 = "0000000cY", 285 syscall_1 = "0000000cY",
296 break_0 = "0000000d", 286 break_0 = "0000000d",
297 break_1 = "0000000dY", 287 break_1 = "0000000dY",
298 sync_0 = "0000000f", 288 sync_0 = "0000000f",
299 mfhi_1 = "00000010D", 289 dsllv_3 = mips64 and "00000014DTS",
300 mthi_1 = "00000011S", 290 dsrlv_3 = mips64 and "00000016DTS",
301 mflo_1 = "00000012D", 291 dsrav_3 = mips64 and "00000017DTS",
302 mtlo_1 = "00000013S",
303 mult_2 = "00000018ST",
304 multu_2 = "00000019ST",
305 div_2 = "0000001aST",
306 divu_2 = "0000001bST",
307 add_3 = "00000020DST", 292 add_3 = "00000020DST",
308 move_2 = "00000021DS", 293 move_2 = mips64 and "00000025DS" or "00000021DS",
309 addu_3 = "00000021DST", 294 addu_3 = "00000021DST",
310 sub_3 = "00000022DST", 295 sub_3 = "00000022DST",
311 negu_2 = "00000023DT", 296 negu_2 = mips64 and "0000002fDT" or "00000023DT",
312 subu_3 = "00000023DST", 297 subu_3 = "00000023DST",
313 and_3 = "00000024DST", 298 and_3 = "00000024DST",
314 or_3 = "00000025DST", 299 or_3 = "00000025DST",
@@ -317,6 +302,10 @@ local map_op = {
317 nor_3 = "00000027DST", 302 nor_3 = "00000027DST",
318 slt_3 = "0000002aDST", 303 slt_3 = "0000002aDST",
319 sltu_3 = "0000002bDST", 304 sltu_3 = "0000002bDST",
305 dadd_3 = mips64 and "0000002cDST",
306 daddu_3 = mips64 and "0000002dDST",
307 dsub_3 = mips64 and "0000002eDST",
308 dsubu_3 = mips64 and "0000002fDST",
320 tge_2 = "00000030ST", 309 tge_2 = "00000030ST",
321 tge_3 = "00000030STZ", 310 tge_3 = "00000030STZ",
322 tgeu_2 = "00000031ST", 311 tgeu_2 = "00000031ST",
@@ -329,40 +318,36 @@ local map_op = {
329 teq_3 = "00000034STZ", 318 teq_3 = "00000034STZ",
330 tne_2 = "00000036ST", 319 tne_2 = "00000036ST",
331 tne_3 = "00000036STZ", 320 tne_3 = "00000036STZ",
321 dsll_3 = mips64 and "00000038DTa",
322 dsrl_3 = mips64 and "0000003aDTa",
323 drotr_3 = mips64 and "0020003aDTa",
324 dsra_3 = mips64 and "0000003bDTa",
325 dsll32_3 = mips64 and "0000003cDTA",
326 dsrl32_3 = mips64 and "0000003eDTA",
327 drotr32_3 = mips64 and "0020003eDTA",
328 dsra32_3 = mips64 and "0000003fDTA",
332 329
333 -- Opcode REGIMM. 330 -- Opcode REGIMM.
334 bltz_2 = "04000000SB", 331 bltz_2 = "04000000SB",
335 bgez_2 = "04010000SB", 332 bgez_2 = "04010000SB",
336 bltzl_2 = "04020000SB", 333 bltzl_2 = "04020000SB",
337 bgezl_2 = "04030000SB", 334 bgezl_2 = "04030000SB",
338 tgei_2 = "04080000SI",
339 tgeiu_2 = "04090000SI",
340 tlti_2 = "040a0000SI",
341 tltiu_2 = "040b0000SI",
342 teqi_2 = "040c0000SI",
343 tnei_2 = "040e0000SI",
344 bltzal_2 = "04100000SB",
345 bal_1 = "04110000B", 335 bal_1 = "04110000B",
346 bgezal_2 = "04110000SB",
347 bltzall_2 = "04120000SB",
348 bgezall_2 = "04130000SB",
349 synci_1 = "041f0000O", 336 synci_1 = "041f0000O",
350 337
351 -- Opcode SPECIAL2.
352 madd_2 = "70000000ST",
353 maddu_2 = "70000001ST",
354 mul_3 = "70000002DST",
355 msub_2 = "70000004ST",
356 msubu_2 = "70000005ST",
357 clz_2 = "70000020DS=",
358 clo_2 = "70000021DS=",
359 sdbbp_0 = "7000003f",
360 sdbbp_1 = "7000003fY",
361
362 -- Opcode SPECIAL3. 338 -- Opcode SPECIAL3.
363 ext_4 = "7c000000TSAM", -- Note: last arg is msbd = size-1 339 ext_4 = "7c000000TSAM", -- Note: last arg is msbd = size-1
340 dextm_4 = mips64 and "7c000001TSAM", -- Args: pos | size-1-32
341 dextu_4 = mips64 and "7c000002TSAM", -- Args: pos-32 | size-1
342 dext_4 = mips64 and "7c000003TSAM", -- Args: pos | size-1
343 zextw_2 = mips64 and "7c00f803TS",
364 ins_4 = "7c000004TSAM", -- Note: last arg is msb = pos+size-1 344 ins_4 = "7c000004TSAM", -- Note: last arg is msb = pos+size-1
345 dinsm_4 = mips64 and "7c000005TSAM", -- Args: pos | pos+size-33
346 dinsu_4 = mips64 and "7c000006TSAM", -- Args: pos-32 | pos+size-33
347 dins_4 = mips64 and "7c000007TSAM", -- Args: pos | pos+size-1
365 wsbh_2 = "7c0000a0DT", 348 wsbh_2 = "7c0000a0DT",
349 dsbh_2 = mips64 and "7c0000a4DT",
350 dshd_2 = mips64 and "7c000164DT",
366 seb_2 = "7c000420DT", 351 seb_2 = "7c000420DT",
367 seh_2 = "7c000620DT", 352 seh_2 = "7c000620DT",
368 rdhwr_2 = "7c00003bTD", 353 rdhwr_2 = "7c00003bTD",
@@ -370,8 +355,12 @@ local map_op = {
370 -- Opcode COP0. 355 -- Opcode COP0.
371 mfc0_2 = "40000000TD", 356 mfc0_2 = "40000000TD",
372 mfc0_3 = "40000000TDW", 357 mfc0_3 = "40000000TDW",
358 dmfc0_2 = mips64 and "40200000TD",
359 dmfc0_3 = mips64 and "40200000TDW",
373 mtc0_2 = "40800000TD", 360 mtc0_2 = "40800000TD",
374 mtc0_3 = "40800000TDW", 361 mtc0_3 = "40800000TDW",
362 dmtc0_2 = mips64 and "40a00000TD",
363 dmtc0_3 = mips64 and "40a00000TDW",
375 rdpgpr_2 = "41400000DT", 364 rdpgpr_2 = "41400000DT",
376 di_0 = "41606000", 365 di_0 = "41606000",
377 di_1 = "41606000T", 366 di_1 = "41606000T",
@@ -388,21 +377,14 @@ local map_op = {
388 377
389 -- Opcode COP1. 378 -- Opcode COP1.
390 mfc1_2 = "44000000TG", 379 mfc1_2 = "44000000TG",
380 dmfc1_2 = mips64 and "44200000TG",
391 cfc1_2 = "44400000TG", 381 cfc1_2 = "44400000TG",
392 mfhc1_2 = "44600000TG", 382 mfhc1_2 = "44600000TG",
393 mtc1_2 = "44800000TG", 383 mtc1_2 = "44800000TG",
384 dmtc1_2 = mips64 and "44a00000TG",
394 ctc1_2 = "44c00000TG", 385 ctc1_2 = "44c00000TG",
395 mthc1_2 = "44e00000TG", 386 mthc1_2 = "44e00000TG",
396 387
397 bc1f_1 = "45000000B",
398 bc1f_2 = "45000000CB",
399 bc1t_1 = "45010000B",
400 bc1t_2 = "45010000CB",
401 bc1fl_1 = "45020000B",
402 bc1fl_2 = "45020000CB",
403 bc1tl_1 = "45030000B",
404 bc1tl_2 = "45030000CB",
405
406 ["add.s_3"] = "46000000FGH", 388 ["add.s_3"] = "46000000FGH",
407 ["sub.s_3"] = "46000001FGH", 389 ["sub.s_3"] = "46000001FGH",
408 ["mul.s_3"] = "46000002FGH", 390 ["mul.s_3"] = "46000002FGH",
@@ -419,51 +401,11 @@ local map_op = {
419 ["trunc.w.s_2"] = "4600000dFG", 401 ["trunc.w.s_2"] = "4600000dFG",
420 ["ceil.w.s_2"] = "4600000eFG", 402 ["ceil.w.s_2"] = "4600000eFG",
421 ["floor.w.s_2"] = "4600000fFG", 403 ["floor.w.s_2"] = "4600000fFG",
422 ["movf.s_2"] = "46000011FG",
423 ["movf.s_3"] = "46000011FGC",
424 ["movt.s_2"] = "46010011FG",
425 ["movt.s_3"] = "46010011FGC",
426 ["movz.s_3"] = "46000012FGT",
427 ["movn.s_3"] = "46000013FGT",
428 ["recip.s_2"] = "46000015FG", 404 ["recip.s_2"] = "46000015FG",
429 ["rsqrt.s_2"] = "46000016FG", 405 ["rsqrt.s_2"] = "46000016FG",
430 ["cvt.d.s_2"] = "46000021FG", 406 ["cvt.d.s_2"] = "46000021FG",
431 ["cvt.w.s_2"] = "46000024FG", 407 ["cvt.w.s_2"] = "46000024FG",
432 ["cvt.l.s_2"] = "46000025FG", 408 ["cvt.l.s_2"] = "46000025FG",
433 ["cvt.ps.s_3"] = "46000026FGH",
434 ["c.f.s_2"] = "46000030GH",
435 ["c.f.s_3"] = "46000030VGH",
436 ["c.un.s_2"] = "46000031GH",
437 ["c.un.s_3"] = "46000031VGH",
438 ["c.eq.s_2"] = "46000032GH",
439 ["c.eq.s_3"] = "46000032VGH",
440 ["c.ueq.s_2"] = "46000033GH",
441 ["c.ueq.s_3"] = "46000033VGH",
442 ["c.olt.s_2"] = "46000034GH",
443 ["c.olt.s_3"] = "46000034VGH",
444 ["c.ult.s_2"] = "46000035GH",
445 ["c.ult.s_3"] = "46000035VGH",
446 ["c.ole.s_2"] = "46000036GH",
447 ["c.ole.s_3"] = "46000036VGH",
448 ["c.ule.s_2"] = "46000037GH",
449 ["c.ule.s_3"] = "46000037VGH",
450 ["c.sf.s_2"] = "46000038GH",
451 ["c.sf.s_3"] = "46000038VGH",
452 ["c.ngle.s_2"] = "46000039GH",
453 ["c.ngle.s_3"] = "46000039VGH",
454 ["c.seq.s_2"] = "4600003aGH",
455 ["c.seq.s_3"] = "4600003aVGH",
456 ["c.ngl.s_2"] = "4600003bGH",
457 ["c.ngl.s_3"] = "4600003bVGH",
458 ["c.lt.s_2"] = "4600003cGH",
459 ["c.lt.s_3"] = "4600003cVGH",
460 ["c.nge.s_2"] = "4600003dGH",
461 ["c.nge.s_3"] = "4600003dVGH",
462 ["c.le.s_2"] = "4600003eGH",
463 ["c.le.s_3"] = "4600003eVGH",
464 ["c.ngt.s_2"] = "4600003fGH",
465 ["c.ngt.s_3"] = "4600003fVGH",
466
467 ["add.d_3"] = "46200000FGH", 409 ["add.d_3"] = "46200000FGH",
468 ["sub.d_3"] = "46200001FGH", 410 ["sub.d_3"] = "46200001FGH",
469 ["mul.d_3"] = "46200002FGH", 411 ["mul.d_3"] = "46200002FGH",
@@ -480,130 +422,410 @@ local map_op = {
480 ["trunc.w.d_2"] = "4620000dFG", 422 ["trunc.w.d_2"] = "4620000dFG",
481 ["ceil.w.d_2"] = "4620000eFG", 423 ["ceil.w.d_2"] = "4620000eFG",
482 ["floor.w.d_2"] = "4620000fFG", 424 ["floor.w.d_2"] = "4620000fFG",
483 ["movf.d_2"] = "46200011FG",
484 ["movf.d_3"] = "46200011FGC",
485 ["movt.d_2"] = "46210011FG",
486 ["movt.d_3"] = "46210011FGC",
487 ["movz.d_3"] = "46200012FGT",
488 ["movn.d_3"] = "46200013FGT",
489 ["recip.d_2"] = "46200015FG", 425 ["recip.d_2"] = "46200015FG",
490 ["rsqrt.d_2"] = "46200016FG", 426 ["rsqrt.d_2"] = "46200016FG",
491 ["cvt.s.d_2"] = "46200020FG", 427 ["cvt.s.d_2"] = "46200020FG",
492 ["cvt.w.d_2"] = "46200024FG", 428 ["cvt.w.d_2"] = "46200024FG",
493 ["cvt.l.d_2"] = "46200025FG", 429 ["cvt.l.d_2"] = "46200025FG",
494 ["c.f.d_2"] = "46200030GH",
495 ["c.f.d_3"] = "46200030VGH",
496 ["c.un.d_2"] = "46200031GH",
497 ["c.un.d_3"] = "46200031VGH",
498 ["c.eq.d_2"] = "46200032GH",
499 ["c.eq.d_3"] = "46200032VGH",
500 ["c.ueq.d_2"] = "46200033GH",
501 ["c.ueq.d_3"] = "46200033VGH",
502 ["c.olt.d_2"] = "46200034GH",
503 ["c.olt.d_3"] = "46200034VGH",
504 ["c.ult.d_2"] = "46200035GH",
505 ["c.ult.d_3"] = "46200035VGH",
506 ["c.ole.d_2"] = "46200036GH",
507 ["c.ole.d_3"] = "46200036VGH",
508 ["c.ule.d_2"] = "46200037GH",
509 ["c.ule.d_3"] = "46200037VGH",
510 ["c.sf.d_2"] = "46200038GH",
511 ["c.sf.d_3"] = "46200038VGH",
512 ["c.ngle.d_2"] = "46200039GH",
513 ["c.ngle.d_3"] = "46200039VGH",
514 ["c.seq.d_2"] = "4620003aGH",
515 ["c.seq.d_3"] = "4620003aVGH",
516 ["c.ngl.d_2"] = "4620003bGH",
517 ["c.ngl.d_3"] = "4620003bVGH",
518 ["c.lt.d_2"] = "4620003cGH",
519 ["c.lt.d_3"] = "4620003cVGH",
520 ["c.nge.d_2"] = "4620003dGH",
521 ["c.nge.d_3"] = "4620003dVGH",
522 ["c.le.d_2"] = "4620003eGH",
523 ["c.le.d_3"] = "4620003eVGH",
524 ["c.ngt.d_2"] = "4620003fGH",
525 ["c.ngt.d_3"] = "4620003fVGH",
526
527 ["add.ps_3"] = "46c00000FGH",
528 ["sub.ps_3"] = "46c00001FGH",
529 ["mul.ps_3"] = "46c00002FGH",
530 ["abs.ps_2"] = "46c00005FG",
531 ["mov.ps_2"] = "46c00006FG",
532 ["neg.ps_2"] = "46c00007FG",
533 ["movf.ps_2"] = "46c00011FG",
534 ["movf.ps_3"] = "46c00011FGC",
535 ["movt.ps_2"] = "46c10011FG",
536 ["movt.ps_3"] = "46c10011FGC",
537 ["movz.ps_3"] = "46c00012FGT",
538 ["movn.ps_3"] = "46c00013FGT",
539 ["cvt.s.pu_2"] = "46c00020FG",
540 ["cvt.s.pl_2"] = "46c00028FG",
541 ["pll.ps_3"] = "46c0002cFGH",
542 ["plu.ps_3"] = "46c0002dFGH",
543 ["pul.ps_3"] = "46c0002eFGH",
544 ["puu.ps_3"] = "46c0002fFGH",
545 ["c.f.ps_2"] = "46c00030GH",
546 ["c.f.ps_3"] = "46c00030VGH",
547 ["c.un.ps_2"] = "46c00031GH",
548 ["c.un.ps_3"] = "46c00031VGH",
549 ["c.eq.ps_2"] = "46c00032GH",
550 ["c.eq.ps_3"] = "46c00032VGH",
551 ["c.ueq.ps_2"] = "46c00033GH",
552 ["c.ueq.ps_3"] = "46c00033VGH",
553 ["c.olt.ps_2"] = "46c00034GH",
554 ["c.olt.ps_3"] = "46c00034VGH",
555 ["c.ult.ps_2"] = "46c00035GH",
556 ["c.ult.ps_3"] = "46c00035VGH",
557 ["c.ole.ps_2"] = "46c00036GH",
558 ["c.ole.ps_3"] = "46c00036VGH",
559 ["c.ule.ps_2"] = "46c00037GH",
560 ["c.ule.ps_3"] = "46c00037VGH",
561 ["c.sf.ps_2"] = "46c00038GH",
562 ["c.sf.ps_3"] = "46c00038VGH",
563 ["c.ngle.ps_2"] = "46c00039GH",
564 ["c.ngle.ps_3"] = "46c00039VGH",
565 ["c.seq.ps_2"] = "46c0003aGH",
566 ["c.seq.ps_3"] = "46c0003aVGH",
567 ["c.ngl.ps_2"] = "46c0003bGH",
568 ["c.ngl.ps_3"] = "46c0003bVGH",
569 ["c.lt.ps_2"] = "46c0003cGH",
570 ["c.lt.ps_3"] = "46c0003cVGH",
571 ["c.nge.ps_2"] = "46c0003dGH",
572 ["c.nge.ps_3"] = "46c0003dVGH",
573 ["c.le.ps_2"] = "46c0003eGH",
574 ["c.le.ps_3"] = "46c0003eVGH",
575 ["c.ngt.ps_2"] = "46c0003fGH",
576 ["c.ngt.ps_3"] = "46c0003fVGH",
577
578 ["cvt.s.w_2"] = "46800020FG", 430 ["cvt.s.w_2"] = "46800020FG",
579 ["cvt.d.w_2"] = "46800021FG", 431 ["cvt.d.w_2"] = "46800021FG",
580
581 ["cvt.s.l_2"] = "46a00020FG", 432 ["cvt.s.l_2"] = "46a00020FG",
582 ["cvt.d.l_2"] = "46a00021FG", 433 ["cvt.d.l_2"] = "46a00021FG",
583
584 -- Opcode COP1X.
585 lwxc1_2 = "4c000000FX",
586 ldxc1_2 = "4c000001FX",
587 luxc1_2 = "4c000005FX",
588 swxc1_2 = "4c000008FX",
589 sdxc1_2 = "4c000009FX",
590 suxc1_2 = "4c00000dFX",
591 prefx_2 = "4c00000fMX",
592 ["alnv.ps_4"] = "4c00001eFGHS",
593 ["madd.s_4"] = "4c000020FRGH",
594 ["madd.d_4"] = "4c000021FRGH",
595 ["madd.ps_4"] = "4c000026FRGH",
596 ["msub.s_4"] = "4c000028FRGH",
597 ["msub.d_4"] = "4c000029FRGH",
598 ["msub.ps_4"] = "4c00002eFRGH",
599 ["nmadd.s_4"] = "4c000030FRGH",
600 ["nmadd.d_4"] = "4c000031FRGH",
601 ["nmadd.ps_4"] = "4c000036FRGH",
602 ["nmsub.s_4"] = "4c000038FRGH",
603 ["nmsub.d_4"] = "4c000039FRGH",
604 ["nmsub.ps_4"] = "4c00003eFRGH",
605} 434}
606 435
436if mipsr6 then -- Instructions added with MIPSR6.
437
438 for k,v in pairs({
439
440 -- Add immediate to upper bits.
441 aui_3 = "3c000000TSI",
442 daui_3 = mips64 and "74000000TSI",
443 dahi_2 = mips64 and "04060000SI",
444 dati_2 = mips64 and "041e0000SI",
445
446 -- TODO: addiupc, auipc, aluipc, lwpc, lwupc, ldpc.
447
448 -- Compact branches.
449 blezalc_2 = "18000000TB", -- rt != 0.
450 bgezalc_2 = "18000000T=SB", -- rt != 0.
451 bgtzalc_2 = "1c000000TB", -- rt != 0.
452 bltzalc_2 = "1c000000T=SB", -- rt != 0.
453
454 blezc_2 = "58000000TB", -- rt != 0.
455 bgezc_2 = "58000000T=SB", -- rt != 0.
456 bgec_3 = "58000000STB", -- rs != rt.
457 blec_3 = "58000000TSB", -- rt != rs.
458
459 bgtzc_2 = "5c000000TB", -- rt != 0.
460 bltzc_2 = "5c000000T=SB", -- rt != 0.
461 bltc_3 = "5c000000STB", -- rs != rt.
462 bgtc_3 = "5c000000TSB", -- rt != rs.
463
464 bgeuc_3 = "18000000STB", -- rs != rt.
465 bleuc_3 = "18000000TSB", -- rt != rs.
466 bltuc_3 = "1c000000STB", -- rs != rt.
467 bgtuc_3 = "1c000000TSB", -- rt != rs.
468
469 beqzalc_2 = "20000000TB", -- rt != 0.
470 bnezalc_2 = "60000000TB", -- rt != 0.
471 beqc_3 = "20000000STB", -- rs < rt.
472 bnec_3 = "60000000STB", -- rs < rt.
473 bovc_3 = "20000000STB", -- rs >= rt.
474 bnvc_3 = "60000000STB", -- rs >= rt.
475
476 beqzc_2 = "d8000000SK", -- rs != 0.
477 bnezc_2 = "f8000000SK", -- rs != 0.
478 jic_2 = "d8000000TI",
479 jialc_2 = "f8000000TI",
480 bc_1 = "c8000000L",
481 balc_1 = "e8000000L",
482
483 -- Opcode SPECIAL.
484 jr_1 = "00000009S",
485 sdbbp_0 = "0000000e",
486 sdbbp_1 = "0000000eY",
487 lsa_4 = "00000005DSTA",
488 dlsa_4 = mips64 and "00000015DSTA",
489 seleqz_3 = "00000035DST",
490 selnez_3 = "00000037DST",
491 clz_2 = "00000050DS",
492 clo_2 = "00000051DS",
493 dclz_2 = mips64 and "00000052DS",
494 dclo_2 = mips64 and "00000053DS",
495 mul_3 = "00000098DST",
496 muh_3 = "000000d8DST",
497 mulu_3 = "00000099DST",
498 muhu_3 = "000000d9DST",
499 div_3 = "0000009aDST",
500 mod_3 = "000000daDST",
501 divu_3 = "0000009bDST",
502 modu_3 = "000000dbDST",
503 dmul_3 = mips64 and "0000009cDST",
504 dmuh_3 = mips64 and "000000dcDST",
505 dmulu_3 = mips64 and "0000009dDST",
506 dmuhu_3 = mips64 and "000000ddDST",
507 ddiv_3 = mips64 and "0000009eDST",
508 dmod_3 = mips64 and "000000deDST",
509 ddivu_3 = mips64 and "0000009fDST",
510 dmodu_3 = mips64 and "000000dfDST",
511
512 -- Opcode SPECIAL3.
513 align_4 = "7c000220DSTA",
514 dalign_4 = mips64 and "7c000224DSTA",
515 bitswap_2 = "7c000020DT",
516 dbitswap_2 = mips64 and "7c000024DT",
517
518 -- Opcode COP1.
519 bc1eqz_2 = "45200000HB",
520 bc1nez_2 = "45a00000HB",
521
522 ["sel.s_3"] = "46000010FGH",
523 ["seleqz.s_3"] = "46000014FGH",
524 ["selnez.s_3"] = "46000017FGH",
525 ["maddf.s_3"] = "46000018FGH",
526 ["msubf.s_3"] = "46000019FGH",
527 ["rint.s_2"] = "4600001aFG",
528 ["class.s_2"] = "4600001bFG",
529 ["min.s_3"] = "4600001cFGH",
530 ["mina.s_3"] = "4600001dFGH",
531 ["max.s_3"] = "4600001eFGH",
532 ["maxa.s_3"] = "4600001fFGH",
533 ["cmp.af.s_3"] = "46800000FGH",
534 ["cmp.un.s_3"] = "46800001FGH",
535 ["cmp.or.s_3"] = "46800011FGH",
536 ["cmp.eq.s_3"] = "46800002FGH",
537 ["cmp.une.s_3"] = "46800012FGH",
538 ["cmp.ueq.s_3"] = "46800003FGH",
539 ["cmp.ne.s_3"] = "46800013FGH",
540 ["cmp.lt.s_3"] = "46800004FGH",
541 ["cmp.ult.s_3"] = "46800005FGH",
542 ["cmp.le.s_3"] = "46800006FGH",
543 ["cmp.ule.s_3"] = "46800007FGH",
544 ["cmp.saf.s_3"] = "46800008FGH",
545 ["cmp.sun.s_3"] = "46800009FGH",
546 ["cmp.sor.s_3"] = "46800019FGH",
547 ["cmp.seq.s_3"] = "4680000aFGH",
548 ["cmp.sune.s_3"] = "4680001aFGH",
549 ["cmp.sueq.s_3"] = "4680000bFGH",
550 ["cmp.sne.s_3"] = "4680001bFGH",
551 ["cmp.slt.s_3"] = "4680000cFGH",
552 ["cmp.sult.s_3"] = "4680000dFGH",
553 ["cmp.sle.s_3"] = "4680000eFGH",
554 ["cmp.sule.s_3"] = "4680000fFGH",
555
556 ["sel.d_3"] = "46200010FGH",
557 ["seleqz.d_3"] = "46200014FGH",
558 ["selnez.d_3"] = "46200017FGH",
559 ["maddf.d_3"] = "46200018FGH",
560 ["msubf.d_3"] = "46200019FGH",
561 ["rint.d_2"] = "4620001aFG",
562 ["class.d_2"] = "4620001bFG",
563 ["min.d_3"] = "4620001cFGH",
564 ["mina.d_3"] = "4620001dFGH",
565 ["max.d_3"] = "4620001eFGH",
566 ["maxa.d_3"] = "4620001fFGH",
567 ["cmp.af.d_3"] = "46a00000FGH",
568 ["cmp.un.d_3"] = "46a00001FGH",
569 ["cmp.or.d_3"] = "46a00011FGH",
570 ["cmp.eq.d_3"] = "46a00002FGH",
571 ["cmp.une.d_3"] = "46a00012FGH",
572 ["cmp.ueq.d_3"] = "46a00003FGH",
573 ["cmp.ne.d_3"] = "46a00013FGH",
574 ["cmp.lt.d_3"] = "46a00004FGH",
575 ["cmp.ult.d_3"] = "46a00005FGH",
576 ["cmp.le.d_3"] = "46a00006FGH",
577 ["cmp.ule.d_3"] = "46a00007FGH",
578 ["cmp.saf.d_3"] = "46a00008FGH",
579 ["cmp.sun.d_3"] = "46a00009FGH",
580 ["cmp.sor.d_3"] = "46a00019FGH",
581 ["cmp.seq.d_3"] = "46a0000aFGH",
582 ["cmp.sune.d_3"] = "46a0001aFGH",
583 ["cmp.sueq.d_3"] = "46a0000bFGH",
584 ["cmp.sne.d_3"] = "46a0001bFGH",
585 ["cmp.slt.d_3"] = "46a0000cFGH",
586 ["cmp.sult.d_3"] = "46a0000dFGH",
587 ["cmp.sle.d_3"] = "46a0000eFGH",
588 ["cmp.sule.d_3"] = "46a0000fFGH",
589
590 }) do map_op[k] = v end
591
592else -- Instructions removed by MIPSR6.
593
594 for k,v in pairs({
595 -- Traps, don't use.
596 addi_3 = "20000000TSI",
597 daddi_3 = mips64 and "60000000TSI",
598
599 -- Branch on likely, don't use.
600 beqzl_2 = "50000000SB",
601 beql_3 = "50000000STB",
602 bnezl_2 = "54000000SB",
603 bnel_3 = "54000000STB",
604 blezl_2 = "58000000SB",
605 bgtzl_2 = "5c000000SB",
606
607 lwl_2 = "88000000TO",
608 lwr_2 = "98000000TO",
609 swl_2 = "a8000000TO",
610 sdl_2 = mips64 and "b0000000TO",
611 sdr_2 = mips64 and "b1000000TO",
612 swr_2 = "b8000000TO",
613 cache_2 = "bc000000NO",
614 ll_2 = "c0000000TO",
615 pref_2 = "cc000000NO",
616 sc_2 = "e0000000TO",
617 scd_2 = mips64 and "f0000000TO",
618
619 -- Opcode SPECIAL.
620 movf_2 = "00000001DS",
621 movf_3 = "00000001DSC",
622 movt_2 = "00010001DS",
623 movt_3 = "00010001DSC",
624 jr_1 = "00000008S",
625 movz_3 = "0000000aDST",
626 movn_3 = "0000000bDST",
627 mfhi_1 = "00000010D",
628 mthi_1 = "00000011S",
629 mflo_1 = "00000012D",
630 mtlo_1 = "00000013S",
631 mult_2 = "00000018ST",
632 multu_2 = "00000019ST",
633 div_3 = "0000001aST",
634 divu_3 = "0000001bST",
635 ddiv_3 = mips64 and "0000001eST",
636 ddivu_3 = mips64 and "0000001fST",
637 dmult_2 = mips64 and "0000001cST",
638 dmultu_2 = mips64 and "0000001dST",
639
640 -- Opcode REGIMM.
641 tgei_2 = "04080000SI",
642 tgeiu_2 = "04090000SI",
643 tlti_2 = "040a0000SI",
644 tltiu_2 = "040b0000SI",
645 teqi_2 = "040c0000SI",
646 tnei_2 = "040e0000SI",
647 bltzal_2 = "04100000SB",
648 bgezal_2 = "04110000SB",
649 bltzall_2 = "04120000SB",
650 bgezall_2 = "04130000SB",
651
652 -- Opcode SPECIAL2.
653 madd_2 = "70000000ST",
654 maddu_2 = "70000001ST",
655 mul_3 = "70000002DST",
656 msub_2 = "70000004ST",
657 msubu_2 = "70000005ST",
658 clz_2 = "70000020D=TS",
659 clo_2 = "70000021D=TS",
660 dclz_2 = mips64 and "70000024D=TS",
661 dclo_2 = mips64 and "70000025D=TS",
662 sdbbp_0 = "7000003f",
663 sdbbp_1 = "7000003fY",
664
665 -- Opcode COP1.
666 bc1f_1 = "45000000B",
667 bc1f_2 = "45000000CB",
668 bc1t_1 = "45010000B",
669 bc1t_2 = "45010000CB",
670 bc1fl_1 = "45020000B",
671 bc1fl_2 = "45020000CB",
672 bc1tl_1 = "45030000B",
673 bc1tl_2 = "45030000CB",
674
675 ["movf.s_2"] = "46000011FG",
676 ["movf.s_3"] = "46000011FGC",
677 ["movt.s_2"] = "46010011FG",
678 ["movt.s_3"] = "46010011FGC",
679 ["movz.s_3"] = "46000012FGT",
680 ["movn.s_3"] = "46000013FGT",
681 ["cvt.ps.s_3"] = "46000026FGH",
682 ["c.f.s_2"] = "46000030GH",
683 ["c.f.s_3"] = "46000030VGH",
684 ["c.un.s_2"] = "46000031GH",
685 ["c.un.s_3"] = "46000031VGH",
686 ["c.eq.s_2"] = "46000032GH",
687 ["c.eq.s_3"] = "46000032VGH",
688 ["c.ueq.s_2"] = "46000033GH",
689 ["c.ueq.s_3"] = "46000033VGH",
690 ["c.olt.s_2"] = "46000034GH",
691 ["c.olt.s_3"] = "46000034VGH",
692 ["c.ult.s_2"] = "46000035GH",
693 ["c.ult.s_3"] = "46000035VGH",
694 ["c.ole.s_2"] = "46000036GH",
695 ["c.ole.s_3"] = "46000036VGH",
696 ["c.ule.s_2"] = "46000037GH",
697 ["c.ule.s_3"] = "46000037VGH",
698 ["c.sf.s_2"] = "46000038GH",
699 ["c.sf.s_3"] = "46000038VGH",
700 ["c.ngle.s_2"] = "46000039GH",
701 ["c.ngle.s_3"] = "46000039VGH",
702 ["c.seq.s_2"] = "4600003aGH",
703 ["c.seq.s_3"] = "4600003aVGH",
704 ["c.ngl.s_2"] = "4600003bGH",
705 ["c.ngl.s_3"] = "4600003bVGH",
706 ["c.lt.s_2"] = "4600003cGH",
707 ["c.lt.s_3"] = "4600003cVGH",
708 ["c.nge.s_2"] = "4600003dGH",
709 ["c.nge.s_3"] = "4600003dVGH",
710 ["c.le.s_2"] = "4600003eGH",
711 ["c.le.s_3"] = "4600003eVGH",
712 ["c.ngt.s_2"] = "4600003fGH",
713 ["c.ngt.s_3"] = "4600003fVGH",
714 ["movf.d_2"] = "46200011FG",
715 ["movf.d_3"] = "46200011FGC",
716 ["movt.d_2"] = "46210011FG",
717 ["movt.d_3"] = "46210011FGC",
718 ["movz.d_3"] = "46200012FGT",
719 ["movn.d_3"] = "46200013FGT",
720 ["c.f.d_2"] = "46200030GH",
721 ["c.f.d_3"] = "46200030VGH",
722 ["c.un.d_2"] = "46200031GH",
723 ["c.un.d_3"] = "46200031VGH",
724 ["c.eq.d_2"] = "46200032GH",
725 ["c.eq.d_3"] = "46200032VGH",
726 ["c.ueq.d_2"] = "46200033GH",
727 ["c.ueq.d_3"] = "46200033VGH",
728 ["c.olt.d_2"] = "46200034GH",
729 ["c.olt.d_3"] = "46200034VGH",
730 ["c.ult.d_2"] = "46200035GH",
731 ["c.ult.d_3"] = "46200035VGH",
732 ["c.ole.d_2"] = "46200036GH",
733 ["c.ole.d_3"] = "46200036VGH",
734 ["c.ule.d_2"] = "46200037GH",
735 ["c.ule.d_3"] = "46200037VGH",
736 ["c.sf.d_2"] = "46200038GH",
737 ["c.sf.d_3"] = "46200038VGH",
738 ["c.ngle.d_2"] = "46200039GH",
739 ["c.ngle.d_3"] = "46200039VGH",
740 ["c.seq.d_2"] = "4620003aGH",
741 ["c.seq.d_3"] = "4620003aVGH",
742 ["c.ngl.d_2"] = "4620003bGH",
743 ["c.ngl.d_3"] = "4620003bVGH",
744 ["c.lt.d_2"] = "4620003cGH",
745 ["c.lt.d_3"] = "4620003cVGH",
746 ["c.nge.d_2"] = "4620003dGH",
747 ["c.nge.d_3"] = "4620003dVGH",
748 ["c.le.d_2"] = "4620003eGH",
749 ["c.le.d_3"] = "4620003eVGH",
750 ["c.ngt.d_2"] = "4620003fGH",
751 ["c.ngt.d_3"] = "4620003fVGH",
752 ["add.ps_3"] = "46c00000FGH",
753 ["sub.ps_3"] = "46c00001FGH",
754 ["mul.ps_3"] = "46c00002FGH",
755 ["abs.ps_2"] = "46c00005FG",
756 ["mov.ps_2"] = "46c00006FG",
757 ["neg.ps_2"] = "46c00007FG",
758 ["movf.ps_2"] = "46c00011FG",
759 ["movf.ps_3"] = "46c00011FGC",
760 ["movt.ps_2"] = "46c10011FG",
761 ["movt.ps_3"] = "46c10011FGC",
762 ["movz.ps_3"] = "46c00012FGT",
763 ["movn.ps_3"] = "46c00013FGT",
764 ["cvt.s.pu_2"] = "46c00020FG",
765 ["cvt.s.pl_2"] = "46c00028FG",
766 ["pll.ps_3"] = "46c0002cFGH",
767 ["plu.ps_3"] = "46c0002dFGH",
768 ["pul.ps_3"] = "46c0002eFGH",
769 ["puu.ps_3"] = "46c0002fFGH",
770 ["c.f.ps_2"] = "46c00030GH",
771 ["c.f.ps_3"] = "46c00030VGH",
772 ["c.un.ps_2"] = "46c00031GH",
773 ["c.un.ps_3"] = "46c00031VGH",
774 ["c.eq.ps_2"] = "46c00032GH",
775 ["c.eq.ps_3"] = "46c00032VGH",
776 ["c.ueq.ps_2"] = "46c00033GH",
777 ["c.ueq.ps_3"] = "46c00033VGH",
778 ["c.olt.ps_2"] = "46c00034GH",
779 ["c.olt.ps_3"] = "46c00034VGH",
780 ["c.ult.ps_2"] = "46c00035GH",
781 ["c.ult.ps_3"] = "46c00035VGH",
782 ["c.ole.ps_2"] = "46c00036GH",
783 ["c.ole.ps_3"] = "46c00036VGH",
784 ["c.ule.ps_2"] = "46c00037GH",
785 ["c.ule.ps_3"] = "46c00037VGH",
786 ["c.sf.ps_2"] = "46c00038GH",
787 ["c.sf.ps_3"] = "46c00038VGH",
788 ["c.ngle.ps_2"] = "46c00039GH",
789 ["c.ngle.ps_3"] = "46c00039VGH",
790 ["c.seq.ps_2"] = "46c0003aGH",
791 ["c.seq.ps_3"] = "46c0003aVGH",
792 ["c.ngl.ps_2"] = "46c0003bGH",
793 ["c.ngl.ps_3"] = "46c0003bVGH",
794 ["c.lt.ps_2"] = "46c0003cGH",
795 ["c.lt.ps_3"] = "46c0003cVGH",
796 ["c.nge.ps_2"] = "46c0003dGH",
797 ["c.nge.ps_3"] = "46c0003dVGH",
798 ["c.le.ps_2"] = "46c0003eGH",
799 ["c.le.ps_3"] = "46c0003eVGH",
800 ["c.ngt.ps_2"] = "46c0003fGH",
801 ["c.ngt.ps_3"] = "46c0003fVGH",
802
803 -- Opcode COP1X.
804 lwxc1_2 = "4c000000FX",
805 ldxc1_2 = "4c000001FX",
806 luxc1_2 = "4c000005FX",
807 swxc1_2 = "4c000008FX",
808 sdxc1_2 = "4c000009FX",
809 suxc1_2 = "4c00000dFX",
810 prefx_2 = "4c00000fMX",
811 ["alnv.ps_4"] = "4c00001eFGHS",
812 ["madd.s_4"] = "4c000020FRGH",
813 ["madd.d_4"] = "4c000021FRGH",
814 ["madd.ps_4"] = "4c000026FRGH",
815 ["msub.s_4"] = "4c000028FRGH",
816 ["msub.d_4"] = "4c000029FRGH",
817 ["msub.ps_4"] = "4c00002eFRGH",
818 ["nmadd.s_4"] = "4c000030FRGH",
819 ["nmadd.d_4"] = "4c000031FRGH",
820 ["nmadd.ps_4"] = "4c000036FRGH",
821 ["nmsub.s_4"] = "4c000038FRGH",
822 ["nmsub.d_4"] = "4c000039FRGH",
823 ["nmsub.ps_4"] = "4c00003eFRGH",
824
825 }) do map_op[k] = v end
826
827end
828
607------------------------------------------------------------------------------ 829------------------------------------------------------------------------------
608 830
609local function parse_gpr(expr) 831local function parse_gpr(expr)
@@ -633,7 +855,7 @@ local function parse_fpr(expr)
633 werror("bad register name `"..expr.."'") 855 werror("bad register name `"..expr.."'")
634end 856end
635 857
636local function parse_imm(imm, bits, shift, scale, signed) 858local function parse_imm(imm, bits, shift, scale, signed, action)
637 local n = tonumber(imm) 859 local n = tonumber(imm)
638 if n then 860 if n then
639 local m = sar(n, scale) 861 local m = sar(n, scale)
@@ -651,7 +873,8 @@ local function parse_imm(imm, bits, shift, scale, signed)
651 match(imm, "^([%w_]+):([rf][1-3]?[0-9])$") then 873 match(imm, "^([%w_]+):([rf][1-3]?[0-9])$") then
652 werror("expected immediate operand, got register") 874 werror("expected immediate operand, got register")
653 else 875 else
654 waction("IMM", (signed and 32768 or 0)+scale*1024+bits*32+shift, imm) 876 waction(action or "IMM",
877 (signed and 32768 or 0)+shl(scale, 10)+shl(bits, 5)+shift, imm)
655 return 0 878 return 0
656 end 879 end
657end 880end
@@ -756,13 +979,18 @@ map_op[".template__"] = function(params, template, nparams)
756 op = op + parse_disp(params[n]); n = n + 1 979 op = op + parse_disp(params[n]); n = n + 1
757 elseif p == "X" then 980 elseif p == "X" then
758 op = op + parse_index(params[n]); n = n + 1 981 op = op + parse_index(params[n]); n = n + 1
759 elseif p == "B" or p == "J" then 982 elseif p == "B" or p == "J" or p == "K" or p == "L" then
760 local mode, m, s = parse_label(params[n], false) 983 local mode, m, s = parse_label(params[n], false)
761 if p == "B" then m = m + 2048 end 984 if p == "J" then m = m + 0xa800
985 elseif p == "K" then m = m + 0x5000
986 elseif p == "L" then m = m + 0xa000 end
762 waction("REL_"..mode, m, s, 1) 987 waction("REL_"..mode, m, s, 1)
763 n = n + 1 988 n = n + 1
764 elseif p == "A" then 989 elseif p == "A" then
765 op = op + parse_imm(params[n], 5, 6, 0, false); n = n + 1 990 op = op + parse_imm(params[n], 5, 6, 0, false); n = n + 1
991 elseif p == "a" then
992 local m = parse_imm(params[n], 6, 6, 0, false, "IMMS"); n = n + 1
993 op = op + band(m, 0x7c0) + band(shr(m, 9), 4)
766 elseif p == "M" then 994 elseif p == "M" then
767 op = op + parse_imm(params[n], 5, 11, 0, false); n = n + 1 995 op = op + parse_imm(params[n], 5, 11, 0, false); n = n + 1
768 elseif p == "N" then 996 elseif p == "N" then
@@ -778,7 +1006,7 @@ map_op[".template__"] = function(params, template, nparams)
778 elseif p == "Z" then 1006 elseif p == "Z" then
779 op = op + parse_imm(params[n], 10, 6, 0, false); n = n + 1 1007 op = op + parse_imm(params[n], 10, 6, 0, false); n = n + 1
780 elseif p == "=" then 1008 elseif p == "=" then
781 op = op + shl(band(op, 0xf800), 5) -- Copy D to T for clz, clo. 1009 n = n - 1 -- Re-use previous parameter for next template char.
782 else 1010 else
783 assert(false) 1011 assert(false)
784 end 1012 end
diff --git a/dynasm/dasm_mips64.lua b/dynasm/dasm_mips64.lua
new file mode 100644
index 00000000..0aae291b
--- /dev/null
+++ b/dynasm/dasm_mips64.lua
@@ -0,0 +1,12 @@
1------------------------------------------------------------------------------
2-- DynASM MIPS64 module.
3--
4-- Copyright (C) 2005-2020 Mike Pall. All rights reserved.
5-- See dynasm.lua for full copyright notice.
6------------------------------------------------------------------------------
7-- This module just sets 64 bit mode for the combined MIPS/MIPS64 module.
8-- All the interesting stuff is there.
9------------------------------------------------------------------------------
10
11mips64 = true -- Using a global is an ugly, but effective solution.
12return require("dasm_mips")
diff --git a/dynasm/dasm_ppc.h b/dynasm/dasm_ppc.h
index 699d5c31..6e7cc7ab 100644
--- a/dynasm/dasm_ppc.h
+++ b/dynasm/dasm_ppc.h
@@ -1,5 +1,5 @@
1/* 1/*
2** DynASM PPC encoding engine. 2** DynASM PPC/PPC64 encoding engine.
3** Copyright (C) 2005-2020 Mike Pall. All rights reserved. 3** Copyright (C) 2005-2020 Mike Pall. All rights reserved.
4** Released under the MIT license. See dynasm.lua for full copyright notice. 4** Released under the MIT license. See dynasm.lua for full copyright notice.
5*/ 5*/
@@ -21,7 +21,7 @@ enum {
21 /* The following actions need a buffer position. */ 21 /* The following actions need a buffer position. */
22 DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG, 22 DASM_ALIGN, DASM_REL_LG, DASM_LABEL_LG,
23 /* The following actions also have an argument. */ 23 /* The following actions also have an argument. */
24 DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, 24 DASM_REL_PC, DASM_LABEL_PC, DASM_IMM, DASM_IMMSH,
25 DASM__MAX 25 DASM__MAX
26}; 26};
27 27
@@ -244,6 +244,10 @@ void dasm_put(Dst_DECL, int start, ...)
244#endif 244#endif
245 b[pos++] = n; 245 b[pos++] = n;
246 break; 246 break;
247 case DASM_IMMSH:
248 CK((n >> 6) == 0, RANGE_I);
249 b[pos++] = n;
250 break;
247 } 251 }
248 } 252 }
249 } 253 }
@@ -299,7 +303,7 @@ int dasm_link(Dst_DECL, size_t *szp)
299 case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break; 303 case DASM_ALIGN: ofs -= (b[pos++] + ofs) & (ins & 255); break;
300 case DASM_REL_LG: case DASM_REL_PC: pos++; break; 304 case DASM_REL_LG: case DASM_REL_PC: pos++; break;
301 case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break; 305 case DASM_LABEL_LG: case DASM_LABEL_PC: b[pos++] += ofs; break;
302 case DASM_IMM: pos++; break; 306 case DASM_IMM: case DASM_IMMSH: pos++; break;
303 } 307 }
304 } 308 }
305 stop: (void)0; 309 stop: (void)0;
@@ -367,6 +371,9 @@ int dasm_encode(Dst_DECL, void *buffer)
367 case DASM_IMM: 371 case DASM_IMM:
368 cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31); 372 cp[-1] |= (n & ((1<<((ins>>5)&31))-1)) << (ins&31);
369 break; 373 break;
374 case DASM_IMMSH:
375 cp[-1] |= (ins & 1) ? ((n&31)<<11)|((n&32)>>4) : ((n&31)<<6)|(n&32);
376 break;
370 default: *cp++ = ins; break; 377 default: *cp++ = ins; break;
371 } 378 }
372 } 379 }
diff --git a/dynasm/dasm_ppc.lua b/dynasm/dasm_ppc.lua
index 05981760..20634e13 100644
--- a/dynasm/dasm_ppc.lua
+++ b/dynasm/dasm_ppc.lua
@@ -1,17 +1,19 @@
1------------------------------------------------------------------------------ 1------------------------------------------------------------------------------
2-- DynASM PPC module. 2-- DynASM PPC/PPC64 module.
3-- 3--
4-- Copyright (C) 2005-2020 Mike Pall. All rights reserved. 4-- Copyright (C) 2005-2020 Mike Pall. All rights reserved.
5-- See dynasm.lua for full copyright notice. 5-- See dynasm.lua for full copyright notice.
6--
7-- Support for various extensions contributed by Caio Souza Oliveira.
6------------------------------------------------------------------------------ 8------------------------------------------------------------------------------
7 9
8-- Module information: 10-- Module information:
9local _info = { 11local _info = {
10 arch = "ppc", 12 arch = "ppc",
11 description = "DynASM PPC module", 13 description = "DynASM PPC module",
12 version = "1.3.0", 14 version = "1.4.0",
13 vernum = 10300, 15 vernum = 10400,
14 release = "2011-05-05", 16 release = "2015-10-18",
15 author = "Mike Pall", 17 author = "Mike Pall",
16 license = "MIT", 18 license = "MIT",
17} 19}
@@ -39,7 +41,7 @@ local wline, werror, wfatal, wwarn
39local action_names = { 41local action_names = {
40 "STOP", "SECTION", "ESC", "REL_EXT", 42 "STOP", "SECTION", "ESC", "REL_EXT",
41 "ALIGN", "REL_LG", "LABEL_LG", 43 "ALIGN", "REL_LG", "LABEL_LG",
42 "REL_PC", "LABEL_PC", "IMM", 44 "REL_PC", "LABEL_PC", "IMM", "IMMSH"
43} 45}
44 46
45-- Maximum number of section buffer positions for dasm_put(). 47-- Maximum number of section buffer positions for dasm_put().
@@ -228,8 +230,18 @@ local map_cond = {
228 230
229------------------------------------------------------------------------------ 231------------------------------------------------------------------------------
230 232
233local map_op, op_template
234
235local function op_alias(opname, f)
236 return function(params, nparams)
237 if not params then return "-> "..opname:sub(1, -3) end
238 f(params, nparams)
239 op_template(params, map_op[opname], nparams)
240 end
241end
242
231-- Template strings for PPC instructions. 243-- Template strings for PPC instructions.
232local map_op = { 244map_op = {
233 tdi_3 = "08000000ARI", 245 tdi_3 = "08000000ARI",
234 twi_3 = "0c000000ARI", 246 twi_3 = "0c000000ARI",
235 mulli_3 = "1c000000RRI", 247 mulli_3 = "1c000000RRI",
@@ -297,6 +309,250 @@ local map_op = {
297 std_2 = "f8000000RD", 309 std_2 = "f8000000RD",
298 stdu_2 = "f8000001RD", 310 stdu_2 = "f8000001RD",
299 311
312 subi_3 = op_alias("addi_3", function(p) p[3] = "-("..p[3]..")" end),
313 subis_3 = op_alias("addis_3", function(p) p[3] = "-("..p[3]..")" end),
314 subic_3 = op_alias("addic_3", function(p) p[3] = "-("..p[3]..")" end),
315 ["subic._3"] = op_alias("addic._3", function(p) p[3] = "-("..p[3]..")" end),
316
317 rotlwi_3 = op_alias("rlwinm_5", function(p)
318 p[4] = "0"; p[5] = "31"
319 end),
320 rotrwi_3 = op_alias("rlwinm_5", function(p)
321 p[3] = "32-("..p[3]..")"; p[4] = "0"; p[5] = "31"
322 end),
323 rotlw_3 = op_alias("rlwnm_5", function(p)
324 p[4] = "0"; p[5] = "31"
325 end),
326 slwi_3 = op_alias("rlwinm_5", function(p)
327 p[5] = "31-("..p[3]..")"; p[4] = "0"
328 end),
329 srwi_3 = op_alias("rlwinm_5", function(p)
330 p[4] = p[3]; p[3] = "32-("..p[3]..")"; p[5] = "31"
331 end),
332 clrlwi_3 = op_alias("rlwinm_5", function(p)
333 p[4] = p[3]; p[3] = "0"; p[5] = "31"
334 end),
335 clrrwi_3 = op_alias("rlwinm_5", function(p)
336 p[5] = "31-("..p[3]..")"; p[3] = "0"; p[4] = "0"
337 end),
338
339 -- Primary opcode 4:
340 mulhhwu_3 = "10000010RRR.",
341 machhwu_3 = "10000018RRR.",
342 mulhhw_3 = "10000050RRR.",
343 nmachhw_3 = "1000005cRRR.",
344 machhwsu_3 = "10000098RRR.",
345 machhws_3 = "100000d8RRR.",
346 nmachhws_3 = "100000dcRRR.",
347 mulchwu_3 = "10000110RRR.",
348 macchwu_3 = "10000118RRR.",
349 mulchw_3 = "10000150RRR.",
350 macchw_3 = "10000158RRR.",
351 nmacchw_3 = "1000015cRRR.",
352 macchwsu_3 = "10000198RRR.",
353 macchws_3 = "100001d8RRR.",
354 nmacchws_3 = "100001dcRRR.",
355 mullhw_3 = "10000350RRR.",
356 maclhw_3 = "10000358RRR.",
357 nmaclhw_3 = "1000035cRRR.",
358 maclhwsu_3 = "10000398RRR.",
359 maclhws_3 = "100003d8RRR.",
360 nmaclhws_3 = "100003dcRRR.",
361 machhwuo_3 = "10000418RRR.",
362 nmachhwo_3 = "1000045cRRR.",
363 machhwsuo_3 = "10000498RRR.",
364 machhwso_3 = "100004d8RRR.",
365 nmachhwso_3 = "100004dcRRR.",
366 macchwuo_3 = "10000518RRR.",
367 macchwo_3 = "10000558RRR.",
368 nmacchwo_3 = "1000055cRRR.",
369 macchwsuo_3 = "10000598RRR.",
370 macchwso_3 = "100005d8RRR.",
371 nmacchwso_3 = "100005dcRRR.",
372 maclhwo_3 = "10000758RRR.",
373 nmaclhwo_3 = "1000075cRRR.",
374 maclhwsuo_3 = "10000798RRR.",
375 maclhwso_3 = "100007d8RRR.",
376 nmaclhwso_3 = "100007dcRRR.",
377
378 vaddubm_3 = "10000000VVV",
379 vmaxub_3 = "10000002VVV",
380 vrlb_3 = "10000004VVV",
381 vcmpequb_3 = "10000006VVV",
382 vmuloub_3 = "10000008VVV",
383 vaddfp_3 = "1000000aVVV",
384 vmrghb_3 = "1000000cVVV",
385 vpkuhum_3 = "1000000eVVV",
386 vmhaddshs_4 = "10000020VVVV",
387 vmhraddshs_4 = "10000021VVVV",
388 vmladduhm_4 = "10000022VVVV",
389 vmsumubm_4 = "10000024VVVV",
390 vmsummbm_4 = "10000025VVVV",
391 vmsumuhm_4 = "10000026VVVV",
392 vmsumuhs_4 = "10000027VVVV",
393 vmsumshm_4 = "10000028VVVV",
394 vmsumshs_4 = "10000029VVVV",
395 vsel_4 = "1000002aVVVV",
396 vperm_4 = "1000002bVVVV",
397 vsldoi_4 = "1000002cVVVP",
398 vpermxor_4 = "1000002dVVVV",
399 vmaddfp_4 = "1000002eVVVV~",
400 vnmsubfp_4 = "1000002fVVVV~",
401 vaddeuqm_4 = "1000003cVVVV",
402 vaddecuq_4 = "1000003dVVVV",
403 vsubeuqm_4 = "1000003eVVVV",
404 vsubecuq_4 = "1000003fVVVV",
405 vadduhm_3 = "10000040VVV",
406 vmaxuh_3 = "10000042VVV",
407 vrlh_3 = "10000044VVV",
408 vcmpequh_3 = "10000046VVV",
409 vmulouh_3 = "10000048VVV",
410 vsubfp_3 = "1000004aVVV",
411 vmrghh_3 = "1000004cVVV",
412 vpkuwum_3 = "1000004eVVV",
413 vadduwm_3 = "10000080VVV",
414 vmaxuw_3 = "10000082VVV",
415 vrlw_3 = "10000084VVV",
416 vcmpequw_3 = "10000086VVV",
417 vmulouw_3 = "10000088VVV",
418 vmuluwm_3 = "10000089VVV",
419 vmrghw_3 = "1000008cVVV",
420 vpkuhus_3 = "1000008eVVV",
421 vaddudm_3 = "100000c0VVV",
422 vmaxud_3 = "100000c2VVV",
423 vrld_3 = "100000c4VVV",
424 vcmpeqfp_3 = "100000c6VVV",
425 vcmpequd_3 = "100000c7VVV",
426 vpkuwus_3 = "100000ceVVV",
427 vadduqm_3 = "10000100VVV",
428 vmaxsb_3 = "10000102VVV",
429 vslb_3 = "10000104VVV",
430 vmulosb_3 = "10000108VVV",
431 vrefp_2 = "1000010aV-V",
432 vmrglb_3 = "1000010cVVV",
433 vpkshus_3 = "1000010eVVV",
434 vaddcuq_3 = "10000140VVV",
435 vmaxsh_3 = "10000142VVV",
436 vslh_3 = "10000144VVV",
437 vmulosh_3 = "10000148VVV",
438 vrsqrtefp_2 = "1000014aV-V",
439 vmrglh_3 = "1000014cVVV",
440 vpkswus_3 = "1000014eVVV",
441 vaddcuw_3 = "10000180VVV",
442 vmaxsw_3 = "10000182VVV",
443 vslw_3 = "10000184VVV",
444 vmulosw_3 = "10000188VVV",
445 vexptefp_2 = "1000018aV-V",
446 vmrglw_3 = "1000018cVVV",
447 vpkshss_3 = "1000018eVVV",
448 vmaxsd_3 = "100001c2VVV",
449 vsl_3 = "100001c4VVV",
450 vcmpgefp_3 = "100001c6VVV",
451 vlogefp_2 = "100001caV-V",
452 vpkswss_3 = "100001ceVVV",
453 vadduhs_3 = "10000240VVV",
454 vminuh_3 = "10000242VVV",
455 vsrh_3 = "10000244VVV",
456 vcmpgtuh_3 = "10000246VVV",
457 vmuleuh_3 = "10000248VVV",
458 vrfiz_2 = "1000024aV-V",
459 vsplth_3 = "1000024cVV3",
460 vupkhsh_2 = "1000024eV-V",
461 vminuw_3 = "10000282VVV",
462 vminud_3 = "100002c2VVV",
463 vcmpgtud_3 = "100002c7VVV",
464 vrfim_2 = "100002caV-V",
465 vcmpgtsb_3 = "10000306VVV",
466 vcfux_3 = "1000030aVVA~",
467 vaddshs_3 = "10000340VVV",
468 vminsh_3 = "10000342VVV",
469 vsrah_3 = "10000344VVV",
470 vcmpgtsh_3 = "10000346VVV",
471 vmulesh_3 = "10000348VVV",
472 vcfsx_3 = "1000034aVVA~",
473 vspltish_2 = "1000034cVS",
474 vupkhpx_2 = "1000034eV-V",
475 vaddsws_3 = "10000380VVV",
476 vminsw_3 = "10000382VVV",
477 vsraw_3 = "10000384VVV",
478 vcmpgtsw_3 = "10000386VVV",
479 vmulesw_3 = "10000388VVV",
480 vctuxs_3 = "1000038aVVA~",
481 vspltisw_2 = "1000038cVS",
482 vminsd_3 = "100003c2VVV",
483 vsrad_3 = "100003c4VVV",
484 vcmpbfp_3 = "100003c6VVV",
485 vcmpgtsd_3 = "100003c7VVV",
486 vctsxs_3 = "100003caVVA~",
487 vupklpx_2 = "100003ceV-V",
488 vsububm_3 = "10000400VVV",
489 ["bcdadd._4"] = "10000401VVVy.",
490 vavgub_3 = "10000402VVV",
491 vand_3 = "10000404VVV",
492 ["vcmpequb._3"] = "10000406VVV",
493 vmaxfp_3 = "1000040aVVV",
494 vsubuhm_3 = "10000440VVV",
495 ["bcdsub._4"] = "10000441VVVy.",
496 vavguh_3 = "10000442VVV",
497 vandc_3 = "10000444VVV",
498 ["vcmpequh._3"] = "10000446VVV",
499 vminfp_3 = "1000044aVVV",
500 vpkudum_3 = "1000044eVVV",
501 vsubuwm_3 = "10000480VVV",
502 vavguw_3 = "10000482VVV",
503 vor_3 = "10000484VVV",
504 ["vcmpequw._3"] = "10000486VVV",
505 vpmsumw_3 = "10000488VVV",
506 ["vcmpeqfp._3"] = "100004c6VVV",
507 ["vcmpequd._3"] = "100004c7VVV",
508 vpkudus_3 = "100004ceVVV",
509 vavgsb_3 = "10000502VVV",
510 vavgsh_3 = "10000542VVV",
511 vorc_3 = "10000544VVV",
512 vbpermq_3 = "1000054cVVV",
513 vpksdus_3 = "1000054eVVV",
514 vavgsw_3 = "10000582VVV",
515 vsld_3 = "100005c4VVV",
516 ["vcmpgefp._3"] = "100005c6VVV",
517 vpksdss_3 = "100005ceVVV",
518 vsububs_3 = "10000600VVV",
519 mfvscr_1 = "10000604V--",
520 vsum4ubs_3 = "10000608VVV",
521 vsubuhs_3 = "10000640VVV",
522 mtvscr_1 = "10000644--V",
523 ["vcmpgtuh._3"] = "10000646VVV",
524 vsum4shs_3 = "10000648VVV",
525 vupkhsw_2 = "1000064eV-V",
526 vsubuws_3 = "10000680VVV",
527 vshasigmaw_4 = "10000682VVYp",
528 veqv_3 = "10000684VVV",
529 vsum2sws_3 = "10000688VVV",
530 vmrgow_3 = "1000068cVVV",
531 vshasigmad_4 = "100006c2VVYp",
532 vsrd_3 = "100006c4VVV",
533 ["vcmpgtud._3"] = "100006c7VVV",
534 vupklsw_2 = "100006ceV-V",
535 vupkslw_2 = "100006ceV-V",
536 vsubsbs_3 = "10000700VVV",
537 vclzb_2 = "10000702V-V",
538 vpopcntb_2 = "10000703V-V",
539 ["vcmpgtsb._3"] = "10000706VVV",
540 vsum4sbs_3 = "10000708VVV",
541 vsubshs_3 = "10000740VVV",
542 vclzh_2 = "10000742V-V",
543 vpopcnth_2 = "10000743V-V",
544 ["vcmpgtsh._3"] = "10000746VVV",
545 vsubsws_3 = "10000780VVV",
546 vclzw_2 = "10000782V-V",
547 vpopcntw_2 = "10000783V-V",
548 ["vcmpgtsw._3"] = "10000786VVV",
549 vsumsws_3 = "10000788VVV",
550 vmrgew_3 = "1000078cVVV",
551 vclzd_2 = "100007c2V-V",
552 vpopcntd_2 = "100007c3V-V",
553 ["vcmpbfp._3"] = "100007c6VVV",
554 ["vcmpgtsd._3"] = "100007c7VVV",
555
300 -- Primary opcode 19: 556 -- Primary opcode 19:
301 mcrf_2 = "4c000000XX", 557 mcrf_2 = "4c000000XX",
302 isync_0 = "4c00012c", 558 isync_0 = "4c00012c",
@@ -316,6 +572,8 @@ local map_op = {
316 bclrl_2 = "4c000021AA", 572 bclrl_2 = "4c000021AA",
317 bcctr_2 = "4c000420AA", 573 bcctr_2 = "4c000420AA",
318 bcctrl_2 = "4c000421AA", 574 bcctrl_2 = "4c000421AA",
575 bctar_2 = "4c000460AA",
576 bctarl_2 = "4c000461AA",
319 blr_0 = "4e800020", 577 blr_0 = "4e800020",
320 blrl_0 = "4e800021", 578 blrl_0 = "4e800021",
321 bctr_0 = "4e800420", 579 bctr_0 = "4e800420",
@@ -327,6 +585,7 @@ local map_op = {
327 cmpd_3 = "7c200000XRR", 585 cmpd_3 = "7c200000XRR",
328 cmpd_2 = "7c200000-RR", 586 cmpd_2 = "7c200000-RR",
329 tw_3 = "7c000008ARR", 587 tw_3 = "7c000008ARR",
588 lvsl_3 = "7c00000cVRR",
330 subfc_3 = "7c000010RRR.", 589 subfc_3 = "7c000010RRR.",
331 subc_3 = "7c000010RRR~.", 590 subc_3 = "7c000010RRR~.",
332 mulhdu_3 = "7c000012RRR.", 591 mulhdu_3 = "7c000012RRR.",
@@ -351,50 +610,68 @@ local map_op = {
351 cmplw_2 = "7c000040-RR", 610 cmplw_2 = "7c000040-RR",
352 cmpld_3 = "7c200040XRR", 611 cmpld_3 = "7c200040XRR",
353 cmpld_2 = "7c200040-RR", 612 cmpld_2 = "7c200040-RR",
613 lvsr_3 = "7c00004cVRR",
354 subf_3 = "7c000050RRR.", 614 subf_3 = "7c000050RRR.",
355 sub_3 = "7c000050RRR~.", 615 sub_3 = "7c000050RRR~.",
616 lbarx_3 = "7c000068RR0R",
356 ldux_3 = "7c00006aRR0R", 617 ldux_3 = "7c00006aRR0R",
357 dcbst_2 = "7c00006c-RR", 618 dcbst_2 = "7c00006c-RR",
358 lwzux_3 = "7c00006eRR0R", 619 lwzux_3 = "7c00006eRR0R",
359 cntlzd_2 = "7c000074RR~", 620 cntlzd_2 = "7c000074RR~",
360 andc_3 = "7c000078RR~R.", 621 andc_3 = "7c000078RR~R.",
361 td_3 = "7c000088ARR", 622 td_3 = "7c000088ARR",
623 lvewx_3 = "7c00008eVRR",
362 mulhd_3 = "7c000092RRR.", 624 mulhd_3 = "7c000092RRR.",
625 addg6s_3 = "7c000094RRR",
363 mulhw_3 = "7c000096RRR.", 626 mulhw_3 = "7c000096RRR.",
627 dlmzb_3 = "7c00009cRR~R.",
364 ldarx_3 = "7c0000a8RR0R", 628 ldarx_3 = "7c0000a8RR0R",
365 dcbf_2 = "7c0000ac-RR", 629 dcbf_2 = "7c0000ac-RR",
366 lbzx_3 = "7c0000aeRR0R", 630 lbzx_3 = "7c0000aeRR0R",
631 lvx_3 = "7c0000ceVRR",
367 neg_2 = "7c0000d0RR.", 632 neg_2 = "7c0000d0RR.",
633 lharx_3 = "7c0000e8RR0R",
368 lbzux_3 = "7c0000eeRR0R", 634 lbzux_3 = "7c0000eeRR0R",
369 popcntb_2 = "7c0000f4RR~", 635 popcntb_2 = "7c0000f4RR~",
370 not_2 = "7c0000f8RR~%.", 636 not_2 = "7c0000f8RR~%.",
371 nor_3 = "7c0000f8RR~R.", 637 nor_3 = "7c0000f8RR~R.",
638 stvebx_3 = "7c00010eVRR",
372 subfe_3 = "7c000110RRR.", 639 subfe_3 = "7c000110RRR.",
373 sube_3 = "7c000110RRR~.", 640 sube_3 = "7c000110RRR~.",
374 adde_3 = "7c000114RRR.", 641 adde_3 = "7c000114RRR.",
375 stdx_3 = "7c00012aRR0R", 642 stdx_3 = "7c00012aRR0R",
376 stwcx_3 = "7c00012cRR0R.", 643 ["stwcx._3"] = "7c00012dRR0R.",
377 stwx_3 = "7c00012eRR0R", 644 stwx_3 = "7c00012eRR0R",
378 prtyw_2 = "7c000134RR~", 645 prtyw_2 = "7c000134RR~",
646 stvehx_3 = "7c00014eVRR",
379 stdux_3 = "7c00016aRR0R", 647 stdux_3 = "7c00016aRR0R",
648 ["stqcx._3"] = "7c00016dR:R0R.",
380 stwux_3 = "7c00016eRR0R", 649 stwux_3 = "7c00016eRR0R",
381 prtyd_2 = "7c000174RR~", 650 prtyd_2 = "7c000174RR~",
651 stvewx_3 = "7c00018eVRR",
382 subfze_2 = "7c000190RR.", 652 subfze_2 = "7c000190RR.",
383 addze_2 = "7c000194RR.", 653 addze_2 = "7c000194RR.",
384 stdcx_3 = "7c0001acRR0R.", 654 ["stdcx._3"] = "7c0001adRR0R.",
385 stbx_3 = "7c0001aeRR0R", 655 stbx_3 = "7c0001aeRR0R",
656 stvx_3 = "7c0001ceVRR",
386 subfme_2 = "7c0001d0RR.", 657 subfme_2 = "7c0001d0RR.",
387 mulld_3 = "7c0001d2RRR.", 658 mulld_3 = "7c0001d2RRR.",
388 addme_2 = "7c0001d4RR.", 659 addme_2 = "7c0001d4RR.",
389 mullw_3 = "7c0001d6RRR.", 660 mullw_3 = "7c0001d6RRR.",
390 dcbtst_2 = "7c0001ec-RR", 661 dcbtst_2 = "7c0001ec-RR",
391 stbux_3 = "7c0001eeRR0R", 662 stbux_3 = "7c0001eeRR0R",
663 bpermd_3 = "7c0001f8RR~R",
664 lvepxl_3 = "7c00020eVRR",
392 add_3 = "7c000214RRR.", 665 add_3 = "7c000214RRR.",
666 lqarx_3 = "7c000228R:R0R",
393 dcbt_2 = "7c00022c-RR", 667 dcbt_2 = "7c00022c-RR",
394 lhzx_3 = "7c00022eRR0R", 668 lhzx_3 = "7c00022eRR0R",
669 cdtbcd_2 = "7c000234RR~",
395 eqv_3 = "7c000238RR~R.", 670 eqv_3 = "7c000238RR~R.",
671 lvepx_3 = "7c00024eVRR",
396 eciwx_3 = "7c00026cRR0R", 672 eciwx_3 = "7c00026cRR0R",
397 lhzux_3 = "7c00026eRR0R", 673 lhzux_3 = "7c00026eRR0R",
674 cbcdtd_2 = "7c000274RR~",
398 xor_3 = "7c000278RR~R.", 675 xor_3 = "7c000278RR~R.",
399 mfspefscr_1 = "7c0082a6R", 676 mfspefscr_1 = "7c0082a6R",
400 mfxer_1 = "7c0102a6R", 677 mfxer_1 = "7c0102a6R",
@@ -404,8 +681,12 @@ local map_op = {
404 lhax_3 = "7c0002aeRR0R", 681 lhax_3 = "7c0002aeRR0R",
405 mftb_1 = "7c0c42e6R", 682 mftb_1 = "7c0c42e6R",
406 mftbu_1 = "7c0d42e6R", 683 mftbu_1 = "7c0d42e6R",
684 lvxl_3 = "7c0002ceVRR",
407 lwaux_3 = "7c0002eaRR0R", 685 lwaux_3 = "7c0002eaRR0R",
408 lhaux_3 = "7c0002eeRR0R", 686 lhaux_3 = "7c0002eeRR0R",
687 popcntw_2 = "7c0002f4RR~",
688 divdeu_3 = "7c000312RRR.",
689 divweu_3 = "7c000316RRR.",
409 sthx_3 = "7c00032eRR0R", 690 sthx_3 = "7c00032eRR0R",
410 orc_3 = "7c000338RR~R.", 691 orc_3 = "7c000338RR~R.",
411 ecowx_3 = "7c00036cRR0R", 692 ecowx_3 = "7c00036cRR0R",
@@ -420,10 +701,14 @@ local map_op = {
420 mtctr_1 = "7c0903a6R", 701 mtctr_1 = "7c0903a6R",
421 dcbi_2 = "7c0003ac-RR", 702 dcbi_2 = "7c0003ac-RR",
422 nand_3 = "7c0003b8RR~R.", 703 nand_3 = "7c0003b8RR~R.",
704 dsn_2 = "7c0003c6-RR",
705 stvxl_3 = "7c0003ceVRR",
423 divd_3 = "7c0003d2RRR.", 706 divd_3 = "7c0003d2RRR.",
424 divw_3 = "7c0003d6RRR.", 707 divw_3 = "7c0003d6RRR.",
708 popcntd_2 = "7c0003f4RR~",
425 cmpb_3 = "7c0003f8RR~R.", 709 cmpb_3 = "7c0003f8RR~R.",
426 mcrxr_1 = "7c000400X", 710 mcrxr_1 = "7c000400X",
711 lbdx_3 = "7c000406RRR",
427 subfco_3 = "7c000410RRR.", 712 subfco_3 = "7c000410RRR.",
428 subco_3 = "7c000410RRR~.", 713 subco_3 = "7c000410RRR~.",
429 addco_3 = "7c000414RRR.", 714 addco_3 = "7c000414RRR.",
@@ -433,16 +718,20 @@ local map_op = {
433 lfsx_3 = "7c00042eFR0R", 718 lfsx_3 = "7c00042eFR0R",
434 srw_3 = "7c000430RR~R.", 719 srw_3 = "7c000430RR~R.",
435 srd_3 = "7c000436RR~R.", 720 srd_3 = "7c000436RR~R.",
721 lhdx_3 = "7c000446RRR",
436 subfo_3 = "7c000450RRR.", 722 subfo_3 = "7c000450RRR.",
437 subo_3 = "7c000450RRR~.", 723 subo_3 = "7c000450RRR~.",
438 lfsux_3 = "7c00046eFR0R", 724 lfsux_3 = "7c00046eFR0R",
725 lwdx_3 = "7c000486RRR",
439 lswi_3 = "7c0004aaRR0A", 726 lswi_3 = "7c0004aaRR0A",
440 sync_0 = "7c0004ac", 727 sync_0 = "7c0004ac",
441 lwsync_0 = "7c2004ac", 728 lwsync_0 = "7c2004ac",
442 ptesync_0 = "7c4004ac", 729 ptesync_0 = "7c4004ac",
443 lfdx_3 = "7c0004aeFR0R", 730 lfdx_3 = "7c0004aeFR0R",
731 lddx_3 = "7c0004c6RRR",
444 nego_2 = "7c0004d0RR.", 732 nego_2 = "7c0004d0RR.",
445 lfdux_3 = "7c0004eeFR0R", 733 lfdux_3 = "7c0004eeFR0R",
734 stbdx_3 = "7c000506RRR",
446 subfeo_3 = "7c000510RRR.", 735 subfeo_3 = "7c000510RRR.",
447 subeo_3 = "7c000510RRR~.", 736 subeo_3 = "7c000510RRR~.",
448 addeo_3 = "7c000514RRR.", 737 addeo_3 = "7c000514RRR.",
@@ -450,27 +739,42 @@ local map_op = {
450 stswx_3 = "7c00052aRR0R", 739 stswx_3 = "7c00052aRR0R",
451 stwbrx_3 = "7c00052cRR0R", 740 stwbrx_3 = "7c00052cRR0R",
452 stfsx_3 = "7c00052eFR0R", 741 stfsx_3 = "7c00052eFR0R",
742 sthdx_3 = "7c000546RRR",
743 ["stbcx._3"] = "7c00056dRRR",
453 stfsux_3 = "7c00056eFR0R", 744 stfsux_3 = "7c00056eFR0R",
745 stwdx_3 = "7c000586RRR",
454 subfzeo_2 = "7c000590RR.", 746 subfzeo_2 = "7c000590RR.",
455 addzeo_2 = "7c000594RR.", 747 addzeo_2 = "7c000594RR.",
456 stswi_3 = "7c0005aaRR0A", 748 stswi_3 = "7c0005aaRR0A",
749 ["sthcx._3"] = "7c0005adRRR",
457 stfdx_3 = "7c0005aeFR0R", 750 stfdx_3 = "7c0005aeFR0R",
751 stddx_3 = "7c0005c6RRR",
458 subfmeo_2 = "7c0005d0RR.", 752 subfmeo_2 = "7c0005d0RR.",
459 mulldo_3 = "7c0005d2RRR.", 753 mulldo_3 = "7c0005d2RRR.",
460 addmeo_2 = "7c0005d4RR.", 754 addmeo_2 = "7c0005d4RR.",
461 mullwo_3 = "7c0005d6RRR.", 755 mullwo_3 = "7c0005d6RRR.",
462 dcba_2 = "7c0005ec-RR", 756 dcba_2 = "7c0005ec-RR",
463 stfdux_3 = "7c0005eeFR0R", 757 stfdux_3 = "7c0005eeFR0R",
758 stvepxl_3 = "7c00060eVRR",
464 addo_3 = "7c000614RRR.", 759 addo_3 = "7c000614RRR.",
465 lhbrx_3 = "7c00062cRR0R", 760 lhbrx_3 = "7c00062cRR0R",
761 lfdpx_3 = "7c00062eF:RR",
466 sraw_3 = "7c000630RR~R.", 762 sraw_3 = "7c000630RR~R.",
467 srad_3 = "7c000634RR~R.", 763 srad_3 = "7c000634RR~R.",
764 lfddx_3 = "7c000646FRR",
765 stvepx_3 = "7c00064eVRR",
468 srawi_3 = "7c000670RR~A.", 766 srawi_3 = "7c000670RR~A.",
469 sradi_3 = "7c000674RR~H.", 767 sradi_3 = "7c000674RR~H.",
470 eieio_0 = "7c0006ac", 768 eieio_0 = "7c0006ac",
471 lfiwax_3 = "7c0006aeFR0R", 769 lfiwax_3 = "7c0006aeFR0R",
770 divdeuo_3 = "7c000712RRR.",
771 divweuo_3 = "7c000716RRR.",
472 sthbrx_3 = "7c00072cRR0R", 772 sthbrx_3 = "7c00072cRR0R",
773 stfdpx_3 = "7c00072eF:RR",
473 extsh_2 = "7c000734RR~.", 774 extsh_2 = "7c000734RR~.",
775 stfddx_3 = "7c000746FRR",
776 divdeo_3 = "7c000752RRR.",
777 divweo_3 = "7c000756RRR.",
474 extsb_2 = "7c000774RR~.", 778 extsb_2 = "7c000774RR~.",
475 divduo_3 = "7c000792RRR.", 779 divduo_3 = "7c000792RRR.",
476 divwou_3 = "7c000796RRR.", 780 divwou_3 = "7c000796RRR.",
@@ -481,6 +785,40 @@ local map_op = {
481 divwo_3 = "7c0007d6RRR.", 785 divwo_3 = "7c0007d6RRR.",
482 dcbz_2 = "7c0007ec-RR", 786 dcbz_2 = "7c0007ec-RR",
483 787
788 ["tbegin._1"] = "7c00051d1",
789 ["tbegin._0"] = "7c00051d",
790 ["tend._1"] = "7c00055dY",
791 ["tend._0"] = "7c00055d",
792 ["tendall._0"] = "7e00055d",
793 tcheck_1 = "7c00059cX",
794 ["tsr._1"] = "7c0005dd1",
795 ["tsuspend._0"] = "7c0005dd",
796 ["tresume._0"] = "7c2005dd",
797 ["tabortwc._3"] = "7c00061dARR",
798 ["tabortdc._3"] = "7c00065dARR",
799 ["tabortwci._3"] = "7c00069dARS",
800 ["tabortdci._3"] = "7c0006ddARS",
801 ["tabort._1"] = "7c00071d-R-",
802 ["treclaim._1"] = "7c00075d-R",
803 ["trechkpt._0"] = "7c0007dd",
804
805 lxsiwzx_3 = "7c000018QRR",
806 lxsiwax_3 = "7c000098QRR",
807 mfvsrd_2 = "7c000066-Rq",
808 mfvsrwz_2 = "7c0000e6-Rq",
809 stxsiwx_3 = "7c000118QRR",
810 mtvsrd_2 = "7c000166QR",
811 mtvsrwa_2 = "7c0001a6QR",
812 lxvdsx_3 = "7c000298QRR",
813 lxsspx_3 = "7c000418QRR",
814 lxsdx_3 = "7c000498QRR",
815 stxsspx_3 = "7c000518QRR",
816 stxsdx_3 = "7c000598QRR",
817 lxvw4x_3 = "7c000618QRR",
818 lxvd2x_3 = "7c000698QRR",
819 stxvw4x_3 = "7c000718QRR",
820 stxvd2x_3 = "7c000798QRR",
821
484 -- Primary opcode 30: 822 -- Primary opcode 30:
485 rldicl_4 = "78000000RR~HM.", 823 rldicl_4 = "78000000RR~HM.",
486 rldicr_4 = "78000004RR~HM.", 824 rldicr_4 = "78000004RR~HM.",
@@ -489,6 +827,34 @@ local map_op = {
489 rldcl_4 = "78000010RR~RM.", 827 rldcl_4 = "78000010RR~RM.",
490 rldcr_4 = "78000012RR~RM.", 828 rldcr_4 = "78000012RR~RM.",
491 829
830 rotldi_3 = op_alias("rldicl_4", function(p)
831 p[4] = "0"
832 end),
833 rotrdi_3 = op_alias("rldicl_4", function(p)
834 p[3] = "64-("..p[3]..")"; p[4] = "0"
835 end),
836 rotld_3 = op_alias("rldcl_4", function(p)
837 p[4] = "0"
838 end),
839 sldi_3 = op_alias("rldicr_4", function(p)
840 p[4] = "63-("..p[3]..")"
841 end),
842 srdi_3 = op_alias("rldicl_4", function(p)
843 p[4] = p[3]; p[3] = "64-("..p[3]..")"
844 end),
845 clrldi_3 = op_alias("rldicl_4", function(p)
846 p[4] = p[3]; p[3] = "0"
847 end),
848 clrrdi_3 = op_alias("rldicr_4", function(p)
849 p[4] = "63-("..p[3]..")"; p[3] = "0"
850 end),
851
852 -- Primary opcode 56:
853 lq_2 = "e0000000R:D", -- NYI: displacement must be divisible by 8.
854
855 -- Primary opcode 57:
856 lfdp_2 = "e4000000F:D", -- NYI: displacement must be divisible by 4.
857
492 -- Primary opcode 59: 858 -- Primary opcode 59:
493 fdivs_3 = "ec000024FFF.", 859 fdivs_3 = "ec000024FFF.",
494 fsubs_3 = "ec000028FFF.", 860 fsubs_3 = "ec000028FFF.",
@@ -501,6 +867,200 @@ local map_op = {
501 fmadds_4 = "ec00003aFFFF~.", 867 fmadds_4 = "ec00003aFFFF~.",
502 fnmsubs_4 = "ec00003cFFFF~.", 868 fnmsubs_4 = "ec00003cFFFF~.",
503 fnmadds_4 = "ec00003eFFFF~.", 869 fnmadds_4 = "ec00003eFFFF~.",
870 fcfids_2 = "ec00069cF-F.",
871 fcfidus_2 = "ec00079cF-F.",
872
873 dadd_3 = "ec000004FFF.",
874 dqua_4 = "ec000006FFFZ.",
875 dmul_3 = "ec000044FFF.",
876 drrnd_4 = "ec000046FFFZ.",
877 dscli_3 = "ec000084FF6.",
878 dquai_4 = "ec000086SF~FZ.",
879 dscri_3 = "ec0000c4FF6.",
880 drintx_4 = "ec0000c61F~FZ.",
881 dcmpo_3 = "ec000104XFF",
882 dtstex_3 = "ec000144XFF",
883 dtstdc_3 = "ec000184XF6",
884 dtstdg_3 = "ec0001c4XF6",
885 drintn_4 = "ec0001c61F~FZ.",
886 dctdp_2 = "ec000204F-F.",
887 dctfix_2 = "ec000244F-F.",
888 ddedpd_3 = "ec000284ZF~F.",
889 dxex_2 = "ec0002c4F-F.",
890 dsub_3 = "ec000404FFF.",
891 ddiv_3 = "ec000444FFF.",
892 dcmpu_3 = "ec000504XFF",
893 dtstsf_3 = "ec000544XFF",
894 drsp_2 = "ec000604F-F.",
895 dcffix_2 = "ec000644F-F.",
896 denbcd_3 = "ec000684YF~F.",
897 diex_3 = "ec0006c4FFF.",
898
899 -- Primary opcode 60:
900 xsaddsp_3 = "f0000000QQQ",
901 xsmaddasp_3 = "f0000008QQQ",
902 xxsldwi_4 = "f0000010QQQz",
903 xsrsqrtesp_2 = "f0000028Q-Q",
904 xssqrtsp_2 = "f000002cQ-Q",
905 xxsel_4 = "f0000030QQQQ",
906 xssubsp_3 = "f0000040QQQ",
907 xsmaddmsp_3 = "f0000048QQQ",
908 xxpermdi_4 = "f0000050QQQz",
909 xsresp_2 = "f0000068Q-Q",
910 xsmulsp_3 = "f0000080QQQ",
911 xsmsubasp_3 = "f0000088QQQ",
912 xxmrghw_3 = "f0000090QQQ",
913 xsdivsp_3 = "f00000c0QQQ",
914 xsmsubmsp_3 = "f00000c8QQQ",
915 xsadddp_3 = "f0000100QQQ",
916 xsmaddadp_3 = "f0000108QQQ",
917 xscmpudp_3 = "f0000118XQQ",
918 xscvdpuxws_2 = "f0000120Q-Q",
919 xsrdpi_2 = "f0000124Q-Q",
920 xsrsqrtedp_2 = "f0000128Q-Q",
921 xssqrtdp_2 = "f000012cQ-Q",
922 xssubdp_3 = "f0000140QQQ",
923 xsmaddmdp_3 = "f0000148QQQ",
924 xscmpodp_3 = "f0000158XQQ",
925 xscvdpsxws_2 = "f0000160Q-Q",
926 xsrdpiz_2 = "f0000164Q-Q",
927 xsredp_2 = "f0000168Q-Q",
928 xsmuldp_3 = "f0000180QQQ",
929 xsmsubadp_3 = "f0000188QQQ",
930 xxmrglw_3 = "f0000190QQQ",
931 xsrdpip_2 = "f00001a4Q-Q",
932 xstsqrtdp_2 = "f00001a8X-Q",
933 xsrdpic_2 = "f00001acQ-Q",
934 xsdivdp_3 = "f00001c0QQQ",
935 xsmsubmdp_3 = "f00001c8QQQ",
936 xsrdpim_2 = "f00001e4Q-Q",
937 xstdivdp_3 = "f00001e8XQQ",
938 xvaddsp_3 = "f0000200QQQ",
939 xvmaddasp_3 = "f0000208QQQ",
940 xvcmpeqsp_3 = "f0000218QQQ",
941 xvcvspuxws_2 = "f0000220Q-Q",
942 xvrspi_2 = "f0000224Q-Q",
943 xvrsqrtesp_2 = "f0000228Q-Q",
944 xvsqrtsp_2 = "f000022cQ-Q",
945 xvsubsp_3 = "f0000240QQQ",
946 xvmaddmsp_3 = "f0000248QQQ",
947 xvcmpgtsp_3 = "f0000258QQQ",
948 xvcvspsxws_2 = "f0000260Q-Q",
949 xvrspiz_2 = "f0000264Q-Q",
950 xvresp_2 = "f0000268Q-Q",
951 xvmulsp_3 = "f0000280QQQ",
952 xvmsubasp_3 = "f0000288QQQ",
953 xxspltw_3 = "f0000290QQg~",
954 xvcmpgesp_3 = "f0000298QQQ",
955 xvcvuxwsp_2 = "f00002a0Q-Q",
956 xvrspip_2 = "f00002a4Q-Q",
957 xvtsqrtsp_2 = "f00002a8X-Q",
958 xvrspic_2 = "f00002acQ-Q",
959 xvdivsp_3 = "f00002c0QQQ",
960 xvmsubmsp_3 = "f00002c8QQQ",
961 xvcvsxwsp_2 = "f00002e0Q-Q",
962 xvrspim_2 = "f00002e4Q-Q",
963 xvtdivsp_3 = "f00002e8XQQ",
964 xvadddp_3 = "f0000300QQQ",
965 xvmaddadp_3 = "f0000308QQQ",
966 xvcmpeqdp_3 = "f0000318QQQ",
967 xvcvdpuxws_2 = "f0000320Q-Q",
968 xvrdpi_2 = "f0000324Q-Q",
969 xvrsqrtedp_2 = "f0000328Q-Q",
970 xvsqrtdp_2 = "f000032cQ-Q",
971 xvsubdp_3 = "f0000340QQQ",
972 xvmaddmdp_3 = "f0000348QQQ",
973 xvcmpgtdp_3 = "f0000358QQQ",
974 xvcvdpsxws_2 = "f0000360Q-Q",
975 xvrdpiz_2 = "f0000364Q-Q",
976 xvredp_2 = "f0000368Q-Q",
977 xvmuldp_3 = "f0000380QQQ",
978 xvmsubadp_3 = "f0000388QQQ",
979 xvcmpgedp_3 = "f0000398QQQ",
980 xvcvuxwdp_2 = "f00003a0Q-Q",
981 xvrdpip_2 = "f00003a4Q-Q",
982 xvtsqrtdp_2 = "f00003a8X-Q",
983 xvrdpic_2 = "f00003acQ-Q",
984 xvdivdp_3 = "f00003c0QQQ",
985 xvmsubmdp_3 = "f00003c8QQQ",
986 xvcvsxwdp_2 = "f00003e0Q-Q",
987 xvrdpim_2 = "f00003e4Q-Q",
988 xvtdivdp_3 = "f00003e8XQQ",
989 xsnmaddasp_3 = "f0000408QQQ",
990 xxland_3 = "f0000410QQQ",
991 xscvdpsp_2 = "f0000424Q-Q",
992 xscvdpspn_2 = "f000042cQ-Q",
993 xsnmaddmsp_3 = "f0000448QQQ",
994 xxlandc_3 = "f0000450QQQ",
995 xsrsp_2 = "f0000464Q-Q",
996 xsnmsubasp_3 = "f0000488QQQ",
997 xxlor_3 = "f0000490QQQ",
998 xscvuxdsp_2 = "f00004a0Q-Q",
999 xsnmsubmsp_3 = "f00004c8QQQ",
1000 xxlxor_3 = "f00004d0QQQ",
1001 xscvsxdsp_2 = "f00004e0Q-Q",
1002 xsmaxdp_3 = "f0000500QQQ",
1003 xsnmaddadp_3 = "f0000508QQQ",
1004 xxlnor_3 = "f0000510QQQ",
1005 xscvdpuxds_2 = "f0000520Q-Q",
1006 xscvspdp_2 = "f0000524Q-Q",
1007 xscvspdpn_2 = "f000052cQ-Q",
1008 xsmindp_3 = "f0000540QQQ",
1009 xsnmaddmdp_3 = "f0000548QQQ",
1010 xxlorc_3 = "f0000550QQQ",
1011 xscvdpsxds_2 = "f0000560Q-Q",
1012 xsabsdp_2 = "f0000564Q-Q",
1013 xscpsgndp_3 = "f0000580QQQ",
1014 xsnmsubadp_3 = "f0000588QQQ",
1015 xxlnand_3 = "f0000590QQQ",
1016 xscvuxddp_2 = "f00005a0Q-Q",
1017 xsnabsdp_2 = "f00005a4Q-Q",
1018 xsnmsubmdp_3 = "f00005c8QQQ",
1019 xxleqv_3 = "f00005d0QQQ",
1020 xscvsxddp_2 = "f00005e0Q-Q",
1021 xsnegdp_2 = "f00005e4Q-Q",
1022 xvmaxsp_3 = "f0000600QQQ",
1023 xvnmaddasp_3 = "f0000608QQQ",
1024 ["xvcmpeqsp._3"] = "f0000618QQQ",
1025 xvcvspuxds_2 = "f0000620Q-Q",
1026 xvcvdpsp_2 = "f0000624Q-Q",
1027 xvminsp_3 = "f0000640QQQ",
1028 xvnmaddmsp_3 = "f0000648QQQ",
1029 ["xvcmpgtsp._3"] = "f0000658QQQ",
1030 xvcvspsxds_2 = "f0000660Q-Q",
1031 xvabssp_2 = "f0000664Q-Q",
1032 xvcpsgnsp_3 = "f0000680QQQ",
1033 xvnmsubasp_3 = "f0000688QQQ",
1034 ["xvcmpgesp._3"] = "f0000698QQQ",
1035 xvcvuxdsp_2 = "f00006a0Q-Q",
1036 xvnabssp_2 = "f00006a4Q-Q",
1037 xvnmsubmsp_3 = "f00006c8QQQ",
1038 xvcvsxdsp_2 = "f00006e0Q-Q",
1039 xvnegsp_2 = "f00006e4Q-Q",
1040 xvmaxdp_3 = "f0000700QQQ",
1041 xvnmaddadp_3 = "f0000708QQQ",
1042 ["xvcmpeqdp._3"] = "f0000718QQQ",
1043 xvcvdpuxds_2 = "f0000720Q-Q",
1044 xvcvspdp_2 = "f0000724Q-Q",
1045 xvmindp_3 = "f0000740QQQ",
1046 xvnmaddmdp_3 = "f0000748QQQ",
1047 ["xvcmpgtdp._3"] = "f0000758QQQ",
1048 xvcvdpsxds_2 = "f0000760Q-Q",
1049 xvabsdp_2 = "f0000764Q-Q",
1050 xvcpsgndp_3 = "f0000780QQQ",
1051 xvnmsubadp_3 = "f0000788QQQ",
1052 ["xvcmpgedp._3"] = "f0000798QQQ",
1053 xvcvuxddp_2 = "f00007a0Q-Q",
1054 xvnabsdp_2 = "f00007a4Q-Q",
1055 xvnmsubmdp_3 = "f00007c8QQQ",
1056 xvcvsxddp_2 = "f00007e0Q-Q",
1057 xvnegdp_2 = "f00007e4Q-Q",
1058
1059 -- Primary opcode 61:
1060 stfdp_2 = "f4000000F:D", -- NYI: displacement must be divisible by 4.
1061
1062 -- Primary opcode 62:
1063 stq_2 = "f8000002R:D", -- NYI: displacement must be divisible by 8.
504 1064
505 -- Primary opcode 63: 1065 -- Primary opcode 63:
506 fdiv_3 = "fc000024FFF.", 1066 fdiv_3 = "fc000024FFF.",
@@ -526,8 +1086,12 @@ local map_op = {
526 frsp_2 = "fc000018F-F.", 1086 frsp_2 = "fc000018F-F.",
527 fctiw_2 = "fc00001cF-F.", 1087 fctiw_2 = "fc00001cF-F.",
528 fctiwz_2 = "fc00001eF-F.", 1088 fctiwz_2 = "fc00001eF-F.",
1089 ftdiv_2 = "fc000100X-F.",
1090 fctiwu_2 = "fc00011cF-F.",
1091 fctiwuz_2 = "fc00011eF-F.",
529 mtfsfi_2 = "fc00010cAA", -- NYI: upshift. 1092 mtfsfi_2 = "fc00010cAA", -- NYI: upshift.
530 fnabs_2 = "fc000110F-F.", 1093 fnabs_2 = "fc000110F-F.",
1094 ftsqrt_2 = "fc000140X-F.",
531 fabs_2 = "fc000210F-F.", 1095 fabs_2 = "fc000210F-F.",
532 frin_2 = "fc000310F-F.", 1096 frin_2 = "fc000310F-F.",
533 friz_2 = "fc000350F-F.", 1097 friz_2 = "fc000350F-F.",
@@ -537,7 +1101,38 @@ local map_op = {
537 -- NYI: mtfsf, mtfsb0, mtfsb1. 1101 -- NYI: mtfsf, mtfsb0, mtfsb1.
538 fctid_2 = "fc00065cF-F.", 1102 fctid_2 = "fc00065cF-F.",
539 fctidz_2 = "fc00065eF-F.", 1103 fctidz_2 = "fc00065eF-F.",
1104 fmrgow_3 = "fc00068cFFF",
540 fcfid_2 = "fc00069cF-F.", 1105 fcfid_2 = "fc00069cF-F.",
1106 fctidu_2 = "fc00075cF-F.",
1107 fctiduz_2 = "fc00075eF-F.",
1108 fmrgew_3 = "fc00078cFFF",
1109 fcfidu_2 = "fc00079cF-F.",
1110
1111 daddq_3 = "fc000004F:F:F:.",
1112 dquaq_4 = "fc000006F:F:F:Z.",
1113 dmulq_3 = "fc000044F:F:F:.",
1114 drrndq_4 = "fc000046F:F:F:Z.",
1115 dscliq_3 = "fc000084F:F:6.",
1116 dquaiq_4 = "fc000086SF:~F:Z.",
1117 dscriq_3 = "fc0000c4F:F:6.",
1118 drintxq_4 = "fc0000c61F:~F:Z.",
1119 dcmpoq_3 = "fc000104XF:F:",
1120 dtstexq_3 = "fc000144XF:F:",
1121 dtstdcq_3 = "fc000184XF:6",
1122 dtstdgq_3 = "fc0001c4XF:6",
1123 drintnq_4 = "fc0001c61F:~F:Z.",
1124 dctqpq_2 = "fc000204F:-F:.",
1125 dctfixq_2 = "fc000244F:-F:.",
1126 ddedpdq_3 = "fc000284ZF:~F:.",
1127 dxexq_2 = "fc0002c4F:-F:.",
1128 dsubq_3 = "fc000404F:F:F:.",
1129 ddivq_3 = "fc000444F:F:F:.",
1130 dcmpuq_3 = "fc000504XF:F:",
1131 dtstsfq_3 = "fc000544XF:F:",
1132 drdpq_2 = "fc000604F:-F:.",
1133 dcffixq_2 = "fc000644F:-F:.",
1134 denbcdq_3 = "fc000684YF:~F:.",
1135 diexq_3 = "fc0006c4F:FF:.",
541 1136
542 -- Primary opcode 4, SPE APU extension: 1137 -- Primary opcode 4, SPE APU extension:
543 evaddw_3 = "10000200RRR", 1138 evaddw_3 = "10000200RRR",
@@ -822,7 +1417,7 @@ local map_op = {
822do 1417do
823 local t = {} 1418 local t = {}
824 for k,v in pairs(map_op) do 1419 for k,v in pairs(map_op) do
825 if sub(v, -1) == "." then 1420 if type(v) == "string" and sub(v, -1) == "." then
826 local v2 = sub(v, 1, 7)..char(byte(v, 8)+1)..sub(v, 9, -2) 1421 local v2 = sub(v, 1, 7)..char(byte(v, 8)+1)..sub(v, 9, -2)
827 t[sub(k, 1, -3).."."..sub(k, -2)] = v2 1422 t[sub(k, 1, -3).."."..sub(k, -2)] = v2
828 end 1423 end
@@ -884,6 +1479,24 @@ local function parse_fpr(expr)
884 werror("bad register name `"..expr.."'") 1479 werror("bad register name `"..expr.."'")
885end 1480end
886 1481
1482local function parse_vr(expr)
1483 local r = match(expr, "^v([1-3]?[0-9])$")
1484 if r then
1485 r = tonumber(r)
1486 if r <= 31 then return r end
1487 end
1488 werror("bad register name `"..expr.."'")
1489end
1490
1491local function parse_vs(expr)
1492 local r = match(expr, "^vs([1-6]?[0-9])$")
1493 if r then
1494 r = tonumber(r)
1495 if r <= 63 then return r end
1496 end
1497 werror("bad register name `"..expr.."'")
1498end
1499
887local function parse_cr(expr) 1500local function parse_cr(expr)
888 local r = match(expr, "^cr([0-7])$") 1501 local r = match(expr, "^cr([0-7])$")
889 if r then return tonumber(r) end 1502 if r then return tonumber(r) end
@@ -900,8 +1513,30 @@ local function parse_cond(expr)
900 werror("bad condition bit name `"..expr.."'") 1513 werror("bad condition bit name `"..expr.."'")
901end 1514end
902 1515
1516local parse_ctx = {}
1517
1518local loadenv = setfenv and function(s)
1519 local code = loadstring(s, "")
1520 if code then setfenv(code, parse_ctx) end
1521 return code
1522end or function(s)
1523 return load(s, "", nil, parse_ctx)
1524end
1525
1526-- Try to parse simple arithmetic, too, since some basic ops are aliases.
1527local function parse_number(n)
1528 local x = tonumber(n)
1529 if x then return x end
1530 local code = loadenv("return "..n)
1531 if code then
1532 local ok, y = pcall(code)
1533 if ok then return y end
1534 end
1535 return nil
1536end
1537
903local function parse_imm(imm, bits, shift, scale, signed) 1538local function parse_imm(imm, bits, shift, scale, signed)
904 local n = tonumber(imm) 1539 local n = parse_number(imm)
905 if n then 1540 if n then
906 local m = sar(n, scale) 1541 local m = sar(n, scale)
907 if shl(m, scale) == n then 1542 if shl(m, scale) == n then
@@ -914,7 +1549,8 @@ local function parse_imm(imm, bits, shift, scale, signed)
914 end 1549 end
915 end 1550 end
916 werror("out of range immediate `"..imm.."'") 1551 werror("out of range immediate `"..imm.."'")
917 elseif match(imm, "^r([1-3]?[0-9])$") or 1552 elseif match(imm, "^[rfv]([1-3]?[0-9])$") or
1553 match(imm, "^vs([1-6]?[0-9])$") or
918 match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then 1554 match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then
919 werror("expected immediate operand, got register") 1555 werror("expected immediate operand, got register")
920 else 1556 else
@@ -924,11 +1560,11 @@ local function parse_imm(imm, bits, shift, scale, signed)
924end 1560end
925 1561
926local function parse_shiftmask(imm, isshift) 1562local function parse_shiftmask(imm, isshift)
927 local n = tonumber(imm) 1563 local n = parse_number(imm)
928 if n then 1564 if n then
929 if shr(n, 6) == 0 then 1565 if shr(n, 6) == 0 then
930 local lsb = band(imm, 31) 1566 local lsb = band(n, 31)
931 local msb = imm - lsb 1567 local msb = n - lsb
932 return isshift and (shl(lsb, 11)+shr(msb, 4)) or (shl(lsb, 6)+msb) 1568 return isshift and (shl(lsb, 11)+shr(msb, 4)) or (shl(lsb, 6)+msb)
933 end 1569 end
934 werror("out of range immediate `"..imm.."'") 1570 werror("out of range immediate `"..imm.."'")
@@ -936,7 +1572,8 @@ local function parse_shiftmask(imm, isshift)
936 match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then 1572 match(imm, "^([%w_]+):(r[1-3]?[0-9])$") then
937 werror("expected immediate operand, got register") 1573 werror("expected immediate operand, got register")
938 else 1574 else
939 werror("NYI: parameterized 64 bit shift/mask") 1575 waction("IMMSH", isshift and 1 or 0, imm)
1576 return 0;
940 end 1577 end
941end 1578end
942 1579
@@ -1011,7 +1648,7 @@ end
1011------------------------------------------------------------------------------ 1648------------------------------------------------------------------------------
1012 1649
1013-- Handle opcodes defined with template strings. 1650-- Handle opcodes defined with template strings.
1014map_op[".template__"] = function(params, template, nparams) 1651op_template = function(params, template, nparams)
1015 if not params then return sub(template, 9) end 1652 if not params then return sub(template, 9) end
1016 local op = tonumber(sub(template, 1, 8), 16) 1653 local op = tonumber(sub(template, 1, 8), 16)
1017 local n, rs = 1, 26 1654 local n, rs = 1, 26
@@ -1027,6 +1664,15 @@ map_op[".template__"] = function(params, template, nparams)
1027 rs = rs - 5; op = op + shl(parse_gpr(params[n]), rs); n = n + 1 1664 rs = rs - 5; op = op + shl(parse_gpr(params[n]), rs); n = n + 1
1028 elseif p == "F" then 1665 elseif p == "F" then
1029 rs = rs - 5; op = op + shl(parse_fpr(params[n]), rs); n = n + 1 1666 rs = rs - 5; op = op + shl(parse_fpr(params[n]), rs); n = n + 1
1667 elseif p == "V" then
1668 rs = rs - 5; op = op + shl(parse_vr(params[n]), rs); n = n + 1
1669 elseif p == "Q" then
1670 local vs = parse_vs(params[n]); n = n + 1; rs = rs - 5
1671 local sh = rs == 6 and 2 or 3 + band(shr(rs, 1), 3)
1672 op = op + shl(band(vs, 31), rs) + shr(band(vs, 32), sh)
1673 elseif p == "q" then
1674 local vs = parse_vs(params[n]); n = n + 1
1675 op = op + shl(band(vs, 31), 21) + shr(band(vs, 32), 5)
1030 elseif p == "A" then 1676 elseif p == "A" then
1031 rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, false); n = n + 1 1677 rs = rs - 5; op = op + parse_imm(params[n], 5, rs, 0, false); n = n + 1
1032 elseif p == "S" then 1678 elseif p == "S" then
@@ -1047,6 +1693,26 @@ map_op[".template__"] = function(params, template, nparams)
1047 rs = rs - 5; op = op + shl(parse_cond(params[n]), rs); n = n + 1 1693 rs = rs - 5; op = op + shl(parse_cond(params[n]), rs); n = n + 1
1048 elseif p == "X" then 1694 elseif p == "X" then
1049 rs = rs - 5; op = op + shl(parse_cr(params[n]), rs+2); n = n + 1 1695 rs = rs - 5; op = op + shl(parse_cr(params[n]), rs+2); n = n + 1
1696 elseif p == "1" then
1697 rs = rs - 5; op = op + parse_imm(params[n], 1, rs, 0, false); n = n + 1
1698 elseif p == "g" then
1699 rs = rs - 5; op = op + parse_imm(params[n], 2, rs, 0, false); n = n + 1
1700 elseif p == "3" then
1701 rs = rs - 5; op = op + parse_imm(params[n], 3, rs, 0, false); n = n + 1
1702 elseif p == "P" then
1703 rs = rs - 5; op = op + parse_imm(params[n], 4, rs, 0, false); n = n + 1
1704 elseif p == "p" then
1705 op = op + parse_imm(params[n], 4, rs, 0, false); n = n + 1
1706 elseif p == "6" then
1707 rs = rs - 6; op = op + parse_imm(params[n], 6, rs, 0, false); n = n + 1
1708 elseif p == "Y" then
1709 rs = rs - 5; op = op + parse_imm(params[n], 1, rs+4, 0, false); n = n + 1
1710 elseif p == "y" then
1711 rs = rs - 5; op = op + parse_imm(params[n], 1, rs+3, 0, false); n = n + 1
1712 elseif p == "Z" then
1713 rs = rs - 5; op = op + parse_imm(params[n], 2, rs+3, 0, false); n = n + 1
1714 elseif p == "z" then
1715 rs = rs - 5; op = op + parse_imm(params[n], 2, rs+2, 0, false); n = n + 1
1050 elseif p == "W" then 1716 elseif p == "W" then
1051 op = op + parse_cr(params[n]); n = n + 1 1717 op = op + parse_cr(params[n]); n = n + 1
1052 elseif p == "G" then 1718 elseif p == "G" then
@@ -1071,6 +1737,8 @@ map_op[".template__"] = function(params, template, nparams)
1071 local lo = band(op, mm) 1737 local lo = band(op, mm)
1072 local hi = band(op, shl(mm, 5)) 1738 local hi = band(op, shl(mm, 5))
1073 op = op - lo - hi + shl(lo, 5) + shr(hi, 5) 1739 op = op - lo - hi + shl(lo, 5) + shr(hi, 5)
1740 elseif p == ":" then
1741 if band(shr(op, rs), 1) ~= 0 then werror("register pair expected") end
1074 elseif p == "-" then 1742 elseif p == "-" then
1075 rs = rs - 5 1743 rs = rs - 5
1076 elseif p == "." then 1744 elseif p == "." then
@@ -1082,6 +1750,8 @@ map_op[".template__"] = function(params, template, nparams)
1082 wputpos(pos, op) 1750 wputpos(pos, op)
1083end 1751end
1084 1752
1753map_op[".template__"] = op_template
1754
1085------------------------------------------------------------------------------ 1755------------------------------------------------------------------------------
1086 1756
1087-- Pseudo-opcode to mark the position where the action list is to be emitted. 1757-- Pseudo-opcode to mark the position where the action list is to be emitted.
diff --git a/dynasm/dasm_proto.h b/dynasm/dasm_proto.h
index a7278e85..ba038e87 100644
--- a/dynasm/dasm_proto.h
+++ b/dynasm/dasm_proto.h
@@ -10,8 +10,8 @@
10#include <stddef.h> 10#include <stddef.h>
11#include <stdarg.h> 11#include <stdarg.h>
12 12
13#define DASM_IDENT "DynASM 1.3.0" 13#define DASM_IDENT "DynASM 1.4.0"
14#define DASM_VERSION 10300 /* 1.3.0 */ 14#define DASM_VERSION 10400 /* 1.4.0 */
15 15
16#ifndef Dst_DECL 16#ifndef Dst_DECL
17#define Dst_DECL dasm_State **Dst 17#define Dst_DECL dasm_State **Dst
diff --git a/dynasm/dasm_x86.h b/dynasm/dasm_x86.h
index 84b9d17f..edaddf54 100644
--- a/dynasm/dasm_x86.h
+++ b/dynasm/dasm_x86.h
@@ -170,7 +170,7 @@ void dasm_put(Dst_DECL, int start, ...)
170 dasm_State *D = Dst_REF; 170 dasm_State *D = Dst_REF;
171 dasm_ActList p = D->actionlist + start; 171 dasm_ActList p = D->actionlist + start;
172 dasm_Section *sec = D->section; 172 dasm_Section *sec = D->section;
173 int pos = sec->pos, ofs = sec->ofs, mrm = 4; 173 int pos = sec->pos, ofs = sec->ofs, mrm = -1;
174 int *b; 174 int *b;
175 175
176 if (pos >= sec->epos) { 176 if (pos >= sec->epos) {
@@ -193,7 +193,7 @@ void dasm_put(Dst_DECL, int start, ...)
193 b[pos++] = n; 193 b[pos++] = n;
194 switch (action) { 194 switch (action) {
195 case DASM_DISP: 195 case DASM_DISP:
196 if (n == 0) { if ((mrm&7) == 4) mrm = p[-2]; if ((mrm&7) != 5) break; } 196 if (n == 0) { if (mrm < 0) mrm = p[-2]; if ((mrm&7) != 5) break; }
197 /* fallthrough */ 197 /* fallthrough */
198 case DASM_IMM_DB: if (((n+128)&-256) == 0) goto ob; /* fallthrough */ 198 case DASM_IMM_DB: if (((n+128)&-256) == 0) goto ob; /* fallthrough */
199 case DASM_REL_A: /* Assumes ptrdiff_t is int. !x64 */ 199 case DASM_REL_A: /* Assumes ptrdiff_t is int. !x64 */
@@ -204,11 +204,17 @@ void dasm_put(Dst_DECL, int start, ...)
204 case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break; 204 case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break;
205 case DASM_SPACE: p++; ofs += n; break; 205 case DASM_SPACE: p++; ofs += n; break;
206 case DASM_SETLABEL: b[pos-2] = -0x40000000; break; /* Neg. label ofs. */ 206 case DASM_SETLABEL: b[pos-2] = -0x40000000; break; /* Neg. label ofs. */
207 case DASM_VREG: CK((n&-8) == 0 && (n != 4 || (*p&1) == 0), RANGE_VREG); 207 case DASM_VREG: CK((n&-16) == 0 && (n != 4 || (*p>>5) != 2), RANGE_VREG);
208 if (*p++ == 1 && *p == DASM_DISP) mrm = n; 208 if (*p < 0x40 && p[1] == DASM_DISP) mrm = n;
209 if (*p < 0x20 && (n&7) == 4) ofs++;
210 switch ((*p++ >> 3) & 3) {
211 case 3: n |= b[pos-3]; /* fallthrough */
212 case 2: n |= b[pos-2]; /* fallthrough */
213 case 1: if (n <= 7) { b[pos-1] |= 0x10; ofs--; }
214 }
209 continue; 215 continue;
210 } 216 }
211 mrm = 4; 217 mrm = -1;
212 } else { 218 } else {
213 int *pl, n; 219 int *pl, n;
214 switch (action) { 220 switch (action) {
@@ -399,7 +405,27 @@ int dasm_encode(Dst_DECL, void *buffer)
399 case DASM_IMM_WB: if (((n+128)&-256) == 0) goto db; else mark = NULL; 405 case DASM_IMM_WB: if (((n+128)&-256) == 0) goto db; else mark = NULL;
400 /* fallthrough */ 406 /* fallthrough */
401 case DASM_IMM_W: dasmw(n); break; 407 case DASM_IMM_W: dasmw(n); break;
402 case DASM_VREG: { int t = *p++; if (t >= 2) n<<=3; cp[-1] |= n; break; } 408 case DASM_VREG: {
409 int t = *p++;
410 unsigned char *ex = cp - (t&7);
411 if ((n & 8) && t < 0xa0) {
412 if (*ex & 0x80) ex[1] ^= 0x20 << (t>>6); else *ex ^= 1 << (t>>6);
413 n &= 7;
414 } else if (n & 0x10) {
415 if (*ex & 0x80) {
416 *ex = 0xc5; ex[1] = (ex[1] & 0x80) | ex[2]; ex += 2;
417 }
418 while (++ex < cp) ex[-1] = *ex;
419 if (mark) mark--;
420 cp--;
421 n &= 7;
422 }
423 if (t >= 0xc0) n <<= 4;
424 else if (t >= 0x40) n <<= 3;
425 else if (n == 4 && t < 0x20) { cp[-1] ^= n; *cp++ = 0x20; }
426 cp[-1] ^= n;
427 break;
428 }
403 case DASM_REL_LG: p++; if (n >= 0) goto rel_pc; 429 case DASM_REL_LG: p++; if (n >= 0) goto rel_pc;
404 b++; n = (int)(ptrdiff_t)D->globals[-n]; 430 b++; n = (int)(ptrdiff_t)D->globals[-n];
405 /* fallthrough */ 431 /* fallthrough */
diff --git a/dynasm/dasm_x86.lua b/dynasm/dasm_x86.lua
index 13aa68ff..c5c8c17b 100644
--- a/dynasm/dasm_x86.lua
+++ b/dynasm/dasm_x86.lua
@@ -11,9 +11,9 @@ local x64 = x64
11local _info = { 11local _info = {
12 arch = x64 and "x64" or "x86", 12 arch = x64 and "x64" or "x86",
13 description = "DynASM x86/x64 module", 13 description = "DynASM x86/x64 module",
14 version = "1.3.0", 14 version = "1.4.0",
15 vernum = 10300, 15 vernum = 10400,
16 release = "2011-05-05", 16 release = "2015-10-18",
17 author = "Mike Pall", 17 author = "Mike Pall",
18 license = "MIT", 18 license = "MIT",
19} 19}
@@ -27,9 +27,9 @@ local assert, unpack, setmetatable = assert, unpack or table.unpack, setmetatabl
27local _s = string 27local _s = string
28local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char 28local sub, format, byte, char = _s.sub, _s.format, _s.byte, _s.char
29local find, match, gmatch, gsub = _s.find, _s.match, _s.gmatch, _s.gsub 29local find, match, gmatch, gsub = _s.find, _s.match, _s.gmatch, _s.gsub
30local concat, sort = table.concat, table.sort 30local concat, sort, remove = table.concat, table.sort, table.remove
31local bit = bit or require("bit") 31local bit = bit or require("bit")
32local band, shl, shr = bit.band, bit.lshift, bit.rshift 32local band, bxor, shl, shr = bit.band, bit.bxor, bit.lshift, bit.rshift
33 33
34-- Inherited tables and callbacks. 34-- Inherited tables and callbacks.
35local g_opt, g_arch 35local g_opt, g_arch
@@ -41,7 +41,7 @@ local action_names = {
41 -- int arg, 1 buffer pos: 41 -- int arg, 1 buffer pos:
42 "DISP", "IMM_S", "IMM_B", "IMM_W", "IMM_D", "IMM_WB", "IMM_DB", 42 "DISP", "IMM_S", "IMM_B", "IMM_W", "IMM_D", "IMM_WB", "IMM_DB",
43 -- action arg (1 byte), int arg, 1 buffer pos (reg/num): 43 -- action arg (1 byte), int arg, 1 buffer pos (reg/num):
44 "VREG", "SPACE", -- !x64: VREG support NYI. 44 "VREG", "SPACE",
45 -- ptrdiff_t arg, 1 buffer pos (address): !x64 45 -- ptrdiff_t arg, 1 buffer pos (address): !x64
46 "SETLABEL", "REL_A", 46 "SETLABEL", "REL_A",
47 -- action arg (1 byte) or int arg, 2 buffer pos (link, offset): 47 -- action arg (1 byte) or int arg, 2 buffer pos (link, offset):
@@ -83,6 +83,21 @@ local actargs = { 0 }
83-- Current number of section buffer positions for dasm_put(). 83-- Current number of section buffer positions for dasm_put().
84local secpos = 1 84local secpos = 1
85 85
86-- VREG kind encodings, pre-shifted by 5 bits.
87local map_vreg = {
88 ["modrm.rm.m"] = 0x00,
89 ["modrm.rm.r"] = 0x20,
90 ["opcode"] = 0x20,
91 ["sib.base"] = 0x20,
92 ["sib.index"] = 0x40,
93 ["modrm.reg"] = 0x80,
94 ["vex.v"] = 0xa0,
95 ["imm.hi"] = 0xc0,
96}
97
98-- Current number of VREG actions contributing to REX/VEX shrinkage.
99local vreg_shrink_count = 0
100
86------------------------------------------------------------------------------ 101------------------------------------------------------------------------------
87 102
88-- Compute action numbers for action names. 103-- Compute action numbers for action names.
@@ -134,6 +149,21 @@ local function waction(action, a, num)
134 if a or num then secpos = secpos + (num or 1) end 149 if a or num then secpos = secpos + (num or 1) end
135end 150end
136 151
152-- Optionally add a VREG action.
153local function wvreg(kind, vreg, psz, sk, defer)
154 if not vreg then return end
155 waction("VREG", vreg)
156 local b = assert(map_vreg[kind], "bad vreg kind `"..vreg.."'")
157 if b < (sk or 0) then
158 vreg_shrink_count = vreg_shrink_count + 1
159 end
160 if not defer then
161 b = b + vreg_shrink_count * 8
162 vreg_shrink_count = 0
163 end
164 wputxb(b + (psz or 0))
165end
166
137-- Add call to embedded DynASM C code. 167-- Add call to embedded DynASM C code.
138local function wcall(func, args) 168local function wcall(func, args)
139 wline(format("dasm_%s(Dst, %s);", func, concat(args, ", ")), true) 169 wline(format("dasm_%s(Dst, %s);", func, concat(args, ", ")), true)
@@ -299,7 +329,7 @@ local function mkrmap(sz, cl, names)
299 local iname = format("@%s%x%s", sz, i, needrex and "R" or "") 329 local iname = format("@%s%x%s", sz, i, needrex and "R" or "")
300 if needrex then map_reg_needrex[iname] = true end 330 if needrex then map_reg_needrex[iname] = true end
301 local name 331 local name
302 if sz == "o" then name = format("xmm%d", i) 332 if sz == "o" or sz == "y" then name = format("%s%d", cl, i)
303 elseif sz == "f" then name = format("st%d", i) 333 elseif sz == "f" then name = format("st%d", i)
304 else name = format("r%d%s", i, sz == addrsize and "" or sz) end 334 else name = format("r%d%s", i, sz == addrsize and "" or sz) end
305 map_archdef[name] = iname 335 map_archdef[name] = iname
@@ -326,6 +356,7 @@ mkrmap("w", "Rw", {"ax", "cx", "dx", "bx", "sp", "bp", "si", "di"})
326mkrmap("b", "Rb", {"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"}) 356mkrmap("b", "Rb", {"al", "cl", "dl", "bl", "ah", "ch", "dh", "bh"})
327map_reg_valid_index[map_archdef.esp] = false 357map_reg_valid_index[map_archdef.esp] = false
328if x64 then map_reg_valid_index[map_archdef.rsp] = false end 358if x64 then map_reg_valid_index[map_archdef.rsp] = false end
359if x64 then map_reg_needrex[map_archdef.Rb] = true end
329map_archdef["Ra"] = "@"..addrsize 360map_archdef["Ra"] = "@"..addrsize
330 361
331-- FP registers (internally tword sized, but use "f" as operand size). 362-- FP registers (internally tword sized, but use "f" as operand size).
@@ -334,21 +365,24 @@ mkrmap("f", "Rf")
334-- SSE registers (oword sized, but qword and dword accessible). 365-- SSE registers (oword sized, but qword and dword accessible).
335mkrmap("o", "xmm") 366mkrmap("o", "xmm")
336 367
368-- AVX registers (yword sized, but oword, qword and dword accessible).
369mkrmap("y", "ymm")
370
337-- Operand size prefixes to codes. 371-- Operand size prefixes to codes.
338local map_opsize = { 372local map_opsize = {
339 byte = "b", word = "w", dword = "d", qword = "q", oword = "o", tword = "t", 373 byte = "b", word = "w", dword = "d", qword = "q", oword = "o", yword = "y",
340 aword = addrsize, 374 tword = "t", aword = addrsize,
341} 375}
342 376
343-- Operand size code to number. 377-- Operand size code to number.
344local map_opsizenum = { 378local map_opsizenum = {
345 b = 1, w = 2, d = 4, q = 8, o = 16, t = 10, 379 b = 1, w = 2, d = 4, q = 8, o = 16, y = 32, t = 10,
346} 380}
347 381
348-- Operand size code to name. 382-- Operand size code to name.
349local map_opsizename = { 383local map_opsizename = {
350 b = "byte", w = "word", d = "dword", q = "qword", o = "oword", t = "tword", 384 b = "byte", w = "word", d = "dword", q = "qword", o = "oword", y = "yword",
351 f = "fpword", 385 t = "tword", f = "fpword",
352} 386}
353 387
354-- Valid index register scale factors. 388-- Valid index register scale factors.
@@ -460,9 +494,45 @@ local function wputszarg(sz, n)
460end 494end
461 495
462-- Put multi-byte opcode with operand-size dependent modifications. 496-- Put multi-byte opcode with operand-size dependent modifications.
463local function wputop(sz, op, rex) 497local function wputop(sz, op, rex, vex, vregr, vregxb)
498 local psz, sk = 0, nil
499 if vex then
500 local tail
501 if vex.m == 1 and band(rex, 11) == 0 then
502 if x64 and vregxb then
503 sk = map_vreg["modrm.reg"]
504 else
505 wputb(0xc5)
506 tail = shl(bxor(band(rex, 4), 4), 5)
507 psz = 3
508 end
509 end
510 if not tail then
511 wputb(0xc4)
512 wputb(shl(bxor(band(rex, 7), 7), 5) + vex.m)
513 tail = shl(band(rex, 8), 4)
514 psz = 4
515 end
516 local reg, vreg = 0, nil
517 if vex.v then
518 reg = vex.v.reg
519 if not reg then werror("bad vex operand") end
520 if reg < 0 then reg = 0; vreg = vex.v.vreg end
521 end
522 if sz == "y" or vex.l then tail = tail + 4 end
523 wputb(tail + shl(bxor(reg, 15), 3) + vex.p)
524 wvreg("vex.v", vreg)
525 rex = 0
526 if op >= 256 then werror("bad vex opcode") end
527 else
528 if rex ~= 0 then
529 if not x64 then werror("bad operand size") end
530 elseif (vregr or vregxb) and x64 then
531 rex = 0x10
532 sk = map_vreg["vex.v"]
533 end
534 end
464 local r 535 local r
465 if rex ~= 0 and not x64 then werror("bad operand size") end
466 if sz == "w" then wputb(102) end 536 if sz == "w" then wputb(102) end
467 -- Needs >32 bit numbers, but only for crc32 eax, word [ebx] 537 -- Needs >32 bit numbers, but only for crc32 eax, word [ebx]
468 if op >= 4294967296 then r = op%4294967296 wputb((op-r)/4294967296) op = r end 538 if op >= 4294967296 then r = op%4294967296 wputb((op-r)/4294967296) op = r end
@@ -471,20 +541,20 @@ local function wputop(sz, op, rex)
471 if rex ~= 0 then 541 if rex ~= 0 then
472 local opc3 = band(op, 0xffff00) 542 local opc3 = band(op, 0xffff00)
473 if opc3 == 0x0f3a00 or opc3 == 0x0f3800 then 543 if opc3 == 0x0f3a00 or opc3 == 0x0f3800 then
474 wputb(64 + band(rex, 15)); rex = 0 544 wputb(64 + band(rex, 15)); rex = 0; psz = 2
475 end 545 end
476 end 546 end
477 wputb(shr(op, 16)); op = band(op, 0xffff) 547 wputb(shr(op, 16)); op = band(op, 0xffff); psz = psz + 1
478 end 548 end
479 if op >= 256 then 549 if op >= 256 then
480 local b = shr(op, 8) 550 local b = shr(op, 8)
481 if b == 15 and rex ~= 0 then wputb(64 + band(rex, 15)); rex = 0 end 551 if b == 15 and rex ~= 0 then wputb(64 + band(rex, 15)); rex = 0; psz = 2 end
482 wputb(b) 552 wputb(b); op = band(op, 255); psz = psz + 1
483 op = band(op, 255)
484 end 553 end
485 if rex ~= 0 then wputb(64 + band(rex, 15)) end 554 if rex ~= 0 then wputb(64 + band(rex, 15)); psz = 2 end
486 if sz == "b" then op = op - 1 end 555 if sz == "b" then op = op - 1 end
487 wputb(op) 556 wputb(op)
557 return psz, sk
488end 558end
489 559
490-- Put ModRM or SIB formatted byte. 560-- Put ModRM or SIB formatted byte.
@@ -494,7 +564,7 @@ local function wputmodrm(m, s, rm, vs, vrm)
494end 564end
495 565
496-- Put ModRM/SIB plus optional displacement. 566-- Put ModRM/SIB plus optional displacement.
497local function wputmrmsib(t, imark, s, vsreg) 567local function wputmrmsib(t, imark, s, vsreg, psz, sk)
498 local vreg, vxreg 568 local vreg, vxreg
499 local reg, xreg = t.reg, t.xreg 569 local reg, xreg = t.reg, t.xreg
500 if reg and reg < 0 then reg = 0; vreg = t.vreg end 570 if reg and reg < 0 then reg = 0; vreg = t.vreg end
@@ -504,8 +574,8 @@ local function wputmrmsib(t, imark, s, vsreg)
504 -- Register mode. 574 -- Register mode.
505 if sub(t.mode, 1, 1) == "r" then 575 if sub(t.mode, 1, 1) == "r" then
506 wputmodrm(3, s, reg) 576 wputmodrm(3, s, reg)
507 if vsreg then waction("VREG", vsreg); wputxb(2) end 577 wvreg("modrm.reg", vsreg, psz+1, sk, vreg)
508 if vreg then waction("VREG", vreg); wputxb(0) end 578 wvreg("modrm.rm.r", vreg, psz+1, sk)
509 return 579 return
510 end 580 end
511 581
@@ -519,21 +589,22 @@ local function wputmrmsib(t, imark, s, vsreg)
519 -- [xreg*xsc+disp] -> (0, s, esp) (xsc, xreg, ebp) 589 -- [xreg*xsc+disp] -> (0, s, esp) (xsc, xreg, ebp)
520 wputmodrm(0, s, 4) 590 wputmodrm(0, s, 4)
521 if imark == "I" then waction("MARK") end 591 if imark == "I" then waction("MARK") end
522 if vsreg then waction("VREG", vsreg); wputxb(2) end 592 wvreg("modrm.reg", vsreg, psz+1, sk, vxreg)
523 wputmodrm(t.xsc, xreg, 5) 593 wputmodrm(t.xsc, xreg, 5)
524 if vxreg then waction("VREG", vxreg); wputxb(3) end 594 wvreg("sib.index", vxreg, psz+2, sk)
525 else 595 else
526 -- Pure 32 bit displacement. 596 -- Pure 32 bit displacement.
527 if x64 and tdisp ~= "table" then 597 if x64 and tdisp ~= "table" then
528 wputmodrm(0, s, 4) -- [disp] -> (0, s, esp) (0, esp, ebp) 598 wputmodrm(0, s, 4) -- [disp] -> (0, s, esp) (0, esp, ebp)
599 wvreg("modrm.reg", vsreg, psz+1, sk)
529 if imark == "I" then waction("MARK") end 600 if imark == "I" then waction("MARK") end
530 wputmodrm(0, 4, 5) 601 wputmodrm(0, 4, 5)
531 else 602 else
532 riprel = x64 603 riprel = x64
533 wputmodrm(0, s, 5) -- [disp|rip-label] -> (0, s, ebp) 604 wputmodrm(0, s, 5) -- [disp|rip-label] -> (0, s, ebp)
605 wvreg("modrm.reg", vsreg, psz+1, sk)
534 if imark == "I" then waction("MARK") end 606 if imark == "I" then waction("MARK") end
535 end 607 end
536 if vsreg then waction("VREG", vsreg); wputxb(2) end
537 end 608 end
538 if riprel then -- Emit rip-relative displacement. 609 if riprel then -- Emit rip-relative displacement.
539 if match("UWSiI", imark) then 610 if match("UWSiI", imark) then
@@ -561,16 +632,16 @@ local function wputmrmsib(t, imark, s, vsreg)
561 if xreg or band(reg, 7) == 4 then 632 if xreg or band(reg, 7) == 4 then
562 wputmodrm(m or 2, s, 4) -- ModRM. 633 wputmodrm(m or 2, s, 4) -- ModRM.
563 if m == nil or imark == "I" then waction("MARK") end 634 if m == nil or imark == "I" then waction("MARK") end
564 if vsreg then waction("VREG", vsreg); wputxb(2) end 635 wvreg("modrm.reg", vsreg, psz+1, sk, vxreg or vreg)
565 wputmodrm(t.xsc or 0, xreg or 4, reg) -- SIB. 636 wputmodrm(t.xsc or 0, xreg or 4, reg) -- SIB.
566 if vxreg then waction("VREG", vxreg); wputxb(3) end 637 wvreg("sib.index", vxreg, psz+2, sk, vreg)
567 if vreg then waction("VREG", vreg); wputxb(1) end 638 wvreg("sib.base", vreg, psz+2, sk)
568 else 639 else
569 wputmodrm(m or 2, s, reg) -- ModRM. 640 wputmodrm(m or 2, s, reg) -- ModRM.
570 if (imark == "I" and (m == 1 or m == 2)) or 641 if (imark == "I" and (m == 1 or m == 2)) or
571 (m == nil and (vsreg or vreg)) then waction("MARK") end 642 (m == nil and (vsreg or vreg)) then waction("MARK") end
572 if vsreg then waction("VREG", vsreg); wputxb(2) end 643 wvreg("modrm.reg", vsreg, psz+1, sk, vreg)
573 if vreg then waction("VREG", vreg); wputxb(1) end 644 wvreg("modrm.rm.m", vreg, psz+1, sk)
574 end 645 end
575 646
576 -- Put displacement. 647 -- Put displacement.
@@ -881,9 +952,16 @@ end
881-- "m"/"M" generates ModRM/SIB from the 1st/2nd operand. 952-- "m"/"M" generates ModRM/SIB from the 1st/2nd operand.
882-- The spare 3 bits are either filled with the last hex digit or 953-- The spare 3 bits are either filled with the last hex digit or
883-- the result from a previous "r"/"R". The opcode is restored. 954-- the result from a previous "r"/"R". The opcode is restored.
955-- "u" Use VEX encoding, vvvv unused.
956-- "v"/"V" Use VEX encoding, vvvv from 1st/2nd operand (the operand is
957-- removed from the list used by future characters).
958-- "w" Use VEX encoding, vvvv from 3rd operand.
959-- "L" Force VEX.L
884-- 960--
885-- All of the following characters force a flush of the opcode: 961-- All of the following characters force a flush of the opcode:
886-- "o"/"O" stores a pure 32 bit disp (offset) from the 1st/2nd operand. 962-- "o"/"O" stores a pure 32 bit disp (offset) from the 1st/2nd operand.
963-- "s" stores a 4 bit immediate from the last register operand,
964-- followed by 4 zero bits.
887-- "S" stores a signed 8 bit immediate from the last operand. 965-- "S" stores a signed 8 bit immediate from the last operand.
888-- "U" stores an unsigned 8 bit immediate from the last operand. 966-- "U" stores an unsigned 8 bit immediate from the last operand.
889-- "W" stores an unsigned 16 bit immediate from the last operand. 967-- "W" stores an unsigned 16 bit immediate from the last operand.
@@ -1226,46 +1304,14 @@ local map_op = {
1226 movups_2 = "rmo:0F10rM|mro:0F11Rm", 1304 movups_2 = "rmo:0F10rM|mro:0F11Rm",
1227 orpd_2 = "rmo:660F56rM", 1305 orpd_2 = "rmo:660F56rM",
1228 orps_2 = "rmo:0F56rM", 1306 orps_2 = "rmo:0F56rM",
1229 packssdw_2 = "rmo:660F6BrM",
1230 packsswb_2 = "rmo:660F63rM",
1231 packuswb_2 = "rmo:660F67rM",
1232 paddb_2 = "rmo:660FFCrM",
1233 paddd_2 = "rmo:660FFErM",
1234 paddq_2 = "rmo:660FD4rM",
1235 paddsb_2 = "rmo:660FECrM",
1236 paddsw_2 = "rmo:660FEDrM",
1237 paddusb_2 = "rmo:660FDCrM",
1238 paddusw_2 = "rmo:660FDDrM",
1239 paddw_2 = "rmo:660FFDrM",
1240 pand_2 = "rmo:660FDBrM",
1241 pandn_2 = "rmo:660FDFrM",
1242 pause_0 = "F390", 1307 pause_0 = "F390",
1243 pavgb_2 = "rmo:660FE0rM",
1244 pavgw_2 = "rmo:660FE3rM",
1245 pcmpeqb_2 = "rmo:660F74rM",
1246 pcmpeqd_2 = "rmo:660F76rM",
1247 pcmpeqw_2 = "rmo:660F75rM",
1248 pcmpgtb_2 = "rmo:660F64rM",
1249 pcmpgtd_2 = "rmo:660F66rM",
1250 pcmpgtw_2 = "rmo:660F65rM",
1251 pextrw_3 = "rri/do:660FC5rMU|xri/wo:660F3A15nRmU", -- Mem op: SSE4.1 only. 1308 pextrw_3 = "rri/do:660FC5rMU|xri/wo:660F3A15nRmU", -- Mem op: SSE4.1 only.
1252 pinsrw_3 = "rri/od:660FC4rMU|rxi/ow:", 1309 pinsrw_3 = "rri/od:660FC4rMU|rxi/ow:",
1253 pmaddwd_2 = "rmo:660FF5rM",
1254 pmaxsw_2 = "rmo:660FEErM",
1255 pmaxub_2 = "rmo:660FDErM",
1256 pminsw_2 = "rmo:660FEArM",
1257 pminub_2 = "rmo:660FDArM",
1258 pmovmskb_2 = "rr/do:660FD7rM", 1310 pmovmskb_2 = "rr/do:660FD7rM",
1259 pmulhuw_2 = "rmo:660FE4rM",
1260 pmulhw_2 = "rmo:660FE5rM",
1261 pmullw_2 = "rmo:660FD5rM",
1262 pmuludq_2 = "rmo:660FF4rM",
1263 por_2 = "rmo:660FEBrM",
1264 prefetchnta_1 = "xb:n0F180m", 1311 prefetchnta_1 = "xb:n0F180m",
1265 prefetcht0_1 = "xb:n0F181m", 1312 prefetcht0_1 = "xb:n0F181m",
1266 prefetcht1_1 = "xb:n0F182m", 1313 prefetcht1_1 = "xb:n0F182m",
1267 prefetcht2_1 = "xb:n0F183m", 1314 prefetcht2_1 = "xb:n0F183m",
1268 psadbw_2 = "rmo:660FF6rM",
1269 pshufd_3 = "rmio:660F70rMU", 1315 pshufd_3 = "rmio:660F70rMU",
1270 pshufhw_3 = "rmio:F30F70rMU", 1316 pshufhw_3 = "rmio:F30F70rMU",
1271 pshuflw_3 = "rmio:F20F70rMU", 1317 pshuflw_3 = "rmio:F20F70rMU",
@@ -1279,23 +1325,6 @@ local map_op = {
1279 psrldq_2 = "rio:660F733mU", 1325 psrldq_2 = "rio:660F733mU",
1280 psrlq_2 = "rmo:660FD3rM|rio:660F732mU", 1326 psrlq_2 = "rmo:660FD3rM|rio:660F732mU",
1281 psrlw_2 = "rmo:660FD1rM|rio:660F712mU", 1327 psrlw_2 = "rmo:660FD1rM|rio:660F712mU",
1282 psubb_2 = "rmo:660FF8rM",
1283 psubd_2 = "rmo:660FFArM",
1284 psubq_2 = "rmo:660FFBrM",
1285 psubsb_2 = "rmo:660FE8rM",
1286 psubsw_2 = "rmo:660FE9rM",
1287 psubusb_2 = "rmo:660FD8rM",
1288 psubusw_2 = "rmo:660FD9rM",
1289 psubw_2 = "rmo:660FF9rM",
1290 punpckhbw_2 = "rmo:660F68rM",
1291 punpckhdq_2 = "rmo:660F6ArM",
1292 punpckhqdq_2 = "rmo:660F6DrM",
1293 punpckhwd_2 = "rmo:660F69rM",
1294 punpcklbw_2 = "rmo:660F60rM",
1295 punpckldq_2 = "rmo:660F62rM",
1296 punpcklqdq_2 = "rmo:660F6CrM",
1297 punpcklwd_2 = "rmo:660F61rM",
1298 pxor_2 = "rmo:660FEFrM",
1299 rcpps_2 = "rmo:0F53rM", 1328 rcpps_2 = "rmo:0F53rM",
1300 rcpss_2 = "rro:F30F53rM|rx/od:", 1329 rcpss_2 = "rro:F30F53rM|rx/od:",
1301 rsqrtps_2 = "rmo:0F52rM", 1330 rsqrtps_2 = "rmo:0F52rM",
@@ -1413,6 +1442,327 @@ local map_op = {
1413 movntsd_2 = "xr/qo:nF20F2BRm", 1442 movntsd_2 = "xr/qo:nF20F2BRm",
1414 movntss_2 = "xr/do:F30F2BRm", 1443 movntss_2 = "xr/do:F30F2BRm",
1415 -- popcnt is also in SSE4.2 1444 -- popcnt is also in SSE4.2
1445
1446 -- AES-NI
1447 aesdec_2 = "rmo:660F38DErM",
1448 aesdeclast_2 = "rmo:660F38DFrM",
1449 aesenc_2 = "rmo:660F38DCrM",
1450 aesenclast_2 = "rmo:660F38DDrM",
1451 aesimc_2 = "rmo:660F38DBrM",
1452 aeskeygenassist_3 = "rmio:660F3ADFrMU",
1453 pclmulqdq_3 = "rmio:660F3A44rMU",
1454
1455 -- AVX FP ops
1456 vaddsubpd_3 = "rrmoy:660FVD0rM",
1457 vaddsubps_3 = "rrmoy:F20FVD0rM",
1458 vandpd_3 = "rrmoy:660FV54rM",
1459 vandps_3 = "rrmoy:0FV54rM",
1460 vandnpd_3 = "rrmoy:660FV55rM",
1461 vandnps_3 = "rrmoy:0FV55rM",
1462 vblendpd_4 = "rrmioy:660F3AV0DrMU",
1463 vblendps_4 = "rrmioy:660F3AV0CrMU",
1464 vblendvpd_4 = "rrmroy:660F3AV4BrMs",
1465 vblendvps_4 = "rrmroy:660F3AV4ArMs",
1466 vbroadcastf128_2 = "rx/yo:660F38u1ArM",
1467 vcmppd_4 = "rrmioy:660FVC2rMU",
1468 vcmpps_4 = "rrmioy:0FVC2rMU",
1469 vcmpsd_4 = "rrrio:F20FVC2rMU|rrxi/ooq:",
1470 vcmpss_4 = "rrrio:F30FVC2rMU|rrxi/ood:",
1471 vcomisd_2 = "rro:660Fu2FrM|rx/oq:",
1472 vcomiss_2 = "rro:0Fu2FrM|rx/od:",
1473 vcvtdq2pd_2 = "rro:F30FuE6rM|rx/oq:|rm/yo:",
1474 vcvtdq2ps_2 = "rmoy:0Fu5BrM",
1475 vcvtpd2dq_2 = "rmoy:F20FuE6rM",
1476 vcvtpd2ps_2 = "rmoy:660Fu5ArM",
1477 vcvtps2dq_2 = "rmoy:660Fu5BrM",
1478 vcvtps2pd_2 = "rro:0Fu5ArM|rx/oq:|rm/yo:",
1479 vcvtsd2si_2 = "rr/do:F20Fu2DrM|rx/dq:|rr/qo:|rxq:",
1480 vcvtsd2ss_3 = "rrro:F20FV5ArM|rrx/ooq:",
1481 vcvtsi2sd_3 = "rrm/ood:F20FV2ArM|rrm/ooq:F20FVX2ArM",
1482 vcvtsi2ss_3 = "rrm/ood:F30FV2ArM|rrm/ooq:F30FVX2ArM",
1483 vcvtss2sd_3 = "rrro:F30FV5ArM|rrx/ood:",
1484 vcvtss2si_2 = "rr/do:F30Fu2DrM|rxd:|rr/qo:|rx/qd:",
1485 vcvttpd2dq_2 = "rmo:660FuE6rM|rm/oy:660FuLE6rM",
1486 vcvttps2dq_2 = "rmoy:F30Fu5BrM",
1487 vcvttsd2si_2 = "rr/do:F20Fu2CrM|rx/dq:|rr/qo:|rxq:",
1488 vcvttss2si_2 = "rr/do:F30Fu2CrM|rxd:|rr/qo:|rx/qd:",
1489 vdppd_4 = "rrmio:660F3AV41rMU",
1490 vdpps_4 = "rrmioy:660F3AV40rMU",
1491 vextractf128_3 = "mri/oy:660F3AuL19RmU",
1492 vextractps_3 = "mri/do:660F3Au17RmU",
1493 vhaddpd_3 = "rrmoy:660FV7CrM",
1494 vhaddps_3 = "rrmoy:F20FV7CrM",
1495 vhsubpd_3 = "rrmoy:660FV7DrM",
1496 vhsubps_3 = "rrmoy:F20FV7DrM",
1497 vinsertf128_4 = "rrmi/yyo:660F3AV18rMU",
1498 vinsertps_4 = "rrrio:660F3AV21rMU|rrxi/ood:",
1499 vldmxcsr_1 = "xd:0FuAE2m",
1500 vmaskmovps_3 = "rrxoy:660F38V2CrM|xrroy:660F38V2ERm",
1501 vmaskmovpd_3 = "rrxoy:660F38V2DrM|xrroy:660F38V2FRm",
1502 vmovapd_2 = "rmoy:660Fu28rM|mroy:660Fu29Rm",
1503 vmovaps_2 = "rmoy:0Fu28rM|mroy:0Fu29Rm",
1504 vmovd_2 = "rm/od:660Fu6ErM|rm/oq:660FuX6ErM|mr/do:660Fu7ERm|mr/qo:",
1505 vmovq_2 = "rro:F30Fu7ErM|rx/oq:|xr/qo:660FuD6Rm",
1506 vmovddup_2 = "rmy:F20Fu12rM|rro:|rx/oq:",
1507 vmovhlps_3 = "rrro:0FV12rM",
1508 vmovhpd_2 = "xr/qo:660Fu17Rm",
1509 vmovhpd_3 = "rrx/ooq:660FV16rM",
1510 vmovhps_2 = "xr/qo:0Fu17Rm",
1511 vmovhps_3 = "rrx/ooq:0FV16rM",
1512 vmovlhps_3 = "rrro:0FV16rM",
1513 vmovlpd_2 = "xr/qo:660Fu13Rm",
1514 vmovlpd_3 = "rrx/ooq:660FV12rM",
1515 vmovlps_2 = "xr/qo:0Fu13Rm",
1516 vmovlps_3 = "rrx/ooq:0FV12rM",
1517 vmovmskpd_2 = "rr/do:660Fu50rM|rr/dy:660FuL50rM",
1518 vmovmskps_2 = "rr/do:0Fu50rM|rr/dy:0FuL50rM",
1519 vmovntpd_2 = "xroy:660Fu2BRm",
1520 vmovntps_2 = "xroy:0Fu2BRm",
1521 vmovsd_2 = "rx/oq:F20Fu10rM|xr/qo:F20Fu11Rm",
1522 vmovsd_3 = "rrro:F20FV10rM",
1523 vmovshdup_2 = "rmoy:F30Fu16rM",
1524 vmovsldup_2 = "rmoy:F30Fu12rM",
1525 vmovss_2 = "rx/od:F30Fu10rM|xr/do:F30Fu11Rm",
1526 vmovss_3 = "rrro:F30FV10rM",
1527 vmovupd_2 = "rmoy:660Fu10rM|mroy:660Fu11Rm",
1528 vmovups_2 = "rmoy:0Fu10rM|mroy:0Fu11Rm",
1529 vorpd_3 = "rrmoy:660FV56rM",
1530 vorps_3 = "rrmoy:0FV56rM",
1531 vpermilpd_3 = "rrmoy:660F38V0DrM|rmioy:660F3Au05rMU",
1532 vpermilps_3 = "rrmoy:660F38V0CrM|rmioy:660F3Au04rMU",
1533 vperm2f128_4 = "rrmiy:660F3AV06rMU",
1534 vptestpd_2 = "rmoy:660F38u0FrM",
1535 vptestps_2 = "rmoy:660F38u0ErM",
1536 vrcpps_2 = "rmoy:0Fu53rM",
1537 vrcpss_3 = "rrro:F30FV53rM|rrx/ood:",
1538 vrsqrtps_2 = "rmoy:0Fu52rM",
1539 vrsqrtss_3 = "rrro:F30FV52rM|rrx/ood:",
1540 vroundpd_3 = "rmioy:660F3Au09rMU",
1541 vroundps_3 = "rmioy:660F3Au08rMU",
1542 vroundsd_4 = "rrrio:660F3AV0BrMU|rrxi/ooq:",
1543 vroundss_4 = "rrrio:660F3AV0ArMU|rrxi/ood:",
1544 vshufpd_4 = "rrmioy:660FVC6rMU",
1545 vshufps_4 = "rrmioy:0FVC6rMU",
1546 vsqrtps_2 = "rmoy:0Fu51rM",
1547 vsqrtss_2 = "rro:F30Fu51rM|rx/od:",
1548 vsqrtpd_2 = "rmoy:660Fu51rM",
1549 vsqrtsd_2 = "rro:F20Fu51rM|rx/oq:",
1550 vstmxcsr_1 = "xd:0FuAE3m",
1551 vucomisd_2 = "rro:660Fu2ErM|rx/oq:",
1552 vucomiss_2 = "rro:0Fu2ErM|rx/od:",
1553 vunpckhpd_3 = "rrmoy:660FV15rM",
1554 vunpckhps_3 = "rrmoy:0FV15rM",
1555 vunpcklpd_3 = "rrmoy:660FV14rM",
1556 vunpcklps_3 = "rrmoy:0FV14rM",
1557 vxorpd_3 = "rrmoy:660FV57rM",
1558 vxorps_3 = "rrmoy:0FV57rM",
1559 vzeroall_0 = "0FuL77",
1560 vzeroupper_0 = "0Fu77",
1561
1562 -- AVX2 FP ops
1563 vbroadcastss_2 = "rx/od:660F38u18rM|rx/yd:|rro:|rr/yo:",
1564 vbroadcastsd_2 = "rx/yq:660F38u19rM|rr/yo:",
1565 -- *vgather* (!vsib)
1566 vpermpd_3 = "rmiy:660F3AuX01rMU",
1567 vpermps_3 = "rrmy:660F38V16rM",
1568
1569 -- AVX, AVX2 integer ops
1570 -- In general, xmm requires AVX, ymm requires AVX2.
1571 vaesdec_3 = "rrmo:660F38VDErM",
1572 vaesdeclast_3 = "rrmo:660F38VDFrM",
1573 vaesenc_3 = "rrmo:660F38VDCrM",
1574 vaesenclast_3 = "rrmo:660F38VDDrM",
1575 vaesimc_2 = "rmo:660F38uDBrM",
1576 vaeskeygenassist_3 = "rmio:660F3AuDFrMU",
1577 vlddqu_2 = "rxoy:F20FuF0rM",
1578 vmaskmovdqu_2 = "rro:660FuF7rM",
1579 vmovdqa_2 = "rmoy:660Fu6FrM|mroy:660Fu7FRm",
1580 vmovdqu_2 = "rmoy:F30Fu6FrM|mroy:F30Fu7FRm",
1581 vmovntdq_2 = "xroy:660FuE7Rm",
1582 vmovntdqa_2 = "rxoy:660F38u2ArM",
1583 vmpsadbw_4 = "rrmioy:660F3AV42rMU",
1584 vpabsb_2 = "rmoy:660F38u1CrM",
1585 vpabsd_2 = "rmoy:660F38u1ErM",
1586 vpabsw_2 = "rmoy:660F38u1DrM",
1587 vpackusdw_3 = "rrmoy:660F38V2BrM",
1588 vpalignr_4 = "rrmioy:660F3AV0FrMU",
1589 vpblendvb_4 = "rrmroy:660F3AV4CrMs",
1590 vpblendw_4 = "rrmioy:660F3AV0ErMU",
1591 vpclmulqdq_4 = "rrmio:660F3AV44rMU",
1592 vpcmpeqq_3 = "rrmoy:660F38V29rM",
1593 vpcmpestri_3 = "rmio:660F3Au61rMU",
1594 vpcmpestrm_3 = "rmio:660F3Au60rMU",
1595 vpcmpgtq_3 = "rrmoy:660F38V37rM",
1596 vpcmpistri_3 = "rmio:660F3Au63rMU",
1597 vpcmpistrm_3 = "rmio:660F3Au62rMU",
1598 vpextrb_3 = "rri/do:660F3Au14nRmU|rri/qo:|xri/bo:",
1599 vpextrw_3 = "rri/do:660FuC5rMU|xri/wo:660F3Au15nRmU",
1600 vpextrd_3 = "mri/do:660F3Au16RmU",
1601 vpextrq_3 = "mri/qo:660F3Au16RmU",
1602 vphaddw_3 = "rrmoy:660F38V01rM",
1603 vphaddd_3 = "rrmoy:660F38V02rM",
1604 vphaddsw_3 = "rrmoy:660F38V03rM",
1605 vphminposuw_2 = "rmo:660F38u41rM",
1606 vphsubw_3 = "rrmoy:660F38V05rM",
1607 vphsubd_3 = "rrmoy:660F38V06rM",
1608 vphsubsw_3 = "rrmoy:660F38V07rM",
1609 vpinsrb_4 = "rrri/ood:660F3AV20rMU|rrxi/oob:",
1610 vpinsrw_4 = "rrri/ood:660FVC4rMU|rrxi/oow:",
1611 vpinsrd_4 = "rrmi/ood:660F3AV22rMU",
1612 vpinsrq_4 = "rrmi/ooq:660F3AVX22rMU",
1613 vpmaddubsw_3 = "rrmoy:660F38V04rM",
1614 vpmaxsb_3 = "rrmoy:660F38V3CrM",
1615 vpmaxsd_3 = "rrmoy:660F38V3DrM",
1616 vpmaxuw_3 = "rrmoy:660F38V3ErM",
1617 vpmaxud_3 = "rrmoy:660F38V3FrM",
1618 vpminsb_3 = "rrmoy:660F38V38rM",
1619 vpminsd_3 = "rrmoy:660F38V39rM",
1620 vpminuw_3 = "rrmoy:660F38V3ArM",
1621 vpminud_3 = "rrmoy:660F38V3BrM",
1622 vpmovmskb_2 = "rr/do:660FuD7rM|rr/dy:660FuLD7rM",
1623 vpmovsxbw_2 = "rroy:660F38u20rM|rx/oq:|rx/yo:",
1624 vpmovsxbd_2 = "rroy:660F38u21rM|rx/od:|rx/yq:",
1625 vpmovsxbq_2 = "rroy:660F38u22rM|rx/ow:|rx/yd:",
1626 vpmovsxwd_2 = "rroy:660F38u23rM|rx/oq:|rx/yo:",
1627 vpmovsxwq_2 = "rroy:660F38u24rM|rx/od:|rx/yq:",
1628 vpmovsxdq_2 = "rroy:660F38u25rM|rx/oq:|rx/yo:",
1629 vpmovzxbw_2 = "rroy:660F38u30rM|rx/oq:|rx/yo:",
1630 vpmovzxbd_2 = "rroy:660F38u31rM|rx/od:|rx/yq:",
1631 vpmovzxbq_2 = "rroy:660F38u32rM|rx/ow:|rx/yd:",
1632 vpmovzxwd_2 = "rroy:660F38u33rM|rx/oq:|rx/yo:",
1633 vpmovzxwq_2 = "rroy:660F38u34rM|rx/od:|rx/yq:",
1634 vpmovzxdq_2 = "rroy:660F38u35rM|rx/oq:|rx/yo:",
1635 vpmuldq_3 = "rrmoy:660F38V28rM",
1636 vpmulhrsw_3 = "rrmoy:660F38V0BrM",
1637 vpmulld_3 = "rrmoy:660F38V40rM",
1638 vpshufb_3 = "rrmoy:660F38V00rM",
1639 vpshufd_3 = "rmioy:660Fu70rMU",
1640 vpshufhw_3 = "rmioy:F30Fu70rMU",
1641 vpshuflw_3 = "rmioy:F20Fu70rMU",
1642 vpsignb_3 = "rrmoy:660F38V08rM",
1643 vpsignw_3 = "rrmoy:660F38V09rM",
1644 vpsignd_3 = "rrmoy:660F38V0ArM",
1645 vpslldq_3 = "rrioy:660Fv737mU",
1646 vpsllw_3 = "rrmoy:660FVF1rM|rrioy:660Fv716mU",
1647 vpslld_3 = "rrmoy:660FVF2rM|rrioy:660Fv726mU",
1648 vpsllq_3 = "rrmoy:660FVF3rM|rrioy:660Fv736mU",
1649 vpsraw_3 = "rrmoy:660FVE1rM|rrioy:660Fv714mU",
1650 vpsrad_3 = "rrmoy:660FVE2rM|rrioy:660Fv724mU",
1651 vpsrldq_3 = "rrioy:660Fv733mU",
1652 vpsrlw_3 = "rrmoy:660FVD1rM|rrioy:660Fv712mU",
1653 vpsrld_3 = "rrmoy:660FVD2rM|rrioy:660Fv722mU",
1654 vpsrlq_3 = "rrmoy:660FVD3rM|rrioy:660Fv732mU",
1655 vptest_2 = "rmoy:660F38u17rM",
1656
1657 -- AVX2 integer ops
1658 vbroadcasti128_2 = "rx/yo:660F38u5ArM",
1659 vinserti128_4 = "rrmi/yyo:660F3AV38rMU",
1660 vextracti128_3 = "mri/oy:660F3AuL39RmU",
1661 vpblendd_4 = "rrmioy:660F3AV02rMU",
1662 vpbroadcastb_2 = "rro:660F38u78rM|rx/ob:|rr/yo:|rx/yb:",
1663 vpbroadcastw_2 = "rro:660F38u79rM|rx/ow:|rr/yo:|rx/yw:",
1664 vpbroadcastd_2 = "rro:660F38u58rM|rx/od:|rr/yo:|rx/yd:",
1665 vpbroadcastq_2 = "rro:660F38u59rM|rx/oq:|rr/yo:|rx/yq:",
1666 vpermd_3 = "rrmy:660F38V36rM",
1667 vpermq_3 = "rmiy:660F3AuX00rMU",
1668 -- *vpgather* (!vsib)
1669 vperm2i128_4 = "rrmiy:660F3AV46rMU",
1670 vpmaskmovd_3 = "rrxoy:660F38V8CrM|xrroy:660F38V8ERm",
1671 vpmaskmovq_3 = "rrxoy:660F38VX8CrM|xrroy:660F38VX8ERm",
1672 vpsllvd_3 = "rrmoy:660F38V47rM",
1673 vpsllvq_3 = "rrmoy:660F38VX47rM",
1674 vpsravd_3 = "rrmoy:660F38V46rM",
1675 vpsrlvd_3 = "rrmoy:660F38V45rM",
1676 vpsrlvq_3 = "rrmoy:660F38VX45rM",
1677
1678 -- Intel ADX
1679 adcx_2 = "rmqd:660F38F6rM",
1680 adox_2 = "rmqd:F30F38F6rM",
1681
1682 -- BMI1
1683 andn_3 = "rrmqd:0F38VF2rM",
1684 bextr_3 = "rmrqd:0F38wF7rM",
1685 blsi_2 = "rmqd:0F38vF33m",
1686 blsmsk_2 = "rmqd:0F38vF32m",
1687 blsr_2 = "rmqd:0F38vF31m",
1688 tzcnt_2 = "rmqdw:F30FBCrM",
1689
1690 -- BMI2
1691 bzhi_3 = "rmrqd:0F38wF5rM",
1692 mulx_3 = "rrmqd:F20F38VF6rM",
1693 pdep_3 = "rrmqd:F20F38VF5rM",
1694 pext_3 = "rrmqd:F30F38VF5rM",
1695 rorx_3 = "rmSqd:F20F3AuF0rMS",
1696 sarx_3 = "rmrqd:F30F38wF7rM",
1697 shrx_3 = "rmrqd:F20F38wF7rM",
1698 shlx_3 = "rmrqd:660F38wF7rM",
1699
1700 -- FMA3
1701 vfmaddsub132pd_3 = "rrmoy:660F38VX96rM",
1702 vfmaddsub132ps_3 = "rrmoy:660F38V96rM",
1703 vfmaddsub213pd_3 = "rrmoy:660F38VXA6rM",
1704 vfmaddsub213ps_3 = "rrmoy:660F38VA6rM",
1705 vfmaddsub231pd_3 = "rrmoy:660F38VXB6rM",
1706 vfmaddsub231ps_3 = "rrmoy:660F38VB6rM",
1707
1708 vfmsubadd132pd_3 = "rrmoy:660F38VX97rM",
1709 vfmsubadd132ps_3 = "rrmoy:660F38V97rM",
1710 vfmsubadd213pd_3 = "rrmoy:660F38VXA7rM",
1711 vfmsubadd213ps_3 = "rrmoy:660F38VA7rM",
1712 vfmsubadd231pd_3 = "rrmoy:660F38VXB7rM",
1713 vfmsubadd231ps_3 = "rrmoy:660F38VB7rM",
1714
1715 vfmadd132pd_3 = "rrmoy:660F38VX98rM",
1716 vfmadd132ps_3 = "rrmoy:660F38V98rM",
1717 vfmadd132sd_3 = "rrro:660F38VX99rM|rrx/ooq:",
1718 vfmadd132ss_3 = "rrro:660F38V99rM|rrx/ood:",
1719 vfmadd213pd_3 = "rrmoy:660F38VXA8rM",
1720 vfmadd213ps_3 = "rrmoy:660F38VA8rM",
1721 vfmadd213sd_3 = "rrro:660F38VXA9rM|rrx/ooq:",
1722 vfmadd213ss_3 = "rrro:660F38VA9rM|rrx/ood:",
1723 vfmadd231pd_3 = "rrmoy:660F38VXB8rM",
1724 vfmadd231ps_3 = "rrmoy:660F38VB8rM",
1725 vfmadd231sd_3 = "rrro:660F38VXB9rM|rrx/ooq:",
1726 vfmadd231ss_3 = "rrro:660F38VB9rM|rrx/ood:",
1727
1728 vfmsub132pd_3 = "rrmoy:660F38VX9ArM",
1729 vfmsub132ps_3 = "rrmoy:660F38V9ArM",
1730 vfmsub132sd_3 = "rrro:660F38VX9BrM|rrx/ooq:",
1731 vfmsub132ss_3 = "rrro:660F38V9BrM|rrx/ood:",
1732 vfmsub213pd_3 = "rrmoy:660F38VXAArM",
1733 vfmsub213ps_3 = "rrmoy:660F38VAArM",
1734 vfmsub213sd_3 = "rrro:660F38VXABrM|rrx/ooq:",
1735 vfmsub213ss_3 = "rrro:660F38VABrM|rrx/ood:",
1736 vfmsub231pd_3 = "rrmoy:660F38VXBArM",
1737 vfmsub231ps_3 = "rrmoy:660F38VBArM",
1738 vfmsub231sd_3 = "rrro:660F38VXBBrM|rrx/ooq:",
1739 vfmsub231ss_3 = "rrro:660F38VBBrM|rrx/ood:",
1740
1741 vfnmadd132pd_3 = "rrmoy:660F38VX9CrM",
1742 vfnmadd132ps_3 = "rrmoy:660F38V9CrM",
1743 vfnmadd132sd_3 = "rrro:660F38VX9DrM|rrx/ooq:",
1744 vfnmadd132ss_3 = "rrro:660F38V9DrM|rrx/ood:",
1745 vfnmadd213pd_3 = "rrmoy:660F38VXACrM",
1746 vfnmadd213ps_3 = "rrmoy:660F38VACrM",
1747 vfnmadd213sd_3 = "rrro:660F38VXADrM|rrx/ooq:",
1748 vfnmadd213ss_3 = "rrro:660F38VADrM|rrx/ood:",
1749 vfnmadd231pd_3 = "rrmoy:660F38VXBCrM",
1750 vfnmadd231ps_3 = "rrmoy:660F38VBCrM",
1751 vfnmadd231sd_3 = "rrro:660F38VXBDrM|rrx/ooq:",
1752 vfnmadd231ss_3 = "rrro:660F38VBDrM|rrx/ood:",
1753
1754 vfnmsub132pd_3 = "rrmoy:660F38VX9ErM",
1755 vfnmsub132ps_3 = "rrmoy:660F38V9ErM",
1756 vfnmsub132sd_3 = "rrro:660F38VX9FrM|rrx/ooq:",
1757 vfnmsub132ss_3 = "rrro:660F38V9FrM|rrx/ood:",
1758 vfnmsub213pd_3 = "rrmoy:660F38VXAErM",
1759 vfnmsub213ps_3 = "rrmoy:660F38VAErM",
1760 vfnmsub213sd_3 = "rrro:660F38VXAFrM|rrx/ooq:",
1761 vfnmsub213ss_3 = "rrro:660F38VAFrM|rrx/ood:",
1762 vfnmsub231pd_3 = "rrmoy:660F38VXBErM",
1763 vfnmsub231ps_3 = "rrmoy:660F38VBErM",
1764 vfnmsub231sd_3 = "rrro:660F38VXBFrM|rrx/ooq:",
1765 vfnmsub231ss_3 = "rrro:660F38VBFrM|rrx/ood:",
1416} 1766}
1417 1767
1418------------------------------------------------------------------------------ 1768------------------------------------------------------------------------------
@@ -1463,28 +1813,58 @@ for cc,n in pairs{ b=0, e=1, be=2, u=3, nb=4, ne=5, nbe=6, nu=7 } do
1463 map_op["fcmov"..cc.."_2"] = format("Fff:%04XR", nc) -- P6+ 1813 map_op["fcmov"..cc.."_2"] = format("Fff:%04XR", nc) -- P6+
1464end 1814end
1465 1815
1466-- SSE FP arithmetic ops. 1816-- SSE / AVX FP arithmetic ops.
1467for name,n in pairs{ sqrt = 1, add = 8, mul = 9, 1817for name,n in pairs{ sqrt = 1, add = 8, mul = 9,
1468 sub = 12, min = 13, div = 14, max = 15 } do 1818 sub = 12, min = 13, div = 14, max = 15 } do
1469 map_op[name.."ps_2"] = format("rmo:0F5%XrM", n) 1819 map_op[name.."ps_2"] = format("rmo:0F5%XrM", n)
1470 map_op[name.."ss_2"] = format("rro:F30F5%XrM|rx/od:", n) 1820 map_op[name.."ss_2"] = format("rro:F30F5%XrM|rx/od:", n)
1471 map_op[name.."pd_2"] = format("rmo:660F5%XrM", n) 1821 map_op[name.."pd_2"] = format("rmo:660F5%XrM", n)
1472 map_op[name.."sd_2"] = format("rro:F20F5%XrM|rx/oq:", n) 1822 map_op[name.."sd_2"] = format("rro:F20F5%XrM|rx/oq:", n)
1823 if n ~= 1 then
1824 map_op["v"..name.."ps_3"] = format("rrmoy:0FV5%XrM", n)
1825 map_op["v"..name.."ss_3"] = format("rrro:F30FV5%XrM|rrx/ood:", n)
1826 map_op["v"..name.."pd_3"] = format("rrmoy:660FV5%XrM", n)
1827 map_op["v"..name.."sd_3"] = format("rrro:F20FV5%XrM|rrx/ooq:", n)
1828 end
1829end
1830
1831-- SSE2 / AVX / AVX2 integer arithmetic ops (66 0F leaf).
1832for name,n in pairs{
1833 paddb = 0xFC, paddw = 0xFD, paddd = 0xFE, paddq = 0xD4,
1834 paddsb = 0xEC, paddsw = 0xED, packssdw = 0x6B,
1835 packsswb = 0x63, packuswb = 0x67, paddusb = 0xDC,
1836 paddusw = 0xDD, pand = 0xDB, pandn = 0xDF, pavgb = 0xE0,
1837 pavgw = 0xE3, pcmpeqb = 0x74, pcmpeqd = 0x76,
1838 pcmpeqw = 0x75, pcmpgtb = 0x64, pcmpgtd = 0x66,
1839 pcmpgtw = 0x65, pmaddwd = 0xF5, pmaxsw = 0xEE,
1840 pmaxub = 0xDE, pminsw = 0xEA, pminub = 0xDA,
1841 pmulhuw = 0xE4, pmulhw = 0xE5, pmullw = 0xD5,
1842 pmuludq = 0xF4, por = 0xEB, psadbw = 0xF6, psubb = 0xF8,
1843 psubw = 0xF9, psubd = 0xFA, psubq = 0xFB, psubsb = 0xE8,
1844 psubsw = 0xE9, psubusb = 0xD8, psubusw = 0xD9,
1845 punpckhbw = 0x68, punpckhwd = 0x69, punpckhdq = 0x6A,
1846 punpckhqdq = 0x6D, punpcklbw = 0x60, punpcklwd = 0x61,
1847 punpckldq = 0x62, punpcklqdq = 0x6C, pxor = 0xEF
1848} do
1849 map_op[name.."_2"] = format("rmo:660F%02XrM", n)
1850 map_op["v"..name.."_3"] = format("rrmoy:660FV%02XrM", n)
1473end 1851end
1474 1852
1475------------------------------------------------------------------------------ 1853------------------------------------------------------------------------------
1476 1854
1855local map_vexarg = { u = false, v = 1, V = 2, w = 3 }
1856
1477-- Process pattern string. 1857-- Process pattern string.
1478local function dopattern(pat, args, sz, op, needrex) 1858local function dopattern(pat, args, sz, op, needrex)
1479 local digit, addin 1859 local digit, addin, vex
1480 local opcode = 0 1860 local opcode = 0
1481 local szov = sz 1861 local szov = sz
1482 local narg = 1 1862 local narg = 1
1483 local rex = 0 1863 local rex = 0
1484 1864
1485 -- Limit number of section buffer positions used by a single dasm_put(). 1865 -- Limit number of section buffer positions used by a single dasm_put().
1486 -- A single opcode needs a maximum of 5 positions. 1866 -- A single opcode needs a maximum of 6 positions.
1487 if secpos+5 > maxsecpos then wflush() end 1867 if secpos+6 > maxsecpos then wflush() end
1488 1868
1489 -- Process each character. 1869 -- Process each character.
1490 for c in gmatch(pat.."|", ".") do 1870 for c in gmatch(pat.."|", ".") do
@@ -1498,6 +1878,8 @@ local function dopattern(pat, args, sz, op, needrex)
1498 szov = nil 1878 szov = nil
1499 elseif c == "X" then -- Force REX.W. 1879 elseif c == "X" then -- Force REX.W.
1500 rex = 8 1880 rex = 8
1881 elseif c == "L" then -- Force VEX.L.
1882 vex.l = true
1501 elseif c == "r" then -- Merge 1st operand regno. into opcode. 1883 elseif c == "r" then -- Merge 1st operand regno. into opcode.
1502 addin = args[1]; opcode = opcode + (addin.reg % 8) 1884 addin = args[1]; opcode = opcode + (addin.reg % 8)
1503 if narg < 2 then narg = 2 end 1885 if narg < 2 then narg = 2 end
@@ -1521,21 +1903,42 @@ local function dopattern(pat, args, sz, op, needrex)
1521 if t.xreg and t.xreg > 7 then rex = rex + 2 end 1903 if t.xreg and t.xreg > 7 then rex = rex + 2 end
1522 if s > 7 then rex = rex + 4 end 1904 if s > 7 then rex = rex + 4 end
1523 if needrex then rex = rex + 16 end 1905 if needrex then rex = rex + 16 end
1524 wputop(szov, opcode, rex); opcode = nil 1906 local psz, sk = wputop(szov, opcode, rex, vex, s < 0, t.vreg or t.vxreg)
1907 opcode = nil
1525 local imark = sub(pat, -1) -- Force a mark (ugly). 1908 local imark = sub(pat, -1) -- Force a mark (ugly).
1526 -- Put ModRM/SIB with regno/last digit as spare. 1909 -- Put ModRM/SIB with regno/last digit as spare.
1527 wputmrmsib(t, imark, s, addin and addin.vreg) 1910 wputmrmsib(t, imark, s, addin and addin.vreg, psz, sk)
1528 addin = nil 1911 addin = nil
1912 elseif map_vexarg[c] ~= nil then -- Encode using VEX prefix
1913 local b = band(opcode, 255); opcode = shr(opcode, 8)
1914 local m = 1
1915 if b == 0x38 then m = 2
1916 elseif b == 0x3a then m = 3 end
1917 if m ~= 1 then b = band(opcode, 255); opcode = shr(opcode, 8) end
1918 if b ~= 0x0f then
1919 werror("expected `0F', `0F38', or `0F3A' to precede `"..c..
1920 "' in pattern `"..pat.."' for `"..op.."'")
1921 end
1922 local v = map_vexarg[c]
1923 if v then v = remove(args, v) end
1924 b = band(opcode, 255)
1925 local p = 0
1926 if b == 0x66 then p = 1
1927 elseif b == 0xf3 then p = 2
1928 elseif b == 0xf2 then p = 3 end
1929 if p ~= 0 then opcode = shr(opcode, 8) end
1930 if opcode ~= 0 then wputop(nil, opcode, 0); opcode = 0 end
1931 vex = { m = m, p = p, v = v }
1529 else 1932 else
1530 if opcode then -- Flush opcode. 1933 if opcode then -- Flush opcode.
1531 if szov == "q" and rex == 0 then rex = rex + 8 end 1934 if szov == "q" and rex == 0 then rex = rex + 8 end
1532 if needrex then rex = rex + 16 end 1935 if needrex then rex = rex + 16 end
1533 if addin and addin.reg == -1 then 1936 if addin and addin.reg == -1 then
1534 wputop(szov, opcode - 7, rex) 1937 local psz, sk = wputop(szov, opcode - 7, rex, vex, true)
1535 waction("VREG", addin.vreg); wputxb(0) 1938 wvreg("opcode", addin.vreg, psz, sk)
1536 else 1939 else
1537 if addin and addin.reg > 7 then rex = rex + 1 end 1940 if addin and addin.reg > 7 then rex = rex + 1 end
1538 wputop(szov, opcode, rex) 1941 wputop(szov, opcode, rex, vex)
1539 end 1942 end
1540 opcode = nil 1943 opcode = nil
1541 end 1944 end
@@ -1572,6 +1975,14 @@ local function dopattern(pat, args, sz, op, needrex)
1572 else 1975 else
1573 wputlabel("REL_", imm, 2) 1976 wputlabel("REL_", imm, 2)
1574 end 1977 end
1978 elseif c == "s" then
1979 local reg = a.reg
1980 if reg < 0 then
1981 wputb(0)
1982 wvreg("imm.hi", a.vreg)
1983 else
1984 wputb(shl(reg, 4))
1985 end
1575 else 1986 else
1576 werror("bad char `"..c.."' in pattern `"..pat.."' for `"..op.."'") 1987 werror("bad char `"..c.."' in pattern `"..pat.."' for `"..op.."'")
1577 end 1988 end
@@ -1648,11 +2059,14 @@ map_op[".template__"] = function(params, template, nparams)
1648 if pat == "" then pat = lastpat else lastpat = pat end 2059 if pat == "" then pat = lastpat else lastpat = pat end
1649 if matchtm(tm, args) then 2060 if matchtm(tm, args) then
1650 local prefix = sub(szm, 1, 1) 2061 local prefix = sub(szm, 1, 1)
1651 if prefix == "/" then -- Match both operand sizes. 2062 if prefix == "/" then -- Exactly match leading operand sizes.
1652 if args[1].opsize == sub(szm, 2, 2) and 2063 for i = #szm,1,-1 do
1653 args[2].opsize == sub(szm, 3, 3) then 2064 if i == 1 then
1654 dopattern(pat, args, sz, params.op, needrex) -- Process pattern. 2065 dopattern(pat, args, sz, params.op, needrex) -- Process pattern.
1655 return 2066 return
2067 elseif args[i-1].opsize ~= sub(szm, i, i) then
2068 break
2069 end
1656 end 2070 end
1657 else -- Match common operand size. 2071 else -- Match common operand size.
1658 local szp = sz 2072 local szp = sz
@@ -1717,8 +2131,8 @@ if x64 then
1717 rex = a.reg > 7 and 9 or 8 2131 rex = a.reg > 7 and 9 or 8
1718 end 2132 end
1719 end 2133 end
1720 wputop(sz, opcode, rex) 2134 local psz, sk = wputop(sz, opcode, rex, nil, vreg)
1721 if vreg then waction("VREG", vreg); wputxb(0) end 2135 wvreg("opcode", vreg, psz, sk)
1722 waction("IMM_D", format("(unsigned int)(%s)", op64)) 2136 waction("IMM_D", format("(unsigned int)(%s)", op64))
1723 waction("IMM_D", format("(unsigned int)((%s)>>32)", op64)) 2137 waction("IMM_D", format("(unsigned int)((%s)>>32)", op64))
1724 end 2138 end
diff --git a/dynasm/dynasm.lua b/dynasm/dynasm.lua
index 5fda425b..8e85af24 100644
--- a/dynasm/dynasm.lua
+++ b/dynasm/dynasm.lua
@@ -10,9 +10,9 @@
10local _info = { 10local _info = {
11 name = "DynASM", 11 name = "DynASM",
12 description = "A dynamic assembler for code generation engines", 12 description = "A dynamic assembler for code generation engines",
13 version = "1.3.0", 13 version = "1.4.0",
14 vernum = 10300, 14 vernum = 10400,
15 release = "2011-05-05", 15 release = "2015-10-18",
16 author = "Mike Pall", 16 author = "Mike Pall",
17 url = "http://luajit.org/dynasm.html", 17 url = "http://luajit.org/dynasm.html",
18 license = "MIT", 18 license = "MIT",
@@ -630,6 +630,7 @@ end
630-- Load architecture-specific module. 630-- Load architecture-specific module.
631local function loadarch(arch) 631local function loadarch(arch)
632 if not match(arch, "^[%w_]+$") then return "bad arch name" end 632 if not match(arch, "^[%w_]+$") then return "bad arch name" end
633 _G._map_def = map_def
633 local ok, m_arch = pcall(require, "dasm_"..arch) 634 local ok, m_arch = pcall(require, "dasm_"..arch)
634 if not ok then return "cannot load module: "..m_arch end 635 if not ok then return "cannot load module: "..m_arch end
635 g_arch = m_arch 636 g_arch = m_arch