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