Debian Codegraph @

texk/web2c/luatexdir/luaffi/dynasm/dasm_x86.h

raw
1 /*
2 ** DynASM x86 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 "x86"
13
14 #ifndef DASM_EXTERN
15 #define DASM_EXTERN(a,b,c,d) 0
16 #endif
17
18 /* Action definitions. DASM_STOP must be 255. */
19 enum {
20 DASM_DISP = 233,
21 DASM_IMM_S, DASM_IMM_B, DASM_IMM_W, DASM_IMM_D, DASM_IMM_WB, DASM_IMM_DB,
22 DASM_VREG, DASM_SPACE, DASM_SETLABEL, DASM_REL_A, DASM_REL_LG, DASM_REL_PC,
23 DASM_IMM_LG, DASM_IMM_PC, DASM_LABEL_LG, DASM_LABEL_PC, DASM_ALIGN,
24 DASM_EXTERN, DASM_ESC, DASM_MARK, DASM_SECTION, DASM_STOP
25 };
26
27 /* Maximum number of section buffer positions for a single dasm_put() call. */
28 #define DASM_MAXSECPOS 25
29
30 /* DynASM encoder status codes. Action list offset or number are or'ed in. */
31 #define DASM_S_OK 0x00000000
32 #define DASM_S_NOMEM 0x01000000
33 #define DASM_S_PHASE 0x02000000
34 #define DASM_S_MATCH_SEC 0x03000000
35 #define DASM_S_RANGE_I 0x11000000
36 #define DASM_S_RANGE_SEC 0x12000000
37 #define DASM_S_RANGE_LG 0x13000000
38 #define DASM_S_RANGE_PC 0x14000000
39 #define DASM_S_RANGE_VREG 0x15000000
40 #define DASM_S_UNDEF_L 0x21000000
41 #define DASM_S_UNDEF_PC 0x22000000
42
43 /* Macros to convert positions (8 bit section + 24 bit index). */
44 #define DASM_POS2IDX(pos) ((pos)&0x00ffffff)
45 #define DASM_POS2BIAS(pos) ((pos)&0xff000000)
46 #define DASM_SEC2POS(sec) ((sec)<<24)
47 #define DASM_POS2SEC(pos) ((pos)>>24)
48 #define DASM_POS2PTR(D, pos) (D->sections[DASM_POS2SEC(pos)].rbuf + (pos))
49
50 /* Action list type. */
51 typedef const unsigned char *dasm_ActList;
52
53 /* Per-section structure. */
54 typedef struct dasm_Section {
55 int *rbuf; /* Biased buffer pointer (negative section bias). */
56 int *buf; /* True buffer pointer. */
57 size_t bsize; /* Buffer size in bytes. */
58 int pos; /* Biased buffer position. */
59 int epos; /* End of biased buffer position - max single put. */
60 int ofs; /* Byte offset into section. */
61 } dasm_Section;
62
63 /* Core structure holding the DynASM encoding state. */
64 struct dasm_State {
65 size_t psize; /* Allocated size of this structure. */
66 dasm_ActList actionlist; /* Current actionlist pointer. */
67 int *lglabels; /* Local/global chain/pos ptrs. */
68 size_t lgsize;
69 int *pclabels; /* PC label chains/pos ptrs. */
70 size_t pcsize;
71 void **globals; /* Array of globals (bias -10). */
72 dasm_Section *section; /* Pointer to active section. */
73 size_t codesize; /* Total size of all code sections. */
74 int maxsection; /* 0 <= sectionidx < maxsection. */
75 int status; /* Status code. */
76 dasm_Section sections[1]; /* All sections. Alloc-extended. */
77 };
78
79 /* The size of the core structure depends on the max. number of sections. */
80 #define DASM_PSZ(ms) (sizeof(dasm_State)+(ms-1)*sizeof(dasm_Section))
81
82
83 /* Initialize DynASM state. */
84 void dasm_init(Dst_DECL, int maxsection)
85 {
86 dasm_State *D;
87 size_t psz = 0;
88 int i;
89 Dst_REF = NULL;
90 DASM_M_GROW(Dst, struct dasm_State, Dst_REF, psz, DASM_PSZ(maxsection));
91 D = Dst_REF;
92 D->psize = psz;
93 D->lglabels = NULL;
94 D->lgsize = 0;
95 D->pclabels = NULL;
96 D->pcsize = 0;
97 D->globals = NULL;
98 D->maxsection = maxsection;
99 for (i = 0; i < maxsection; i++) {
100 D->sections[i].buf = NULL; /* Need this for pass3. */
101 D->sections[i].rbuf = D->sections[i].buf - DASM_SEC2POS(i);
102 D->sections[i].bsize = 0;
103 D->sections[i].epos = 0; /* Wrong, but is recalculated after resize. */
104 }
105 }
106
107 /* Free DynASM state. */
108 void dasm_free(Dst_DECL)
109 {
110 dasm_State *D = Dst_REF;
111 int i;
112 for (i = 0; i < D->maxsection; i++)
113 if (D->sections[i].buf)
114 DASM_M_FREE(Dst, D->sections[i].buf, D->sections[i].bsize);
115 if (D->pclabels) DASM_M_FREE(Dst, D->pclabels, D->pcsize);
116 if (D->lglabels) DASM_M_FREE(Dst, D->lglabels, D->lgsize);
117 DASM_M_FREE(Dst, D, D->psize);
118 }
119
120 /* Setup global label array. Must be called before dasm_setup(). */
121 void dasm_setupglobal(Dst_DECL, void **gl, unsigned int maxgl)
122 {
123 dasm_State *D = Dst_REF;
124 D->globals = gl - 10; /* Negative bias to compensate for locals. */
125 DASM_M_GROW(Dst, int, D->lglabels, D->lgsize, (10+maxgl)*sizeof(int));
126 }
127
128 /* Grow PC label array. Can be called after dasm_setup(), too. */
129 void dasm_growpc(Dst_DECL, unsigned int maxpc)
130 {
131 dasm_State *D = Dst_REF;
132 size_t osz = D->pcsize;
133 DASM_M_GROW(Dst, int, D->pclabels, D->pcsize, maxpc*sizeof(int));
134 memset((void *)(((unsigned char *)D->pclabels)+osz), 0, D->pcsize-osz);
135 }
136
137 /* Setup encoder. */
138 void dasm_setup(Dst_DECL, const void *actionlist)
139 {
140 dasm_State *D = Dst_REF;
141 int i;
142 D->actionlist = (dasm_ActList)actionlist;
143 D->status = DASM_S_OK;
144 D->section = &D->sections[0];
145 memset((void *)D->lglabels, 0, D->lgsize);
146 if (D->pclabels) memset((void *)D->pclabels, 0, D->pcsize);
147 for (i = 0; i < D->maxsection; i++) {
148 D->sections[i].pos = DASM_SEC2POS(i);
149 D->sections[i].ofs = 0;
150 }
151 }
152
153
154 #ifdef DASM_CHECKS
155 #define CK(x, st) \
156 do { if (!(x)) { \
157 D->status = DASM_S_##st|(int)(p-D->actionlist-1); return; } } while (0)
158 #define CKPL(kind, st) \
159 do { if ((size_t)((char *)pl-(char *)D->kind##labels) >= D->kind##size) { \
160 D->status=DASM_S_RANGE_##st|(int)(p-D->actionlist-1); return; } } while (0)
161 #else
162 #define CK(x, st) ((void)0)
163 #define CKPL(kind, st) ((void)0)
164 #endif
165
166 /* Pass 1: Store actions and args, link branches/labels, estimate offsets. */
167 void dasm_put(Dst_DECL, int start, ...)
168 {
169 va_list ap;
170 dasm_State *D = Dst_REF;
171 dasm_ActList p = D->actionlist + start;
172 dasm_Section *sec = D->section;
173 int pos = sec->pos, ofs = sec->ofs, mrm = -1;
174 int *b;
175
176 if (pos >= sec->epos) {
177 DASM_M_GROW(Dst, int, sec->buf, sec->bsize,
178 sec->bsize + 2*DASM_MAXSECPOS*sizeof(int));
179 sec->rbuf = sec->buf - DASM_POS2BIAS(pos);
180 sec->epos = (int)sec->bsize/sizeof(int) - DASM_MAXSECPOS+DASM_POS2BIAS(pos);
181 }
182
183 b = sec->rbuf;
184 b[pos++] = start;
185
186 va_start(ap, start);
187 while (1) {
188 int action = *p++;
189 if (action < DASM_DISP) {
190 ofs++;
191 } else if (action <= DASM_REL_A) {
192 int n = va_arg(ap, int);
193 b[pos++] = n;
194 switch (action) {
195 case DASM_DISP:
196 if (n == 0) { if (mrm < 0) mrm = p[-2]; if ((mrm&7) != 5) break; }
197 /* 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 */
200 case DASM_IMM_D: ofs += 4; break;
201 case DASM_IMM_S: CK(((n+128)&-256) == 0, RANGE_I); goto ob;
202 case DASM_IMM_B: CK((n&-256) == 0, RANGE_I); ob: ofs++; break;
203 case DASM_IMM_WB: if (((n+128)&-256) == 0) goto ob; /* fallthrough */
204 case DASM_IMM_W: CK((n&-65536) == 0, RANGE_I); ofs += 2; break;
205 case DASM_SPACE: p++; ofs += n; break;
206 case DASM_SETLABEL: b[pos-2] = -0x40000000; break; /* Neg. label ofs. */
207 case DASM_VREG: CK((n&-16) == 0 && (n != 4 || (*p>>5) != 2), RANGE_VREG);
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 }
215 continue;
216 }
217 mrm = -1;
218 } else {
219 int *pl, n;
220 switch (action) {
221 case DASM_REL_LG:
222 case DASM_IMM_LG:
223 n = *p++; pl = D->lglabels + n;
224 /* Bkwd rel or global. */
225 if (n <= 246) { CK(n>=10||*pl<0, RANGE_LG); CKPL(lg, LG); goto putrel; }
226 pl -= 246; n = *pl;
227 if (n < 0) n = 0; /* Start new chain for fwd rel if label exists. */
228 goto linkrel;
229 case DASM_REL_PC:
230 case DASM_IMM_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC);
231 putrel:
232 n = *pl;
233 if (n < 0) { /* Label exists. Get label pos and store it. */
234 b[pos] = -n;
235 } else {
236 linkrel:
237 b[pos] = n; /* Else link to rel chain, anchored at label. */
238 *pl = pos;
239 }
240 pos++;
241 ofs += 4; /* Maximum offset needed. */
242 if (action == DASM_REL_LG || action == DASM_REL_PC)
243 b[pos++] = ofs; /* Store pass1 offset estimate. */
244 break;
245 case DASM_LABEL_LG: pl = D->lglabels + *p++; CKPL(lg, LG); goto putlabel;
246 case DASM_LABEL_PC: pl = D->pclabels + va_arg(ap, int); CKPL(pc, PC);
247 putlabel:
248 n = *pl; /* n > 0: Collapse rel chain and replace with label pos. */
249 while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = pos; }
250 *pl = -pos; /* Label exists now. */
251 b[pos++] = ofs; /* Store pass1 offset estimate. */
252 break;
253 case DASM_ALIGN:
254 ofs += *p++; /* Maximum alignment needed (arg is 2**n-1). */
255 b[pos++] = ofs; /* Store pass1 offset estimate. */
256 break;
257 case DASM_EXTERN: p += 2; ofs += 4; break;
258 case DASM_ESC: p++; ofs++; break;
259 case DASM_MARK: mrm = p[-2]; break;
260 case DASM_SECTION:
261 n = *p; CK(n < D->maxsection, RANGE_SEC); D->section = &D->sections[n];
262 case DASM_STOP: goto stop;
263 }
264 }
265 }
266 stop:
267 va_end(ap);
268 sec->pos = pos;
269 sec->ofs = ofs;
270 }
271 #undef CK
272
273 /* Pass 2: Link sections, shrink branches/aligns, fix label offsets. */
274 int dasm_link(Dst_DECL, size_t *szp)
275 {
276 dasm_State *D = Dst_REF;
277 int secnum;
278 int ofs = 0;
279
280 #ifdef DASM_CHECKS
281 *szp = 0;
282 if (D->status != DASM_S_OK) return D->status;
283 {
284 int pc;
285 for (pc = 0; pc*sizeof(int) < D->pcsize; pc++)
286 if (D->pclabels[pc] > 0) return DASM_S_UNDEF_PC|pc;
287 }
288 #endif
289
290 { /* Handle globals not defined in this translation unit. */
291 int idx;
292 for (idx = 10; idx*sizeof(int) < D->lgsize; idx++) {
293 int n = D->lglabels[idx];
294 /* Undefined label: Collapse rel chain and replace with marker (< 0). */
295 while (n > 0) { int *pb = DASM_POS2PTR(D, n); n = *pb; *pb = -idx; }
296 }
297 }
298
299 /* Combine all code sections. No support for data sections (yet). */
300 for (secnum = 0; secnum < D->maxsection; secnum++) {
301 dasm_Section *sec = D->sections + secnum;
302 int *b = sec->rbuf;
303 int pos = DASM_SEC2POS(secnum);
304 int lastpos = sec->pos;
305
306 while (pos != lastpos) {
307 dasm_ActList p = D->actionlist + b[pos++];
308 int op = 0;
309 while (1) {
310 int action = *p++;
311 switch (action) {
312 case DASM_REL_LG: p++;
313 /* fallthrough */
314 case DASM_REL_PC: {
315 int shrink = op == 0xe9 ? 3 : ((op&0xf0) == 0x80 ? 4 : 0);
316 if (shrink) { /* Shrinkable branch opcode? */
317 int lofs, lpos = b[pos];
318 if (lpos < 0) goto noshrink; /* Ext global? */
319 lofs = *DASM_POS2PTR(D, lpos);
320 if (lpos > pos) { /* Fwd label: add cumulative section offsets. */
321 int i;
322 for (i = secnum; i < DASM_POS2SEC(lpos); i++)
323 lofs += D->sections[i].ofs;
324 } else {
325 lofs -= ofs; /* Bkwd label: unfix offset. */
326 }
327 lofs -= b[pos+1]; /* Short branch ok? */
328 if (lofs >= -128-shrink && lofs <= 127) ofs -= shrink; /* Yes. */
329 else { noshrink: shrink = 0; } /* No, cannot shrink op. */
330 }
331 b[pos+1] = shrink;
332 pos += 2;
333 break;
334 }
335 /* fallthrough */
336 case DASM_SPACE: case DASM_IMM_LG: case DASM_VREG: p++;
337 /* fallthrough */
338 case DASM_DISP: case DASM_IMM_S: case DASM_IMM_B: case DASM_IMM_W:
339 case DASM_IMM_D: case DASM_IMM_WB: case DASM_IMM_DB:
340 case DASM_SETLABEL: case DASM_REL_A: case DASM_IMM_PC: pos++; break;
341 case DASM_LABEL_LG: p++;
342 /* fallthrough */
343 case DASM_LABEL_PC: b[pos++] += ofs; break; /* Fix label offset. */
344 case DASM_ALIGN: ofs -= (b[pos++]+ofs)&*p++; break; /* Adjust ofs. */
345 case DASM_EXTERN: p += 2; break;
346 case DASM_ESC: op = *p++; break;
347 case DASM_MARK: break;
348 case DASM_SECTION: case DASM_STOP: goto stop;
349 default: op = action; break;
350 }
351 }
352 stop: (void)0;
353 }
354 ofs += sec->ofs; /* Next section starts right after current section. */
355 }
356
357 D->codesize = ofs; /* Total size of all code sections */
358 *szp = ofs;
359 return DASM_S_OK;
360 }
361
362 #define dasmb(x) *cp++ = (unsigned char)(x)
363 #ifndef DASM_ALIGNED_WRITES
364 #define dasmw(x) \
365 do { *((unsigned short *)cp) = (unsigned short)(x); cp+=2; } while (0)
366 #define dasmd(x) \
367 do { *((unsigned int *)cp) = (unsigned int)(x); cp+=4; } while (0)
368 #else
369 #define dasmw(x) do { dasmb(x); dasmb((x)>>8); } while (0)
370 #define dasmd(x) do { dasmw(x); dasmw((x)>>16); } while (0)
371 #endif
372
373 /* Pass 3: Encode sections. */
374 int dasm_encode(Dst_DECL, void *buffer)
375 {
376 dasm_State *D = Dst_REF;
377 unsigned char *base = (unsigned char *)buffer;
378 unsigned char *cp = base;
379 int secnum;
380
381 /* Encode all code sections. No support for data sections (yet). */
382 for (secnum = 0; secnum < D->maxsection; secnum++) {
383 dasm_Section *sec = D->sections + secnum;
384 int *b = sec->buf;
385 int *endb = sec->rbuf + sec->pos;
386
387 while (b != endb) {
388 dasm_ActList p = D->actionlist + *b++;
389 unsigned char *mark = NULL;
390 while (1) {
391 int action = *p++;
392 int n = (action >= DASM_DISP && action <= DASM_ALIGN) ? *b++ : 0;
393 switch (action) {
394 case DASM_DISP: if (!mark) mark = cp; {
395 unsigned char *mm = mark;
396 if (*p != DASM_IMM_DB && *p != DASM_IMM_WB) mark = NULL;
397 if (n == 0) { int mrm = mm[-1]&7; if (mrm == 4) mrm = mm[0]&7;
398 if (mrm != 5) { mm[-1] -= 0x80; break; } }
399 if (((n+128) & -256) != 0) goto wd; else mm[-1] -= 0x40;
400 }
401 /* fallthrough */
402 case DASM_IMM_S: case DASM_IMM_B: wb: dasmb(n); break;
403 case DASM_IMM_DB: if (((n+128)&-256) == 0) {
404 db: if (!mark) mark = cp; mark[-2] += 2; mark = NULL; goto wb;
405 } else mark = NULL;
406 /* fallthrough */
407 case DASM_IMM_D: wd: dasmd(n); break;
408 case DASM_IMM_WB: if (((n+128)&-256) == 0) goto db; else mark = NULL;
409 /* fallthrough */
410 case DASM_IMM_W: dasmw(n); break;
411 case DASM_VREG: {
412 int t = *p++;
413 unsigned char *ex = cp - (t&7);
414 if ((n & 8) && t < 0xa0) {
415 if (*ex & 0x80) ex[1] ^= 0x20 << (t>>6); else *ex ^= 1 << (t>>6);
416 n &= 7;
417 } else if (n & 0x10) {
418 if (*ex & 0x80) {
419 *ex = 0xc5; ex[1] = (ex[1] & 0x80) | ex[2]; ex += 2;
420 }
421 while (++ex < cp) ex[-1] = *ex;
422 if (mark) mark--;
423 cp--;
424 n &= 7;
425 }
426 if (t >= 0xc0) n <<= 4;
427 else if (t >= 0x40) n <<= 3;
428 else if (n == 4 && t < 0x20) { cp[-1] ^= n; *cp++ = 0x20; }
429 cp[-1] ^= n;
430 break;
431 }
432 case DASM_REL_LG: p++; if (n >= 0) goto rel_pc;
433 b++; n = (int)(ptrdiff_t)D->globals[-n];
434 /* fallthrough */
435 case DASM_REL_A: rel_a:
436 n -= (unsigned int)(ptrdiff_t)(cp+4); goto wd; /* !x64 */
437 case DASM_REL_PC: rel_pc: {
438 int shrink = *b++;
439 int *pb = DASM_POS2PTR(D, n); if (*pb < 0) { n = pb[1]; goto rel_a; }
440 n = *pb - ((int)(cp-base) + 4-shrink);
441 if (shrink == 0) goto wd;
442 if (shrink == 4) { cp--; cp[-1] = *cp-0x10; } else cp[-1] = 0xeb;
443 goto wb;
444 }
445 case DASM_IMM_LG:
446 p++; if (n < 0) { n = (int)(ptrdiff_t)D->globals[-n]; goto wd; }
447 /* fallthrough */
448 case DASM_IMM_PC: {
449 int *pb = DASM_POS2PTR(D, n);
450 n = *pb < 0 ? pb[1] : (*pb + (int)(ptrdiff_t)base);
451 goto wd;
452 }
453 case DASM_LABEL_LG: {
454 int idx = *p++;
455 if (idx >= 10)
456 D->globals[idx] = (void *)(base + (*p == DASM_SETLABEL ? *b : n));
457 break;
458 }
459 case DASM_LABEL_PC: case DASM_SETLABEL: break;
460 case DASM_SPACE: { int fill = *p++; while (n--) *cp++ = fill; break; }
461 case DASM_ALIGN:
462 n = *p++;
463 while (((cp-base) & n)) *cp++ = 0x90; /* nop */
464 break;
465 case DASM_EXTERN: n = DASM_EXTERN(Dst, cp, p[1], *p); p += 2; goto wd;
466 case DASM_MARK: mark = cp; break;
467 case DASM_ESC: action = *p++;
468 /* fallthrough */
469 default: *cp++ = action; break;
470 case DASM_SECTION: case DASM_STOP: goto stop;
471 }
472 }
473 stop: (void)0;
474 }
475 }
476
477 if (base + D->codesize != cp) /* Check for phase errors. */
478 return DASM_S_PHASE;
479 return DASM_S_OK;
480 }
481
482 /* Get PC label offset. */
483 int dasm_getpclabel(Dst_DECL, unsigned int pc)
484 {
485 dasm_State *D = Dst_REF;
486 if (pc*sizeof(int) < D->pcsize) {
487 int pos = D->pclabels[pc];
488 if (pos < 0) return *DASM_POS2PTR(D, -pos);
489 if (pos > 0) return -1; /* Undefined. */
490 }
491 return -2; /* Unused or out of range. */
492 }
493
494 #ifdef DASM_CHECKS
495 /* Optional sanity checker to call between isolated encoding steps. */
496 int dasm_checkstep(Dst_DECL, int secmatch)
497 {
498 dasm_State *D = Dst_REF;
499 if (D->status == DASM_S_OK) {
500 int i;
501 for (i = 1; i <= 9; i++) {
502 if (D->lglabels[i] > 0) { D->status = DASM_S_UNDEF_L|i; break; }
503 D->lglabels[i] = 0;
504 }
505 }
506 if (D->status == DASM_S_OK && secmatch >= 0 &&
507 D->section != &D->sections[secmatch])
508 D->status = DASM_S_MATCH_SEC|(int)(D->section-D->sections);
509 return D->status;
510 }
511 #endif
512