History

asm support

bellard committed 23 years ago

Commit 7893a9e1 parent 8e40dd93 files · +2265 −0Browse files

Unified · Split

3 files
  1. i386-asm.c +1077 −0
  2. i386-asm.h +444 −0
  3. tccasm.c +744 −0
i386-asm.c+1077−0
@@ -0,0 +1,1077 @@
1+/*
2+ * i386 specific functions for TCC assembler
3+ *
4+ * Copyright (c) 2001, 2002 Fabrice Bellard
5+ *
6+ * This program is free software; you can redistribute it and/or modify
7+ * it under the terms of the GNU General Public License as published by
8+ * the Free Software Foundation; either version 2 of the License, or
9+ * (at your option) any later version.
10+ *
11+ * This program is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+ * GNU General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU General Public License
17+ * along with this program; if not, write to the Free Software
18+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19+ */
20+
21+#define MAX_OPERANDS 3
22+
23+typedef struct ASMInstr {
24+ uint16_t sym;
25+ uint16_t opcode;
26+ uint16_t instr_type;
27+#define OPC_JMP 0x01 /* jmp operand */
28+#define OPC_B 0x02 /* only used zith OPC_WL */
29+#define OPC_WL 0x04 /* accepts w, l or no suffix */
30+#define OPC_BWL (OPC_B | OPC_WL) /* accepts b, w, l or no suffix */
31+#define OPC_REG 0x08 /* register is added to opcode */
32+#define OPC_MODRM 0x10 /* modrm encoding */
33+#define OPC_FWAIT 0x20 /* add fwait opcode */
34+#define OPC_TEST 0x40 /* test opcodes */
35+#define OPC_SHIFT 0x80 /* shift opcodes */
36+#define OPC_D16 0x0100 /* generate data16 prefix */
37+#define OPC_ARITH 0x0200 /* arithmetic opcodes */
38+#define OPC_SHORTJMP 0x0400 /* short jmp operand */
39+#define OPC_FARITH 0x0800 /* FPU arithmetic opcodes */
40+#define OPC_GROUP_SHIFT 13
41+
42+/* in order to compress the operand type, we use specific operands and
43+ we or only with EA */
44+#define OPT_REG8 0 /* warning: value is hardcoded from TOK_ASM_xxx */
45+#define OPT_REG16 1 /* warning: value is hardcoded from TOK_ASM_xxx */
46+#define OPT_REG32 2 /* warning: value is hardcoded from TOK_ASM_xxx */
47+#define OPT_MMX 3 /* warning: value is hardcoded from TOK_ASM_xxx */
48+#define OPT_SSE 4 /* warning: value is hardcoded from TOK_ASM_xxx */
49+#define OPT_CR 5 /* warning: value is hardcoded from TOK_ASM_xxx */
50+#define OPT_TR 6 /* warning: value is hardcoded from TOK_ASM_xxx */
51+#define OPT_DB 7 /* warning: value is hardcoded from TOK_ASM_xxx */
52+#define OPT_SEG 8
53+#define OPT_ST 9
54+#define OPT_IM8 10
55+#define OPT_IM8S 11
56+#define OPT_IM16 12
57+#define OPT_IM32 13
58+#define OPT_EAX 14 /* %al, %ax or %eax register */
59+#define OPT_ST0 15 /* %st(0) register */
60+#define OPT_CL 16 /* %cl register */
61+#define OPT_DX 17 /* %dx register */
62+#define OPT_ADDR 18 /* OP_EA with only offset */
63+#define OPT_INDIR 19 /* *(expr) */
64+
65+/* composite types */
66+#define OPT_COMPOSITE_FIRST 20
67+#define OPT_IM 20 /* IM8 | IM16 | IM32 */
68+#define OPT_REG 21 /* REG8 | REG16 | REG32 */
69+#define OPT_REGW 22 /* REG16 | REG32 */
70+#define OPT_IMW 23 /* IM16 | IM32 */
71+
72+/* can be ored with any OPT_xxx */
73+#define OPT_EA 0x80
74+
75+ uint8_t nb_ops;
76+ uint8_t op_type[MAX_OPERANDS]; /* see OP_xxx */
77+} ASMInstr;
78+
79+typedef struct Operand {
80+ uint32_t type;
81+#define OP_REG8 (1 << OPT_REG8)
82+#define OP_REG16 (1 << OPT_REG16)
83+#define OP_REG32 (1 << OPT_REG32)
84+#define OP_MMX (1 << OPT_MMX)
85+#define OP_SSE (1 << OPT_SSE)
86+#define OP_CR (1 << OPT_CR)
87+#define OP_TR (1 << OPT_TR)
88+#define OP_DB (1 << OPT_DB)
89+#define OP_SEG (1 << OPT_SEG)
90+#define OP_ST (1 << OPT_ST)
91+#define OP_IM8 (1 << OPT_IM8)
92+#define OP_IM8S (1 << OPT_IM8S)
93+#define OP_IM16 (1 << OPT_IM16)
94+#define OP_IM32 (1 << OPT_IM32)
95+#define OP_EAX (1 << OPT_EAX)
96+#define OP_ST0 (1 << OPT_ST0)
97+#define OP_CL (1 << OPT_CL)
98+#define OP_DX (1 << OPT_DX)
99+#define OP_ADDR (1 << OPT_ADDR)
100+#define OP_INDIR (1 << OPT_INDIR)
101+
102+#define OP_EA 0x40000000
103+#define OP_REG (OP_REG8 | OP_REG16 | OP_REG32)
104+#define OP_IM OP_IM32
105+ int8_t reg; /* register, -1 if none */
106+ int8_t reg2; /* second register, -1 if none */
107+ uint8_t shift;
108+ ExprValue e;
109+} Operand;
110+
111+static const uint8_t reg_to_size[5] = {
112+ [OP_REG8] = 0,
113+ [OP_REG16] = 1,
114+ [OP_REG32] = 2,
115+};
116+
117+#define WORD_PREFIX_OPCODE 0x66
118+
119+#define NB_TEST_OPCODES 30
120+
121+static const uint8_t test_bits[NB_TEST_OPCODES] = {
122+ 0x00, /* o */
123+ 0x01, /* no */
124+ 0x02, /* b */
125+ 0x02, /* c */
126+ 0x02, /* nae */
127+ 0x03, /* nb */
128+ 0x03, /* nc */
129+ 0x03, /* ae */
130+ 0x04, /* e */
131+ 0x04, /* z */
132+ 0x05, /* ne */
133+ 0x05, /* nz */
134+ 0x06, /* be */
135+ 0x06, /* na */
136+ 0x07, /* nbe */
137+ 0x07, /* a */
138+ 0x08, /* s */
139+ 0x09, /* ns */
140+ 0x0a, /* p */
141+ 0x0a, /* pe */
142+ 0x0b, /* np */
143+ 0x0b, /* po */
144+ 0x0c, /* l */
145+ 0x0c, /* nge */
146+ 0x0d, /* nl */
147+ 0x0d, /* ge */
148+ 0x0e, /* le */
149+ 0x0e, /* ng */
150+ 0x0f, /* nle */
151+ 0x0f, /* g */
152+};
153+
154+static const ASMInstr asm_instrs[] = {
155+#define ALT(x) x
156+#define DEF_ASM_OP0(name, opcode)
157+#define DEF_ASM_OP0L(name, opcode, group, instr_type) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 0 },
158+#define DEF_ASM_OP1(name, opcode, group, instr_type, op0) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 1, { op0 }},
159+#define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 2, { op0, op1 }},
160+#define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2) { TOK_ASM_ ## name, opcode, (instr_type | group << OPC_GROUP_SHIFT), 3, { op0, op1, op2 }},
161+#include "i386-asm.h"
162+
163+ /* last operation */
164+ { 0, },
165+};
166+
167+static const uint16_t op0_codes[] = {
168+#define ALT(x)
169+#define DEF_ASM_OP0(x, opcode) opcode,
170+#define DEF_ASM_OP0L(name, opcode, group, instr_type)
171+#define DEF_ASM_OP1(name, opcode, group, instr_type, op0)
172+#define DEF_ASM_OP2(name, opcode, group, instr_type, op0, op1)
173+#define DEF_ASM_OP3(name, opcode, group, instr_type, op0, op1, op2)
174+#include "i386-asm.h"
175+};
176+
177+static inline int get_reg_shift(TCCState *s1)
178+{
179+ int shift, v;
180+
181+ v = asm_int_expr(s1);
182+ switch(v) {
183+ case 1:
184+ shift = 0;
185+ break;
186+ case 2:
187+ shift = 1;
188+ break;
189+ case 4:
190+ shift = 2;
191+ break;
192+ case 8:
193+ shift = 3;
194+ break;
195+ default:
196+ expect("1, 2, 4 or 8 constant");
197+ shift = 0;
198+ break;
199+ }
200+ return shift;
201+}
202+
203+static int asm_parse_reg(void)
204+{
205+ int reg;
206+ if (tok != '%')
207+ goto error_32;
208+ next();
209+ if (tok >= TOK_ASM_eax && tok <= TOK_ASM_edi) {
210+ reg = tok - TOK_ASM_eax;
211+ next();
212+ return reg;
213+ } else {
214+ error_32:
215+ expect("32 bit register");
216+ return 0;
217+ }
218+}
219+
220+static void parse_operand(TCCState *s1, Operand *op)
221+{
222+ ExprValue e;
223+ int reg, indir;
224+ const char *p;
225+
226+ indir = 0;
227+ if (tok == '*') {
228+ next();
229+ indir = OP_INDIR;
230+ }
231+
232+ if (tok == '%') {
233+ next();
234+ if (tok >= TOK_ASM_al && tok <= TOK_ASM_db7) {
235+ reg = tok - TOK_ASM_al;
236+ op->type = 1 << (reg >> 3); /* WARNING: do not change constant order */
237+ op->reg = reg & 7;
238+ if ((op->type & OP_REG) && op->reg == TREG_EAX)
239+ op->type |= OP_EAX;
240+ else if (op->type == OP_REG8 && op->reg == TREG_ECX)
241+ op->type |= OP_CL;
242+ else if (op->type == OP_REG16 && op->reg == TREG_EDX)
243+ op->type |= OP_DX;
244+ } else if (tok >= TOK_ASM_dr0 && tok <= TOK_ASM_dr7) {
245+ op->type = OP_DB;
246+ op->reg = tok - TOK_ASM_dr0;
247+ } else if (tok >= TOK_ASM_es && tok <= TOK_ASM_gs) {
248+ op->type = OP_SEG;
249+ op->reg = tok - TOK_ASM_es;
250+ } else if (tok == TOK_ASM_st) {
251+ op->type = OP_ST;
252+ op->reg = 0;
253+ next();
254+ if (tok == '(') {
255+ next();
256+ if (tok != TOK_PPNUM)
257+ goto reg_error;
258+ p = tokc.cstr->data;
259+ reg = p[0] - '0';
260+ if ((unsigned)reg >= 8 || p[1] != '\0')
261+ goto reg_error;
262+ op->reg = reg;
263+ next();
264+ skip(')');
265+ }
266+ if (op->reg == 0)
267+ op->type |= OP_ST0;
268+ goto no_skip;
269+ } else {
270+ reg_error:
271+ error("unknown register");
272+ }
273+ next();
274+ no_skip: ;
275+ } else if (tok == '$') {
276+ /* constant value */
277+ next();
278+ asm_expr(s1, &e);
279+ op->type = OP_IM32;
280+ op->e.v = e.v;
281+ op->e.sym = e.sym;
282+ if (!op->e.sym) {
283+ if (op->e.v == (uint8_t)op->e.v)
284+ op->type |= OP_IM8;
285+ if (op->e.v == (int8_t)op->e.v)
286+ op->type |= OP_IM8S;
287+ if (op->e.v == (uint16_t)op->e.v)
288+ op->type |= OP_IM16;
289+ }
290+ } else {
291+ /* address(reg,reg2,shift) with all variants */
292+ op->type = OP_EA;
293+ op->reg = -1;
294+ op->reg2 = -1;
295+ op->shift = 0;
296+ if (tok != '(') {
297+ asm_expr(s1, &e);
298+ op->e.v = e.v;
299+ op->e.sym = e.sym;
300+ } else {
301+ op->e.v = 0;
302+ op->e.sym = NULL;
303+ }
304+ if (tok == '(') {
305+ next();
306+ if (tok != ',') {
307+ op->reg = asm_parse_reg();
308+ }
309+ if (tok == ',') {
310+ next();
311+ if (tok != ',') {
312+ op->reg2 = asm_parse_reg();
313+ }
314+ skip(',');
315+ op->shift = get_reg_shift(s1);
316+ }
317+ skip(')');
318+ }
319+ if (op->reg == -1 && op->reg2 == -1)
320+ op->type |= OP_ADDR;
321+ }
322+ op->type |= indir;
323+}
324+
325+/* XXX: unify with C code output ? */
326+static void gen_expr32(ExprValue *pe)
327+{
328+ if (pe->sym)
329+ greloc(cur_text_section, pe->sym, ind, R_386_32);
330+ gen_le32(pe->v);
331+}
332+
333+/* XXX: unify with C code output ? */
334+static void gen_disp32(ExprValue *pe)
335+{
336+ Sym *sym;
337+ sym = pe->sym;
338+ if (sym) {
339+ if (sym->r == cur_text_section->sh_num) {
340+ /* same section: we can output an absolute value. Note
341+ that the TCC compiler behaves differently here because
342+ it always outputs a relocation to ease (future) code
343+ elimination in the linker */
344+ gen_le32(pe->v + (long)sym->next - ind - 4);
345+ } else {
346+ greloc(cur_text_section, sym, ind, R_386_PC32);
347+ gen_le32(pe->v - 4);
348+ }
349+ } else {
350+ /* put an empty PC32 relocation */
351+ put_elf_reloc(symtab_section, cur_text_section,
352+ ind, R_386_PC32, 0);
353+ gen_le32(pe->v - 4);
354+ }
355+}
356+
357+
358+static void gen_le16(int v)
359+{
360+ g(v);
361+ g(v >> 8);
362+}
363+
364+/* generate the modrm operand */
365+static inline void asm_modrm(int reg, Operand *op)
366+{
367+ int mod, reg1, reg2;
368+
369+ if (op->type & (OP_REG | OP_MMX | OP_SSE)) {
370+ g(0xc0 + (reg << 3) + op->reg);
371+ } else if (op->reg == -1 && op->reg2 == -1) {
372+ /* displacement only */
373+ g(0x05 + (reg << 3));
374+ gen_expr32(&op->e);
375+ } else {
376+ /* fist compute displacement encoding */
377+ if (op->e.v == 0 && !op->e.sym && op->reg != 5) {
378+ mod = 0x00;
379+ } else if (op->e.v == (int8_t)op->e.v && !op->e.sym) {
380+ mod = 0x40;
381+ } else {
382+ mod = 0x80;
383+ }
384+ /* compute if sib byte needed */
385+ reg1 = op->reg;
386+ if (op->reg2 != -1)
387+ reg1 = 4;
388+ g(mod + (reg << 3) + reg1);
389+ if (reg1 == 4) {
390+ /* add sib byte */
391+ reg2 = op->reg2;
392+ if (reg2 == -1)
393+ reg2 = 4; /* indicate no index */
394+ g((op->shift << 6) + (reg2 << 3) + op->reg);
395+ }
396+
397+ /* add offset */
398+ if (mod == 0x40) {
399+ g(op->e.v);
400+ } else if (mod == 0x80) {
401+ gen_expr32(&op->e);
402+ }
403+ }
404+}
405+
406+static void asm_opcode(TCCState *s1, int opcode)
407+{
408+ const ASMInstr *pa;
409+ int i, modrm_index, reg, v, op1, is_short_jmp;
410+ int nb_ops, s, ss;
411+ Operand ops[MAX_OPERANDS], *pop;
412+ int op_type[3]; /* decoded op type */
413+
414+ /* get operands */
415+ pop = ops;
416+ nb_ops = 0;
417+ for(;;) {
418+ if (tok == ';' || tok == TOK_LINEFEED)
419+ break;
420+ if (nb_ops >= MAX_OPERANDS) {
421+ error("incorrect number of operands");
422+ }
423+ parse_operand(s1, pop);
424+ pop++;
425+ nb_ops++;
426+ if (tok != ',')
427+ break;
428+ next();
429+ }
430+
431+ is_short_jmp = 0;
432+ s = 0; /* avoid warning */
433+
434+ /* optimize matching by using a lookup table (no hashing is needed
435+ !) */
436+ for(pa = asm_instrs; pa->sym != 0; pa++) {
437+ s = 0;
438+ if (pa->instr_type & OPC_FARITH) {
439+ v = opcode - pa->sym;
440+ if (!((unsigned)v < 8 * 6 && (v % 6) == 0))
441+ continue;
442+ } else if (pa->instr_type & OPC_ARITH) {
443+ if (!(opcode >= pa->sym && opcode < pa->sym + 8 * 4))
444+ continue;
445+ goto compute_size;
446+ } else if (pa->instr_type & OPC_SHIFT) {
447+ if (!(opcode >= pa->sym && opcode < pa->sym + 7 * 4))
448+ continue;
449+ goto compute_size;
450+ } else if (pa->instr_type & OPC_TEST) {
451+ if (!(opcode >= pa->sym && opcode < pa->sym + NB_TEST_OPCODES))
452+ continue;
453+ } else if (pa->instr_type & OPC_B) {
454+ if (!(opcode >= pa->sym && opcode <= pa->sym + 3))
455+ continue;
456+ compute_size:
457+ s = (opcode - pa->sym) & 3;
458+ } else if (pa->instr_type & OPC_WL) {
459+ if (!(opcode >= pa->sym && opcode <= pa->sym + 2))
460+ continue;
461+ s = opcode - pa->sym + 1;
462+ } else {
463+ if (pa->sym != opcode)
464+ continue;
465+ }
466+ if (pa->nb_ops != nb_ops)
467+ continue;
468+ /* now decode and check each operand */
469+ for(i = 0; i < nb_ops; i++) {
470+ int op1, op2;
471+ op1 = pa->op_type[i];
472+ op2 = op1 & 0x1f;
473+ switch(op2) {
474+ case OPT_IM:
475+ v = OP_IM8 | OP_IM16 | OP_IM32;
476+ break;
477+ case OPT_REG:
478+ v = OP_REG8 | OP_REG16 | OP_REG32;
479+ break;
480+ case OPT_REGW:
481+ v = OP_REG16 | OP_REG32;
482+ break;
483+ case OPT_IMW:
484+ v = OP_IM16 | OP_IM32;
485+ break;
486+ default:
487+ v = 1 << op2;
488+ break;
489+ }
490+ if (op1 & OPT_EA)
491+ v |= OP_EA;
492+ op_type[i] = v;
493+ if ((ops[i].type & v) == 0)
494+ goto next;
495+ }
496+ /* all is matching ! */
497+ break;
498+ next: ;
499+ }
500+ if (pa->sym == 0) {
501+ if (opcode >= TOK_ASM_pusha && opcode <= TOK_ASM_emms) {
502+ int b;
503+ b = op0_codes[opcode - TOK_ASM_pusha];
504+ if (b & 0xff00)
505+ g(b >> 8);
506+ g(b);
507+ return;
508+ } else {
509+ error("unknown opcode '%s'",
510+ get_tok_str(opcode, NULL));
511+ }
512+ }
513+ /* if the size is unknown, then evaluate it (OPC_B or OPC_WL case) */
514+ if (s == 3) {
515+ for(i = 0; s == 3 && i < nb_ops; i++) {
516+ if ((ops[i].type & OP_REG) && !(op_type[i] & (OP_CL | OP_DX)))
517+ s = reg_to_size[ops[i].type & OP_REG];
518+ }
519+ if (s == 3) {
520+ error("cannot infer opcode suffix");
521+ }
522+ }
523+
524+ /* generate data16 prefix if needed */
525+ ss = s;
526+ if (s == 1 || (pa->instr_type & OPC_D16))
527+ g(WORD_PREFIX_OPCODE);
528+ else if (s == 2)
529+ s = 1;
530+ /* now generates the operation */
531+ if (pa->instr_type & OPC_FWAIT)
532+ g(0x9b);
533+
534+ v = pa->opcode;
535+ if (v == 0x69 || v == 0x69) {
536+ /* kludge for imul $im, %reg */
537+ nb_ops = 3;
538+ ops[2] = ops[1];
539+ } else if (v == 0xcd && ops[0].e.v == 3 && !ops[0].e.sym) {
540+ v--; /* int $3 case */
541+ nb_ops = 0;
542+ } else if ((v == 0x06 || v == 0x07)) {
543+ if (ops[0].reg >= 4) {
544+ /* push/pop %fs or %gs */
545+ v = 0x0fa0 + (v - 0x06) + ((ops[0].reg - 4) << 3);
546+ } else {
547+ v += ops[0].reg << 3;
548+ }
549+ nb_ops = 0;
550+ } else if (v <= 0x05) {
551+ /* arith case */
552+ v += ((opcode - TOK_ASM_addb) >> 2) << 3;
553+ } else if ((pa->instr_type & (OPC_FARITH | OPC_MODRM)) == OPC_FARITH) {
554+ /* fpu arith case */
555+ v += ((opcode - pa->sym) / 6) << 3;
556+ }
557+ if (pa->instr_type & OPC_REG) {
558+ for(i = 0; i < nb_ops; i++) {
559+ if (op_type[i] & (OP_REG | OP_ST)) {
560+ v += ops[i].reg;
561+ break;
562+ }
563+ }
564+ /* mov $im, %reg case */
565+ if (pa->opcode == 0xb0 && s >= 1)
566+ v += 7;
567+ }
568+ if (pa->instr_type & OPC_B)
569+ v += s;
570+ if (pa->instr_type & OPC_TEST)
571+ v += test_bits[opcode - pa->sym];
572+ if (pa->instr_type & OPC_SHORTJMP) {
573+ Sym *sym;
574+ int jmp_disp;
575+
576+ /* see if we can really generate the jump with a byte offset */
577+ sym = ops[0].e.sym;
578+ if (!sym)
579+ goto no_short_jump;
580+ if (sym->r != cur_text_section->sh_num)
581+ goto no_short_jump;
582+ jmp_disp = ops[0].e.v + (long)sym->next - ind - 2;
583+ if (jmp_disp == (int8_t)jmp_disp) {
584+ /* OK to generate jump */
585+ is_short_jmp = 1;
586+ ops[0].e.v = jmp_disp;
587+ } else {
588+ no_short_jump:
589+ if (pa->instr_type & OPC_JMP) {
590+ /* long jump will be allowed. need to modify the
591+ opcode slightly */
592+ if (v == 0xeb)
593+ v = 0xe9;
594+ else
595+ v += 0x0f10;
596+ } else {
597+ error("invalid displacement");
598+ }
599+ }
600+ }
601+ op1 = v >> 8;
602+ if (op1)
603+ g(op1);
604+ g(v);
605+
606+ /* search which operand will used for modrm */
607+ modrm_index = 0;
608+ if (pa->instr_type & OPC_SHIFT) {
609+ reg = (opcode - pa->sym) >> 2;
610+ if (reg == 6)
611+ reg = 7;
612+ } else if (pa->instr_type & OPC_ARITH) {
613+ reg = (opcode - pa->sym) >> 2;
614+ } else if (pa->instr_type & OPC_FARITH) {
615+ reg = (opcode - pa->sym) / 6;
616+ } else {
617+ reg = (pa->instr_type >> OPC_GROUP_SHIFT) & 7;
618+ }
619+ if (pa->instr_type & OPC_MODRM) {
620+ /* first look for an ea operand */
621+ for(i = 0;i < nb_ops; i++) {
622+ if (op_type[i] & OP_EA)
623+ goto modrm_found;
624+ }
625+ /* then if not found, a register or indirection (shift instructions) */
626+ for(i = 0;i < nb_ops; i++) {
627+ if (op_type[i] & (OP_REG | OP_MMX | OP_SSE | OP_INDIR))
628+ goto modrm_found;
629+ }
630+#ifdef ASM_DEBUG
631+ error("bad op table");
632+#endif
633+ modrm_found:
634+ modrm_index = i;
635+ /* if a register is used in another operand then it is
636+ used instead of group */
637+ for(i = 0;i < nb_ops; i++) {
638+ v = op_type[i];
639+ if (i != modrm_index &&
640+ (v & (OP_REG | OP_MMX | OP_SSE | OP_CR | OP_TR | OP_DB | OP_SEG))) {
641+ reg = ops[i].reg;
642+ break;
643+ }
644+ }
645+
646+ asm_modrm(reg, &ops[modrm_index]);
647+ }
648+
649+ /* emit constants */
650+ if (pa->opcode == 0x9a || pa->opcode == 0xea) {
651+ /* ljmp or lcall kludge */
652+ gen_expr32(&ops[1].e);
653+ if (ops[0].e.sym)
654+ error("cannot relocate");
655+ gen_le16(ops[0].e.v);
656+ } else {
657+ for(i = 0;i < nb_ops; i++) {
658+ v = op_type[i];
659+ if (v & (OP_IM8 | OP_IM16 | OP_IM32 | OP_IM8S | OP_ADDR)) {
660+ /* if multiple sizes are given it means we must look
661+ at the op size */
662+ if (v == (OP_IM8 | OP_IM16 | OP_IM32) ||
663+ v == (OP_IM16 | OP_IM32)) {
664+ if (ss == 0)
665+ v = OP_IM8;
666+ else if (ss == 1)
667+ v = OP_IM16;
668+ else
669+ v = OP_IM32;
670+ }
671+ if (v & (OP_IM8 | OP_IM8S)) {
672+ if (ops[i].e.sym)
673+ goto error_relocate;
674+ g(ops[i].e.v);
675+ } else if (v & OP_IM16) {
676+ if (ops[i].e.sym) {
677+ error_relocate:
678+ error("cannot relocate");
679+ }
680+ gen_le16(ops[i].e.v);
681+ } else {
682+ if (pa->instr_type & (OPC_JMP | OPC_SHORTJMP)) {
683+ if (is_short_jmp)
684+ g(ops[i].e.v);
685+ else
686+ gen_disp32(&ops[i].e);
687+ } else {
688+ gen_expr32(&ops[i].e);
689+ }
690+ }
691+ }
692+ }
693+ }
694+}
695+
696+#define NB_SAVED_REGS 3
697+#define NB_ASM_REGS 8
698+
699+/* return the constraint priority (we allocate first the lowest
700+ numbered constraints) */
701+static inline int constraint_priority(const char *str)
702+{
703+ int priority, c, pr;
704+
705+ /* we take the lowest priority */
706+ priority = 0;
707+ for(;;) {
708+ c = *str;
709+ if (c == '\0')
710+ break;
711+ str++;
712+ switch(c) {
713+ case 'a':
714+ case 'b':
715+ case 'c':
716+ case 'd':
717+ case 'S':
718+ case 'D':
719+ pr = 0;
720+ break;
721+ case 'q':
722+ pr = 1;
723+ break;
724+ case 'r':
725+ pr = 2;
726+ break;
727+ case 'N':
728+ case 'M':
729+ case 'I':
730+ case 'i':
731+ case 'm':
732+ case 'g':
733+ pr = 3;
734+ break;
735+ default:
736+ error("unknown constraint '%c'", c);
737+ pr = 0;
738+ }
739+ if (pr > priority)
740+ priority = pr;
741+ }
742+ return priority;
743+}
744+
745+static void asm_compute_constraints(uint8_t *regs_allocated,
746+ ASMOperand *operands,
747+ int nb_operands1, int nb_outputs,
748+ int is_output,
749+ uint8_t *input_regs_allocated)
750+{
751+ ASMOperand *op;
752+ int sorted_op[MAX_ASM_OPERANDS];
753+ int i, j, k, p1, p2, tmp, reg, c, base, nb_operands;
754+ const char *str;
755+
756+ if (is_output) {
757+ base = 0;
758+ nb_operands = nb_outputs;
759+ } else {
760+ base = nb_outputs;
761+ nb_operands = nb_operands1 - nb_outputs;
762+ }
763+
764+ /* compute constraint priority and evaluate references to output
765+ constraints if input constraints */
766+ for(i=0;i<nb_operands;i++) {
767+ j = base + i;
768+ op = &operands[j];
769+ str = op->constraint;
770+ op->ref_index = -1;
771+ op->reg = -1;
772+ if (!is_output && (isnum(*str) || *str == '[')) {
773+ /* this is a reference to another constraint */
774+ k = find_constraint(operands, nb_operands1, str, NULL);
775+ if ((unsigned)k >= j)
776+ error("invalid reference in constraint %d ('%s')",
777+ j, str);
778+ op->ref_index = k;
779+ str = operands[k].constraint;
780+ }
781+ while (*str == '=' || *str == '&' || *str == '+')
782+ str++;
783+ op->priority = constraint_priority(str);
784+ }
785+
786+ /* sort operands according to their priority */
787+ for(i=0;i<nb_operands;i++)
788+ sorted_op[i] = base + i;
789+ for(i=0;i<nb_operands - 1;i++) {
790+ for(j=i+1;j<nb_operands;j++) {
791+ p1 = operands[sorted_op[i]].priority;
792+ p2 = operands[sorted_op[j]].priority;
793+ if (p2 < p1) {
794+ tmp = sorted_op[i];
795+ sorted_op[i] = sorted_op[j];
796+ sorted_op[j] = tmp;
797+ }
798+ }
799+ }
800+
801+ memset(regs_allocated, 0, NB_ASM_REGS);
802+ regs_allocated[4] = 1; /* esp cannot be used */
803+ regs_allocated[5] = 1; /* ebp cannot be used yet */
804+
805+ /* allocate registers and generate corresponding asm moves */
806+ for(i=0;i<nb_operands;i++) {
807+ j = sorted_op[i];
808+ op = &operands[j];
809+ str = op->constraint;
810+
811+ if (op->ref_index >= 0) {
812+ str = operands[op->ref_index].constraint;
813+ }
814+
815+ while (*str == '=' || *str == '&' || *str == '+')
816+ str++;
817+ try_next:
818+ c = *str++;
819+ switch(c) {
820+ case 'a':
821+ reg = TREG_EAX;
822+ goto alloc_reg;
823+ case 'b':
824+ reg = 3;
825+ goto alloc_reg;
826+ case 'c':
827+ reg = TREG_ECX;
828+ goto alloc_reg;
829+ case 'd':
830+ reg = TREG_EDX;
831+ goto alloc_reg;
832+ case 'S':
833+ reg = 6;
834+ goto alloc_reg;
835+ case 'D':
836+ reg = 7;
837+ alloc_reg:
838+ if (regs_allocated[reg])
839+ goto try_next;
840+ goto reg_found;
841+ case 'q':
842+ /* eax, ebx, ecx or edx */
843+ for(reg = 0; reg < 4; reg++) {
844+ if (!regs_allocated[reg])
845+ goto reg_found;
846+ }
847+ goto try_next;
848+ case 'r':
849+ /* any general register */
850+ for(reg = 0; reg < 8; reg++) {
851+ if (!regs_allocated[reg])
852+ goto reg_found;
853+ }
854+ goto try_next;
855+ reg_found:
856+ /* now we can reload in the register */
857+ op->reg = reg;
858+ regs_allocated[reg] = 1;
859+ break;
860+ case 'i':
861+ if (!((op->vt->r & (VT_VALMASK | VT_LVAL)) == VT_CONST))
862+ goto try_next;
863+ break;
864+ case 'I':
865+ case 'N':
866+ case 'M':
867+ if (!((op->vt->r & (VT_VALMASK | VT_LVAL | VT_SYM)) == VT_CONST))
868+ goto try_next;
869+ break;
870+ case 'm':
871+ case 'g':
872+ /* nothing special to do because the operand is
873+ already in memory */
874+ /* XXX: fix constant case */
875+ if (is_output) {
876+ /* if it is a reference to a memory zone, it must lie
877+ in a register, so we reserve the register in the
878+ input registers and a load will be generated
879+ later */
880+ if ((op->vt->r & VT_VALMASK) == VT_LLOCAL) {
881+ /* any general register */
882+ for(reg = 0; reg < 8; reg++) {
883+ if (!input_regs_allocated[reg])
884+ goto reg_found1;
885+ }
886+ goto try_next;
887+ reg_found1:
888+ /* now we can reload in the register */
889+ input_regs_allocated[reg] = 1;
890+ op->reg = reg;
891+ }
892+ }
893+ break;
894+ default:
895+ error("asm constraint %d ('%s') could not be satisfied",
896+ j, op->constraint);
897+ break;
898+ }
899+ }
900+
901+ /* print sorted constraints */
902+#ifdef ASM_DEBUG
903+ if (is_output)
904+ printf("outputs=\n");
905+ else
906+ printf("inputs=\n");
907+ for(i=0;i<nb_operands;i++) {
908+ j = sorted_op[i];
909+ op = &operands[j];
910+ printf("%%%d [%s]: \"%s\" r=0x%04x reg=%d\n",
911+ j,
912+ op->id ? get_tok_str(op->id, NULL) : "",
913+ op->constraint,
914+ op->vt->r,
915+ op->reg);
916+ }
917+#endif
918+}
919+
920+static void subst_asm_operand(CString *add_str,
921+ SValue *sv, int modifier)
922+{
923+ int r, reg, size, val;
924+ char buf[64];
925+
926+ r = sv->r;
927+ if ((r & VT_VALMASK) == VT_CONST) {
928+ if (!(r & VT_LVAL) && modifier != 'c' && modifier != 'n')
929+ cstr_ccat(add_str, '$');
930+ if (r & VT_SYM) {
931+ cstr_cat(add_str, get_tok_str(sv->sym->v, NULL));
932+ if (sv->c.i != 0) {
933+ cstr_ccat(add_str, '+');
934+ } else {
935+ return;
936+ }
937+ }
938+ val = sv->c.i;
939+ if (modifier == 'n')
940+ val = -val;
941+ snprintf(buf, sizeof(buf), "%d", sv->c.i);
942+ cstr_cat(add_str, buf);
943+ } else if ((r & VT_VALMASK) == VT_LOCAL) {
944+ snprintf(buf, sizeof(buf), "%d(%%ebp)", sv->c.i);
945+ cstr_cat(add_str, buf);
946+ } else if (r & VT_LVAL) {
947+ reg = r & VT_VALMASK;
948+ if (reg >= VT_CONST)
949+ error("internal compiler error");
950+ snprintf(buf, sizeof(buf), "(%%%s)",
951+ get_tok_str(TOK_ASM_eax + reg, NULL));
952+ cstr_cat(add_str, buf);
953+ } else {
954+ /* register case */
955+ reg = r & VT_VALMASK;
956+ if (reg >= VT_CONST)
957+ error("internal compiler error");
958+
959+ /* choose register operand size */
960+ if ((sv->type.t & VT_BTYPE) == VT_BYTE)
961+ size = 1;
962+ else if ((sv->type.t & VT_BTYPE) == VT_SHORT)
963+ size = 2;
964+ else
965+ size = 4;
966+ if (size == 1 && reg >= 4)
967+ size = 4;
968+
969+ if (modifier == 'b') {
970+ if (reg >= 4)
971+ error("cannot use byte register");
972+ size = 1;
973+ } else if (modifier == 'h') {
974+ if (reg >= 4)
975+ error("cannot use byte register");
976+ size = -1;
977+ } else if (modifier == 'w') {
978+ size = 2;
979+ }
980+
981+ switch(size) {
982+ case -1:
983+ reg = TOK_ASM_ah + reg;
984+ break;
985+ case 1:
986+ reg = TOK_ASM_al + reg;
987+ break;
988+ case 2:
989+ reg = TOK_ASM_ax + reg;
990+ break;
991+ default:
992+ reg = TOK_ASM_eax + reg;
993+ break;
994+ }
995+ snprintf(buf, sizeof(buf), "%%%s", get_tok_str(reg, NULL));
996+ cstr_cat(add_str, buf);
997+ }
998+}
999+
1000+/* generate prolog and epilog code for asm statment */
1001+static void asm_gen_code(ASMOperand *operands, int nb_operands,
1002+ int nb_outputs, int is_output,
1003+ uint8_t *clobber_regs)
1004+{
1005+ uint8_t regs_allocated[NB_ASM_REGS];
1006+ ASMOperand *op;
1007+ int i, reg;
1008+ static uint8_t reg_saved[NB_SAVED_REGS] = { 3, 6, 7 };
1009+
1010+ /* mark all used registers */
1011+ memcpy(regs_allocated, clobber_regs, sizeof(regs_allocated));
1012+ for(i = 0; i < nb_operands;i++) {
1013+ op = &operands[i];
1014+ if (op->reg >= 0)
1015+ regs_allocated[op->reg] = 1;
1016+ }
1017+ if (!is_output) {
1018+ /* generate reg save code */
1019+ for(i = 0; i < NB_SAVED_REGS; i++) {
1020+ reg = reg_saved[i];
1021+ if (regs_allocated[reg])
1022+ g(0x50 + reg);
1023+ }
1024+
1025+ /* generate load code */
1026+ for(i = nb_outputs ; i < nb_operands; i++) {
1027+ op = &operands[i];
1028+ if (op->reg >= 0) {
1029+ load(op->reg, op->vt);
1030+ }
1031+ }
1032+ /* generate load code for output memory references */
1033+ for(i = 0 ; i < nb_outputs; i++) {
1034+ op = &operands[i];
1035+ if (op->reg >= 0 && ((op->vt->r & VT_VALMASK) == VT_LLOCAL)) {
1036+ SValue sv;
1037+ sv = *op->vt;
1038+ sv.r = (sv.r & ~VT_VALMASK) | VT_LOCAL;
1039+ load(op->reg, &sv);
1040+ }
1041+ }
1042+ } else {
1043+ /* generate save code */
1044+ for(i = 0 ; i < nb_outputs; i++) {
1045+ op = &operands[i];
1046+ if (op->reg >= 0 && ((op->vt->r & VT_VALMASK) != VT_LLOCAL)) {
1047+ store(op->reg, op->vt);
1048+ }
1049+ }
1050+ /* generate reg restore code */
1051+ for(i = NB_SAVED_REGS - 1; i >= 0; i--) {
1052+ reg = reg_saved[i];
1053+ if (regs_allocated[reg])
1054+ g(0x58 + reg);
1055+ }
1056+ }
1057+}
1058+
1059+static void asm_clobber(uint8_t *clobber_regs, const char *str)
1060+{
1061+ int reg;
1062+ TokenSym *ts;
1063+
1064+ if (!strcmp(str, "memory") ||
1065+ !strcmp(str, "cc"))
1066+ return;
1067+ ts = tok_alloc(str, strlen(str));
1068+ reg = ts->tok;
1069+ if (reg >= TOK_ASM_eax && reg <= TOK_ASM_edi) {
1070+ reg -= TOK_ASM_eax;
1071+ } else if (reg >= TOK_ASM_ax && reg <= TOK_ASM_di) {
1072+ reg -= TOK_ASM_ax;
1073+ } else {
1074+ error("invalid clobber register '%s'", str);
1075+ }
1076+ clobber_regs[reg] = 1;
1077+}
i386-asm.h+444−0
@@ -0,0 +1,444 @@
1+ DEF_ASM_OP0(pusha, 0x60) /* must be first OP0 */
2+ DEF_ASM_OP0(popa, 0x61)
3+ DEF_ASM_OP0(clc, 0xf8)
4+ DEF_ASM_OP0(cld, 0xfc)
5+ DEF_ASM_OP0(cli, 0xfa)
6+ DEF_ASM_OP0(clts, 0x0f06)
7+ DEF_ASM_OP0(cmc, 0xf5)
8+ DEF_ASM_OP0(lahf, 0x9f)
9+ DEF_ASM_OP0(sahf, 0x9e)
10+ DEF_ASM_OP0(pushfl, 0x9c)
11+ DEF_ASM_OP0(popfl, 0x9d)
12+ DEF_ASM_OP0(pushf, 0x9c)
13+ DEF_ASM_OP0(popf, 0x9d)
14+ DEF_ASM_OP0(stc, 0xf9)
15+ DEF_ASM_OP0(std, 0xfd)
16+ DEF_ASM_OP0(sti, 0xfb)
17+ DEF_ASM_OP0(aaa, 0x37)
18+ DEF_ASM_OP0(aas, 0x3f)
19+ DEF_ASM_OP0(daa, 0x27)
20+ DEF_ASM_OP0(das, 0x2f)
21+ DEF_ASM_OP0(aad, 0xd50a)
22+ DEF_ASM_OP0(aam, 0xd40a)
23+ DEF_ASM_OP0(cbw, 0x6698)
24+ DEF_ASM_OP0(cwd, 0x6699)
25+ DEF_ASM_OP0(cwde, 0x98)
26+ DEF_ASM_OP0(cdq, 0x99)
27+ DEF_ASM_OP0(cbtw, 0x6698)
28+ DEF_ASM_OP0(cwtl, 0x98)
29+ DEF_ASM_OP0(cwtd, 0x6699)
30+ DEF_ASM_OP0(cltd, 0x99)
31+ DEF_ASM_OP0(int3, 0xcc)
32+ DEF_ASM_OP0(into, 0xce)
33+ DEF_ASM_OP0(iret, 0xcf)
34+ DEF_ASM_OP0(rsm, 0x0faa)
35+ DEF_ASM_OP0(hlt, 0xf4)
36+ DEF_ASM_OP0(wait, 0x9b)
37+ DEF_ASM_OP0(nop, 0x90)
38+ DEF_ASM_OP0(xlat, 0xd7)
39+
40+ /* strings */
41+ALT(DEF_ASM_OP0L(cmpsb, 0xa6, 0, OPC_BWL))
42+ALT(DEF_ASM_OP0L(scmpb, 0xa6, 0, OPC_BWL))
43+
44+ALT(DEF_ASM_OP0L(insb, 0x6c, 0, OPC_BWL))
45+ALT(DEF_ASM_OP0L(outsb, 0x6e, 0, OPC_BWL))
46+
47+ALT(DEF_ASM_OP0L(lodsb, 0xac, 0, OPC_BWL))
48+ALT(DEF_ASM_OP0L(slodb, 0xac, 0, OPC_BWL))
49+
50+ALT(DEF_ASM_OP0L(movsb, 0xa4, 0, OPC_BWL))
51+ALT(DEF_ASM_OP0L(smovb, 0xa4, 0, OPC_BWL))
52+
53+ALT(DEF_ASM_OP0L(scasb, 0xae, 0, OPC_BWL))
54+ALT(DEF_ASM_OP0L(sscab, 0xae, 0, OPC_BWL))
55+
56+ALT(DEF_ASM_OP0L(stosb, 0xaa, 0, OPC_BWL))
57+ALT(DEF_ASM_OP0L(sstob, 0xaa, 0, OPC_BWL))
58+
59+ /* bits */
60+
61+ALT(DEF_ASM_OP2(bsfw, 0x0fbc, 0, OPC_MODRM | OPC_WL, OPT_REGW | OPT_EA, OPT_REGW))
62+ALT(DEF_ASM_OP2(bsrw, 0x0fbd, 0, OPC_MODRM | OPC_WL, OPT_REGW | OPT_EA, OPT_REGW))
63+
64+ALT(DEF_ASM_OP2(btw, 0x0fa3, 0, OPC_MODRM | OPC_WL, OPT_REGW, OPT_REGW | OPT_EA))
65+ALT(DEF_ASM_OP2(btw, 0x0fba, 4, OPC_MODRM | OPC_WL, OPT_IM8, OPT_REGW | OPT_EA))
66+
67+ALT(DEF_ASM_OP2(btsw, 0x0fab, 0, OPC_MODRM | OPC_WL, OPT_REGW, OPT_REGW | OPT_EA))
68+ALT(DEF_ASM_OP2(btsw, 0x0fba, 5, OPC_MODRM | OPC_WL, OPT_IM8, OPT_REGW | OPT_EA))
69+
70+ALT(DEF_ASM_OP2(btrw, 0x0fb3, 0, OPC_MODRM | OPC_WL, OPT_REGW, OPT_REGW | OPT_EA))
71+ALT(DEF_ASM_OP2(btrw, 0x0fba, 6, OPC_MODRM | OPC_WL, OPT_IM8, OPT_REGW | OPT_EA))
72+
73+ALT(DEF_ASM_OP2(btcw, 0x0fbb, 0, OPC_MODRM | OPC_WL, OPT_REGW, OPT_REGW | OPT_EA))
74+ALT(DEF_ASM_OP2(btcw, 0x0fba, 7, OPC_MODRM | OPC_WL, OPT_IM8, OPT_REGW | OPT_EA))
75+
76+ /* prefixes */
77+ DEF_ASM_OP0(aword, 0x67)
78+ DEF_ASM_OP0(addr16, 0x67)
79+ DEF_ASM_OP0(word, 0x66)
80+ DEF_ASM_OP0(data16, 0x66)
81+ DEF_ASM_OP0(lock, 0xf0)
82+ DEF_ASM_OP0(rep, 0xf3)
83+ DEF_ASM_OP0(repe, 0xf3)
84+ DEF_ASM_OP0(repz, 0xf3)
85+ DEF_ASM_OP0(repne, 0xf2)
86+ DEF_ASM_OP0(repnz, 0xf2)
87+
88+ DEF_ASM_OP0(invd, 0x0f08)
89+ DEF_ASM_OP0(wbinvd, 0x0f09)
90+ DEF_ASM_OP0(cpuid, 0x0fa2)
91+ DEF_ASM_OP0(wrmsr, 0x0f30)
92+ DEF_ASM_OP0(rdtsc, 0x0f31)
93+ DEF_ASM_OP0(rdmsr, 0x0f32)
94+ DEF_ASM_OP0(rdpmc, 0x0f33)
95+ DEF_ASM_OP0(ud2, 0x0f0b)
96+
97+ /* NOTE: we took the same order as gas opcode definition order */
98+ALT(DEF_ASM_OP2(movb, 0xa0, 0, OPC_BWL, OPT_ADDR, OPT_EAX))
99+ALT(DEF_ASM_OP2(movb, 0xa2, 0, OPC_BWL, OPT_EAX, OPT_ADDR))
100+ALT(DEF_ASM_OP2(movb, 0x88, 0, OPC_MODRM | OPC_BWL, OPT_REG, OPT_EA | OPT_REG))
101+ALT(DEF_ASM_OP2(movb, 0x8a, 0, OPC_MODRM | OPC_BWL, OPT_EA | OPT_REG, OPT_REG))
102+ALT(DEF_ASM_OP2(movb, 0xb0, 0, OPC_REG | OPC_BWL, OPT_IM, OPT_REG))
103+ALT(DEF_ASM_OP2(movb, 0xc6, 0, OPC_MODRM | OPC_BWL, OPT_IM, OPT_REG | OPT_EA))
104+
105+ALT(DEF_ASM_OP2(movw, 0x8c, 0, OPC_MODRM | OPC_WL, OPT_SEG, OPT_EA | OPT_REG))
106+ALT(DEF_ASM_OP2(movw, 0x8e, 0, OPC_MODRM | OPC_WL, OPT_EA | OPT_REG, OPT_SEG))
107+
108+ALT(DEF_ASM_OP2(movw, 0x0f20, 0, OPC_MODRM | OPC_WL, OPT_CR, OPT_REG32))
109+ALT(DEF_ASM_OP2(movw, 0x0f21, 0, OPC_MODRM | OPC_WL, OPT_DB, OPT_REG32))
110+ALT(DEF_ASM_OP2(movw, 0x0f24, 0, OPC_MODRM | OPC_WL, OPT_TR, OPT_REG32))
111+ALT(DEF_ASM_OP2(movw, 0x0f22, 0, OPC_MODRM | OPC_WL, OPT_REG32, OPT_CR))
112+ALT(DEF_ASM_OP2(movw, 0x0f23, 0, OPC_MODRM | OPC_WL, OPT_REG32, OPT_DB))
113+ALT(DEF_ASM_OP2(movw, 0x0f26, 0, OPC_MODRM | OPC_WL, OPT_REG32, OPT_TR))
114+
115+ALT(DEF_ASM_OP2(movsbl, 0x0fbe, 0, OPC_MODRM, OPT_REG8 | OPT_EA, OPT_REG32))
116+ALT(DEF_ASM_OP2(movsbw, 0x0fbe, 0, OPC_MODRM | OPC_D16, OPT_REG8 | OPT_EA, OPT_REG16))
117+ALT(DEF_ASM_OP2(movswl, 0x0fbf, 0, OPC_MODRM, OPT_REG16 | OPT_EA, OPT_REG32))
118+ALT(DEF_ASM_OP2(movzbw, 0x0fb6, 0, OPC_MODRM | OPC_WL, OPT_REG8 | OPT_EA, OPT_REGW))
119+ALT(DEF_ASM_OP2(movzwl, 0x0fb7, 0, OPC_MODRM, OPT_REG16 | OPT_EA, OPT_REG32))
120+
121+ALT(DEF_ASM_OP1(pushw, 0x50, 0, OPC_REG | OPC_WL, OPT_REGW))
122+ALT(DEF_ASM_OP1(pushw, 0xff, 6, OPC_MODRM | OPC_WL, OPT_REGW | OPT_EA))
123+ALT(DEF_ASM_OP1(push, 0x6a, 0, 0, OPT_IM8S))
124+ALT(DEF_ASM_OP1(push, 0x68, 0, 0, OPT_IM32))
125+ALT(DEF_ASM_OP1(push, 0x06, 0, 0, OPT_SEG))
126+
127+ALT(DEF_ASM_OP1(popw, 0x58, 0, OPC_REG | OPC_WL, OPT_REGW))
128+ALT(DEF_ASM_OP1(popw, 0x8f, 0, OPC_MODRM | OPC_WL, OPT_REGW | OPT_EA))
129+ALT(DEF_ASM_OP1(pop, 0x07, 0, 0, OPT_SEG))
130+
131+ALT(DEF_ASM_OP2(xchgw, 0x90, 0, OPC_REG | OPC_WL, OPT_REG, OPT_EAX))
132+ALT(DEF_ASM_OP2(xchgw, 0x90, 0, OPC_REG | OPC_WL, OPT_EAX, OPT_REG))
133+ALT(DEF_ASM_OP2(xchgb, 0x86, 0, OPC_MODRM | OPC_BWL, OPT_REG, OPT_EA | OPT_REG))
134+ALT(DEF_ASM_OP2(xchgb, 0x86, 0, OPC_MODRM | OPC_BWL, OPT_EA | OPT_REG, OPT_REG))
135+
136+ALT(DEF_ASM_OP2(inb, 0xe4, 0, OPC_BWL, OPT_IM8, OPT_EAX))
137+ALT(DEF_ASM_OP1(inb, 0xe4, 0, OPC_BWL, OPT_IM8))
138+ALT(DEF_ASM_OP2(inb, 0xec, 0, OPC_BWL, OPT_DX, OPT_EAX))
139+ALT(DEF_ASM_OP1(inb, 0xec, 0, OPC_BWL, OPT_DX))
140+
141+ALT(DEF_ASM_OP2(outb, 0xe6, 0, OPC_BWL, OPT_EAX, OPT_IM8))
142+ALT(DEF_ASM_OP1(outb, 0xe6, 0, OPC_BWL, OPT_IM8))
143+ALT(DEF_ASM_OP2(outb, 0xee, 0, OPC_BWL, OPT_EAX, OPT_DX))
144+ALT(DEF_ASM_OP1(outb, 0xee, 0, OPC_BWL, OPT_DX))
145+
146+ALT(DEF_ASM_OP2(leaw, 0x8d, 0, OPC_MODRM | OPC_WL, OPT_EA, OPT_REG))
147+
148+ALT(DEF_ASM_OP2(les, 0xc4, 0, OPC_MODRM, OPT_EA, OPT_REG32))
149+ALT(DEF_ASM_OP2(lds, 0xc5, 0, OPC_MODRM, OPT_EA, OPT_REG32))
150+ALT(DEF_ASM_OP2(lss, 0x0fb2, 0, OPC_MODRM, OPT_EA, OPT_REG32))
151+ALT(DEF_ASM_OP2(lfs, 0x0fb4, 0, OPC_MODRM, OPT_EA, OPT_REG32))
152+ALT(DEF_ASM_OP2(lgs, 0x0fb5, 0, OPC_MODRM, OPT_EA, OPT_REG32))
153+
154+ /* arith */
155+ALT(DEF_ASM_OP2(addb, 0x00, 0, OPC_ARITH | OPC_MODRM | OPC_BWL, OPT_REG, OPT_EA | OPT_REG)) /* XXX: use D bit ? */
156+ALT(DEF_ASM_OP2(addb, 0x02, 0, OPC_ARITH | OPC_MODRM | OPC_BWL, OPT_EA | OPT_REG, OPT_REG))
157+ALT(DEF_ASM_OP2(addb, 0x04, 0, OPC_ARITH | OPC_BWL, OPT_IM, OPT_EAX))
158+ALT(DEF_ASM_OP2(addb, 0x80, 0, OPC_ARITH | OPC_MODRM | OPC_BWL, OPT_IM, OPT_EA | OPT_REG))
159+ALT(DEF_ASM_OP2(addw, 0x83, 0, OPC_ARITH | OPC_MODRM | OPC_WL, OPT_IM8S, OPT_EA | OPT_REG))
160+
161+ALT(DEF_ASM_OP2(testb, 0x84, 0, OPC_MODRM | OPC_BWL, OPT_EA | OPT_REG, OPT_REG))
162+ALT(DEF_ASM_OP2(testb, 0x84, 0, OPC_MODRM | OPC_BWL, OPT_REG, OPT_EA | OPT_REG))
163+ALT(DEF_ASM_OP2(testb, 0xa8, 0, OPC_BWL, OPT_IM, OPT_EAX))
164+ALT(DEF_ASM_OP2(testb, 0xf6, 0, OPC_MODRM | OPC_BWL, OPT_IM, OPT_EA | OPT_REG))
165+
166+ALT(DEF_ASM_OP1(incw, 0x40, 0, OPC_REG | OPC_WL, OPT_REGW))
167+ALT(DEF_ASM_OP1(incb, 0xfe, 0, OPC_MODRM | OPC_BWL, OPT_REG | OPT_EA))
168+ALT(DEF_ASM_OP1(decw, 0x48, 0, OPC_REG | OPC_WL, OPT_REGW))
169+ALT(DEF_ASM_OP1(decb, 0xfe, 1, OPC_MODRM | OPC_BWL, OPT_REG | OPT_EA))
170+
171+ALT(DEF_ASM_OP1(notb, 0xf6, 2, OPC_MODRM | OPC_BWL, OPT_REG | OPT_EA))
172+ALT(DEF_ASM_OP1(negb, 0xf6, 3, OPC_MODRM | OPC_BWL, OPT_REG | OPT_EA))
173+
174+ALT(DEF_ASM_OP1(mulb, 0xf6, 4, OPC_MODRM | OPC_BWL, OPT_REG | OPT_EA))
175+ALT(DEF_ASM_OP1(imulb, 0xf6, 5, OPC_MODRM | OPC_BWL, OPT_REG | OPT_EA))
176+
177+ALT(DEF_ASM_OP2(imulw, 0x0faf, 0, OPC_MODRM | OPC_WL, OPT_REG | OPT_EA, OPT_REG))
178+ALT(DEF_ASM_OP3(imulw, 0x6b, 0, OPC_MODRM | OPC_WL, OPT_IM8S, OPT_REGW | OPT_EA, OPT_REGW))
179+ALT(DEF_ASM_OP2(imulw, 0x6b, 0, OPC_MODRM | OPC_WL, OPT_IM8S, OPT_REGW))
180+ALT(DEF_ASM_OP3(imulw, 0x69, 0, OPC_MODRM | OPC_WL, OPT_IMW, OPT_REGW | OPT_EA, OPT_REGW))
181+ALT(DEF_ASM_OP2(imulw, 0x69, 0, OPC_MODRM | OPC_WL, OPT_IMW, OPT_REGW))
182+
183+ALT(DEF_ASM_OP1(divb, 0xf6, 6, OPC_MODRM | OPC_BWL, OPT_REG | OPT_EA))
184+ALT(DEF_ASM_OP2(divb, 0xf6, 6, OPC_MODRM | OPC_BWL, OPT_REG | OPT_EA, OPT_EAX))
185+ALT(DEF_ASM_OP1(idivb, 0xf6, 7, OPC_MODRM | OPC_BWL, OPT_REG | OPT_EA))
186+ALT(DEF_ASM_OP2(idivb, 0xf6, 7, OPC_MODRM | OPC_BWL, OPT_REG | OPT_EA, OPT_EAX))
187+
188+ /* shifts */
189+ALT(DEF_ASM_OP2(rolb, 0xc0, 0, OPC_MODRM | OPC_BWL | OPC_SHIFT, OPT_IM8, OPT_EA | OPT_REG))
190+ALT(DEF_ASM_OP2(rolb, 0xd2, 0, OPC_MODRM | OPC_BWL | OPC_SHIFT, OPT_CL, OPT_EA | OPT_REG))
191+ALT(DEF_ASM_OP1(rolb, 0xd0, 0, OPC_MODRM | OPC_BWL | OPC_SHIFT, OPT_EA | OPT_REG))
192+
193+ALT(DEF_ASM_OP3(shldw, 0x0fa4, 0, OPC_MODRM | OPC_WL, OPT_IM8, OPT_REGW, OPT_EA | OPT_REGW))
194+ALT(DEF_ASM_OP3(shldw, 0x0fa5, 0, OPC_MODRM | OPC_WL, OPT_CL, OPT_REGW, OPT_EA | OPT_REGW))
195+ALT(DEF_ASM_OP2(shldw, 0x0fa5, 0, OPC_MODRM | OPC_WL, OPT_REGW, OPT_EA | OPT_REGW))
196+ALT(DEF_ASM_OP3(shrdw, 0x0fac, 0, OPC_MODRM | OPC_WL, OPT_IM8, OPT_REGW, OPT_EA | OPT_REGW))
197+ALT(DEF_ASM_OP3(shrdw, 0x0fad, 0, OPC_MODRM | OPC_WL, OPT_CL, OPT_REGW, OPT_EA | OPT_REGW))
198+ALT(DEF_ASM_OP2(shrdw, 0x0fad, 0, OPC_MODRM | OPC_WL, OPT_REGW, OPT_EA | OPT_REGW))
199+
200+ALT(DEF_ASM_OP1(call, 0xff, 2, OPC_MODRM, OPT_INDIR))
201+ALT(DEF_ASM_OP1(call, 0xe8, 0, OPC_JMP, OPT_ADDR))
202+ALT(DEF_ASM_OP1(jmp, 0xff, 4, OPC_MODRM, OPT_INDIR))
203+ALT(DEF_ASM_OP1(jmp, 0xeb, 0, OPC_SHORTJMP | OPC_JMP, OPT_ADDR))
204+
205+ALT(DEF_ASM_OP2(lcall, 0x9a, 0, 0, OPT_IM16, OPT_IM32))
206+ALT(DEF_ASM_OP1(lcall, 0xff, 3, 0, OPT_EA))
207+ALT(DEF_ASM_OP2(ljmp, 0xea, 0, 0, OPT_IM16, OPT_IM32))
208+ALT(DEF_ASM_OP1(ljmp, 0xff, 5, 0, OPT_EA))
209+
210+ALT(DEF_ASM_OP1(int, 0xcd, 0, 0, OPT_IM8))
211+ALT(DEF_ASM_OP1(seto, 0x0f90, 0, OPC_MODRM | OPC_TEST, OPT_REG8 | OPT_EA))
212+ DEF_ASM_OP2(enter, 0xc8, 0, 0, OPT_IM16, OPT_IM8)
213+ DEF_ASM_OP0(leave, 0xc9)
214+ DEF_ASM_OP0(ret, 0xc3)
215+ALT(DEF_ASM_OP1(ret, 0xc2, 0, 0, OPT_IM16))
216+ DEF_ASM_OP0(lret, 0xcb)
217+ALT(DEF_ASM_OP1(lret, 0xca, 0, 0, OPT_IM16))
218+
219+ALT(DEF_ASM_OP1(jo, 0x70, 0, OPC_SHORTJMP | OPC_JMP | OPC_TEST, OPT_ADDR))
220+ DEF_ASM_OP1(loopne, 0xe0, 0, OPC_SHORTJMP, OPT_ADDR)
221+ DEF_ASM_OP1(loopnz, 0xe0, 0, OPC_SHORTJMP, OPT_ADDR)
222+ DEF_ASM_OP1(loope, 0xe1, 0, OPC_SHORTJMP, OPT_ADDR)
223+ DEF_ASM_OP1(loopz, 0xe1, 0, OPC_SHORTJMP, OPT_ADDR)
224+ DEF_ASM_OP1(loop, 0xe2, 0, OPC_SHORTJMP, OPT_ADDR)
225+ DEF_ASM_OP1(jecxz, 0xe3, 0, OPC_SHORTJMP, OPT_ADDR)
226+
227+ /* float */
228+ /* specific fcomp handling */
229+ALT(DEF_ASM_OP0L(fcomp, 0xd8d9, 0, 0))
230+
231+ALT(DEF_ASM_OP1(fadd, 0xd8c0, 0, OPC_FARITH | OPC_REG, OPT_ST))
232+ALT(DEF_ASM_OP2(fadd, 0xd8c0, 0, OPC_FARITH | OPC_REG, OPT_ST, OPT_ST0))
233+ALT(DEF_ASM_OP0L(fadd, 0xdec1, 0, OPC_FARITH))
234+ALT(DEF_ASM_OP1(faddp, 0xdec0, 0, OPC_FARITH | OPC_REG, OPT_ST))
235+ALT(DEF_ASM_OP2(faddp, 0xdec0, 0, OPC_FARITH | OPC_REG, OPT_ST, OPT_ST0))
236+ALT(DEF_ASM_OP2(faddp, 0xdec0, 0, OPC_FARITH | OPC_REG, OPT_ST0, OPT_ST))
237+ALT(DEF_ASM_OP0L(faddp, 0xdec1, 0, OPC_FARITH))
238+ALT(DEF_ASM_OP1(fadds, 0xd8, 0, OPC_FARITH | OPC_MODRM, OPT_EA))
239+ALT(DEF_ASM_OP1(fiaddl, 0xda, 0, OPC_FARITH | OPC_MODRM, OPT_EA))
240+ALT(DEF_ASM_OP1(faddl, 0xdc, 0, OPC_FARITH | OPC_MODRM, OPT_EA))
241+ALT(DEF_ASM_OP1(fiadds, 0xde, 0, OPC_FARITH | OPC_MODRM, OPT_EA))
242+
243+ DEF_ASM_OP0(fucompp, 0xdae9)
244+ DEF_ASM_OP0(ftst, 0xd9e4)
245+ DEF_ASM_OP0(fxam, 0xd9e5)
246+ DEF_ASM_OP0(fld1, 0xd9e8)
247+ DEF_ASM_OP0(fldl2t, 0xd9e9)
248+ DEF_ASM_OP0(fldl2e, 0xd9ea)
249+ DEF_ASM_OP0(fldpi, 0xd9eb)
250+ DEF_ASM_OP0(fldlg2, 0xd9ec)
251+ DEF_ASM_OP0(fldln2, 0xd9ed)
252+ DEF_ASM_OP0(fldz, 0xd9ee)
253+
254+ DEF_ASM_OP0(f2xm1, 0xd9f0)
255+ DEF_ASM_OP0(fyl2x, 0xd9f1)
256+ DEF_ASM_OP0(fptan, 0xd9f2)
257+ DEF_ASM_OP0(fpatan, 0xd9f3)
258+ DEF_ASM_OP0(fxtract, 0xd9f4)
259+ DEF_ASM_OP0(fprem1, 0xd9f5)
260+ DEF_ASM_OP0(fdecstp, 0xd9f6)
261+ DEF_ASM_OP0(fincstp, 0xd9f7)
262+ DEF_ASM_OP0(fprem, 0xd9f8)
263+ DEF_ASM_OP0(fyl2xp1, 0xd9f9)
264+ DEF_ASM_OP0(fsqrt, 0xd9fa)
265+ DEF_ASM_OP0(fsincos, 0xd9fb)
266+ DEF_ASM_OP0(frndint, 0xd9fc)
267+ DEF_ASM_OP0(fscale, 0xd9fd)
268+ DEF_ASM_OP0(fsin, 0xd9fe)
269+ DEF_ASM_OP0(fcos, 0xd9ff)
270+ DEF_ASM_OP0(fchs, 0xd9e0)
271+ DEF_ASM_OP0(fabs, 0xd9e1)
272+ DEF_ASM_OP0(fninit, 0xdbe3)
273+ DEF_ASM_OP0(fnclex, 0xdbe2)
274+ DEF_ASM_OP0(fnop, 0xd9d0)
275+ DEF_ASM_OP0(fwait, 0x9b)
276+
277+ /* fp load */
278+ DEF_ASM_OP1(fld, 0xd9c0, 0, OPC_REG, OPT_ST)
279+ DEF_ASM_OP1(fldl, 0xd9c0, 0, OPC_REG, OPT_ST)
280+ DEF_ASM_OP1(flds, 0xd9, 0, OPC_MODRM, OPT_EA)
281+ALT(DEF_ASM_OP1(fldl, 0xdd, 0, OPC_MODRM, OPT_EA))
282+ DEF_ASM_OP1(fildl, 0xdb, 0, OPC_MODRM, OPT_EA)
283+ DEF_ASM_OP1(fildq, 0xdf, 5, OPC_MODRM, OPT_EA)
284+ DEF_ASM_OP1(fildll, 0xdf, 5, OPC_MODRM,OPT_EA)
285+ DEF_ASM_OP1(fldt, 0xdb, 5, OPC_MODRM, OPT_EA)
286+ DEF_ASM_OP1(fbld, 0xdf, 4, OPC_MODRM, OPT_EA)
287+
288+ /* fp store */
289+ DEF_ASM_OP1(fst, 0xddd0, 0, OPC_REG, OPT_ST)
290+ DEF_ASM_OP1(fstl, 0xddd0, 0, OPC_REG, OPT_ST)
291+ DEF_ASM_OP1(fsts, 0xd9, 2, OPC_MODRM, OPT_EA)
292+ DEF_ASM_OP1(fstps, 0xd9, 3, OPC_MODRM, OPT_EA)
293+ALT(DEF_ASM_OP1(fstl, 0xdd, 2, OPC_MODRM, OPT_EA))
294+ DEF_ASM_OP1(fstpl, 0xdd, 3, OPC_MODRM, OPT_EA)
295+ DEF_ASM_OP1(fist, 0xdf, 2, OPC_MODRM, OPT_EA)
296+ DEF_ASM_OP1(fistp, 0xdf, 3, OPC_MODRM, OPT_EA)
297+ DEF_ASM_OP1(fistl, 0xdb, 2, OPC_MODRM, OPT_EA)
298+ DEF_ASM_OP1(fistpl, 0xdb, 3, OPC_MODRM, OPT_EA)
299+
300+ DEF_ASM_OP1(fstp, 0xddd8, 0, OPC_REG, OPT_ST)
301+ DEF_ASM_OP1(fistpq, 0xdf, 7, OPC_MODRM, OPT_EA)
302+ DEF_ASM_OP1(fistpll, 0xdf, 7, OPC_MODRM, OPT_EA)
303+ DEF_ASM_OP1(fstpt, 0xdb, 7, OPC_MODRM, OPT_EA)
304+ DEF_ASM_OP1(fbstp, 0xdf, 6, OPC_MODRM, OPT_EA)
305+
306+ /* exchange */
307+ DEF_ASM_OP0(fxch, 0xd9c9)
308+ALT(DEF_ASM_OP1(fxch, 0xd9c8, 0, OPC_REG, OPT_ST))
309+
310+ /* misc FPU */
311+ DEF_ASM_OP1(fucom, 0xdde0, 0, OPC_REG, OPT_ST )
312+ DEF_ASM_OP1(fucomp, 0xdde8, 0, OPC_REG, OPT_ST )
313+
314+ DEF_ASM_OP0L(finit, 0xdbe3, 0, OPC_FWAIT)
315+ DEF_ASM_OP1(fldcw, 0xd9, 5, OPC_MODRM, OPT_EA )
316+ DEF_ASM_OP1(fnstcw, 0xd9, 7, OPC_MODRM, OPT_EA )
317+ DEF_ASM_OP1(fstcw, 0xd9, 7, OPC_MODRM | OPC_FWAIT, OPT_EA )
318+ DEF_ASM_OP0(fnstsw, 0xdfe0)
319+ALT(DEF_ASM_OP1(fnstsw, 0xdfe0, 0, 0, OPT_EAX ))
320+ALT(DEF_ASM_OP1(fnstsw, 0xdd, 7, OPC_MODRM, OPT_EA ))
321+ DEF_ASM_OP1(fstsw, 0xdfe0, 0, OPC_FWAIT, OPT_EAX )
322+ALT(DEF_ASM_OP0L(fstsw, 0xdfe0, 0, OPC_FWAIT))
323+ALT(DEF_ASM_OP1(fstsw, 0xdd, 7, OPC_MODRM | OPC_FWAIT, OPT_EA ))
324+ DEF_ASM_OP0L(fclex, 0xdbe2, 0, OPC_FWAIT)
325+ DEF_ASM_OP1(fnstenv, 0xd9, 6, OPC_MODRM, OPT_EA )
326+ DEF_ASM_OP1(fstenv, 0xd9, 6, OPC_MODRM | OPC_FWAIT, OPT_EA )
327+ DEF_ASM_OP1(fldenv, 0xd9, 4, OPC_MODRM, OPT_EA )
328+ DEF_ASM_OP1(fnsave, 0xdd, 6, OPC_MODRM, OPT_EA )
329+ DEF_ASM_OP1(fsave, 0xdd, 6, OPC_MODRM | OPC_FWAIT, OPT_EA )
330+ DEF_ASM_OP1(frstor, 0xdd, 4, OPC_MODRM, OPT_EA )
331+ DEF_ASM_OP1(ffree, 0xddc0, 4, OPC_REG, OPT_ST )
332+ DEF_ASM_OP1(ffreep, 0xdfc0, 4, OPC_REG, OPT_ST )
333+
334+ /* segments */
335+ DEF_ASM_OP2(arpl, 0x63, 0, OPC_MODRM, OPT_REG16, OPT_REG16 | OPT_EA)
336+ DEF_ASM_OP2(lar, 0x0f02, 0, OPC_MODRM, OPT_REG32 | OPT_EA, OPT_REG32)
337+ DEF_ASM_OP1(lgdt, 0x0f01, 2, OPC_MODRM, OPT_EA)
338+ DEF_ASM_OP1(lidt, 0x0f01, 3, OPC_MODRM, OPT_EA)
339+ DEF_ASM_OP1(lldt, 0x0f00, 2, OPC_MODRM, OPT_EA | OPT_REG)
340+ DEF_ASM_OP1(lmsw, 0x0f01, 6, OPC_MODRM, OPT_EA | OPT_REG)
341+ALT(DEF_ASM_OP2(lslw, 0x0f03, 0, OPC_MODRM | OPC_WL, OPT_EA | OPT_REG, OPT_REG))
342+ DEF_ASM_OP1(ltr, 0x0f00, 3, OPC_MODRM, OPT_EA | OPT_REG)
343+ DEF_ASM_OP1(sgdt, 0x0f01, 0, OPC_MODRM, OPT_EA)
344+ DEF_ASM_OP1(sidt, 0x0f01, 1, OPC_MODRM, OPT_EA)
345+ DEF_ASM_OP1(sldt, 0x0f00, 0, OPC_MODRM, OPT_REG | OPT_EA)
346+ DEF_ASM_OP1(smsw, 0x0f01, 4, OPC_MODRM, OPT_REG | OPT_EA)
347+ DEF_ASM_OP1(str, 0x0f00, 1, OPC_MODRM, OPT_REG16| OPT_EA)
348+ DEF_ASM_OP1(verr, 0x0f00, 4, OPC_MODRM, OPT_REG | OPT_EA)
349+ DEF_ASM_OP1(verw, 0x0f00, 5, OPC_MODRM, OPT_REG | OPT_EA)
350+
351+ /* 486 */
352+ DEF_ASM_OP1(bswap, 0x0fc8, 0, OPC_REG, OPT_REG32 )
353+ALT(DEF_ASM_OP2(xaddb, 0x0fc0, 0, OPC_MODRM | OPC_BWL, OPT_REG, OPT_REG | OPT_EA ))
354+ALT(DEF_ASM_OP2(cmpxchgb, 0x0fb0, 0, OPC_MODRM | OPC_BWL, OPT_REG, OPT_REG | OPT_EA ))
355+ DEF_ASM_OP1(invlpg, 0x0f01, 7, OPC_MODRM, OPT_EA )
356+
357+ DEF_ASM_OP2(boundl, 0x62, 0, OPC_MODRM, OPT_REG32, OPT_EA)
358+ DEF_ASM_OP2(boundw, 0x62, 0, OPC_MODRM | OPC_D16, OPT_REG16, OPT_EA)
359+
360+ /* pentium */
361+ DEF_ASM_OP1(cmpxchg8b, 0x0fc7, 1, OPC_MODRM, OPT_EA )
362+
363+ /* pentium pro */
364+ ALT(DEF_ASM_OP2(cmovo, 0x0f40, 0, OPC_MODRM | OPC_TEST, OPT_REG32 | OPT_EA, OPT_REG32))
365+
366+ DEF_ASM_OP2(fcmovb, 0xdac0, 0, OPC_REG, OPT_ST, OPT_ST0 )
367+ DEF_ASM_OP2(fcmove, 0xdac8, 0, OPC_REG, OPT_ST, OPT_ST0 )
368+ DEF_ASM_OP2(fcmovbe, 0xdad0, 0, OPC_REG, OPT_ST, OPT_ST0 )
369+ DEF_ASM_OP2(fcmovu, 0xdad8, 0, OPC_REG, OPT_ST, OPT_ST0 )
370+ DEF_ASM_OP2(fcmovnb, 0xdbc0, 0, OPC_REG, OPT_ST, OPT_ST0 )
371+ DEF_ASM_OP2(fcmovne, 0xdbc8, 0, OPC_REG, OPT_ST, OPT_ST0 )
372+ DEF_ASM_OP2(fcmovnbe, 0xdbd0, 0, OPC_REG, OPT_ST, OPT_ST0 )
373+ DEF_ASM_OP2(fcmovnu, 0xdbd8, 0, OPC_REG, OPT_ST, OPT_ST0 )
374+
375+ DEF_ASM_OP2(fucomi, 0xdbe8, 0, OPC_REG, OPT_ST, OPT_ST0 )
376+ DEF_ASM_OP2(fcomi, 0xdbf0, 0, OPC_REG, OPT_ST, OPT_ST0 )
377+ DEF_ASM_OP2(fucomip, 0xdfe8, 0, OPC_REG, OPT_ST, OPT_ST0 )
378+ DEF_ASM_OP2(fcomip, 0xdff0, 0, OPC_REG, OPT_ST, OPT_ST0 )
379+
380+ /* mmx */
381+ DEF_ASM_OP0(emms, 0x0f77) /* must be last OP0 */
382+ DEF_ASM_OP2(movd, 0x0f6e, 0, OPC_MODRM, OPT_EA | OPT_REG32, OPT_MMX )
383+ALT(DEF_ASM_OP2(movd, 0x0f7e, 0, OPC_MODRM, OPT_MMX, OPT_EA | OPT_REG32 ))
384+ DEF_ASM_OP2(movq, 0x0f6f, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
385+ALT(DEF_ASM_OP2(movq, 0x0f7f, 0, OPC_MODRM, OPT_MMX, OPT_EA | OPT_MMX ))
386+ DEF_ASM_OP2(packssdw, 0x0f6b, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
387+ DEF_ASM_OP2(packsswb, 0x0f63, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
388+ DEF_ASM_OP2(packuswb, 0x0f67, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
389+ DEF_ASM_OP2(paddb, 0x0ffc, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
390+ DEF_ASM_OP2(paddw, 0x0ffd, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
391+ DEF_ASM_OP2(paddd, 0x0ffe, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
392+ DEF_ASM_OP2(paddsb, 0x0fec, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
393+ DEF_ASM_OP2(paddsw, 0x0fed, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
394+ DEF_ASM_OP2(paddusb, 0x0fdc, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
395+ DEF_ASM_OP2(paddusw, 0x0fdd, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
396+ DEF_ASM_OP2(pand, 0x0fdb, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
397+ DEF_ASM_OP2(pandn, 0x0fdf, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
398+ DEF_ASM_OP2(pcmpeqb, 0x0f74, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
399+ DEF_ASM_OP2(pcmpeqw, 0x0f75, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
400+ DEF_ASM_OP2(pcmpeqd, 0x0f76, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
401+ DEF_ASM_OP2(pcmpgtb, 0x0f64, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
402+ DEF_ASM_OP2(pcmpgtw, 0x0f65, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
403+ DEF_ASM_OP2(pcmpgtd, 0x0f66, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
404+ DEF_ASM_OP2(pmaddwd, 0x0ff5, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
405+ DEF_ASM_OP2(pmulhw, 0x0fe5, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
406+ DEF_ASM_OP2(pmullw, 0x0fd5, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
407+ DEF_ASM_OP2(por, 0x0feb, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
408+ DEF_ASM_OP2(psllw, 0x0ff1, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
409+ALT(DEF_ASM_OP2(psllw, 0x0f71, 6, OPC_MODRM, OPT_IM8, OPT_MMX ))
410+ DEF_ASM_OP2(pslld, 0x0ff2, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
411+ALT(DEF_ASM_OP2(pslld, 0x0f72, 6, OPC_MODRM, OPT_IM8, OPT_MMX ))
412+ DEF_ASM_OP2(psllq, 0x0ff3, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
413+ALT(DEF_ASM_OP2(psllq, 0x0f73, 6, OPC_MODRM, OPT_IM8, OPT_MMX ))
414+ DEF_ASM_OP2(psraw, 0x0fe1, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
415+ALT(DEF_ASM_OP2(psraw, 0x0f71, 4, OPC_MODRM, OPT_IM8, OPT_MMX ))
416+ DEF_ASM_OP2(psrad, 0x0fe2, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
417+ALT(DEF_ASM_OP2(psrad, 0x0f72, 4, OPC_MODRM, OPT_IM8, OPT_MMX ))
418+ DEF_ASM_OP2(psrlw, 0x0fd1, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
419+ALT(DEF_ASM_OP2(psrlw, 0x0f71, 2, OPC_MODRM, OPT_IM8, OPT_MMX ))
420+ DEF_ASM_OP2(psrld, 0x0fd2, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
421+ALT(DEF_ASM_OP2(psrld, 0x0f72, 2, OPC_MODRM, OPT_IM8, OPT_MMX ))
422+ DEF_ASM_OP2(psrlq, 0x0fd3, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
423+ALT(DEF_ASM_OP2(psrlq, 0x0f73, 2, OPC_MODRM, OPT_IM8, OPT_MMX ))
424+ DEF_ASM_OP2(psubb, 0x0ff8, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
425+ DEF_ASM_OP2(psubw, 0x0ff9, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
426+ DEF_ASM_OP2(psubd, 0x0ffa, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
427+ DEF_ASM_OP2(psubsb, 0x0fe8, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
428+ DEF_ASM_OP2(psubsw, 0x0fe9, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
429+ DEF_ASM_OP2(psubusb, 0x0fd8, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
430+ DEF_ASM_OP2(psubusw, 0x0fd9, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
431+ DEF_ASM_OP2(punpckhbw, 0x0f68, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
432+ DEF_ASM_OP2(punpckhwd, 0x0f69, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
433+ DEF_ASM_OP2(punpckhdq, 0x0f6a, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
434+ DEF_ASM_OP2(punpcklbw, 0x0f60, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
435+ DEF_ASM_OP2(punpcklwd, 0x0f61, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
436+ DEF_ASM_OP2(punpckldq, 0x0f62, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
437+ DEF_ASM_OP2(pxor, 0x0fef, 0, OPC_MODRM, OPT_EA | OPT_MMX, OPT_MMX )
438+
439+#undef ALT
440+#undef DEF_ASM_OP0
441+#undef DEF_ASM_OP0L
442+#undef DEF_ASM_OP1
443+#undef DEF_ASM_OP2
444+#undef DEF_ASM_OP3
tccasm.c+744−0
@@ -0,0 +1,744 @@
1+/*
2+ * GAS like assembler for TCC
3+ *
4+ * Copyright (c) 2001, 2002 Fabrice Bellard
5+ *
6+ * This program is free software; you can redistribute it and/or modify
7+ * it under the terms of the GNU General Public License as published by
8+ * the Free Software Foundation; either version 2 of the License, or
9+ * (at your option) any later version.
10+ *
11+ * This program is distributed in the hope that it will be useful,
12+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
13+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14+ * GNU General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU General Public License
17+ * along with this program; if not, write to the Free Software
18+ * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19+ */
20+
21+static int asm_get_local_label_name(TCCState *s1, unsigned int n)
22+{
23+ char buf[64];
24+ TokenSym *ts;
25+
26+ snprintf(buf, sizeof(buf), "L..%u", n);
27+ ts = tok_alloc(buf, strlen(buf));
28+ return ts->tok;
29+}
30+
31+/* We do not use the C expression parser to handle symbols. Maybe the
32+ C expression parser could be tweaked to do so. */
33+
34+static void asm_expr_unary(TCCState *s1, ExprValue *pe)
35+{
36+ Sym *sym;
37+ int op, n, label;
38+ const char *p;
39+
40+ switch(tok) {
41+ case TOK_PPNUM:
42+ p = tokc.cstr->data;
43+ n = strtol(p, (char **)&p, 0);
44+ if (*p == 'b' || *p == 'f') {
45+ /* backward or forward label */
46+ label = asm_get_local_label_name(s1, n);
47+ sym = label_find(label);
48+ if (*p == 'b') {
49+ /* backward : find the last corresponding defined label */
50+ if (sym && sym->r == 0)
51+ sym = sym->prev_tok;
52+ if (!sym)
53+ error("local label '%d' not found backward", n);
54+ } else {
55+ /* forward */
56+ if (!sym || sym->r) {
57+ /* if the last label is defined, then define a new one */
58+ sym = label_push(&s1->asm_labels, label, 0);
59+ sym->type.t = VT_STATIC | VT_VOID;
60+ }
61+ }
62+ pe->v = 0;
63+ pe->sym = sym;
64+ } else if (*p == '\0') {
65+ pe->v = n;
66+ pe->sym = NULL;
67+ } else {
68+ error("invalid number syntax");
69+ }
70+ next();
71+ break;
72+ case '+':
73+ next();
74+ asm_expr_unary(s1, pe);
75+ break;
76+ case '-':
77+ case '~':
78+ op = tok;
79+ next();
80+ asm_expr_unary(s1, pe);
81+ if (pe->sym)
82+ error("invalid operation with label");
83+ if (op == '-')
84+ pe->v = -pe->v;
85+ else
86+ pe->v = ~pe->v;
87+ break;
88+ default:
89+ if (tok >= TOK_IDENT) {
90+ /* label case : if the label was not found, add one */
91+ sym = label_find(tok);
92+ if (!sym) {
93+ sym = label_push(&s1->asm_labels, tok, 0);
94+ /* NOTE: by default, the symbol is global */
95+ sym->type.t = VT_VOID;
96+ }
97+ pe->v = 0;
98+ pe->sym = sym;
99+ next();
100+ } else {
101+ error("bad expression syntax [%s]", get_tok_str(tok, &tokc));
102+ }
103+ break;
104+ }
105+}
106+
107+static void asm_expr_prod(TCCState *s1, ExprValue *pe)
108+{
109+ int op;
110+ ExprValue e2;
111+
112+ asm_expr_unary(s1, pe);
113+ for(;;) {
114+ op = tok;
115+ if (op != '*' && op != '/' && op != '%' &&
116+ op != TOK_SHL && op != TOK_SAR)
117+ break;
118+ next();
119+ asm_expr_unary(s1, &e2);
120+ if (pe->sym || e2.sym)
121+ error("invalid operation with label");
122+ switch(op) {
123+ case '*':
124+ pe->v *= e2.v;
125+ break;
126+ case '/':
127+ if (e2.v == 0) {
128+ div_error:
129+ error("division by zero");
130+ }
131+ pe->v /= e2.v;
132+ break;
133+ case '%':
134+ if (e2.v == 0)
135+ goto div_error;
136+ pe->v %= e2.v;
137+ break;
138+ case TOK_SHL:
139+ pe->v <<= e2.v;
140+ break;
141+ default:
142+ case TOK_SAR:
143+ pe->v >>= e2.v;
144+ break;
145+ }
146+ }
147+}
148+
149+static void asm_expr_logic(TCCState *s1, ExprValue *pe)
150+{
151+ int op;
152+ ExprValue e2;
153+
154+ asm_expr_prod(s1, pe);
155+ for(;;) {
156+ op = tok;
157+ if (op != '&' && op != '|' && op != '^')
158+ break;
159+ next();
160+ asm_expr_prod(s1, &e2);
161+ if (pe->sym || e2.sym)
162+ error("invalid operation with label");
163+ switch(op) {
164+ case '&':
165+ pe->v &= e2.v;
166+ break;
167+ case '|':
168+ pe->v |= e2.v;
169+ break;
170+ default:
171+ case '^':
172+ pe->v ^= e2.v;
173+ break;
174+ }
175+ }
176+}
177+
178+static inline void asm_expr_sum(TCCState *s1, ExprValue *pe)
179+{
180+ int op;
181+ ExprValue e2;
182+
183+ asm_expr_logic(s1, pe);
184+ for(;;) {
185+ op = tok;
186+ if (op != '+' && op != '-')
187+ break;
188+ next();
189+ asm_expr_logic(s1, &e2);
190+ if (op == '+') {
191+ if (pe->sym != NULL && e2.sym != NULL)
192+ goto cannot_relocate;
193+ pe->v += e2.v;
194+ if (pe->sym == NULL && e2.sym != NULL)
195+ pe->sym = e2.sym;
196+ } else {
197+ pe->v -= e2.v;
198+ /* NOTE: we are less powerful than gas in that case
199+ because we store only one symbol in the expression */
200+ if (!pe->sym && !e2.sym) {
201+ /* OK */
202+ } else if (pe->sym && !e2.sym) {
203+ /* OK */
204+ } else if (pe->sym && e2.sym) {
205+ if (pe->sym == e2.sym) {
206+ /* OK */
207+ } else if (pe->sym->r == e2.sym->r && pe->sym->r != 0) {
208+ /* we also accept defined symbols in the same section */
209+ pe->v += (long)pe->sym->next - (long)e2.sym->next;
210+ } else {
211+ goto cannot_relocate;
212+ }
213+ pe->sym = NULL; /* same symbols can be substracted to NULL */
214+ } else {
215+ cannot_relocate:
216+ error("invalid operation with label");
217+ }
218+ }
219+ }
220+}
221+
222+static void asm_expr(TCCState *s1, ExprValue *pe)
223+{
224+ asm_expr_sum(s1, pe);
225+}
226+
227+static int asm_int_expr(TCCState *s1)
228+{
229+ ExprValue e;
230+ asm_expr(s1, &e);
231+ if (e.sym)
232+ expect("constant");
233+ return e.v;
234+}
235+
236+/* NOTE: the same name space as C labels is used to avoid using too
237+ much memory when storing labels in TokenStrings */
238+static void asm_new_label(TCCState *s1, int label, int is_local)
239+{
240+ Sym *sym;
241+
242+ sym = label_find(label);
243+ if (sym) {
244+ if (sym->r) {
245+ /* the label is already defined */
246+ if (!is_local) {
247+ error("assembler label '%s' already defined",
248+ get_tok_str(label, NULL));
249+ } else {
250+ /* redefinition of local labels is possible */
251+ goto new_label;
252+ }
253+ }
254+ } else {
255+ new_label:
256+ sym = label_push(&s1->asm_labels, label, 0);
257+ sym->type.t = VT_STATIC | VT_VOID;
258+ }
259+ sym->r = cur_text_section->sh_num;
260+ sym->next = (void *)ind;
261+}
262+
263+static void asm_free_labels(TCCState *st)
264+{
265+ Sym *s, *s1;
266+ for(s = st->asm_labels; s != NULL; s = s1) {
267+ s1 = s->prev;
268+ /* define symbol value in object file */
269+ if (s->r) {
270+ put_extern_sym(s, st->sections[s->r], (long)s->next, 0);
271+ }
272+ /* remove label */
273+ table_ident[s->v - TOK_IDENT]->sym_label = NULL;
274+ tcc_free(s);
275+ }
276+ st->asm_labels = NULL;
277+}
278+
279+static void asm_parse_directive(TCCState *s1)
280+{
281+ int n, offset, v, size, tok1;
282+ Section *sec;
283+ uint8_t *ptr;
284+
285+ /* assembler directive */
286+ next();
287+ sec = cur_text_section;
288+ switch(tok) {
289+ case TOK_ASM_align:
290+ case TOK_ASM_skip:
291+ case TOK_ASM_space:
292+ tok1 = tok;
293+ next();
294+ n = asm_int_expr(s1);
295+ if (tok1 == TOK_ASM_align) {
296+ if (n < 0 || (n & (n-1)) != 0)
297+ error("alignment must be a positive power of two");
298+ offset = (ind + n - 1) & -n;
299+ size = offset - ind;
300+ } else {
301+ size = n;
302+ }
303+ v = 0;
304+ if (tok == ',') {
305+ next();
306+ v = asm_int_expr(s1);
307+ }
308+ if (sec->sh_type != SHT_NOBITS) {
309+ sec->data_offset = ind;
310+ ptr = section_ptr_add(sec, size);
311+ memset(ptr, v, size);
312+ }
313+ ind += size;
314+ break;
315+ case TOK_ASM_byte:
316+ size = 1;
317+ goto asm_data;
318+ case TOK_ASM_word:
319+ case TOK_SHORT:
320+ size = 2;
321+ goto asm_data;
322+ case TOK_LONG:
323+ case TOK_INT:
324+ size = 4;
325+ asm_data:
326+ next();
327+ for(;;) {
328+ ExprValue e;
329+ asm_expr(s1, &e);
330+ if (sec->sh_type != SHT_NOBITS) {
331+ if (size == 4) {
332+ gen_expr32(&e);
333+ } else {
334+ if (e.sym)
335+ expect("constant");
336+ if (size == 1)
337+ g(e.v);
338+ else
339+ gen_le16(e.v);
340+ }
341+ } else {
342+ ind += size;
343+ }
344+ if (tok != ',')
345+ break;
346+ next();
347+ }
348+ break;
349+ default:
350+ error("unknown assembler directive .%s", get_tok_str(tok, NULL));
351+ break;
352+ }
353+}
354+
355+
356+/* assemble a file */
357+static int tcc_assemble_internal(TCCState *s1, int do_preprocess)
358+{
359+ int opcode;
360+
361+#if 0
362+ /* print stats about opcodes */
363+ {
364+ const ASMInstr *pa;
365+ int freq[4];
366+ int op_vals[500];
367+ int nb_op_vals, i, j;
368+
369+ nb_op_vals = 0;
370+ memset(freq, 0, sizeof(freq));
371+ for(pa = asm_instrs; pa->sym != 0; pa++) {
372+ freq[pa->nb_ops]++;
373+ for(i=0;i<pa->nb_ops;i++) {
374+ for(j=0;j<nb_op_vals;j++) {
375+ if (pa->op_type[i] == op_vals[j])
376+ goto found;
377+ }
378+ op_vals[nb_op_vals++] = pa->op_type[i];
379+ found: ;
380+ }
381+ }
382+ for(i=0;i<nb_op_vals;i++) {
383+ int v = op_vals[i];
384+ if ((v & (v - 1)) != 0)
385+ printf("%3d: %08x\n", i, v);
386+ }
387+ printf("size=%d nb=%d f0=%d f1=%d f2=%d f3=%d\n",
388+ sizeof(asm_instrs), sizeof(asm_instrs) / sizeof(ASMInstr),
389+ freq[0], freq[1], freq[2], freq[3]);
390+ }
391+#endif
392+
393+ /* XXX: undefine C labels */
394+
395+ ch = file->buf_ptr[0];
396+ tok_flags = TOK_FLAG_BOL | TOK_FLAG_BOF;
397+ parse_flags = 0;
398+ if (do_preprocess)
399+ parse_flags |= PARSE_FLAG_PREPROCESS;
400+ next();
401+ for(;;) {
402+ if (tok == TOK_EOF)
403+ break;
404+ parse_flags |= PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
405+ redo:
406+ if (tok == '#') {
407+ /* horrible gas comment */
408+ while (tok != TOK_LINEFEED)
409+ next();
410+ } else if (tok == '.') {
411+ asm_parse_directive(s1);
412+ } else if (tok == TOK_PPNUM) {
413+ const char *p;
414+ int n;
415+ p = tokc.cstr->data;
416+ n = strtol(p, (char **)&p, 10);
417+ if (*p != '\0')
418+ expect("':'");
419+ /* new local label */
420+ asm_new_label(s1, asm_get_local_label_name(s1, n), 1);
421+ next();
422+ skip(':');
423+ goto redo;
424+ } else if (tok >= TOK_IDENT) {
425+ /* instruction or label */
426+ opcode = tok;
427+ next();
428+ if (tok == ':') {
429+ /* new label */
430+ asm_new_label(s1, opcode, 0);
431+ next();
432+ goto redo;
433+ } else {
434+ asm_opcode(s1, opcode);
435+ }
436+ }
437+ /* end of line */
438+ if (tok != ';' && tok != TOK_LINEFEED){
439+ expect("end of line");
440+ }
441+ parse_flags &= ~PARSE_FLAG_LINEFEED; /* XXX: suppress that hack */
442+ next();
443+ }
444+
445+ asm_free_labels(s1);
446+
447+ return 0;
448+}
449+
450+/* Assemble the current file */
451+static int tcc_assemble(TCCState *s1, int do_preprocess)
452+{
453+ int ret;
454+
455+ preprocess_init(s1);
456+
457+ /* default section is text */
458+ cur_text_section = text_section;
459+ ind = cur_text_section->data_offset;
460+
461+ ret = tcc_assemble_internal(s1, do_preprocess);
462+
463+ cur_text_section->data_offset = ind;
464+ return ret;
465+}
466+
467+/********************************************************************/
468+/* GCC inline asm support */
469+
470+/* assemble the string 'str' in the current C compilation unit without
471+ C preprocessing. NOTE: str is modified by modifying the '\0' at the
472+ end */
473+static void tcc_assemble_inline(TCCState *s1, char *str, int len)
474+{
475+ BufferedFile *bf, *saved_file;
476+ int saved_parse_flags, *saved_macro_ptr;
477+
478+ bf = tcc_malloc(sizeof(BufferedFile));
479+ memset(bf, 0, sizeof(BufferedFile));
480+ bf->fd = -1;
481+ bf->buf_ptr = str;
482+ bf->buf_end = str + len;
483+ str[len] = CH_EOB;
484+ /* same name as current file so that errors are correctly
485+ reported */
486+ pstrcpy(bf->filename, sizeof(bf->filename), file->filename);
487+ bf->line_num = file->line_num;
488+ saved_file = file;
489+ file = bf;
490+ saved_parse_flags = parse_flags;
491+ saved_macro_ptr = macro_ptr;
492+ macro_ptr = NULL;
493+
494+ tcc_assemble_internal(s1, 0);
495+
496+ parse_flags = saved_parse_flags;
497+ macro_ptr = saved_macro_ptr;
498+ file = saved_file;
499+ tcc_free(bf);
500+}
501+
502+/* find a constraint by its number or id (gcc 3 extended
503+ syntax). return -1 if not found. Return in *pp in char after the
504+ constraint */
505+static int find_constraint(ASMOperand *operands, int nb_operands,
506+ const char *name, const char **pp)
507+{
508+ int index;
509+ TokenSym *ts;
510+ const char *p;
511+
512+ if (isnum(*name)) {
513+ index = 0;
514+ while (isnum(*name)) {
515+ index = (index * 10) + (*name) - '0';
516+ name++;
517+ }
518+ if ((unsigned)index >= nb_operands)
519+ index = -1;
520+ } else if (*name == '[') {
521+ name++;
522+ p = strchr(name, ']');
523+ if (p) {
524+ ts = tok_alloc(name, p - name);
525+ for(index = 0; index < nb_operands; index++) {
526+ if (operands[index].id == ts->tok)
527+ goto found;
528+ }
529+ index = -1;
530+ found:
531+ name = p + 1;
532+ } else {
533+ index = -1;
534+ }
535+ } else {
536+ index = -1;
537+ }
538+ if (pp)
539+ *pp = name;
540+ return index;
541+}
542+
543+static void subst_asm_operands(ASMOperand *operands, int nb_operands,
544+ int nb_outputs,
545+ CString *out_str, CString *in_str)
546+{
547+ int c, index, modifier;
548+ const char *str;
549+ ASMOperand *op;
550+ SValue sv;
551+
552+ cstr_new(out_str);
553+ str = in_str->data;
554+ for(;;) {
555+ c = *str++;
556+ if (c == '%') {
557+ if (*str == '%') {
558+ str++;
559+ goto add_char;
560+ }
561+ modifier = 0;
562+ if (*str == 'c' || *str == 'n' ||
563+ *str == 'b' || *str == 'w' || *str == 'h')
564+ modifier = *str++;
565+ index = find_constraint(operands, nb_operands, str, &str);
566+ if (index < 0)
567+ error("invalid operand reference after %%");
568+ op = &operands[index];
569+ sv = *op->vt;
570+ if (op->reg >= 0) {
571+ sv.r = op->reg;
572+ if ((op->vt->r & VT_VALMASK) == VT_LLOCAL)
573+ sv.r |= VT_LVAL;
574+ }
575+ subst_asm_operand(out_str, &sv, modifier);
576+ } else {
577+ add_char:
578+ cstr_ccat(out_str, c);
579+ if (c == '\0')
580+ break;
581+ }
582+ }
583+}
584+
585+
586+static void parse_asm_operands(ASMOperand *operands, int *nb_operands_ptr,
587+ int is_output)
588+{
589+ ASMOperand *op;
590+ int nb_operands;
591+
592+ if (tok != ':') {
593+ nb_operands = *nb_operands_ptr;
594+ for(;;) {
595+ if (nb_operands >= MAX_ASM_OPERANDS)
596+ error("too many asm operands");
597+ op = &operands[nb_operands++];
598+ op->id = 0;
599+ if (tok == '[') {
600+ next();
601+ if (tok < TOK_IDENT)
602+ expect("identifier");
603+ op->id = tok;
604+ next();
605+ skip(']');
606+ }
607+ if (tok != TOK_STR)
608+ expect("string constant");
609+ op->constraint = tcc_malloc(tokc.cstr->size);
610+ strcpy(op->constraint, tokc.cstr->data);
611+ next();
612+ skip('(');
613+ gexpr();
614+ if (is_output) {
615+ test_lvalue();
616+ } else {
617+ /* we want to avoid LLOCAL case. note that it may come
618+ from register storage, so we need to convert (reg)
619+ case */
620+ if ((vtop->r & VT_LVAL) &&
621+ ((vtop->r & VT_VALMASK) == VT_LLOCAL ||
622+ (vtop->r & VT_VALMASK) < VT_CONST)) {
623+ gv(RC_INT);
624+ }
625+ }
626+ op->vt = vtop;
627+ skip(')');
628+ if (tok == ',') {
629+ next();
630+ } else {
631+ break;
632+ }
633+ }
634+ *nb_operands_ptr = nb_operands;
635+ }
636+}
637+
638+/* parse the GCC asm() instruction */
639+static void asm_instr(void)
640+{
641+ CString astr, astr1;
642+ ASMOperand operands[MAX_ASM_OPERANDS];
643+ int nb_inputs, nb_outputs, nb_operands, i;
644+ uint8_t input_regs_allocated[NB_ASM_REGS];
645+ uint8_t output_regs_allocated[NB_ASM_REGS];
646+ uint8_t clobber_regs[NB_ASM_REGS];
647+
648+ next();
649+ /* since we always generate the asm() instruction, we can ignore
650+ volatile */
651+ if (tok == TOK_VOLATILE1 || tok == TOK_VOLATILE2 || tok == TOK_VOLATILE3) {
652+ next();
653+ }
654+ skip('(');
655+ /* read the string */
656+ if (tok != TOK_STR)
657+ expect("string constant");
658+ cstr_new(&astr);
659+ while (tok == TOK_STR) {
660+ /* XXX: add \0 handling too ? */
661+ cstr_cat(&astr, tokc.cstr->data);
662+ next();
663+ }
664+ cstr_ccat(&astr, '\0');
665+ nb_operands = 0;
666+ nb_outputs = 0;
667+ memset(clobber_regs, 0, sizeof(clobber_regs));
668+ if (tok == ':') {
669+ next();
670+ /* output args */
671+ parse_asm_operands(operands, &nb_operands, 1);
672+ nb_outputs = nb_operands;
673+ if (tok == ':') {
674+ next();
675+ /* input args */
676+ parse_asm_operands(operands, &nb_operands, 0);
677+ if (tok == ':') {
678+ /* clobber list */
679+ /* XXX: handle registers */
680+ next();
681+ for(;;) {
682+ if (tok != TOK_STR)
683+ expect("string constant");
684+ asm_clobber(clobber_regs, tokc.cstr->data);
685+ next();
686+ if (tok == ',') {
687+ next();
688+ } else {
689+ break;
690+ }
691+ }
692+ }
693+ }
694+ }
695+ skip(')');
696+ /* NOTE: we do not eat the ';' so that we can restore the current
697+ token after the assembler parsing */
698+ if (tok != ';')
699+ expect("';'");
700+ nb_inputs = nb_operands - nb_outputs;
701+
702+ /* save all values in the memory */
703+ save_regs(0);
704+
705+ /* compute constraints */
706+ asm_compute_constraints(input_regs_allocated,
707+ operands, nb_operands, nb_outputs, 0,
708+ NULL);
709+ asm_compute_constraints(output_regs_allocated,
710+ operands, nb_operands, nb_outputs, 1,
711+ input_regs_allocated);
712+
713+ /* substitute the operands in the asm string */
714+#ifdef ASM_DEBUG
715+ printf("asm: \"%s\"\n", (char *)astr.data);
716+#endif
717+ subst_asm_operands(operands, nb_operands, nb_outputs, &astr1, &astr);
718+ cstr_free(&astr);
719+#ifdef ASM_DEBUG
720+ printf("subst_asm: \"%s\"\n", (char *)astr1.data);
721+#endif
722+
723+ /* generate loads */
724+ asm_gen_code(operands, nb_operands, nb_outputs, 0, clobber_regs);
725+
726+ /* assemble the string with tcc internal assembler */
727+ tcc_assemble_inline(tcc_state, astr1.data, astr1.size - 1);
728+
729+ /* restore the current C token */
730+ next();
731+
732+ /* store the output values if needed */
733+ asm_gen_code(operands, nb_operands, nb_outputs, 1, clobber_regs);
734+
735+ /* free everything */
736+ for(i=0;i<nb_operands;i++) {
737+ ASMOperand *op;
738+ op = &operands[i];
739+ tcc_free(op->constraint);
740+ vpop();
741+ }
742+ cstr_free(&astr1);
743+}
744+

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t