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