Remove some vestiges of "native" RESW/RESD support
[nasm.git] / assemble.c
blob5670a5cf80a1b891beb4b48b21046167132bab83
1 /* assemble.c code generation for the Netwide Assembler
3 * The Netwide Assembler is copyright (C) 1996 Simon Tatham and
4 * Julian Hall. All rights reserved. The software is
5 * redistributable under the licence given in the file "Licence"
6 * distributed in the NASM archive.
8 * the actual codes (C syntax, i.e. octal):
9 * \0 - terminates the code. (Unless it's a literal of course.)
10 * \1, \2, \3 - that many literal bytes follow in the code stream
11 * \4, \6 - the POP/PUSH (respectively) codes for CS, DS, ES, SS
12 * (POP is never used for CS) depending on operand 0
13 * \5, \7 - the second byte of POP/PUSH codes for FS, GS, depending
14 * on operand 0
15 * \10..\13 - a literal byte follows in the code stream, to be added
16 * to the register value of operand 0..3
17 * \14..\17 - a signed byte immediate operand, from operand 0..3
18 * \20..\23 - a byte immediate operand, from operand 0..3
19 * \24..\27 - an unsigned byte immediate operand, from operand 0..3
20 * \30..\33 - a word immediate operand, from operand 0..3
21 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
22 * assembly mode or the operand-size override on the operand
23 * \40..\43 - a long immediate operand, from operand 0..3
24 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
25 * depending on the address size of the instruction.
26 * \50..\53 - a byte relative operand, from operand 0..3
27 * \54..\57 - a qword immediate operand, from operand 0..3
28 * \60..\63 - a word relative operand, from operand 0..3
29 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
30 * assembly mode or the operand-size override on the operand
31 * \70..\73 - a long relative operand, from operand 0..3
32 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
33 * \1ab - a ModRM, calculated on EA in operand a, with the spare
34 * field the register value of operand b.
35 * \140..\143 - an immediate word or signed byte for operand 0..3
36 * \144..\147 - or 2 (s-field) into next opcode byte if operand 0..3
37 * is a signed byte rather than a word.
38 * \150..\153 - an immediate dword or signed byte for operand 0..3
39 * \154..\157 - or 2 (s-field) into next opcode byte if operand 0..3
40 * is a signed byte rather than a dword.
41 * \160..\163 - this instruction uses DREX rather than REX, with the
42 * OC0 field set to 0, and the dest field taken from
43 * operand 0..3.
44 * \164..\167 - this instruction uses DREX rather than REX, with the
45 * OC0 field set to 1, and the dest field taken from
46 * operand 0..3.
47 * \170 - encodes the literal byte 0. (Some compilers don't take
48 * kindly to a zero byte in the _middle_ of a compile time
49 * string constant, so I had to put this hack in.)
50 * \171 - placement of DREX suffix in the absence of an EA
51 * \2ab - a ModRM, calculated on EA in operand a, with the spare
52 * field equal to digit b.
53 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
54 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
55 * \312 - (disassembler only) marker on LOOP, LOOPxx instructions.
56 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
57 * \314 - (disassembler only) invalid with REX.B
58 * \315 - (disassembler only) invalid with REX.X
59 * \316 - (disassembler only) invalid with REX.R
60 * \317 - (disassembler only) invalid with REX.W
61 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
62 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
63 * \322 - indicates that this instruction is only valid when the
64 * operand size is the default (instruction to disassembler,
65 * generates no code in the assembler)
66 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
67 * \324 - indicates 64-bit operand size requiring REX prefix.
68 * \330 - a literal byte follows in the code stream, to be added
69 * to the condition code value of the instruction.
70 * \331 - instruction not valid with REP prefix. Hint for
71 * disassembler only; for SSE instructions.
72 * \332 - REP prefix (0xF2 byte) used as opcode extension.
73 * \333 - REP prefix (0xF3 byte) used as opcode extension.
74 * \334 - LOCK prefix used instead of REX.R
75 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
76 * \340 - reserve <operand 0> bytes of uninitialized storage.
77 * Operand 0 had better be a segmentless constant.
78 * \364 - operand-size prefix (0x66) not permitted
79 * \365 - address-size prefix (0x67) not permitted
80 * \366 - operand-size prefix (0x66) used as opcode extension
81 * \367 - address-size prefix (0x67) used as opcode extension
82 * \370,\371,\372 - match only if operand 0 meets byte jump criteria.
83 * 370 is used for Jcc, 371 is used for JMP.
84 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
85 * used for conditional jump over longer jump
88 #include "compiler.h"
90 #include <stdio.h>
91 #include <string.h>
92 #include <inttypes.h>
94 #include "nasm.h"
95 #include "nasmlib.h"
96 #include "assemble.h"
97 #include "insns.h"
98 #include "preproc.h"
99 #include "regflags.c"
100 #include "regvals.c"
102 typedef struct {
103 int sib_present; /* is a SIB byte necessary? */
104 int bytes; /* # of bytes of offset needed */
105 int size; /* lazy - this is sib+bytes+1 */
106 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
107 } ea;
109 static uint32_t cpu; /* cpu level received from nasm.c */
110 static efunc errfunc;
111 static struct ofmt *outfmt;
112 static ListGen *list;
114 static int64_t calcsize(int32_t, int64_t, int, insn *, const char *);
115 static void gencode(int32_t, int64_t, int, insn *, const char *, int64_t);
116 static int matches(const struct itemplate *, insn *, int bits);
117 static int32_t regflag(const operand *);
118 static int32_t regval(const operand *);
119 static int rexflags(int, int32_t, int);
120 static int op_rexflags(const operand *, int);
121 static ea *process_ea(operand *, ea *, int, int, int, int32_t, int);
122 static void add_asp(insn *, int);
124 static int has_prefix(insn * ins, enum prefix_pos pos, enum prefixes prefix)
126 return ins->prefixes[pos] == prefix;
129 static void assert_no_prefix(insn * ins, enum prefix_pos pos)
131 if (ins->prefixes[pos])
132 errfunc(ERR_NONFATAL, "invalid %s prefix",
133 prefix_name(ins->prefixes[pos]));
136 static const char *size_name(int size)
138 switch (size) {
139 case 1:
140 return "byte";
141 case 2:
142 return "word";
143 case 4:
144 return "dword";
145 case 8:
146 return "qword";
147 case 10:
148 return "tword";
149 case 16:
150 return "oword";
151 default:
152 return "???";
156 static void warn_overflow(int size, int64_t data)
158 if (size < 8) {
159 int64_t lim = ((int64_t)1 << (size*8))-1;
161 if (data < ~lim || data > lim)
162 errfunc(ERR_WARNING, "%s data exceeds bounds", size_name(size));
166 * This routine wrappers the real output format's output routine,
167 * in order to pass a copy of the data off to the listing file
168 * generator at the same time.
170 static void out(int64_t offset, int32_t segto, const void *data,
171 enum out_type type, uint64_t size,
172 int32_t segment, int32_t wrt)
174 static int32_t lineno = 0; /* static!!! */
175 static char *lnfname = NULL;
176 uint8_t p[8];
178 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
180 * This is a non-relocated address, and we're going to
181 * convert it into RAWDATA format.
183 uint8_t *q = p;
185 if (size > 8) {
186 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
187 return;
190 WRITEADDR(q, *(int64_t *)data, size);
191 data = p;
192 type = OUT_RAWDATA;
195 list->output(offset, data, type, size);
198 * this call to src_get determines when we call the
199 * debug-format-specific "linenum" function
200 * it updates lineno and lnfname to the current values
201 * returning 0 if "same as last time", -2 if lnfname
202 * changed, and the amount by which lineno changed,
203 * if it did. thus, these variables must be static
206 if (src_get(&lineno, &lnfname)) {
207 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
210 outfmt->output(segto, data, type, size, segment, wrt);
213 static int jmp_match(int32_t segment, int64_t offset, int bits,
214 insn * ins, const char *code)
216 int64_t isize;
217 uint8_t c = code[0];
219 if (c != 0370 && c != 0371)
220 return 0;
221 if (ins->oprs[0].opflags & OPFLAG_FORWARD) {
222 if ((optimizing < 0 || (ins->oprs[0].type & STRICT))
223 && c == 0370)
224 return 1;
225 else
226 return (pass0 == 0); /* match a forward reference */
228 isize = calcsize(segment, offset, bits, ins, code);
229 if (ins->oprs[0].segment != segment)
230 return 0;
231 isize = ins->oprs[0].offset - offset - isize; /* isize is now the delta */
232 if (isize >= -128L && isize <= 127L)
233 return 1; /* it is byte size */
235 return 0;
238 int64_t assemble(int32_t segment, int64_t offset, int bits, uint32_t cp,
239 insn * instruction, struct ofmt *output, efunc error,
240 ListGen * listgen)
242 const struct itemplate *temp;
243 int j;
244 int size_prob;
245 int64_t insn_end;
246 int32_t itimes;
247 int64_t start = offset;
248 int64_t wsize = 0; /* size for DB etc. */
250 errfunc = error; /* to pass to other functions */
251 cpu = cp;
252 outfmt = output; /* likewise */
253 list = listgen; /* and again */
255 switch (instruction->opcode) {
256 case -1:
257 return 0;
258 case I_DB:
259 wsize = 1;
260 break;
261 case I_DW:
262 wsize = 2;
263 break;
264 case I_DD:
265 wsize = 4;
266 break;
267 case I_DQ:
268 wsize = 8;
269 break;
270 case I_DT:
271 wsize = 10;
272 break;
273 case I_DO:
274 wsize = 16;
275 break;
276 default:
277 break;
280 if (wsize) {
281 extop *e;
282 int32_t t = instruction->times;
283 if (t < 0)
284 errfunc(ERR_PANIC,
285 "instruction->times < 0 (%ld) in assemble()", t);
287 while (t--) { /* repeat TIMES times */
288 for (e = instruction->eops; e; e = e->next) {
289 if (e->type == EOT_DB_NUMBER) {
290 if (wsize == 1) {
291 if (e->segment != NO_SEG)
292 errfunc(ERR_NONFATAL,
293 "one-byte relocation attempted");
294 else {
295 uint8_t out_byte = e->offset;
296 out(offset, segment, &out_byte,
297 OUT_RAWDATA, 1, NO_SEG, NO_SEG);
299 } else if (wsize > 8) {
300 errfunc(ERR_NONFATAL, "integer supplied to a DT or DO"
301 " instruction");
302 } else
303 out(offset, segment, &e->offset,
304 OUT_ADDRESS, wsize, e->segment, e->wrt);
305 offset += wsize;
306 } else if (e->type == EOT_DB_STRING) {
307 int align;
309 out(offset, segment, e->stringval,
310 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
311 align = e->stringlen % wsize;
313 if (align) {
314 align = wsize - align;
315 out(offset, segment, "\0\0\0\0\0\0\0\0",
316 OUT_RAWDATA, align, NO_SEG, NO_SEG);
318 offset += e->stringlen + align;
321 if (t > 0 && t == instruction->times - 1) {
323 * Dummy call to list->output to give the offset to the
324 * listing module.
326 list->output(offset, NULL, OUT_RAWDATA, 0);
327 list->uplevel(LIST_TIMES);
330 if (instruction->times > 1)
331 list->downlevel(LIST_TIMES);
332 return offset - start;
335 if (instruction->opcode == I_INCBIN) {
336 static char fname[FILENAME_MAX];
337 FILE *fp;
338 int32_t len;
339 char *prefix = "", *combine;
340 char **pPrevPath = NULL;
342 len = FILENAME_MAX - 1;
343 if (len > instruction->eops->stringlen)
344 len = instruction->eops->stringlen;
345 strncpy(fname, instruction->eops->stringval, len);
346 fname[len] = '\0';
348 while (1) { /* added by alexfru: 'incbin' uses include paths */
349 combine = nasm_malloc(strlen(prefix) + len + 1);
350 strcpy(combine, prefix);
351 strcat(combine, fname);
353 if ((fp = fopen(combine, "rb")) != NULL) {
354 nasm_free(combine);
355 break;
358 nasm_free(combine);
359 pPrevPath = pp_get_include_path_ptr(pPrevPath);
360 if (pPrevPath == NULL)
361 break;
362 prefix = *pPrevPath;
365 if (fp == NULL)
366 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
367 fname);
368 else if (fseek(fp, 0L, SEEK_END) < 0)
369 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
370 fname);
371 else {
372 static char buf[2048];
373 int32_t t = instruction->times;
374 int32_t base = 0;
376 len = ftell(fp);
377 if (instruction->eops->next) {
378 base = instruction->eops->next->offset;
379 len -= base;
380 if (instruction->eops->next->next &&
381 len > instruction->eops->next->next->offset)
382 len = instruction->eops->next->next->offset;
385 * Dummy call to list->output to give the offset to the
386 * listing module.
388 list->output(offset, NULL, OUT_RAWDATA, 0);
389 list->uplevel(LIST_INCBIN);
390 while (t--) {
391 int32_t l;
393 fseek(fp, base, SEEK_SET);
394 l = len;
395 while (l > 0) {
396 int32_t m =
397 fread(buf, 1, (l > (int32_t) sizeof(buf) ? (int32_t) sizeof(buf) : l),
398 fp);
399 if (!m) {
401 * This shouldn't happen unless the file
402 * actually changes while we are reading
403 * it.
405 error(ERR_NONFATAL,
406 "`incbin': unexpected EOF while"
407 " reading file `%s'", fname);
408 t = 0; /* Try to exit cleanly */
409 break;
411 out(offset, segment, buf, OUT_RAWDATA, m,
412 NO_SEG, NO_SEG);
413 l -= m;
416 list->downlevel(LIST_INCBIN);
417 if (instruction->times > 1) {
419 * Dummy call to list->output to give the offset to the
420 * listing module.
422 list->output(offset, NULL, OUT_RAWDATA, 0);
423 list->uplevel(LIST_TIMES);
424 list->downlevel(LIST_TIMES);
426 fclose(fp);
427 return instruction->times * len;
429 return 0; /* if we're here, there's an error */
432 /* Check to see if we need an address-size prefix */
433 add_asp(instruction, bits);
435 size_prob = false;
437 for (temp = nasm_instructions[instruction->opcode]; temp->opcode != -1; temp++){
438 int m = matches(temp, instruction, bits);
440 if (m == 99)
441 m += jmp_match(segment, offset, bits, instruction, temp->code);
443 if (m == 100) { /* matches! */
444 const char *codes = temp->code;
445 int64_t insn_size = calcsize(segment, offset, bits,
446 instruction, codes);
447 itimes = instruction->times;
448 if (insn_size < 0) /* shouldn't be, on pass two */
449 error(ERR_PANIC, "errors made it through from pass one");
450 else
451 while (itimes--) {
452 for (j = 0; j < MAXPREFIX; j++) {
453 uint8_t c = 0;
454 switch (instruction->prefixes[j]) {
455 case P_LOCK:
456 c = 0xF0;
457 break;
458 case P_REPNE:
459 case P_REPNZ:
460 c = 0xF2;
461 break;
462 case P_REPE:
463 case P_REPZ:
464 case P_REP:
465 c = 0xF3;
466 break;
467 case R_CS:
468 if (bits == 64) {
469 error(ERR_WARNING,
470 "cs segment base generated, but will be ignored in 64-bit mode");
472 c = 0x2E;
473 break;
474 case R_DS:
475 if (bits == 64) {
476 error(ERR_WARNING,
477 "ds segment base generated, but will be ignored in 64-bit mode");
479 c = 0x3E;
480 break;
481 case R_ES:
482 if (bits == 64) {
483 error(ERR_WARNING,
484 "es segment base generated, but will be ignored in 64-bit mode");
486 c = 0x26;
487 break;
488 case R_FS:
489 c = 0x64;
490 break;
491 case R_GS:
492 c = 0x65;
493 break;
494 case R_SS:
495 if (bits == 64) {
496 error(ERR_WARNING,
497 "ss segment base generated, but will be ignored in 64-bit mode");
499 c = 0x36;
500 break;
501 case R_SEGR6:
502 case R_SEGR7:
503 error(ERR_NONFATAL,
504 "segr6 and segr7 cannot be used as prefixes");
505 break;
506 case P_A16:
507 if (bits == 64) {
508 error(ERR_NONFATAL,
509 "16-bit addressing is not supported "
510 "in 64-bit mode");
511 } else if (bits != 16)
512 c = 0x67;
513 break;
514 case P_A32:
515 if (bits != 32)
516 c = 0x67;
517 break;
518 case P_A64:
519 if (bits != 64) {
520 error(ERR_NONFATAL,
521 "64-bit addressing is only supported "
522 "in 64-bit mode");
524 break;
525 case P_ASP:
526 c = 0x67;
527 break;
528 case P_O16:
529 if (bits != 16)
530 c = 0x66;
531 break;
532 case P_O32:
533 if (bits == 16)
534 c = 0x66;
535 break;
536 case P_O64:
537 /* REX.W */
538 break;
539 case P_OSP:
540 c = 0x66;
541 break;
542 case P_none:
543 break;
544 default:
545 error(ERR_PANIC, "invalid instruction prefix");
547 if (c != 0) {
548 out(offset, segment, &c, OUT_RAWDATA, 1,
549 NO_SEG, NO_SEG);
550 offset++;
553 insn_end = offset + insn_size;
554 gencode(segment, offset, bits, instruction, codes,
555 insn_end);
556 offset += insn_size;
557 if (itimes > 0 && itimes == instruction->times - 1) {
559 * Dummy call to list->output to give the offset to the
560 * listing module.
562 list->output(offset, NULL, OUT_RAWDATA, 0);
563 list->uplevel(LIST_TIMES);
566 if (instruction->times > 1)
567 list->downlevel(LIST_TIMES);
568 return offset - start;
569 } else if (m > 0 && m > size_prob) {
570 size_prob = m;
572 // temp++;
575 if (temp->opcode == -1) { /* didn't match any instruction */
576 switch (size_prob) {
577 case 1:
578 error(ERR_NONFATAL, "operation size not specified");
579 break;
580 case 2:
581 error(ERR_NONFATAL, "mismatch in operand sizes");
582 break;
583 case 3:
584 error(ERR_NONFATAL, "no instruction for this cpu level");
585 break;
586 case 4:
587 error(ERR_NONFATAL, "instruction not supported in 64-bit mode");
588 break;
589 default:
590 error(ERR_NONFATAL,
591 "invalid combination of opcode and operands");
592 break;
595 return 0;
598 int64_t insn_size(int32_t segment, int64_t offset, int bits, uint32_t cp,
599 insn * instruction, efunc error)
601 const struct itemplate *temp;
603 errfunc = error; /* to pass to other functions */
604 cpu = cp;
606 if (instruction->opcode == -1)
607 return 0;
609 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
610 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
611 instruction->opcode == I_DT || instruction->opcode == I_DO) {
612 extop *e;
613 int32_t isize, osize, wsize = 0; /* placate gcc */
615 isize = 0;
616 switch (instruction->opcode) {
617 case I_DB:
618 wsize = 1;
619 break;
620 case I_DW:
621 wsize = 2;
622 break;
623 case I_DD:
624 wsize = 4;
625 break;
626 case I_DQ:
627 wsize = 8;
628 break;
629 case I_DT:
630 wsize = 10;
631 break;
632 case I_DO:
633 wsize = 16;
634 break;
635 default:
636 break;
639 for (e = instruction->eops; e; e = e->next) {
640 int32_t align;
642 osize = 0;
643 if (e->type == EOT_DB_NUMBER)
644 osize = 1;
645 else if (e->type == EOT_DB_STRING)
646 osize = e->stringlen;
648 align = (-osize) % wsize;
649 if (align < 0)
650 align += wsize;
651 isize += osize + align;
653 return isize * instruction->times;
656 if (instruction->opcode == I_INCBIN) {
657 char fname[FILENAME_MAX];
658 FILE *fp;
659 int32_t len;
660 char *prefix = "", *combine;
661 char **pPrevPath = NULL;
663 len = FILENAME_MAX - 1;
664 if (len > instruction->eops->stringlen)
665 len = instruction->eops->stringlen;
666 strncpy(fname, instruction->eops->stringval, len);
667 fname[len] = '\0';
669 /* added by alexfru: 'incbin' uses include paths */
670 while (1) {
671 combine = nasm_malloc(strlen(prefix) + len + 1);
672 strcpy(combine, prefix);
673 strcat(combine, fname);
675 if ((fp = fopen(combine, "rb")) != NULL) {
676 nasm_free(combine);
677 break;
680 nasm_free(combine);
681 pPrevPath = pp_get_include_path_ptr(pPrevPath);
682 if (pPrevPath == NULL)
683 break;
684 prefix = *pPrevPath;
687 if (fp == NULL)
688 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
689 fname);
690 else if (fseek(fp, 0L, SEEK_END) < 0)
691 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
692 fname);
693 else {
694 len = ftell(fp);
695 fclose(fp);
696 if (instruction->eops->next) {
697 len -= instruction->eops->next->offset;
698 if (instruction->eops->next->next &&
699 len > instruction->eops->next->next->offset) {
700 len = instruction->eops->next->next->offset;
703 return instruction->times * len;
705 return 0; /* if we're here, there's an error */
708 /* Check to see if we need an address-size prefix */
709 add_asp(instruction, bits);
711 for (temp = nasm_instructions[instruction->opcode]; temp->opcode != -1; temp++) {
712 int m = matches(temp, instruction, bits);
713 if (m == 99)
714 m += jmp_match(segment, offset, bits, instruction, temp->code);
716 if (m == 100) {
717 /* we've matched an instruction. */
718 int64_t isize;
719 const char *codes = temp->code;
720 int j;
722 isize = calcsize(segment, offset, bits, instruction, codes);
723 if (isize < 0)
724 return -1;
725 for (j = 0; j < MAXPREFIX; j++) {
726 switch (instruction->prefixes[j]) {
727 case P_A16:
728 if (bits != 16)
729 isize++;
730 break;
731 case P_A32:
732 if (bits != 32)
733 isize++;
734 break;
735 case P_O16:
736 if (bits != 16)
737 isize++;
738 break;
739 case P_O32:
740 if (bits == 16)
741 isize++;
742 break;
743 case P_A64:
744 case P_O64:
745 case P_none:
746 break;
747 default:
748 isize++;
749 break;
752 return isize * instruction->times;
755 return -1; /* didn't match any instruction */
758 /* check that opn[op] is a signed byte of size 16 or 32,
759 and return the signed value*/
760 static int is_sbyte(insn * ins, int op, int size)
762 int32_t v;
763 int ret;
765 ret = !(ins->forw_ref && ins->oprs[op].opflags) && /* dead in the water on forward reference or External */
766 optimizing >= 0 &&
767 !(ins->oprs[op].type & STRICT) &&
768 ins->oprs[op].wrt == NO_SEG && ins->oprs[op].segment == NO_SEG;
770 v = ins->oprs[op].offset;
771 if (size == 16)
772 v = (int16_t)v; /* sign extend if 16 bits */
774 return ret && v >= -128L && v <= 127L;
777 static int64_t calcsize(int32_t segment, int64_t offset, int bits,
778 insn * ins, const char *codes)
780 int64_t length = 0;
781 uint8_t c;
782 int rex_mask = ~0;
783 struct operand *opx;
785 ins->rex = 0; /* Ensure REX is reset */
787 if (ins->prefixes[PPS_OSIZE] == P_O64)
788 ins->rex |= REX_W;
790 (void)segment; /* Don't warn that this parameter is unused */
791 (void)offset; /* Don't warn that this parameter is unused */
793 while (*codes) {
794 c = *codes++;
795 opx = &ins->oprs[c & 3];
796 switch (c) {
797 case 01:
798 case 02:
799 case 03:
800 codes += c, length += c;
801 break;
802 case 04:
803 case 05:
804 case 06:
805 case 07:
806 length++;
807 break;
808 case 010:
809 case 011:
810 case 012:
811 case 013:
812 ins->rex |=
813 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
814 codes++, length++;
815 break;
816 case 014:
817 case 015:
818 case 016:
819 case 017:
820 length++;
821 break;
822 case 020:
823 case 021:
824 case 022:
825 case 023:
826 length++;
827 break;
828 case 024:
829 case 025:
830 case 026:
831 case 027:
832 length++;
833 break;
834 case 030:
835 case 031:
836 case 032:
837 case 033:
838 length += 2;
839 break;
840 case 034:
841 case 035:
842 case 036:
843 case 037:
844 if (opx->type & (BITS16 | BITS32 | BITS64))
845 length += (opx->type & BITS16) ? 2 : 4;
846 else
847 length += (bits == 16) ? 2 : 4;
848 break;
849 case 040:
850 case 041:
851 case 042:
852 case 043:
853 length += 4;
854 break;
855 case 044:
856 case 045:
857 case 046:
858 case 047:
859 length += ins->addr_size >> 3;
860 break;
861 case 050:
862 case 051:
863 case 052:
864 case 053:
865 length++;
866 break;
867 case 054:
868 case 055:
869 case 056:
870 case 057:
871 length += 8; /* MOV reg64/imm */
872 break;
873 case 060:
874 case 061:
875 case 062:
876 case 063:
877 length += 2;
878 break;
879 case 064:
880 case 065:
881 case 066:
882 case 067:
883 if (opx->type & (BITS16 | BITS32 | BITS64))
884 length += (opx->type & BITS16) ? 2 : 4;
885 else
886 length += (bits == 16) ? 2 : 4;
887 break;
888 case 070:
889 case 071:
890 case 072:
891 case 073:
892 length += 4;
893 break;
894 case 074:
895 case 075:
896 case 076:
897 case 077:
898 length += 2;
899 break;
900 case 0140:
901 case 0141:
902 case 0142:
903 case 0143:
904 length += is_sbyte(ins, c & 3, 16) ? 1 : 2;
905 break;
906 case 0144:
907 case 0145:
908 case 0146:
909 case 0147:
910 codes += 2;
911 length++;
912 break;
913 case 0150:
914 case 0151:
915 case 0152:
916 case 0153:
917 length += is_sbyte(ins, c & 3, 32) ? 1 : 4;
918 break;
919 case 0154:
920 case 0155:
921 case 0156:
922 case 0157:
923 codes += 2;
924 length++;
925 break;
926 case 0160:
927 case 0161:
928 case 0162:
929 case 0163:
930 length++;
931 ins->rex |= REX_D;
932 ins->drexdst = regval(&ins->oprs[c & 3]);
933 break;
934 case 0164:
935 case 0165:
936 case 0166:
937 case 0167:
938 length++;
939 ins->rex |= REX_D|REX_OC;
940 ins->drexdst = regval(&ins->oprs[c & 3]);
941 break;
942 case 0170:
943 length++;
944 break;
945 case 0171:
946 break;
947 case 0300:
948 case 0301:
949 case 0302:
950 case 0303:
951 break;
952 case 0310:
953 if (bits == 64)
954 return -1;
955 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
956 break;
957 case 0311:
958 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
959 break;
960 case 0312:
961 break;
962 case 0313:
963 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
964 has_prefix(ins, PPS_ASIZE, P_A32))
965 return -1;
966 break;
967 case 0314:
968 case 0315:
969 case 0316:
970 case 0317:
971 break;
972 case 0320:
973 length += (bits != 16);
974 break;
975 case 0321:
976 length += (bits == 16);
977 break;
978 case 0322:
979 break;
980 case 0323:
981 rex_mask &= ~REX_W;
982 break;
983 case 0324:
984 ins->rex |= REX_W;
985 break;
986 case 0330:
987 codes++, length++;
988 break;
989 case 0331:
990 break;
991 case 0332:
992 case 0333:
993 length++;
994 break;
995 case 0334:
996 ins->rex |= REX_L;
997 break;
998 case 0335:
999 break;
1000 case 0340:
1001 if (ins->oprs[0].segment != NO_SEG)
1002 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1003 " quantity of BSS space");
1004 else
1005 length += ins->oprs[0].offset;
1006 break;
1007 case 0364:
1008 case 0365:
1009 break;
1010 case 0366:
1011 case 0367:
1012 length++;
1013 break;
1014 case 0370:
1015 case 0371:
1016 case 0372:
1017 break;
1018 case 0373:
1019 length++;
1020 break;
1021 default: /* can't do it by 'case' statements */
1022 if (c >= 0100 && c <= 0277) { /* it's an EA */
1023 ea ea_data;
1024 int rfield;
1025 int32_t rflags;
1026 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
1028 if (c <= 0177) {
1029 /* pick rfield from operand b */
1030 rflags = regflag(&ins->oprs[c & 7]);
1031 rfield = regvals[ins->oprs[c & 7].basereg];
1032 } else {
1033 rflags = 0;
1034 rfield = c & 7;
1037 if (!process_ea
1038 (&ins->oprs[(c >> 3) & 7], &ea_data, bits,
1039 ins->addr_size, rfield, rflags, ins->forw_ref)) {
1040 errfunc(ERR_NONFATAL, "invalid effective address");
1041 return -1;
1042 } else {
1043 ins->rex |= ea_data.rex;
1044 length += ea_data.size;
1046 } else {
1047 errfunc(ERR_PANIC, "internal instruction table corrupt"
1048 ": instruction code 0x%02X given", c);
1053 ins->rex &= rex_mask;
1055 if (ins->rex & REX_D) {
1056 if (ins->rex & REX_H) {
1057 errfunc(ERR_NONFATAL, "cannot use high register in drex instruction");
1058 return -1;
1060 if (bits != 64 && ((ins->rex & (REX_W|REX_X|REX_B)) ||
1061 ins->drexdst > 7)) {
1062 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1063 return -1;
1065 length++;
1066 } else if (ins->rex & REX_REAL) {
1067 if (ins->rex & REX_H) {
1068 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1069 return -1;
1070 } else if (bits == 64) {
1071 length++;
1072 } else if ((ins->rex & REX_L) &&
1073 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1074 cpu >= IF_X86_64) {
1075 /* LOCK-as-REX.R */
1076 assert_no_prefix(ins, PPS_LREP);
1077 length++;
1078 } else {
1079 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1080 return -1;
1084 return length;
1087 #define EMIT_REX() \
1088 if (!(ins->rex & REX_D) && (ins->rex & REX_REAL) && (bits == 64)) { \
1089 ins->rex = (ins->rex & REX_REAL)|REX_P; \
1090 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG); \
1091 ins->rex = 0; \
1092 offset += 1; \
1095 static void gencode(int32_t segment, int64_t offset, int bits,
1096 insn * ins, const char *codes, int64_t insn_end)
1098 static char condval[] = { /* conditional opcodes */
1099 0x7, 0x3, 0x2, 0x6, 0x2, 0x4, 0xF, 0xD, 0xC, 0xE, 0x6, 0x2,
1100 0x3, 0x7, 0x3, 0x5, 0xE, 0xC, 0xD, 0xF, 0x1, 0xB, 0x9, 0x5,
1101 0x0, 0xA, 0xA, 0xB, 0x8, 0x4
1103 uint8_t c;
1104 uint8_t bytes[4];
1105 int64_t size;
1106 int64_t data;
1107 struct operand *opx;
1109 while (*codes) {
1110 c = *codes++;
1111 opx = &ins->oprs[c & 3];
1112 switch (c) {
1113 case 01:
1114 case 02:
1115 case 03:
1116 EMIT_REX();
1117 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
1118 codes += c;
1119 offset += c;
1120 break;
1122 case 04:
1123 case 06:
1124 switch (ins->oprs[0].basereg) {
1125 case R_CS:
1126 bytes[0] = 0x0E + (c == 0x04 ? 1 : 0);
1127 break;
1128 case R_DS:
1129 bytes[0] = 0x1E + (c == 0x04 ? 1 : 0);
1130 break;
1131 case R_ES:
1132 bytes[0] = 0x06 + (c == 0x04 ? 1 : 0);
1133 break;
1134 case R_SS:
1135 bytes[0] = 0x16 + (c == 0x04 ? 1 : 0);
1136 break;
1137 default:
1138 errfunc(ERR_PANIC,
1139 "bizarre 8086 segment register received");
1141 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1142 offset++;
1143 break;
1145 case 05:
1146 case 07:
1147 switch (ins->oprs[0].basereg) {
1148 case R_FS:
1149 bytes[0] = 0xA0 + (c == 0x05 ? 1 : 0);
1150 break;
1151 case R_GS:
1152 bytes[0] = 0xA8 + (c == 0x05 ? 1 : 0);
1153 break;
1154 default:
1155 errfunc(ERR_PANIC,
1156 "bizarre 386 segment register received");
1158 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1159 offset++;
1160 break;
1162 case 010:
1163 case 011:
1164 case 012:
1165 case 013:
1166 EMIT_REX();
1167 bytes[0] = *codes++ + ((regval(opx)) & 7);
1168 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1169 offset += 1;
1170 break;
1172 case 014:
1173 case 015:
1174 case 016:
1175 case 017:
1176 if (opx->offset < -128 || opx->offset > 127) {
1177 errfunc(ERR_WARNING, "signed byte value exceeds bounds");
1180 if (opx->segment != NO_SEG) {
1181 data = opx->offset;
1182 out(offset, segment, &data, OUT_ADDRESS, 1,
1183 opx->segment, opx->wrt);
1184 } else {
1185 bytes[0] = opx->offset;
1186 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1187 NO_SEG);
1189 offset += 1;
1190 break;
1192 case 020:
1193 case 021:
1194 case 022:
1195 case 023:
1196 if (opx->offset < -256 || opx->offset > 255) {
1197 errfunc(ERR_WARNING, "byte value exceeds bounds");
1199 if (opx->segment != NO_SEG) {
1200 data = opx->offset;
1201 out(offset, segment, &data, OUT_ADDRESS, 1,
1202 opx->segment, opx->wrt);
1203 } else {
1204 bytes[0] = opx->offset;
1205 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1206 NO_SEG);
1208 offset += 1;
1209 break;
1211 case 024:
1212 case 025:
1213 case 026:
1214 case 027:
1215 if (opx->offset < 0 || opx->offset > 255)
1216 errfunc(ERR_WARNING, "unsigned byte value exceeds bounds");
1217 if (opx->segment != NO_SEG) {
1218 data = opx->offset;
1219 out(offset, segment, &data, OUT_ADDRESS, 1,
1220 opx->segment, opx->wrt);
1221 } else {
1222 bytes[0] = opx->offset;
1223 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1224 NO_SEG);
1226 offset += 1;
1227 break;
1229 case 030:
1230 case 031:
1231 case 032:
1232 case 033:
1233 data = opx->offset;
1234 if (opx->segment == NO_SEG && opx->wrt == NO_SEG)
1235 warn_overflow(2, data);
1236 out(offset, segment, &data, OUT_ADDRESS, 2,
1237 opx->segment, opx->wrt);
1238 offset += 2;
1239 break;
1241 case 034:
1242 case 035:
1243 case 036:
1244 case 037:
1245 if (opx->type & (BITS16 | BITS32))
1246 size = (opx->type & BITS16) ? 2 : 4;
1247 else
1248 size = (bits == 16) ? 2 : 4;
1249 data = opx->offset;
1250 if (opx->segment == NO_SEG && opx->wrt == NO_SEG)
1251 warn_overflow(size, data);
1252 out(offset, segment, &data, OUT_ADDRESS, size,
1253 opx->segment, opx->wrt);
1254 offset += size;
1255 break;
1257 case 040:
1258 case 041:
1259 case 042:
1260 case 043:
1261 data = opx->offset;
1262 out(offset, segment, &data, OUT_ADDRESS, 4,
1263 opx->segment, opx->wrt);
1264 offset += 4;
1265 break;
1267 case 044:
1268 case 045:
1269 case 046:
1270 case 047:
1271 data = opx->offset;
1272 size = ins->addr_size >> 3;
1273 if (opx->segment == NO_SEG &&
1274 opx->wrt == NO_SEG)
1275 warn_overflow(size, data);
1276 out(offset, segment, &data, OUT_ADDRESS, size,
1277 opx->segment, opx->wrt);
1278 offset += size;
1279 break;
1281 case 050:
1282 case 051:
1283 case 052:
1284 case 053:
1285 if (opx->segment != segment)
1286 errfunc(ERR_NONFATAL,
1287 "short relative jump outside segment");
1288 data = opx->offset - insn_end;
1289 if (data > 127 || data < -128)
1290 errfunc(ERR_NONFATAL, "short jump is out of range");
1291 bytes[0] = data;
1292 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1293 offset += 1;
1294 break;
1296 case 054:
1297 case 055:
1298 case 056:
1299 case 057:
1300 data = (int64_t)opx->offset;
1301 out(offset, segment, &data, OUT_ADDRESS, 8,
1302 opx->segment, opx->wrt);
1303 offset += 8;
1304 break;
1306 case 060:
1307 case 061:
1308 case 062:
1309 case 063:
1310 if (opx->segment != segment) {
1311 data = opx->offset;
1312 out(offset, segment, &data,
1313 OUT_REL2ADR, insn_end - offset,
1314 opx->segment, opx->wrt);
1315 } else {
1316 data = opx->offset - insn_end;
1317 out(offset, segment, &data,
1318 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
1320 offset += 2;
1321 break;
1323 case 064:
1324 case 065:
1325 case 066:
1326 case 067:
1327 if (opx->type & (BITS16 | BITS32 | BITS64))
1328 size = (opx->type & BITS16) ? 2 : 4;
1329 else
1330 size = (bits == 16) ? 2 : 4;
1331 if (opx->segment != segment) {
1332 data = opx->offset;
1333 out(offset, segment, &data,
1334 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1335 insn_end - offset, opx->segment, opx->wrt);
1336 } else {
1337 data = opx->offset - insn_end;
1338 out(offset, segment, &data,
1339 OUT_ADDRESS, size, NO_SEG, NO_SEG);
1341 offset += size;
1342 break;
1344 case 070:
1345 case 071:
1346 case 072:
1347 case 073:
1348 if (opx->segment != segment) {
1349 data = opx->offset;
1350 out(offset, segment, &data,
1351 OUT_REL4ADR, insn_end - offset,
1352 opx->segment, opx->wrt);
1353 } else {
1354 data = opx->offset - insn_end;
1355 out(offset, segment, &data,
1356 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
1358 offset += 4;
1359 break;
1361 case 074:
1362 case 075:
1363 case 076:
1364 case 077:
1365 if (opx->segment == NO_SEG)
1366 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1367 " relocatable");
1368 data = 0L;
1369 out(offset, segment, &data, OUT_ADDRESS, 2,
1370 outfmt->segbase(1 + opx->segment),
1371 opx->wrt);
1372 offset += 2;
1373 break;
1375 case 0140:
1376 case 0141:
1377 case 0142:
1378 case 0143:
1379 data = opx->offset;
1380 if (is_sbyte(ins, c & 3, 16)) {
1381 bytes[0] = data;
1382 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1383 NO_SEG);
1384 offset++;
1385 } else {
1386 if (opx->segment == NO_SEG &&
1387 opx->wrt == NO_SEG)
1388 warn_overflow(2, data);
1389 out(offset, segment, &data, OUT_ADDRESS, 2,
1390 opx->segment, opx->wrt);
1391 offset += 2;
1393 break;
1395 case 0144:
1396 case 0145:
1397 case 0146:
1398 case 0147:
1399 EMIT_REX();
1400 codes++;
1401 bytes[0] = *codes++;
1402 if (is_sbyte(ins, c & 3, 16))
1403 bytes[0] |= 2; /* s-bit */
1404 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1405 offset++;
1406 break;
1408 case 0150:
1409 case 0151:
1410 case 0152:
1411 case 0153:
1412 data = opx->offset;
1413 if (is_sbyte(ins, c & 3, 32)) {
1414 bytes[0] = data;
1415 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1416 NO_SEG);
1417 offset++;
1418 } else {
1419 out(offset, segment, &data, OUT_ADDRESS, 4,
1420 opx->segment, opx->wrt);
1421 offset += 4;
1423 break;
1425 case 0154:
1426 case 0155:
1427 case 0156:
1428 case 0157:
1429 EMIT_REX();
1430 codes++;
1431 bytes[0] = *codes++;
1432 if (is_sbyte(ins, c & 3, 32))
1433 bytes[0] |= 2; /* s-bit */
1434 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1435 offset++;
1436 break;
1438 case 0160:
1439 case 0161:
1440 case 0162:
1441 case 0163:
1442 case 0164:
1443 case 0165:
1444 case 0166:
1445 case 0167:
1446 break;
1448 case 0170:
1449 EMIT_REX();
1450 bytes[0] = 0;
1451 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1452 offset += 1;
1453 break;
1455 case 0171:
1456 bytes[0] =
1457 (ins->drexdst << 4) |
1458 (ins->rex & REX_OC ? 0x08 : 0) |
1459 (ins->rex & (REX_R|REX_X|REX_B));
1460 ins->rex = 0;
1461 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1462 offset++;
1463 break;
1465 case 0300:
1466 case 0301:
1467 case 0302:
1468 case 0303:
1469 break;
1471 case 0310:
1472 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
1473 *bytes = 0x67;
1474 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1475 offset += 1;
1476 } else
1477 offset += 0;
1478 break;
1480 case 0311:
1481 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
1482 *bytes = 0x67;
1483 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1484 offset += 1;
1485 } else
1486 offset += 0;
1487 break;
1489 case 0312:
1490 break;
1492 case 0313:
1493 ins->rex = 0;
1494 break;
1496 case 0314:
1497 case 0315:
1498 case 0316:
1499 case 0317:
1500 break;
1502 case 0320:
1503 if (bits != 16) {
1504 *bytes = 0x66;
1505 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1506 offset += 1;
1507 } else
1508 offset += 0;
1509 break;
1511 case 0321:
1512 if (bits == 16) {
1513 *bytes = 0x66;
1514 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1515 offset += 1;
1516 } else
1517 offset += 0;
1518 break;
1520 case 0322:
1521 case 0323:
1522 break;
1524 case 0324:
1525 ins->rex |= REX_W;
1526 break;
1528 case 0330:
1529 *bytes = *codes++ ^ condval[ins->condition];
1530 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1531 offset += 1;
1532 break;
1534 case 0331:
1535 break;
1537 case 0332:
1538 case 0333:
1539 *bytes = c - 0332 + 0xF2;
1540 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1541 offset += 1;
1542 break;
1544 case 0334:
1545 if (ins->rex & REX_R) {
1546 *bytes = 0xF0;
1547 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1548 offset += 1;
1550 ins->rex &= ~(REX_L|REX_R);
1551 break;
1553 case 0335:
1554 break;
1556 case 0340:
1557 if (ins->oprs[0].segment != NO_SEG)
1558 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1559 else {
1560 int64_t size = ins->oprs[0].offset;
1561 if (size > 0)
1562 out(offset, segment, NULL,
1563 OUT_RESERVE, size, NO_SEG, NO_SEG);
1564 offset += size;
1566 break;
1568 case 0364:
1569 case 0365:
1570 break;
1572 case 0366:
1573 case 0367:
1574 *bytes = c - 0366 + 0x66;
1575 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1576 offset += 1;
1577 break;
1579 case 0370:
1580 case 0371:
1581 case 0372:
1582 break;
1584 case 0373:
1585 *bytes = bits == 16 ? 3 : 5;
1586 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1587 offset += 1;
1588 break;
1590 default: /* can't do it by 'case' statements */
1591 if (c >= 0100 && c <= 0277) { /* it's an EA */
1592 ea ea_data;
1593 int rfield;
1594 int32_t rflags;
1595 uint8_t *p;
1596 int32_t s;
1598 if (c <= 0177) {
1599 /* pick rfield from operand b */
1600 rflags = regflag(&ins->oprs[c & 7]);
1601 rfield = regvals[ins->oprs[c & 7].basereg];
1602 } else {
1603 /* rfield is constant */
1604 rflags = 0;
1605 rfield = c & 7;
1608 if (!process_ea
1609 (&ins->oprs[(c >> 3) & 7], &ea_data, bits,
1610 ins->addr_size, rfield, rflags, ins->forw_ref)) {
1611 errfunc(ERR_NONFATAL, "invalid effective address");
1615 p = bytes;
1616 *p++ = ea_data.modrm;
1617 if (ea_data.sib_present)
1618 *p++ = ea_data.sib;
1620 /* DREX suffixes come between the SIB and the displacement */
1621 if (ins->rex & REX_D) {
1622 *p++ =
1623 (ins->drexdst << 4) |
1624 (ins->rex & REX_OC ? 0x08 : 0) |
1625 (ins->rex & (REX_R|REX_X|REX_B));
1626 ins->rex = 0;
1629 s = p - bytes;
1630 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
1632 switch (ea_data.bytes) {
1633 case 0:
1634 break;
1635 case 1:
1636 if (ins->oprs[(c >> 3) & 7].segment != NO_SEG) {
1637 data = ins->oprs[(c >> 3) & 7].offset;
1638 out(offset, segment, &data, OUT_ADDRESS, 1,
1639 ins->oprs[(c >> 3) & 7].segment,
1640 ins->oprs[(c >> 3) & 7].wrt);
1641 } else {
1642 *bytes = ins->oprs[(c >> 3) & 7].offset;
1643 out(offset, segment, bytes, OUT_RAWDATA, 1,
1644 NO_SEG, NO_SEG);
1646 s++;
1647 break;
1648 case 8:
1649 case 2:
1650 case 4:
1651 data = ins->oprs[(c >> 3) & 7].offset;
1652 warn_overflow(ea_data.bytes, data);
1653 out(offset, segment, &data,
1654 ea_data.rip ? OUT_REL4ADR : OUT_ADDRESS,
1655 ea_data.bytes,
1656 ins->oprs[(c >> 3) & 7].segment,
1657 ins->oprs[(c >> 3) & 7].wrt);
1658 s += ea_data.bytes;
1659 break;
1661 offset += s;
1662 } else {
1663 errfunc(ERR_PANIC, "internal instruction table corrupt"
1664 ": instruction code 0x%02X given", c);
1670 static int32_t regflag(const operand * o)
1672 if (o->basereg < EXPR_REG_START || o->basereg >= REG_ENUM_LIMIT) {
1673 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
1675 return reg_flags[o->basereg];
1678 static int32_t regval(const operand * o)
1680 if (o->basereg < EXPR_REG_START || o->basereg >= REG_ENUM_LIMIT) {
1681 errfunc(ERR_PANIC, "invalid operand passed to regval()");
1683 return regvals[o->basereg];
1686 static int op_rexflags(const operand * o, int mask)
1688 int32_t flags;
1689 int val;
1691 if (o->basereg < EXPR_REG_START || o->basereg >= REG_ENUM_LIMIT) {
1692 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
1695 flags = reg_flags[o->basereg];
1696 val = regvals[o->basereg];
1698 return rexflags(val, flags, mask);
1701 static int rexflags(int val, int32_t flags, int mask)
1703 int rex = 0;
1705 if (val >= 8)
1706 rex |= REX_B|REX_X|REX_R;
1707 if (flags & BITS64)
1708 rex |= REX_W;
1709 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1710 rex |= REX_H;
1711 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1712 rex |= REX_P;
1714 return rex & mask;
1717 static int matches(const struct itemplate *itemp, insn * instruction, int bits)
1719 int i, size[MAX_OPERANDS], asize, oprs, ret;
1721 ret = 100;
1724 * Check the opcode
1726 if (itemp->opcode != instruction->opcode)
1727 return 0;
1730 * Count the operands
1732 if (itemp->operands != instruction->operands)
1733 return 0;
1736 * Check that no spurious colons or TOs are present
1738 for (i = 0; i < itemp->operands; i++)
1739 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
1740 return 0;
1743 * Check that the operand flags all match up
1745 for (i = 0; i < itemp->operands; i++) {
1746 if (itemp->opd[i] & SAME_AS) {
1747 int j = itemp->opd[i] & ~SAME_AS;
1748 if (instruction->oprs[i].type != instruction->oprs[j].type ||
1749 instruction->oprs[i].basereg != instruction->oprs[j].basereg)
1750 return 0;
1751 } else if (itemp->opd[i] & ~instruction->oprs[i].type ||
1752 ((itemp->opd[i] & SIZE_MASK) &&
1753 ((itemp->opd[i] ^ instruction->oprs[i].type) & SIZE_MASK))) {
1754 if ((itemp->opd[i] & ~instruction->oprs[i].type & ~SIZE_MASK) ||
1755 (instruction->oprs[i].type & SIZE_MASK))
1756 return 0;
1757 else
1758 return 1;
1763 * Check operand sizes
1765 if (itemp->flags & IF_ARMASK) {
1766 memset(size, 0, sizeof size);
1768 switch (itemp->flags & IF_ARMASK) {
1769 case IF_AR0:
1770 i = 0;
1771 break;
1772 case IF_AR1:
1773 i = 1;
1774 break;
1775 case IF_AR2:
1776 i = 2;
1777 break;
1778 case IF_AR3:
1779 i = 3;
1780 break;
1781 default:
1782 break; /* Shouldn't happen */
1784 switch (itemp->flags & IF_SMASK) {
1785 case IF_SB:
1786 size[i] = BITS8;
1787 break;
1788 case IF_SW:
1789 size[i] = BITS16;
1790 break;
1791 case IF_SD:
1792 size[i] = BITS32;
1793 break;
1794 case IF_SQ:
1795 size[i] = BITS64;
1796 break;
1797 case IF_SO:
1798 size[i] = BITS128;
1799 break;
1800 default:
1801 break;
1803 } else {
1804 asize = 0;
1805 switch (itemp->flags & IF_SMASK) {
1806 case IF_SB:
1807 asize = BITS8;
1808 oprs = itemp->operands;
1809 break;
1810 case IF_SW:
1811 asize = BITS16;
1812 oprs = itemp->operands;
1813 break;
1814 case IF_SD:
1815 asize = BITS32;
1816 oprs = itemp->operands;
1817 break;
1818 case IF_SQ:
1819 asize = BITS64;
1820 oprs = itemp->operands;
1821 break;
1822 case IF_SO:
1823 asize = BITS128;
1824 oprs = itemp->operands;
1825 break;
1826 default:
1827 break;
1829 for (i = 0; i < MAX_OPERANDS; i++)
1830 size[i] = asize;
1833 if (itemp->flags & (IF_SM | IF_SM2)) {
1834 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
1835 asize = 0;
1836 for (i = 0; i < oprs; i++) {
1837 if ((asize = itemp->opd[i] & SIZE_MASK) != 0) {
1838 int j;
1839 for (j = 0; j < oprs; j++)
1840 size[j] = asize;
1841 break;
1844 } else {
1845 oprs = itemp->operands;
1848 for (i = 0; i < itemp->operands; i++) {
1849 if (!(itemp->opd[i] & SIZE_MASK) &&
1850 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
1851 return 2;
1855 * Check template is okay at the set cpu level
1857 if (((itemp->flags & IF_PLEVEL) > cpu))
1858 return 3;
1861 * Check if instruction is available in long mode
1863 if ((itemp->flags & IF_NOLONG) && (bits == 64))
1864 return 4;
1867 * Check if special handling needed for Jumps
1869 if ((uint8_t)(itemp->code[0]) >= 0370)
1870 return 99;
1872 return ret;
1875 static ea *process_ea(operand * input, ea * output, int bits,
1876 int addrbits, int rfield, int32_t rflags, int forw_ref)
1878 output->rip = false;
1880 /* REX flags for the rfield operand */
1881 output->rex |= rexflags(rfield, rflags, REX_R|REX_P|REX_W|REX_H);
1883 if (!(REGISTER & ~input->type)) { /* register direct */
1884 int i;
1885 int32_t f;
1887 if (input->basereg < EXPR_REG_START /* Verify as Register */
1888 || input->basereg >= REG_ENUM_LIMIT)
1889 return NULL;
1890 f = regflag(input);
1891 i = regvals[input->basereg];
1893 if (REG_EA & ~f)
1894 return NULL; /* Invalid EA register */
1896 output->rex |= op_rexflags(input, REX_B|REX_P|REX_W|REX_H);
1898 output->sib_present = false; /* no SIB necessary */
1899 output->bytes = 0; /* no offset necessary either */
1900 output->modrm = 0xC0 | ((rfield & 7) << 3) | (i & 7);
1901 } else { /* it's a memory reference */
1902 if (input->basereg == -1
1903 && (input->indexreg == -1 || input->scale == 0)) {
1904 /* it's a pure offset */
1905 if (bits == 64 && (~input->type & IP_REL)) {
1906 int scale, index, base;
1907 output->sib_present = true;
1908 scale = 0;
1909 index = 4;
1910 base = 5;
1911 output->sib = (scale << 6) | (index << 3) | base;
1912 output->bytes = 4;
1913 output->modrm = 4 | ((rfield & 7) << 3);
1914 output->rip = false;
1915 } else {
1916 output->sib_present = false;
1917 output->bytes = (addrbits != 16 ? 4 : 2);
1918 output->modrm = (addrbits != 16 ? 5 : 6) | ((rfield & 7) << 3);
1919 output->rip = bits == 64;
1921 } else { /* it's an indirection */
1922 int i = input->indexreg, b = input->basereg, s = input->scale;
1923 int32_t o = input->offset, seg = input->segment;
1924 int hb = input->hintbase, ht = input->hinttype;
1925 int t;
1926 int it, bt;
1927 int32_t ix, bx; /* register flags */
1929 if (s == 0)
1930 i = -1; /* make this easy, at least */
1932 if (i >= EXPR_REG_START && i < REG_ENUM_LIMIT) {
1933 it = regvals[i];
1934 ix = reg_flags[i];
1935 } else {
1936 it = -1;
1937 ix = 0;
1940 if (b >= EXPR_REG_START && b < REG_ENUM_LIMIT) {
1941 bt = regvals[b];
1942 bx = reg_flags[b];
1943 } else {
1944 bt = -1;
1945 bx = 0;
1948 /* check for a 32/64-bit memory reference... */
1949 if ((ix|bx) & (BITS32|BITS64)) {
1950 /* it must be a 32/64-bit memory reference. Firstly we have
1951 * to check that all registers involved are type E/Rxx. */
1952 int32_t sok = BITS32|BITS64;
1954 if (it != -1) {
1955 if (!(REG64 & ~ix) || !(REG32 & ~ix))
1956 sok &= ix;
1957 else
1958 return NULL;
1961 if (bt != -1) {
1962 if (REG_GPR & ~bx)
1963 return NULL; /* Invalid register */
1964 if (~sok & bx & SIZE_MASK)
1965 return NULL; /* Invalid size */
1966 sok &= bx;
1969 /* While we're here, ensure the user didn't specify
1970 WORD or QWORD. */
1971 if (input->disp_size == 16 || input->disp_size == 64)
1972 return NULL;
1974 if (addrbits == 16 ||
1975 (addrbits == 32 && !(sok & BITS32)) ||
1976 (addrbits == 64 && !(sok & BITS64)))
1977 return NULL;
1979 /* now reorganize base/index */
1980 if (s == 1 && bt != it && bt != -1 && it != -1 &&
1981 ((hb == b && ht == EAH_NOTBASE)
1982 || (hb == i && ht == EAH_MAKEBASE))) {
1983 /* swap if hints say so */
1984 t = bt, bt = it, it = t;
1985 t = bx, bx = ix, ix = t;
1987 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
1988 bt = -1, bx = 0, s++;
1989 if (bt == -1 && s == 1 && !(hb == it && ht == EAH_NOTBASE)) {
1990 /* make single reg base, unless hint */
1991 bt = it, bx = ix, it = -1, ix = 0;
1993 if (((s == 2 && it != REG_NUM_ESP
1994 && !(input->eaflags & EAF_TIMESTWO)) || s == 3
1995 || s == 5 || s == 9) && bt == -1)
1996 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
1997 if (it == -1 && (bt & 7) != REG_NUM_ESP
1998 && (input->eaflags & EAF_TIMESTWO))
1999 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2000 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2001 if (s == 1 && it == REG_NUM_ESP) {
2002 /* swap ESP into base if scale is 1 */
2003 t = it, it = bt, bt = t;
2004 t = ix, ix = bx, bx = t;
2006 if (it == REG_NUM_ESP
2007 || (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
2008 return NULL; /* wrong, for various reasons */
2010 output->rex |= rexflags(it, ix, REX_X);
2011 output->rex |= rexflags(bt, bx, REX_B);
2013 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
2014 /* no SIB needed */
2015 int mod, rm;
2017 if (bt == -1) {
2018 rm = 5;
2019 mod = 0;
2020 } else {
2021 rm = (bt & 7);
2022 if (rm != REG_NUM_EBP && o == 0 &&
2023 seg == NO_SEG && !forw_ref &&
2024 !(input->eaflags &
2025 (EAF_BYTEOFFS | EAF_WORDOFFS)))
2026 mod = 0;
2027 else if (input->eaflags & EAF_BYTEOFFS ||
2028 (o >= -128 && o <= 127 && seg == NO_SEG
2029 && !forw_ref
2030 && !(input->eaflags & EAF_WORDOFFS)))
2031 mod = 1;
2032 else
2033 mod = 2;
2036 output->sib_present = false;
2037 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2038 output->modrm = (mod << 6) | ((rfield & 7) << 3) | rm;
2039 } else {
2040 /* we need a SIB */
2041 int mod, scale, index, base;
2043 if (it == -1)
2044 index = 4, s = 1;
2045 else
2046 index = (it & 7);
2048 switch (s) {
2049 case 1:
2050 scale = 0;
2051 break;
2052 case 2:
2053 scale = 1;
2054 break;
2055 case 4:
2056 scale = 2;
2057 break;
2058 case 8:
2059 scale = 3;
2060 break;
2061 default: /* then what the smeg is it? */
2062 return NULL; /* panic */
2065 if (bt == -1) {
2066 base = 5;
2067 mod = 0;
2068 } else {
2069 base = (bt & 7);
2070 if (base != REG_NUM_EBP && o == 0 &&
2071 seg == NO_SEG && !forw_ref &&
2072 !(input->eaflags &
2073 (EAF_BYTEOFFS | EAF_WORDOFFS)))
2074 mod = 0;
2075 else if (input->eaflags & EAF_BYTEOFFS ||
2076 (o >= -128 && o <= 127 && seg == NO_SEG
2077 && !forw_ref
2078 && !(input->eaflags & EAF_WORDOFFS)))
2079 mod = 1;
2080 else
2081 mod = 2;
2084 output->sib_present = true;
2085 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2086 output->modrm = (mod << 6) | ((rfield & 7) << 3) | 4;
2087 output->sib = (scale << 6) | (index << 3) | base;
2089 } else { /* it's 16-bit */
2090 int mod, rm;
2092 /* check for 64-bit long mode */
2093 if (addrbits == 64)
2094 return NULL;
2096 /* check all registers are BX, BP, SI or DI */
2097 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI
2098 && b != R_DI) || (i != -1 && i != R_BP && i != R_BX
2099 && i != R_SI && i != R_DI))
2100 return NULL;
2102 /* ensure the user didn't specify DWORD/QWORD */
2103 if (input->disp_size == 32 || input->disp_size == 64)
2104 return NULL;
2106 if (s != 1 && i != -1)
2107 return NULL; /* no can do, in 16-bit EA */
2108 if (b == -1 && i != -1) {
2109 int tmp = b;
2110 b = i;
2111 i = tmp;
2112 } /* swap */
2113 if ((b == R_SI || b == R_DI) && i != -1) {
2114 int tmp = b;
2115 b = i;
2116 i = tmp;
2118 /* have BX/BP as base, SI/DI index */
2119 if (b == i)
2120 return NULL; /* shouldn't ever happen, in theory */
2121 if (i != -1 && b != -1 &&
2122 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
2123 return NULL; /* invalid combinations */
2124 if (b == -1) /* pure offset: handled above */
2125 return NULL; /* so if it gets to here, panic! */
2127 rm = -1;
2128 if (i != -1)
2129 switch (i * 256 + b) {
2130 case R_SI * 256 + R_BX:
2131 rm = 0;
2132 break;
2133 case R_DI * 256 + R_BX:
2134 rm = 1;
2135 break;
2136 case R_SI * 256 + R_BP:
2137 rm = 2;
2138 break;
2139 case R_DI * 256 + R_BP:
2140 rm = 3;
2141 break;
2142 } else
2143 switch (b) {
2144 case R_SI:
2145 rm = 4;
2146 break;
2147 case R_DI:
2148 rm = 5;
2149 break;
2150 case R_BP:
2151 rm = 6;
2152 break;
2153 case R_BX:
2154 rm = 7;
2155 break;
2157 if (rm == -1) /* can't happen, in theory */
2158 return NULL; /* so panic if it does */
2160 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2161 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2162 mod = 0;
2163 else if (input->eaflags & EAF_BYTEOFFS ||
2164 (o >= -128 && o <= 127 && seg == NO_SEG
2165 && !forw_ref
2166 && !(input->eaflags & EAF_WORDOFFS)))
2167 mod = 1;
2168 else
2169 mod = 2;
2171 output->sib_present = false; /* no SIB - it's 16-bit */
2172 output->bytes = mod; /* bytes of offset needed */
2173 output->modrm = (mod << 6) | ((rfield & 7) << 3) | rm;
2178 output->size = 1 + output->sib_present + output->bytes;
2179 return output;
2182 static void add_asp(insn *ins, int addrbits)
2184 int j, valid;
2185 int defdisp;
2187 valid = (addrbits == 64) ? 64|32 : 32|16;
2189 switch (ins->prefixes[PPS_ASIZE]) {
2190 case P_A16:
2191 valid &= 16;
2192 break;
2193 case P_A32:
2194 valid &= 32;
2195 break;
2196 case P_A64:
2197 valid &= 64;
2198 break;
2199 case P_ASP:
2200 valid &= (addrbits == 32) ? 16 : 32;
2201 break;
2202 default:
2203 break;
2206 for (j = 0; j < ins->operands; j++) {
2207 if (!(MEMORY & ~ins->oprs[j].type)) {
2208 int32_t i, b;
2210 /* Verify as Register */
2211 if (ins->oprs[j].indexreg < EXPR_REG_START
2212 || ins->oprs[j].indexreg >= REG_ENUM_LIMIT)
2213 i = 0;
2214 else
2215 i = reg_flags[ins->oprs[j].indexreg];
2217 /* Verify as Register */
2218 if (ins->oprs[j].basereg < EXPR_REG_START
2219 || ins->oprs[j].basereg >= REG_ENUM_LIMIT)
2220 b = 0;
2221 else
2222 b = reg_flags[ins->oprs[j].basereg];
2224 if (ins->oprs[j].scale == 0)
2225 i = 0;
2227 if (!i && !b) {
2228 int ds = ins->oprs[j].disp_size;
2229 if ((addrbits != 64 && ds > 8) ||
2230 (addrbits == 64 && ds == 16))
2231 valid &= ds;
2232 } else {
2233 if (!(REG16 & ~b))
2234 valid &= 16;
2235 if (!(REG32 & ~b))
2236 valid &= 32;
2237 if (!(REG64 & ~b))
2238 valid &= 64;
2240 if (!(REG16 & ~i))
2241 valid &= 16;
2242 if (!(REG32 & ~i))
2243 valid &= 32;
2244 if (!(REG64 & ~i))
2245 valid &= 64;
2250 if (valid & addrbits) {
2251 ins->addr_size = addrbits;
2252 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
2253 /* Add an address size prefix */
2254 enum prefixes pref = (addrbits == 32) ? P_A16 : P_A32;
2255 ins->prefixes[PPS_ASIZE] = pref;
2256 ins->addr_size = (addrbits == 32) ? 16 : 32;
2257 } else {
2258 /* Impossible... */
2259 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2260 ins->addr_size = addrbits; /* Error recovery */
2263 defdisp = ins->addr_size == 16 ? 16 : 32;
2265 for (j = 0; j < ins->operands; j++) {
2266 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2267 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp)
2268 != ins->addr_size) {
2269 /* mem_offs sizes must match the address size; if not,
2270 strip the MEM_OFFS bit and match only EA instructions */
2271 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);