disp8: Consolidate a logic to get compressed displacement
[nasm.git] / assemble.c
bloba09b9643efe8b74b36ee9984f7ccf45e9f4df0fa
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2013 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
35 * assemble.c code generation for the Netwide Assembler
37 * the actual codes (C syntax, i.e. octal):
38 * \0 - terminates the code. (Unless it's a literal of course.)
39 * \1..\4 - that many literal bytes follow in the code stream
40 * \5 - add 4 to the primary operand number (b, low octdigit)
41 * \6 - add 4 to the secondary operand number (a, middle octdigit)
42 * \7 - add 4 to both the primary and the secondary operand number
43 * \10..\13 - a literal byte follows in the code stream, to be added
44 * to the register value of operand 0..3
45 * \14..\17 - the position of index register operand in MIB (BND insns)
46 * \20..\23 - a byte immediate operand, from operand 0..3
47 * \24..\27 - a zero-extended byte immediate operand, from operand 0..3
48 * \30..\33 - a word immediate operand, from operand 0..3
49 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
50 * assembly mode or the operand-size override on the operand
51 * \40..\43 - a long immediate operand, from operand 0..3
52 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
53 * depending on the address size of the instruction.
54 * \50..\53 - a byte relative operand, from operand 0..3
55 * \54..\57 - a qword immediate operand, from operand 0..3
56 * \60..\63 - a word relative operand, from operand 0..3
57 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
58 * assembly mode or the operand-size override on the operand
59 * \70..\73 - a long relative operand, from operand 0..3
60 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
61 * \1ab - a ModRM, calculated on EA in operand a, with the spare
62 * field the register value of operand b.
63 * \172\ab - the register number from operand a in bits 7..4, with
64 * the 4-bit immediate from operand b in bits 3..0.
65 * \173\xab - the register number from operand a in bits 7..4, with
66 * the value b in bits 3..0.
67 * \174..\177 - the register number from operand 0..3 in bits 7..4, and
68 * an arbitrary value in bits 3..0 (assembled as zero.)
69 * \2ab - a ModRM, calculated on EA in operand a, with the spare
70 * field equal to digit b.
72 * \240..\243 - this instruction uses EVEX rather than REX or VEX/XOP, with the
73 * V field taken from operand 0..3.
74 * \250 - this instruction uses EVEX rather than REX or VEX/XOP, with the
75 * V field set to 1111b.
76 * EVEX prefixes are followed by the sequence:
77 * \cm\wlp\tup where cm is:
78 * cc 000 0mm
79 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
80 * and wlp is:
81 * 00 wwl lpp
82 * [l0] ll = 0 (.128, .lz)
83 * [l1] ll = 1 (.256)
84 * [l2] ll = 2 (.512)
85 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
87 * [w0] ww = 0 for W = 0
88 * [w1] ww = 1 for W = 1
89 * [wig] ww = 2 for W don't care (always assembled as 0)
90 * [ww] ww = 3 for W used as REX.W
92 * [p0] pp = 0 for no prefix
93 * [60] pp = 1 for legacy prefix 60
94 * [f3] pp = 2
95 * [f2] pp = 3
97 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
98 * (compressed displacement encoding)
100 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
101 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
102 * V field taken from operand 0..3.
103 * \270 - this instruction uses VEX/XOP rather than REX, with the
104 * V field set to 1111b.
106 * VEX/XOP prefixes are followed by the sequence:
107 * \tmm\wlp where mm is the M field; and wlp is:
108 * 00 wwl lpp
109 * [l0] ll = 0 for L = 0 (.128, .lz)
110 * [l1] ll = 1 for L = 1 (.256)
111 * [lig] ll = 2 for L don't care (always assembled as 0)
113 * [w0] ww = 0 for W = 0
114 * [w1 ] ww = 1 for W = 1
115 * [wig] ww = 2 for W don't care (always assembled as 0)
116 * [ww] ww = 3 for W used as REX.W
118 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
120 * \271 - instruction takes XRELEASE (F3) with or without lock
121 * \272 - instruction takes XACQUIRE/XRELEASE with or without lock
122 * \273 - instruction takes XACQUIRE/XRELEASE with lock only
123 * \274..\277 - a byte immediate operand, from operand 0..3, sign-extended
124 * to the operand size (if o16/o32/o64 present) or the bit size
125 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
126 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
127 * \312 - (disassembler only) invalid with non-default address size.
128 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
129 * \314 - (disassembler only) invalid with REX.B
130 * \315 - (disassembler only) invalid with REX.X
131 * \316 - (disassembler only) invalid with REX.R
132 * \317 - (disassembler only) invalid with REX.W
133 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
134 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
135 * \322 - indicates that this instruction is only valid when the
136 * operand size is the default (instruction to disassembler,
137 * generates no code in the assembler)
138 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
139 * \324 - indicates 64-bit operand size requiring REX prefix.
140 * \325 - instruction which always uses spl/bpl/sil/dil
141 * \326 - instruction not valid with 0xF3 REP prefix. Hint for
142 disassembler only; for SSE instructions.
143 * \330 - a literal byte follows in the code stream, to be added
144 * to the condition code value of the instruction.
145 * \331 - instruction not valid with REP prefix. Hint for
146 * disassembler only; for SSE instructions.
147 * \332 - REP prefix (0xF2 byte) used as opcode extension.
148 * \333 - REP prefix (0xF3 byte) used as opcode extension.
149 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
150 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
151 * \336 - force a REP(E) prefix (0xF3) even if not specified.
152 * \337 - force a REPNE prefix (0xF2) even if not specified.
153 * \336-\337 are still listed as prefixes in the disassembler.
154 * \340 - reserve <operand 0> bytes of uninitialized storage.
155 * Operand 0 had better be a segmentless constant.
156 * \341 - this instruction needs a WAIT "prefix"
157 * \360 - no SSE prefix (== \364\331)
158 * \361 - 66 SSE prefix (== \366\331)
159 * \364 - operand-size prefix (0x66) not permitted
160 * \365 - address-size prefix (0x67) not permitted
161 * \366 - operand-size prefix (0x66) used as opcode extension
162 * \367 - address-size prefix (0x67) used as opcode extension
163 * \370,\371 - match only if operand 0 meets byte jump criteria.
164 * 370 is used for Jcc, 371 is used for JMP.
165 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
166 * used for conditional jump over longer jump
167 * \374 - this instruction takes an XMM VSIB memory EA
168 * \375 - this instruction takes an YMM VSIB memory EA
169 * \376 - this instruction takes an ZMM VSIB memory EA
172 #include "compiler.h"
174 #include <stdio.h>
175 #include <string.h>
176 #include <inttypes.h>
178 #include "nasm.h"
179 #include "nasmlib.h"
180 #include "assemble.h"
181 #include "insns.h"
182 #include "tables.h"
183 #include "disp8.h"
185 enum match_result {
187 * Matching errors. These should be sorted so that more specific
188 * errors come later in the sequence.
190 MERR_INVALOP,
191 MERR_OPSIZEMISSING,
192 MERR_OPSIZEMISMATCH,
193 MERR_BRNUMMISMATCH,
194 MERR_BADCPU,
195 MERR_BADMODE,
196 MERR_BADHLE,
197 MERR_ENCMISMATCH,
198 MERR_BADBND,
200 * Matching success; the conditional ones first
202 MOK_JUMP, /* Matching OK but needs jmp_match() */
203 MOK_GOOD /* Matching unconditionally OK */
206 typedef struct {
207 enum ea_type type; /* what kind of EA is this? */
208 int sib_present; /* is a SIB byte necessary? */
209 int bytes; /* # of bytes of offset needed */
210 int size; /* lazy - this is sib+bytes+1 */
211 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
212 int8_t disp8; /* compressed displacement for EVEX */
213 } ea;
215 #define GEN_SIB(scale, index, base) \
216 (((scale) << 6) | ((index) << 3) | ((base)))
218 #define GEN_MODRM(mod, reg, rm) \
219 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
221 static iflags_t cpu; /* cpu level received from nasm.c */
222 static efunc errfunc;
223 static struct ofmt *outfmt;
224 static ListGen *list;
226 static int64_t calcsize(int32_t, int64_t, int, insn *,
227 const struct itemplate *);
228 static void gencode(int32_t segment, int64_t offset, int bits,
229 insn * ins, const struct itemplate *temp,
230 int64_t insn_end);
231 static enum match_result find_match(const struct itemplate **tempp,
232 insn *instruction,
233 int32_t segment, int64_t offset, int bits);
234 static enum match_result matches(const struct itemplate *, insn *, int bits);
235 static opflags_t regflag(const operand *);
236 static int32_t regval(const operand *);
237 static int rexflags(int, opflags_t, int);
238 static int op_rexflags(const operand *, int);
239 static int op_evexflags(const operand *, int, uint8_t);
240 static void add_asp(insn *, int);
242 static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
244 static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
246 return ins->prefixes[pos] == prefix;
249 static void assert_no_prefix(insn * ins, enum prefix_pos pos)
251 if (ins->prefixes[pos])
252 errfunc(ERR_NONFATAL, "invalid %s prefix",
253 prefix_name(ins->prefixes[pos]));
256 static const char *size_name(int size)
258 switch (size) {
259 case 1:
260 return "byte";
261 case 2:
262 return "word";
263 case 4:
264 return "dword";
265 case 8:
266 return "qword";
267 case 10:
268 return "tword";
269 case 16:
270 return "oword";
271 case 32:
272 return "yword";
273 case 64:
274 return "zword";
275 default:
276 return "???";
280 static void warn_overflow(int pass, int size)
282 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
283 "%s data exceeds bounds", size_name(size));
286 static void warn_overflow_const(int64_t data, int size)
288 if (overflow_general(data, size))
289 warn_overflow(ERR_PASS1, size);
292 static void warn_overflow_opd(const struct operand *o, int size)
294 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
295 if (overflow_general(o->offset, size))
296 warn_overflow(ERR_PASS2, size);
301 * This routine wrappers the real output format's output routine,
302 * in order to pass a copy of the data off to the listing file
303 * generator at the same time.
305 static void out(int64_t offset, int32_t segto, const void *data,
306 enum out_type type, uint64_t size,
307 int32_t segment, int32_t wrt)
309 static int32_t lineno = 0; /* static!!! */
310 static char *lnfname = NULL;
311 uint8_t p[8];
313 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
315 * This is a non-relocated address, and we're going to
316 * convert it into RAWDATA format.
318 uint8_t *q = p;
320 if (size > 8) {
321 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
322 return;
325 WRITEADDR(q, *(int64_t *)data, size);
326 data = p;
327 type = OUT_RAWDATA;
330 list->output(offset, data, type, size);
333 * this call to src_get determines when we call the
334 * debug-format-specific "linenum" function
335 * it updates lineno and lnfname to the current values
336 * returning 0 if "same as last time", -2 if lnfname
337 * changed, and the amount by which lineno changed,
338 * if it did. thus, these variables must be static
341 if (src_get(&lineno, &lnfname))
342 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
344 outfmt->output(segto, data, type, size, segment, wrt);
347 static void out_imm8(int64_t offset, int32_t segment, struct operand *opx)
349 if (opx->segment != NO_SEG) {
350 uint64_t data = opx->offset;
351 out(offset, segment, &data, OUT_ADDRESS, 1, opx->segment, opx->wrt);
352 } else {
353 uint8_t byte = opx->offset;
354 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
358 static bool jmp_match(int32_t segment, int64_t offset, int bits,
359 insn * ins, const struct itemplate *temp)
361 int64_t isize;
362 const uint8_t *code = temp->code;
363 uint8_t c = code[0];
365 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
366 return false;
367 if (!optimizing)
368 return false;
369 if (optimizing < 0 && c == 0371)
370 return false;
372 isize = calcsize(segment, offset, bits, ins, temp);
374 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
375 /* Be optimistic in pass 1 */
376 return true;
378 if (ins->oprs[0].segment != segment)
379 return false;
381 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
382 return (isize >= -128 && isize <= 127); /* is it byte size? */
385 int64_t assemble(int32_t segment, int64_t offset, int bits, iflags_t cp,
386 insn * instruction, struct ofmt *output, efunc error,
387 ListGen * listgen)
389 const struct itemplate *temp;
390 int j;
391 enum match_result m;
392 int64_t insn_end;
393 int32_t itimes;
394 int64_t start = offset;
395 int64_t wsize; /* size for DB etc. */
397 errfunc = error; /* to pass to other functions */
398 cpu = cp;
399 outfmt = output; /* likewise */
400 list = listgen; /* and again */
402 wsize = idata_bytes(instruction->opcode);
403 if (wsize == -1)
404 return 0;
406 if (wsize) {
407 extop *e;
408 int32_t t = instruction->times;
409 if (t < 0)
410 errfunc(ERR_PANIC,
411 "instruction->times < 0 (%ld) in assemble()", t);
413 while (t--) { /* repeat TIMES times */
414 list_for_each(e, instruction->eops) {
415 if (e->type == EOT_DB_NUMBER) {
416 if (wsize > 8) {
417 errfunc(ERR_NONFATAL,
418 "integer supplied to a DT, DO or DY"
419 " instruction");
420 } else {
421 out(offset, segment, &e->offset,
422 OUT_ADDRESS, wsize, e->segment, e->wrt);
423 offset += wsize;
425 } else if (e->type == EOT_DB_STRING ||
426 e->type == EOT_DB_STRING_FREE) {
427 int align;
429 out(offset, segment, e->stringval,
430 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
431 align = e->stringlen % wsize;
433 if (align) {
434 align = wsize - align;
435 out(offset, segment, zero_buffer,
436 OUT_RAWDATA, align, NO_SEG, NO_SEG);
438 offset += e->stringlen + align;
441 if (t > 0 && t == instruction->times - 1) {
443 * Dummy call to list->output to give the offset to the
444 * listing module.
446 list->output(offset, NULL, OUT_RAWDATA, 0);
447 list->uplevel(LIST_TIMES);
450 if (instruction->times > 1)
451 list->downlevel(LIST_TIMES);
452 return offset - start;
455 if (instruction->opcode == I_INCBIN) {
456 const char *fname = instruction->eops->stringval;
457 FILE *fp;
459 fp = fopen(fname, "rb");
460 if (!fp) {
461 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
462 fname);
463 } else if (fseek(fp, 0L, SEEK_END) < 0) {
464 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
465 fname);
466 fclose(fp);
467 } else {
468 static char buf[4096];
469 size_t t = instruction->times;
470 size_t base = 0;
471 size_t len;
473 len = ftell(fp);
474 if (instruction->eops->next) {
475 base = instruction->eops->next->offset;
476 len -= base;
477 if (instruction->eops->next->next &&
478 len > (size_t)instruction->eops->next->next->offset)
479 len = (size_t)instruction->eops->next->next->offset;
482 * Dummy call to list->output to give the offset to the
483 * listing module.
485 list->output(offset, NULL, OUT_RAWDATA, 0);
486 list->uplevel(LIST_INCBIN);
487 while (t--) {
488 size_t l;
490 fseek(fp, base, SEEK_SET);
491 l = len;
492 while (l > 0) {
493 int32_t m;
494 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
495 if (!m) {
497 * This shouldn't happen unless the file
498 * actually changes while we are reading
499 * it.
501 error(ERR_NONFATAL,
502 "`incbin': unexpected EOF while"
503 " reading file `%s'", fname);
504 t = 0; /* Try to exit cleanly */
505 break;
507 out(offset, segment, buf, OUT_RAWDATA, m,
508 NO_SEG, NO_SEG);
509 l -= m;
512 list->downlevel(LIST_INCBIN);
513 if (instruction->times > 1) {
515 * Dummy call to list->output to give the offset to the
516 * listing module.
518 list->output(offset, NULL, OUT_RAWDATA, 0);
519 list->uplevel(LIST_TIMES);
520 list->downlevel(LIST_TIMES);
522 fclose(fp);
523 return instruction->times * len;
525 return 0; /* if we're here, there's an error */
528 /* Check to see if we need an address-size prefix */
529 add_asp(instruction, bits);
531 m = find_match(&temp, instruction, segment, offset, bits);
533 if (m == MOK_GOOD) {
534 /* Matches! */
535 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
536 itimes = instruction->times;
537 if (insn_size < 0) /* shouldn't be, on pass two */
538 error(ERR_PANIC, "errors made it through from pass one");
539 else
540 while (itimes--) {
541 for (j = 0; j < MAXPREFIX; j++) {
542 uint8_t c = 0;
543 switch (instruction->prefixes[j]) {
544 case P_WAIT:
545 c = 0x9B;
546 break;
547 case P_LOCK:
548 c = 0xF0;
549 break;
550 case P_REPNE:
551 case P_REPNZ:
552 case P_XACQUIRE:
553 case P_BND:
554 c = 0xF2;
555 break;
556 case P_REPE:
557 case P_REPZ:
558 case P_REP:
559 case P_XRELEASE:
560 c = 0xF3;
561 break;
562 case R_CS:
563 if (bits == 64) {
564 error(ERR_WARNING | ERR_PASS2,
565 "cs segment base generated, but will be ignored in 64-bit mode");
567 c = 0x2E;
568 break;
569 case R_DS:
570 if (bits == 64) {
571 error(ERR_WARNING | ERR_PASS2,
572 "ds segment base generated, but will be ignored in 64-bit mode");
574 c = 0x3E;
575 break;
576 case R_ES:
577 if (bits == 64) {
578 error(ERR_WARNING | ERR_PASS2,
579 "es segment base generated, but will be ignored in 64-bit mode");
581 c = 0x26;
582 break;
583 case R_FS:
584 c = 0x64;
585 break;
586 case R_GS:
587 c = 0x65;
588 break;
589 case R_SS:
590 if (bits == 64) {
591 error(ERR_WARNING | ERR_PASS2,
592 "ss segment base generated, but will be ignored in 64-bit mode");
594 c = 0x36;
595 break;
596 case R_SEGR6:
597 case R_SEGR7:
598 error(ERR_NONFATAL,
599 "segr6 and segr7 cannot be used as prefixes");
600 break;
601 case P_A16:
602 if (bits == 64) {
603 error(ERR_NONFATAL,
604 "16-bit addressing is not supported "
605 "in 64-bit mode");
606 } else if (bits != 16)
607 c = 0x67;
608 break;
609 case P_A32:
610 if (bits != 32)
611 c = 0x67;
612 break;
613 case P_A64:
614 if (bits != 64) {
615 error(ERR_NONFATAL,
616 "64-bit addressing is only supported "
617 "in 64-bit mode");
619 break;
620 case P_ASP:
621 c = 0x67;
622 break;
623 case P_O16:
624 if (bits != 16)
625 c = 0x66;
626 break;
627 case P_O32:
628 if (bits == 16)
629 c = 0x66;
630 break;
631 case P_O64:
632 /* REX.W */
633 break;
634 case P_OSP:
635 c = 0x66;
636 break;
637 case P_EVEX:
638 /* EVEX */
639 break;
640 case P_none:
641 break;
642 default:
643 error(ERR_PANIC, "invalid instruction prefix");
645 if (c != 0) {
646 out(offset, segment, &c, OUT_RAWDATA, 1,
647 NO_SEG, NO_SEG);
648 offset++;
651 insn_end = offset + insn_size;
652 gencode(segment, offset, bits, instruction,
653 temp, insn_end);
654 offset += insn_size;
655 if (itimes > 0 && itimes == instruction->times - 1) {
657 * Dummy call to list->output to give the offset to the
658 * listing module.
660 list->output(offset, NULL, OUT_RAWDATA, 0);
661 list->uplevel(LIST_TIMES);
664 if (instruction->times > 1)
665 list->downlevel(LIST_TIMES);
666 return offset - start;
667 } else {
668 /* No match */
669 switch (m) {
670 case MERR_OPSIZEMISSING:
671 error(ERR_NONFATAL, "operation size not specified");
672 break;
673 case MERR_OPSIZEMISMATCH:
674 error(ERR_NONFATAL, "mismatch in operand sizes");
675 break;
676 case MERR_BRNUMMISMATCH:
677 error(ERR_NONFATAL,
678 "mismatch in the number of broadcasting elements");
679 break;
680 case MERR_BADCPU:
681 error(ERR_NONFATAL, "no instruction for this cpu level");
682 break;
683 case MERR_BADMODE:
684 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
685 bits);
686 break;
687 default:
688 error(ERR_NONFATAL,
689 "invalid combination of opcode and operands");
690 break;
693 return 0;
696 int64_t insn_size(int32_t segment, int64_t offset, int bits, iflags_t cp,
697 insn * instruction, efunc error)
699 const struct itemplate *temp;
700 enum match_result m;
702 errfunc = error; /* to pass to other functions */
703 cpu = cp;
705 if (instruction->opcode == I_none)
706 return 0;
708 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
709 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
710 instruction->opcode == I_DT || instruction->opcode == I_DO ||
711 instruction->opcode == I_DY) {
712 extop *e;
713 int32_t isize, osize, wsize;
715 isize = 0;
716 wsize = idata_bytes(instruction->opcode);
718 list_for_each(e, instruction->eops) {
719 int32_t align;
721 osize = 0;
722 if (e->type == EOT_DB_NUMBER) {
723 osize = 1;
724 warn_overflow_const(e->offset, wsize);
725 } else if (e->type == EOT_DB_STRING ||
726 e->type == EOT_DB_STRING_FREE)
727 osize = e->stringlen;
729 align = (-osize) % wsize;
730 if (align < 0)
731 align += wsize;
732 isize += osize + align;
734 return isize * instruction->times;
737 if (instruction->opcode == I_INCBIN) {
738 const char *fname = instruction->eops->stringval;
739 FILE *fp;
740 int64_t val = 0;
741 size_t len;
743 fp = fopen(fname, "rb");
744 if (!fp)
745 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
746 fname);
747 else if (fseek(fp, 0L, SEEK_END) < 0)
748 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
749 fname);
750 else {
751 len = ftell(fp);
752 if (instruction->eops->next) {
753 len -= instruction->eops->next->offset;
754 if (instruction->eops->next->next &&
755 len > (size_t)instruction->eops->next->next->offset) {
756 len = (size_t)instruction->eops->next->next->offset;
759 val = instruction->times * len;
761 if (fp)
762 fclose(fp);
763 return val;
766 /* Check to see if we need an address-size prefix */
767 add_asp(instruction, bits);
769 m = find_match(&temp, instruction, segment, offset, bits);
770 if (m == MOK_GOOD) {
771 /* we've matched an instruction. */
772 int64_t isize;
773 int j;
775 isize = calcsize(segment, offset, bits, instruction, temp);
776 if (isize < 0)
777 return -1;
778 for (j = 0; j < MAXPREFIX; j++) {
779 switch (instruction->prefixes[j]) {
780 case P_A16:
781 if (bits != 16)
782 isize++;
783 break;
784 case P_A32:
785 if (bits != 32)
786 isize++;
787 break;
788 case P_O16:
789 if (bits != 16)
790 isize++;
791 break;
792 case P_O32:
793 if (bits == 16)
794 isize++;
795 break;
796 case P_A64:
797 case P_O64:
798 case P_EVEX:
799 case P_none:
800 break;
801 default:
802 isize++;
803 break;
806 return isize * instruction->times;
807 } else {
808 return -1; /* didn't match any instruction */
812 static void bad_hle_warn(const insn * ins, uint8_t hleok)
814 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
815 enum whatwarn { w_none, w_lock, w_inval } ww;
816 static const enum whatwarn warn[2][4] =
818 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
819 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
821 unsigned int n;
823 n = (unsigned int)rep_pfx - P_XACQUIRE;
824 if (n > 1)
825 return; /* Not XACQUIRE/XRELEASE */
827 ww = warn[n][hleok];
828 if (!is_class(MEMORY, ins->oprs[0].type))
829 ww = w_inval; /* HLE requires operand 0 to be memory */
831 switch (ww) {
832 case w_none:
833 break;
835 case w_lock:
836 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
837 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
838 "%s with this instruction requires lock",
839 prefix_name(rep_pfx));
841 break;
843 case w_inval:
844 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
845 "%s invalid with this instruction",
846 prefix_name(rep_pfx));
847 break;
851 /* Common construct */
852 #define case3(x) case (x): case (x)+1: case (x)+2
853 #define case4(x) case3(x): case (x)+3
855 static int64_t calcsize(int32_t segment, int64_t offset, int bits,
856 insn * ins, const struct itemplate *temp)
858 const uint8_t *codes = temp->code;
859 int64_t length = 0;
860 uint8_t c;
861 int rex_mask = ~0;
862 int op1, op2;
863 struct operand *opx;
864 uint8_t opex = 0;
865 enum ea_type eat;
866 uint8_t hleok = 0;
867 bool lockcheck = true;
868 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
870 ins->rex = 0; /* Ensure REX is reset */
871 eat = EA_SCALAR; /* Expect a scalar EA */
872 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
874 if (ins->prefixes[PPS_OSIZE] == P_O64)
875 ins->rex |= REX_W;
877 (void)segment; /* Don't warn that this parameter is unused */
878 (void)offset; /* Don't warn that this parameter is unused */
880 while (*codes) {
881 c = *codes++;
882 op1 = (c & 3) + ((opex & 1) << 2);
883 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
884 opx = &ins->oprs[op1];
885 opex = 0; /* For the next iteration */
887 switch (c) {
888 case4(01):
889 codes += c, length += c;
890 break;
892 case3(05):
893 opex = c;
894 break;
896 case4(010):
897 ins->rex |=
898 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
899 codes++, length++;
900 break;
902 case4(014):
903 /* this is an index reg of MIB operand */
904 mib_index = opx->basereg;
905 break;
907 case4(020):
908 case4(024):
909 length++;
910 break;
912 case4(030):
913 length += 2;
914 break;
916 case4(034):
917 if (opx->type & (BITS16 | BITS32 | BITS64))
918 length += (opx->type & BITS16) ? 2 : 4;
919 else
920 length += (bits == 16) ? 2 : 4;
921 break;
923 case4(040):
924 length += 4;
925 break;
927 case4(044):
928 length += ins->addr_size >> 3;
929 break;
931 case4(050):
932 length++;
933 break;
935 case4(054):
936 length += 8; /* MOV reg64/imm */
937 break;
939 case4(060):
940 length += 2;
941 break;
943 case4(064):
944 if (opx->type & (BITS16 | BITS32 | BITS64))
945 length += (opx->type & BITS16) ? 2 : 4;
946 else
947 length += (bits == 16) ? 2 : 4;
948 break;
950 case4(070):
951 length += 4;
952 break;
954 case4(074):
955 length += 2;
956 break;
958 case 0172:
959 case 0173:
960 codes++;
961 length++;
962 break;
964 case4(0174):
965 length++;
966 break;
968 case4(0240):
969 ins->rex |= REX_EV;
970 ins->vexreg = regval(opx);
971 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
972 ins->vex_cm = *codes++;
973 ins->vex_wlp = *codes++;
974 ins->evex_tuple = (*codes++ - 0300);
975 break;
977 case 0250:
978 ins->rex |= REX_EV;
979 ins->vexreg = 0;
980 ins->vex_cm = *codes++;
981 ins->vex_wlp = *codes++;
982 ins->evex_tuple = (*codes++ - 0300);
983 break;
985 case4(0254):
986 length += 4;
987 break;
989 case4(0260):
990 ins->rex |= REX_V;
991 ins->vexreg = regval(opx);
992 ins->vex_cm = *codes++;
993 ins->vex_wlp = *codes++;
994 break;
996 case 0270:
997 ins->rex |= REX_V;
998 ins->vexreg = 0;
999 ins->vex_cm = *codes++;
1000 ins->vex_wlp = *codes++;
1001 break;
1003 case3(0271):
1004 hleok = c & 3;
1005 break;
1007 case4(0274):
1008 length++;
1009 break;
1011 case4(0300):
1012 break;
1014 case 0310:
1015 if (bits == 64)
1016 return -1;
1017 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
1018 break;
1020 case 0311:
1021 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
1022 break;
1024 case 0312:
1025 break;
1027 case 0313:
1028 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1029 has_prefix(ins, PPS_ASIZE, P_A32))
1030 return -1;
1031 break;
1033 case4(0314):
1034 break;
1036 case 0320:
1038 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1039 if (pfx == P_O16)
1040 break;
1041 if (pfx != P_none)
1042 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1043 else
1044 ins->prefixes[PPS_OSIZE] = P_O16;
1045 break;
1048 case 0321:
1050 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1051 if (pfx == P_O32)
1052 break;
1053 if (pfx != P_none)
1054 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1055 else
1056 ins->prefixes[PPS_OSIZE] = P_O32;
1057 break;
1060 case 0322:
1061 break;
1063 case 0323:
1064 rex_mask &= ~REX_W;
1065 break;
1067 case 0324:
1068 ins->rex |= REX_W;
1069 break;
1071 case 0325:
1072 ins->rex |= REX_NH;
1073 break;
1075 case 0326:
1076 break;
1078 case 0330:
1079 codes++, length++;
1080 break;
1082 case 0331:
1083 break;
1085 case 0332:
1086 case 0333:
1087 length++;
1088 break;
1090 case 0334:
1091 ins->rex |= REX_L;
1092 break;
1094 case 0335:
1095 break;
1097 case 0336:
1098 if (!ins->prefixes[PPS_REP])
1099 ins->prefixes[PPS_REP] = P_REP;
1100 break;
1102 case 0337:
1103 if (!ins->prefixes[PPS_REP])
1104 ins->prefixes[PPS_REP] = P_REPNE;
1105 break;
1107 case 0340:
1108 if (ins->oprs[0].segment != NO_SEG)
1109 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1110 " quantity of BSS space");
1111 else
1112 length += ins->oprs[0].offset;
1113 break;
1115 case 0341:
1116 if (!ins->prefixes[PPS_WAIT])
1117 ins->prefixes[PPS_WAIT] = P_WAIT;
1118 break;
1120 case 0360:
1121 break;
1123 case 0361:
1124 length++;
1125 break;
1127 case 0364:
1128 case 0365:
1129 break;
1131 case 0366:
1132 case 0367:
1133 length++;
1134 break;
1136 case 0370:
1137 case 0371:
1138 break;
1140 case 0373:
1141 length++;
1142 break;
1144 case 0374:
1145 eat = EA_XMMVSIB;
1146 break;
1148 case 0375:
1149 eat = EA_YMMVSIB;
1150 break;
1152 case 0376:
1153 eat = EA_ZMMVSIB;
1154 break;
1156 case4(0100):
1157 case4(0110):
1158 case4(0120):
1159 case4(0130):
1160 case4(0200):
1161 case4(0204):
1162 case4(0210):
1163 case4(0214):
1164 case4(0220):
1165 case4(0224):
1166 case4(0230):
1167 case4(0234):
1169 ea ea_data;
1170 int rfield;
1171 opflags_t rflags;
1172 struct operand *opy = &ins->oprs[op2];
1173 struct operand *op_er_sae;
1175 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
1177 if (c <= 0177) {
1178 /* pick rfield from operand b (opx) */
1179 rflags = regflag(opx);
1180 rfield = nasm_regvals[opx->basereg];
1181 } else {
1182 rflags = 0;
1183 rfield = c & 7;
1186 /* EVEX.b1 : evex_brerop contains the operand position */
1187 op_er_sae = (ins->evex_brerop >= 0 ?
1188 &ins->oprs[ins->evex_brerop] : NULL);
1190 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1191 /* set EVEX.b */
1192 ins->evex_p[2] |= EVEX_P2B;
1193 if (op_er_sae->decoflags & ER) {
1194 /* set EVEX.RC (rounding control) */
1195 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1196 & EVEX_P2RC;
1198 } else {
1199 /* set EVEX.L'L (vector length) */
1200 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
1201 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
1202 if (opy->decoflags & BRDCAST_MASK) {
1203 /* set EVEX.b */
1204 ins->evex_p[2] |= EVEX_P2B;
1209 * if a separate form of MIB (ICC style) is used,
1210 * the index reg info is merged into mem operand
1212 if (mib_index != R_none) {
1213 opy->indexreg = mib_index;
1214 opy->scale = 1;
1215 opy->hintbase = mib_index;
1216 opy->hinttype = EAH_NOTBASE;
1220 * only for mib operands, make a single reg index [reg*1].
1221 * gas uses this form to explicitly denote index register.
1223 if ((temp->flags & IF_MIB) &&
1224 (opy->indexreg == -1 && opy->hintbase == opy->basereg &&
1225 opy->hinttype == EAH_NOTBASE)) {
1226 opy->indexreg = opy->basereg;
1227 opy->basereg = -1;
1228 opy->scale = 1;
1231 if (process_ea(opy, &ea_data, bits,
1232 rfield, rflags, ins) != eat) {
1233 errfunc(ERR_NONFATAL, "invalid effective address");
1234 return -1;
1235 } else {
1236 ins->rex |= ea_data.rex;
1237 length += ea_data.size;
1240 break;
1242 default:
1243 errfunc(ERR_PANIC, "internal instruction table corrupt"
1244 ": instruction code \\%o (0x%02X) given", c, c);
1245 break;
1249 ins->rex &= rex_mask;
1251 if (ins->rex & REX_NH) {
1252 if (ins->rex & REX_H) {
1253 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1254 return -1;
1256 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
1259 if (ins->rex & (REX_V | REX_EV)) {
1260 int bad32 = REX_R|REX_W|REX_X|REX_B;
1262 if (ins->rex & REX_H) {
1263 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
1264 return -1;
1266 switch (ins->vex_wlp & 060) {
1267 case 000:
1268 case 040:
1269 ins->rex &= ~REX_W;
1270 break;
1271 case 020:
1272 ins->rex |= REX_W;
1273 bad32 &= ~REX_W;
1274 break;
1275 case 060:
1276 /* Follow REX_W */
1277 break;
1280 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
1281 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1282 return -1;
1283 } else if (!(ins->rex & REX_EV) &&
1284 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1285 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1286 return -1;
1288 if (ins->rex & REX_EV)
1289 length += 4;
1290 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
1291 length += 3;
1292 else
1293 length += 2;
1294 } else if (ins->rex & REX_REAL) {
1295 if (ins->rex & REX_H) {
1296 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1297 return -1;
1298 } else if (bits == 64) {
1299 length++;
1300 } else if ((ins->rex & REX_L) &&
1301 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1302 cpu >= IF_X86_64) {
1303 /* LOCK-as-REX.R */
1304 assert_no_prefix(ins, PPS_LOCK);
1305 lockcheck = false; /* Already errored, no need for warning */
1306 length++;
1307 } else {
1308 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1309 return -1;
1313 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
1314 (!(temp->flags & IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
1315 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
1316 "instruction is not lockable");
1319 bad_hle_warn(ins, hleok);
1321 return length;
1324 static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1326 if (bits == 64) {
1327 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
1328 ins->rex = (ins->rex & REX_REAL) | REX_P;
1329 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1330 ins->rex = 0;
1331 return 1;
1335 return 0;
1338 static void gencode(int32_t segment, int64_t offset, int bits,
1339 insn * ins, const struct itemplate *temp,
1340 int64_t insn_end)
1342 uint8_t c;
1343 uint8_t bytes[4];
1344 int64_t size;
1345 int64_t data;
1346 int op1, op2;
1347 struct operand *opx;
1348 const uint8_t *codes = temp->code;
1349 uint8_t opex = 0;
1350 enum ea_type eat = EA_SCALAR;
1352 while (*codes) {
1353 c = *codes++;
1354 op1 = (c & 3) + ((opex & 1) << 2);
1355 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1356 opx = &ins->oprs[op1];
1357 opex = 0; /* For the next iteration */
1359 switch (c) {
1360 case 01:
1361 case 02:
1362 case 03:
1363 case 04:
1364 offset += emit_rex(ins, segment, offset, bits);
1365 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
1366 codes += c;
1367 offset += c;
1368 break;
1370 case 05:
1371 case 06:
1372 case 07:
1373 opex = c;
1374 break;
1376 case4(010):
1377 offset += emit_rex(ins, segment, offset, bits);
1378 bytes[0] = *codes++ + (regval(opx) & 7);
1379 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1380 offset += 1;
1381 break;
1383 case4(014):
1384 break;
1386 case4(020):
1387 if (opx->offset < -256 || opx->offset > 255) {
1388 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1389 "byte value exceeds bounds");
1391 out_imm8(offset, segment, opx);
1392 offset += 1;
1393 break;
1395 case4(024):
1396 if (opx->offset < 0 || opx->offset > 255)
1397 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1398 "unsigned byte value exceeds bounds");
1399 out_imm8(offset, segment, opx);
1400 offset += 1;
1401 break;
1403 case4(030):
1404 warn_overflow_opd(opx, 2);
1405 data = opx->offset;
1406 out(offset, segment, &data, OUT_ADDRESS, 2,
1407 opx->segment, opx->wrt);
1408 offset += 2;
1409 break;
1411 case4(034):
1412 if (opx->type & (BITS16 | BITS32))
1413 size = (opx->type & BITS16) ? 2 : 4;
1414 else
1415 size = (bits == 16) ? 2 : 4;
1416 warn_overflow_opd(opx, size);
1417 data = opx->offset;
1418 out(offset, segment, &data, OUT_ADDRESS, size,
1419 opx->segment, opx->wrt);
1420 offset += size;
1421 break;
1423 case4(040):
1424 warn_overflow_opd(opx, 4);
1425 data = opx->offset;
1426 out(offset, segment, &data, OUT_ADDRESS, 4,
1427 opx->segment, opx->wrt);
1428 offset += 4;
1429 break;
1431 case4(044):
1432 data = opx->offset;
1433 size = ins->addr_size >> 3;
1434 warn_overflow_opd(opx, size);
1435 out(offset, segment, &data, OUT_ADDRESS, size,
1436 opx->segment, opx->wrt);
1437 offset += size;
1438 break;
1440 case4(050):
1441 if (opx->segment != segment) {
1442 data = opx->offset;
1443 out(offset, segment, &data,
1444 OUT_REL1ADR, insn_end - offset,
1445 opx->segment, opx->wrt);
1446 } else {
1447 data = opx->offset - insn_end;
1448 if (data > 127 || data < -128)
1449 errfunc(ERR_NONFATAL, "short jump is out of range");
1450 out(offset, segment, &data,
1451 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1453 offset += 1;
1454 break;
1456 case4(054):
1457 data = (int64_t)opx->offset;
1458 out(offset, segment, &data, OUT_ADDRESS, 8,
1459 opx->segment, opx->wrt);
1460 offset += 8;
1461 break;
1463 case4(060):
1464 if (opx->segment != segment) {
1465 data = opx->offset;
1466 out(offset, segment, &data,
1467 OUT_REL2ADR, insn_end - offset,
1468 opx->segment, opx->wrt);
1469 } else {
1470 data = opx->offset - insn_end;
1471 out(offset, segment, &data,
1472 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
1474 offset += 2;
1475 break;
1477 case4(064):
1478 if (opx->type & (BITS16 | BITS32 | BITS64))
1479 size = (opx->type & BITS16) ? 2 : 4;
1480 else
1481 size = (bits == 16) ? 2 : 4;
1482 if (opx->segment != segment) {
1483 data = opx->offset;
1484 out(offset, segment, &data,
1485 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1486 insn_end - offset, opx->segment, opx->wrt);
1487 } else {
1488 data = opx->offset - insn_end;
1489 out(offset, segment, &data,
1490 OUT_ADDRESS, size, NO_SEG, NO_SEG);
1492 offset += size;
1493 break;
1495 case4(070):
1496 if (opx->segment != segment) {
1497 data = opx->offset;
1498 out(offset, segment, &data,
1499 OUT_REL4ADR, insn_end - offset,
1500 opx->segment, opx->wrt);
1501 } else {
1502 data = opx->offset - insn_end;
1503 out(offset, segment, &data,
1504 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
1506 offset += 4;
1507 break;
1509 case4(074):
1510 if (opx->segment == NO_SEG)
1511 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1512 " relocatable");
1513 data = 0;
1514 out(offset, segment, &data, OUT_ADDRESS, 2,
1515 outfmt->segbase(1 + opx->segment),
1516 opx->wrt);
1517 offset += 2;
1518 break;
1520 case 0172:
1521 c = *codes++;
1522 opx = &ins->oprs[c >> 3];
1523 bytes[0] = nasm_regvals[opx->basereg] << 4;
1524 opx = &ins->oprs[c & 7];
1525 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1526 errfunc(ERR_NONFATAL,
1527 "non-absolute expression not permitted as argument %d",
1528 c & 7);
1529 } else {
1530 if (opx->offset & ~15) {
1531 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1532 "four-bit argument exceeds bounds");
1534 bytes[0] |= opx->offset & 15;
1536 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1537 offset++;
1538 break;
1540 case 0173:
1541 c = *codes++;
1542 opx = &ins->oprs[c >> 4];
1543 bytes[0] = nasm_regvals[opx->basereg] << 4;
1544 bytes[0] |= c & 15;
1545 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1546 offset++;
1547 break;
1549 case4(0174):
1550 bytes[0] = nasm_regvals[opx->basereg] << 4;
1551 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1552 offset++;
1553 break;
1555 case4(0254):
1556 data = opx->offset;
1557 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1558 (int32_t)data != (int64_t)data) {
1559 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1560 "signed dword immediate exceeds bounds");
1562 out(offset, segment, &data, OUT_ADDRESS, 4,
1563 opx->segment, opx->wrt);
1564 offset += 4;
1565 break;
1567 case4(0240):
1568 case 0250:
1569 codes += 3;
1570 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1571 EVEX_P2Z | EVEX_P2AAA, 2);
1572 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1573 bytes[0] = 0x62;
1574 /* EVEX.X can be set by either REX or EVEX for different reasons */
1575 bytes[1] = ((((ins->rex & 7) << 5) |
1576 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1577 (ins->vex_cm & 3);
1578 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1579 ((~ins->vexreg & 15) << 3) |
1580 (1 << 2) | (ins->vex_wlp & 3);
1581 bytes[3] = ins->evex_p[2];
1582 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1583 offset += 4;
1584 break;
1586 case4(0260):
1587 case 0270:
1588 codes += 2;
1589 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1590 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1591 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1592 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
1593 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
1594 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1595 offset += 3;
1596 } else {
1597 bytes[0] = 0xc5;
1598 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
1599 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
1600 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1601 offset += 2;
1603 break;
1605 case 0271:
1606 case 0272:
1607 case 0273:
1608 break;
1610 case4(0274):
1612 uint64_t uv, um;
1613 int s;
1615 if (ins->rex & REX_W)
1616 s = 64;
1617 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1618 s = 16;
1619 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1620 s = 32;
1621 else
1622 s = bits;
1624 um = (uint64_t)2 << (s-1);
1625 uv = opx->offset;
1627 if (uv > 127 && uv < (uint64_t)-128 &&
1628 (uv < um-128 || uv > um-1)) {
1629 /* If this wasn't explicitly byte-sized, warn as though we
1630 * had fallen through to the imm16/32/64 case.
1632 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1633 "%s value exceeds bounds",
1634 (opx->type & BITS8) ? "signed byte" :
1635 s == 16 ? "word" :
1636 s == 32 ? "dword" :
1637 "signed dword");
1639 if (opx->segment != NO_SEG) {
1640 data = uv;
1641 out(offset, segment, &data, OUT_ADDRESS, 1,
1642 opx->segment, opx->wrt);
1643 } else {
1644 bytes[0] = uv;
1645 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1646 NO_SEG);
1648 offset += 1;
1649 break;
1652 case4(0300):
1653 break;
1655 case 0310:
1656 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
1657 *bytes = 0x67;
1658 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1659 offset += 1;
1660 } else
1661 offset += 0;
1662 break;
1664 case 0311:
1665 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
1666 *bytes = 0x67;
1667 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1668 offset += 1;
1669 } else
1670 offset += 0;
1671 break;
1673 case 0312:
1674 break;
1676 case 0313:
1677 ins->rex = 0;
1678 break;
1680 case4(0314):
1681 break;
1683 case 0320:
1684 case 0321:
1685 break;
1687 case 0322:
1688 case 0323:
1689 break;
1691 case 0324:
1692 ins->rex |= REX_W;
1693 break;
1695 case 0325:
1696 break;
1698 case 0326:
1699 break;
1701 case 0330:
1702 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
1703 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1704 offset += 1;
1705 break;
1707 case 0331:
1708 break;
1710 case 0332:
1711 case 0333:
1712 *bytes = c - 0332 + 0xF2;
1713 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1714 offset += 1;
1715 break;
1717 case 0334:
1718 if (ins->rex & REX_R) {
1719 *bytes = 0xF0;
1720 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1721 offset += 1;
1723 ins->rex &= ~(REX_L|REX_R);
1724 break;
1726 case 0335:
1727 break;
1729 case 0336:
1730 case 0337:
1731 break;
1733 case 0340:
1734 if (ins->oprs[0].segment != NO_SEG)
1735 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1736 else {
1737 int64_t size = ins->oprs[0].offset;
1738 if (size > 0)
1739 out(offset, segment, NULL,
1740 OUT_RESERVE, size, NO_SEG, NO_SEG);
1741 offset += size;
1743 break;
1745 case 0341:
1746 break;
1748 case 0360:
1749 break;
1751 case 0361:
1752 bytes[0] = 0x66;
1753 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1754 offset += 1;
1755 break;
1757 case 0364:
1758 case 0365:
1759 break;
1761 case 0366:
1762 case 0367:
1763 *bytes = c - 0366 + 0x66;
1764 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1765 offset += 1;
1766 break;
1768 case3(0370):
1769 break;
1771 case 0373:
1772 *bytes = bits == 16 ? 3 : 5;
1773 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1774 offset += 1;
1775 break;
1777 case 0374:
1778 eat = EA_XMMVSIB;
1779 break;
1781 case 0375:
1782 eat = EA_YMMVSIB;
1783 break;
1785 case 0376:
1786 eat = EA_ZMMVSIB;
1787 break;
1789 case4(0100):
1790 case4(0110):
1791 case4(0120):
1792 case4(0130):
1793 case4(0200):
1794 case4(0204):
1795 case4(0210):
1796 case4(0214):
1797 case4(0220):
1798 case4(0224):
1799 case4(0230):
1800 case4(0234):
1802 ea ea_data;
1803 int rfield;
1804 opflags_t rflags;
1805 uint8_t *p;
1806 int32_t s;
1807 struct operand *opy = &ins->oprs[op2];
1809 if (c <= 0177) {
1810 /* pick rfield from operand b (opx) */
1811 rflags = regflag(opx);
1812 rfield = nasm_regvals[opx->basereg];
1813 } else {
1814 /* rfield is constant */
1815 rflags = 0;
1816 rfield = c & 7;
1819 if (process_ea(opy, &ea_data, bits,
1820 rfield, rflags, ins) != eat)
1821 errfunc(ERR_NONFATAL, "invalid effective address");
1823 p = bytes;
1824 *p++ = ea_data.modrm;
1825 if (ea_data.sib_present)
1826 *p++ = ea_data.sib;
1828 s = p - bytes;
1829 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
1832 * Make sure the address gets the right offset in case
1833 * the line breaks in the .lst file (BR 1197827)
1835 offset += s;
1836 s = 0;
1838 switch (ea_data.bytes) {
1839 case 0:
1840 break;
1841 case 1:
1842 case 2:
1843 case 4:
1844 case 8:
1845 /* use compressed displacement, if available */
1846 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
1847 s += ea_data.bytes;
1848 if (ea_data.rip) {
1849 if (opy->segment == segment) {
1850 data -= insn_end;
1851 if (overflow_signed(data, ea_data.bytes))
1852 warn_overflow(ERR_PASS2, ea_data.bytes);
1853 out(offset, segment, &data, OUT_ADDRESS,
1854 ea_data.bytes, NO_SEG, NO_SEG);
1855 } else {
1856 /* overflow check in output/linker? */
1857 out(offset, segment, &data, OUT_REL4ADR,
1858 insn_end - offset, opy->segment, opy->wrt);
1860 } else {
1861 if (overflow_general(data, ins->addr_size >> 3) ||
1862 signed_bits(data, ins->addr_size) !=
1863 signed_bits(data, ea_data.bytes * 8))
1864 warn_overflow(ERR_PASS2, ea_data.bytes);
1866 out(offset, segment, &data, OUT_ADDRESS,
1867 ea_data.bytes, opy->segment, opy->wrt);
1869 break;
1870 default:
1871 /* Impossible! */
1872 errfunc(ERR_PANIC,
1873 "Invalid amount of bytes (%d) for offset?!",
1874 ea_data.bytes);
1875 break;
1877 offset += s;
1879 break;
1881 default:
1882 errfunc(ERR_PANIC, "internal instruction table corrupt"
1883 ": instruction code \\%o (0x%02X) given", c, c);
1884 break;
1889 static opflags_t regflag(const operand * o)
1891 if (!is_register(o->basereg))
1892 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
1893 return nasm_reg_flags[o->basereg];
1896 static int32_t regval(const operand * o)
1898 if (!is_register(o->basereg))
1899 errfunc(ERR_PANIC, "invalid operand passed to regval()");
1900 return nasm_regvals[o->basereg];
1903 static int op_rexflags(const operand * o, int mask)
1905 opflags_t flags;
1906 int val;
1908 if (!is_register(o->basereg))
1909 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
1911 flags = nasm_reg_flags[o->basereg];
1912 val = nasm_regvals[o->basereg];
1914 return rexflags(val, flags, mask);
1917 static int rexflags(int val, opflags_t flags, int mask)
1919 int rex = 0;
1921 if (val >= 0 && (val & 8))
1922 rex |= REX_B|REX_X|REX_R;
1923 if (flags & BITS64)
1924 rex |= REX_W;
1925 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1926 rex |= REX_H;
1927 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1928 rex |= REX_P;
1930 return rex & mask;
1933 static int evexflags(int val, decoflags_t deco,
1934 int mask, uint8_t byte)
1936 int evex = 0;
1938 switch (byte) {
1939 case 0:
1940 if (val >= 0 && (val & 16))
1941 evex |= (EVEX_P0RP | EVEX_P0X);
1942 break;
1943 case 2:
1944 if (val >= 0 && (val & 16))
1945 evex |= EVEX_P2VP;
1946 if (deco & Z)
1947 evex |= EVEX_P2Z;
1948 if (deco & OPMASK_MASK)
1949 evex |= deco & EVEX_P2AAA;
1950 break;
1952 return evex & mask;
1955 static int op_evexflags(const operand * o, int mask, uint8_t byte)
1957 int val;
1959 if (!is_register(o->basereg))
1960 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
1962 val = nasm_regvals[o->basereg];
1964 return evexflags(val, o->decoflags, mask, byte);
1967 static enum match_result find_match(const struct itemplate **tempp,
1968 insn *instruction,
1969 int32_t segment, int64_t offset, int bits)
1971 const struct itemplate *temp;
1972 enum match_result m, merr;
1973 opflags_t xsizeflags[MAX_OPERANDS];
1974 bool opsizemissing = false;
1975 int8_t broadcast = instruction->evex_brerop;
1976 int i;
1978 /* broadcasting uses a different data element size */
1979 for (i = 0; i < instruction->operands; i++)
1980 if (i == broadcast)
1981 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
1982 else
1983 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
1985 merr = MERR_INVALOP;
1987 for (temp = nasm_instructions[instruction->opcode];
1988 temp->opcode != I_none; temp++) {
1989 m = matches(temp, instruction, bits);
1990 if (m == MOK_JUMP) {
1991 if (jmp_match(segment, offset, bits, instruction, temp))
1992 m = MOK_GOOD;
1993 else
1994 m = MERR_INVALOP;
1995 } else if (m == MERR_OPSIZEMISSING &&
1996 (temp->flags & IF_SMASK) != IF_SX) {
1998 * Missing operand size and a candidate for fuzzy matching...
2000 for (i = 0; i < temp->operands; i++)
2001 if (i == broadcast)
2002 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2003 else
2004 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
2005 opsizemissing = true;
2007 if (m > merr)
2008 merr = m;
2009 if (merr == MOK_GOOD)
2010 goto done;
2013 /* No match, but see if we can get a fuzzy operand size match... */
2014 if (!opsizemissing)
2015 goto done;
2017 for (i = 0; i < instruction->operands; i++) {
2019 * We ignore extrinsic operand sizes on registers, so we should
2020 * never try to fuzzy-match on them. This also resolves the case
2021 * when we have e.g. "xmmrm128" in two different positions.
2023 if (is_class(REGISTER, instruction->oprs[i].type))
2024 continue;
2026 /* This tests if xsizeflags[i] has more than one bit set */
2027 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2028 goto done; /* No luck */
2030 if (i == broadcast) {
2031 instruction->oprs[i].decoflags |= xsizeflags[i];
2032 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2033 BITS32 : BITS64);
2034 } else {
2035 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
2039 /* Try matching again... */
2040 for (temp = nasm_instructions[instruction->opcode];
2041 temp->opcode != I_none; temp++) {
2042 m = matches(temp, instruction, bits);
2043 if (m == MOK_JUMP) {
2044 if (jmp_match(segment, offset, bits, instruction, temp))
2045 m = MOK_GOOD;
2046 else
2047 m = MERR_INVALOP;
2049 if (m > merr)
2050 merr = m;
2051 if (merr == MOK_GOOD)
2052 goto done;
2055 done:
2056 *tempp = temp;
2057 return merr;
2060 static enum match_result matches(const struct itemplate *itemp,
2061 insn *instruction, int bits)
2063 opflags_t size[MAX_OPERANDS], asize;
2064 bool opsizemissing = false;
2065 int i, oprs;
2068 * Check the opcode
2070 if (itemp->opcode != instruction->opcode)
2071 return MERR_INVALOP;
2074 * Count the operands
2076 if (itemp->operands != instruction->operands)
2077 return MERR_INVALOP;
2080 * Is it legal?
2082 if (!(optimizing > 0) && (itemp->flags & IF_OPT))
2083 return MERR_INVALOP;
2086 * Check that no spurious colons or TOs are present
2088 for (i = 0; i < itemp->operands; i++)
2089 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
2090 return MERR_INVALOP;
2093 * Process size flags
2095 switch (itemp->flags & IF_SMASK) {
2096 case IF_SB:
2097 asize = BITS8;
2098 break;
2099 case IF_SW:
2100 asize = BITS16;
2101 break;
2102 case IF_SD:
2103 asize = BITS32;
2104 break;
2105 case IF_SQ:
2106 asize = BITS64;
2107 break;
2108 case IF_SO:
2109 asize = BITS128;
2110 break;
2111 case IF_SY:
2112 asize = BITS256;
2113 break;
2114 case IF_SZ:
2115 asize = BITS512;
2116 break;
2117 case IF_SIZE:
2118 switch (bits) {
2119 case 16:
2120 asize = BITS16;
2121 break;
2122 case 32:
2123 asize = BITS32;
2124 break;
2125 case 64:
2126 asize = BITS64;
2127 break;
2128 default:
2129 asize = 0;
2130 break;
2132 break;
2133 default:
2134 asize = 0;
2135 break;
2138 if (itemp->flags & IF_ARMASK) {
2139 /* S- flags only apply to a specific operand */
2140 i = ((itemp->flags & IF_ARMASK) >> IF_ARSHFT) - 1;
2141 memset(size, 0, sizeof size);
2142 size[i] = asize;
2143 } else {
2144 /* S- flags apply to all operands */
2145 for (i = 0; i < MAX_OPERANDS; i++)
2146 size[i] = asize;
2150 * Check that the operand flags all match up,
2151 * it's a bit tricky so lets be verbose:
2153 * 1) Find out the size of operand. If instruction
2154 * doesn't have one specified -- we're trying to
2155 * guess it either from template (IF_S* flag) or
2156 * from code bits.
2158 * 2) If template operand do not match the instruction OR
2159 * template has an operand size specified AND this size differ
2160 * from which instruction has (perhaps we got it from code bits)
2161 * we are:
2162 * a) Check that only size of instruction and operand is differ
2163 * other characteristics do match
2164 * b) Perhaps it's a register specified in instruction so
2165 * for such a case we just mark that operand as "size
2166 * missing" and this will turn on fuzzy operand size
2167 * logic facility (handled by a caller)
2169 for (i = 0; i < itemp->operands; i++) {
2170 opflags_t type = instruction->oprs[i].type;
2171 decoflags_t deco = instruction->oprs[i].decoflags;
2172 bool is_broadcast = deco & BRDCAST_MASK;
2173 uint8_t brcast_num = 0;
2174 opflags_t template_opsize, insn_opsize;
2176 if (!(type & SIZE_MASK))
2177 type |= size[i];
2179 insn_opsize = type & SIZE_MASK;
2180 if (!is_broadcast) {
2181 template_opsize = itemp->opd[i] & SIZE_MASK;
2182 } else {
2183 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2185 * when broadcasting, the element size depends on
2186 * the instruction type. decorator flag should match.
2189 if (deco_brsize) {
2190 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
2191 /* calculate the proper number : {1to<brcast_num>} */
2192 brcast_num = (itemp->opd[i] & SIZE_MASK) / BITS128 *
2193 BITS64 / template_opsize * 2;
2194 } else {
2195 template_opsize = 0;
2199 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
2200 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
2201 return MERR_INVALOP;
2202 } else if (template_opsize) {
2203 if (template_opsize != insn_opsize) {
2204 if (insn_opsize) {
2205 return MERR_INVALOP;
2206 } else if (!is_class(REGISTER, type)) {
2208 * Note: we don't honor extrinsic operand sizes for registers,
2209 * so "missing operand size" for a register should be
2210 * considered a wildcard match rather than an error.
2212 opsizemissing = true;
2214 } else if (is_broadcast &&
2215 (brcast_num !=
2216 (8U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
2218 * broadcasting opsize matches but the number of repeated memory
2219 * element does not match.
2220 * if 64b double precision float is broadcasted to zmm (512b),
2221 * broadcasting decorator must be {1to8}.
2223 return MERR_BRNUMMISMATCH;
2225 } else if (is_register(instruction->oprs[i].basereg) &&
2226 nasm_regvals[instruction->oprs[i].basereg] >= 16 &&
2227 !(itemp->flags & IF_AVX512)) {
2228 return MERR_ENCMISMATCH;
2229 } else if (instruction->prefixes[PPS_EVEX] &&
2230 !(itemp->flags & IF_AVX512)) {
2231 return MERR_ENCMISMATCH;
2235 if (opsizemissing)
2236 return MERR_OPSIZEMISSING;
2239 * Check operand sizes
2241 if (itemp->flags & (IF_SM | IF_SM2)) {
2242 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
2243 for (i = 0; i < oprs; i++) {
2244 asize = itemp->opd[i] & SIZE_MASK;
2245 if (asize) {
2246 for (i = 0; i < oprs; i++)
2247 size[i] = asize;
2248 break;
2251 } else {
2252 oprs = itemp->operands;
2255 for (i = 0; i < itemp->operands; i++) {
2256 if (!(itemp->opd[i] & SIZE_MASK) &&
2257 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
2258 return MERR_OPSIZEMISMATCH;
2262 * Check template is okay at the set cpu level
2264 if (((itemp->flags & IF_PLEVEL) > cpu))
2265 return MERR_BADCPU;
2268 * Verify the appropriate long mode flag.
2270 if ((itemp->flags & (bits == 64 ? IF_NOLONG : IF_LONG)))
2271 return MERR_BADMODE;
2274 * If we have a HLE prefix, look for the NOHLE flag
2276 if ((itemp->flags & IF_NOHLE) &&
2277 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2278 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2279 return MERR_BADHLE;
2282 * Check if special handling needed for Jumps
2284 if ((itemp->code[0] & ~1) == 0370)
2285 return MOK_JUMP;
2288 * Check if BND prefix is allowed
2290 if ((IF_BND & ~itemp->flags) &&
2291 has_prefix(instruction, PPS_REP, P_BND))
2292 return MERR_BADBND;
2294 return MOK_GOOD;
2298 * Check if ModR/M.mod should/can be 01.
2299 * - EAF_BYTEOFFS is set
2300 * - offset can fit in a byte when EVEX is not used
2301 * - offset can be compressed when EVEX is used
2303 #define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2304 (o >= -128 && o <= 127 && \
2305 seg == NO_SEG && !forw_ref && \
2306 !(input->eaflags & EAF_WORDOFFS) && \
2307 !(ins->rex & REX_EV)) || \
2308 (ins->rex & REX_EV && \
2309 is_disp8n(input, ins, &output->disp8)))
2311 static enum ea_type process_ea(operand *input, ea *output, int bits,
2312 int rfield, opflags_t rflags, insn *ins)
2314 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
2315 int addrbits = ins->addr_size;
2317 output->type = EA_SCALAR;
2318 output->rip = false;
2319 output->disp8 = 0;
2321 /* REX flags for the rfield operand */
2322 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
2323 /* EVEX.R' flag for the REG operand */
2324 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
2326 if (is_class(REGISTER, input->type)) {
2328 * It's a direct register.
2330 if (!is_register(input->basereg))
2331 goto err;
2333 if (!is_reg_class(REG_EA, input->basereg))
2334 goto err;
2336 /* broadcasting is not available with a direct register operand. */
2337 if (input->decoflags & BRDCAST_MASK) {
2338 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2339 goto err;
2342 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
2343 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
2344 output->sib_present = false; /* no SIB necessary */
2345 output->bytes = 0; /* no offset necessary either */
2346 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2347 } else {
2349 * It's a memory reference.
2352 /* Embedded rounding or SAE is not available with a mem ref operand. */
2353 if (input->decoflags & (ER | SAE)) {
2354 nasm_error(ERR_NONFATAL,
2355 "Embedded rounding is available only with reg-reg op.");
2356 return -1;
2359 if (input->basereg == -1 &&
2360 (input->indexreg == -1 || input->scale == 0)) {
2362 * It's a pure offset.
2364 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2365 input->segment == NO_SEG) {
2366 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2367 input->type &= ~IP_REL;
2368 input->type |= MEMORY;
2371 if (input->eaflags & EAF_BYTEOFFS ||
2372 (input->eaflags & EAF_WORDOFFS &&
2373 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2374 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2377 if (bits == 64 && (~input->type & IP_REL)) {
2378 output->sib_present = true;
2379 output->sib = GEN_SIB(0, 4, 5);
2380 output->bytes = 4;
2381 output->modrm = GEN_MODRM(0, rfield, 4);
2382 output->rip = false;
2383 } else {
2384 output->sib_present = false;
2385 output->bytes = (addrbits != 16 ? 4 : 2);
2386 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2387 output->rip = bits == 64;
2389 } else {
2391 * It's an indirection.
2393 int i = input->indexreg, b = input->basereg, s = input->scale;
2394 int32_t seg = input->segment;
2395 int hb = input->hintbase, ht = input->hinttype;
2396 int t, it, bt; /* register numbers */
2397 opflags_t x, ix, bx; /* register flags */
2399 if (s == 0)
2400 i = -1; /* make this easy, at least */
2402 if (is_register(i)) {
2403 it = nasm_regvals[i];
2404 ix = nasm_reg_flags[i];
2405 } else {
2406 it = -1;
2407 ix = 0;
2410 if (is_register(b)) {
2411 bt = nasm_regvals[b];
2412 bx = nasm_reg_flags[b];
2413 } else {
2414 bt = -1;
2415 bx = 0;
2418 /* if either one are a vector register... */
2419 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
2420 opflags_t sok = BITS32 | BITS64;
2421 int32_t o = input->offset;
2422 int mod, scale, index, base;
2425 * For a vector SIB, one has to be a vector and the other,
2426 * if present, a GPR. The vector must be the index operand.
2428 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
2429 if (s == 0)
2430 s = 1;
2431 else if (s != 1)
2432 goto err;
2434 t = bt, bt = it, it = t;
2435 x = bx, bx = ix, ix = x;
2438 if (bt != -1) {
2439 if (REG_GPR & ~bx)
2440 goto err;
2441 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2442 sok &= bx;
2443 else
2444 goto err;
2448 * While we're here, ensure the user didn't specify
2449 * WORD or QWORD
2451 if (input->disp_size == 16 || input->disp_size == 64)
2452 goto err;
2454 if (addrbits == 16 ||
2455 (addrbits == 32 && !(sok & BITS32)) ||
2456 (addrbits == 64 && !(sok & BITS64)))
2457 goto err;
2459 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2460 : ((ix & YMMREG & ~REG_EA)
2461 ? EA_YMMVSIB : EA_XMMVSIB));
2463 output->rex |= rexflags(it, ix, REX_X);
2464 output->rex |= rexflags(bt, bx, REX_B);
2465 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
2467 index = it & 7; /* it is known to be != -1 */
2469 switch (s) {
2470 case 1:
2471 scale = 0;
2472 break;
2473 case 2:
2474 scale = 1;
2475 break;
2476 case 4:
2477 scale = 2;
2478 break;
2479 case 8:
2480 scale = 3;
2481 break;
2482 default: /* then what the smeg is it? */
2483 goto err; /* panic */
2486 if (bt == -1) {
2487 base = 5;
2488 mod = 0;
2489 } else {
2490 base = (bt & 7);
2491 if (base != REG_NUM_EBP && o == 0 &&
2492 seg == NO_SEG && !forw_ref &&
2493 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2494 mod = 0;
2495 else if (IS_MOD_01())
2496 mod = 1;
2497 else
2498 mod = 2;
2501 output->sib_present = true;
2502 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2503 output->modrm = GEN_MODRM(mod, rfield, 4);
2504 output->sib = GEN_SIB(scale, index, base);
2505 } else if ((ix|bx) & (BITS32|BITS64)) {
2507 * it must be a 32/64-bit memory reference. Firstly we have
2508 * to check that all registers involved are type E/Rxx.
2510 opflags_t sok = BITS32 | BITS64;
2511 int32_t o = input->offset;
2513 if (it != -1) {
2514 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2515 sok &= ix;
2516 else
2517 goto err;
2520 if (bt != -1) {
2521 if (REG_GPR & ~bx)
2522 goto err; /* Invalid register */
2523 if (~sok & bx & SIZE_MASK)
2524 goto err; /* Invalid size */
2525 sok &= bx;
2529 * While we're here, ensure the user didn't specify
2530 * WORD or QWORD
2532 if (input->disp_size == 16 || input->disp_size == 64)
2533 goto err;
2535 if (addrbits == 16 ||
2536 (addrbits == 32 && !(sok & BITS32)) ||
2537 (addrbits == 64 && !(sok & BITS64)))
2538 goto err;
2540 /* now reorganize base/index */
2541 if (s == 1 && bt != it && bt != -1 && it != -1 &&
2542 ((hb == b && ht == EAH_NOTBASE) ||
2543 (hb == i && ht == EAH_MAKEBASE))) {
2544 /* swap if hints say so */
2545 t = bt, bt = it, it = t;
2546 x = bx, bx = ix, ix = x;
2548 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
2549 bt = -1, bx = 0, s++;
2550 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
2551 /* make single reg base, unless hint */
2552 bt = it, bx = ix, it = -1, ix = 0;
2554 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2555 s == 3 || s == 5 || s == 9) && bt == -1)
2556 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
2557 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2558 (input->eaflags & EAF_TIMESTWO))
2559 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2560 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2561 if (s == 1 && it == REG_NUM_ESP) {
2562 /* swap ESP into base if scale is 1 */
2563 t = it, it = bt, bt = t;
2564 x = ix, ix = bx, bx = x;
2566 if (it == REG_NUM_ESP ||
2567 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
2568 goto err; /* wrong, for various reasons */
2570 output->rex |= rexflags(it, ix, REX_X);
2571 output->rex |= rexflags(bt, bx, REX_B);
2573 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
2574 /* no SIB needed */
2575 int mod, rm;
2577 if (bt == -1) {
2578 rm = 5;
2579 mod = 0;
2580 } else {
2581 rm = (bt & 7);
2582 if (rm != REG_NUM_EBP && o == 0 &&
2583 seg == NO_SEG && !forw_ref &&
2584 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2585 mod = 0;
2586 else if (IS_MOD_01())
2587 mod = 1;
2588 else
2589 mod = 2;
2592 output->sib_present = false;
2593 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2594 output->modrm = GEN_MODRM(mod, rfield, rm);
2595 } else {
2596 /* we need a SIB */
2597 int mod, scale, index, base;
2599 if (it == -1)
2600 index = 4, s = 1;
2601 else
2602 index = (it & 7);
2604 switch (s) {
2605 case 1:
2606 scale = 0;
2607 break;
2608 case 2:
2609 scale = 1;
2610 break;
2611 case 4:
2612 scale = 2;
2613 break;
2614 case 8:
2615 scale = 3;
2616 break;
2617 default: /* then what the smeg is it? */
2618 goto err; /* panic */
2621 if (bt == -1) {
2622 base = 5;
2623 mod = 0;
2624 } else {
2625 base = (bt & 7);
2626 if (base != REG_NUM_EBP && o == 0 &&
2627 seg == NO_SEG && !forw_ref &&
2628 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2629 mod = 0;
2630 else if (IS_MOD_01())
2631 mod = 1;
2632 else
2633 mod = 2;
2636 output->sib_present = true;
2637 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2638 output->modrm = GEN_MODRM(mod, rfield, 4);
2639 output->sib = GEN_SIB(scale, index, base);
2641 } else { /* it's 16-bit */
2642 int mod, rm;
2643 int16_t o = input->offset;
2645 /* check for 64-bit long mode */
2646 if (addrbits == 64)
2647 goto err;
2649 /* check all registers are BX, BP, SI or DI */
2650 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2651 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
2652 goto err;
2654 /* ensure the user didn't specify DWORD/QWORD */
2655 if (input->disp_size == 32 || input->disp_size == 64)
2656 goto err;
2658 if (s != 1 && i != -1)
2659 goto err; /* no can do, in 16-bit EA */
2660 if (b == -1 && i != -1) {
2661 int tmp = b;
2662 b = i;
2663 i = tmp;
2664 } /* swap */
2665 if ((b == R_SI || b == R_DI) && i != -1) {
2666 int tmp = b;
2667 b = i;
2668 i = tmp;
2670 /* have BX/BP as base, SI/DI index */
2671 if (b == i)
2672 goto err; /* shouldn't ever happen, in theory */
2673 if (i != -1 && b != -1 &&
2674 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
2675 goto err; /* invalid combinations */
2676 if (b == -1) /* pure offset: handled above */
2677 goto err; /* so if it gets to here, panic! */
2679 rm = -1;
2680 if (i != -1)
2681 switch (i * 256 + b) {
2682 case R_SI * 256 + R_BX:
2683 rm = 0;
2684 break;
2685 case R_DI * 256 + R_BX:
2686 rm = 1;
2687 break;
2688 case R_SI * 256 + R_BP:
2689 rm = 2;
2690 break;
2691 case R_DI * 256 + R_BP:
2692 rm = 3;
2693 break;
2694 } else
2695 switch (b) {
2696 case R_SI:
2697 rm = 4;
2698 break;
2699 case R_DI:
2700 rm = 5;
2701 break;
2702 case R_BP:
2703 rm = 6;
2704 break;
2705 case R_BX:
2706 rm = 7;
2707 break;
2709 if (rm == -1) /* can't happen, in theory */
2710 goto err; /* so panic if it does */
2712 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2713 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2714 mod = 0;
2715 else if (IS_MOD_01())
2716 mod = 1;
2717 else
2718 mod = 2;
2720 output->sib_present = false; /* no SIB - it's 16-bit */
2721 output->bytes = mod; /* bytes of offset needed */
2722 output->modrm = GEN_MODRM(mod, rfield, rm);
2727 output->size = 1 + output->sib_present + output->bytes;
2728 return output->type;
2730 err:
2731 return output->type = EA_INVALID;
2734 static void add_asp(insn *ins, int addrbits)
2736 int j, valid;
2737 int defdisp;
2739 valid = (addrbits == 64) ? 64|32 : 32|16;
2741 switch (ins->prefixes[PPS_ASIZE]) {
2742 case P_A16:
2743 valid &= 16;
2744 break;
2745 case P_A32:
2746 valid &= 32;
2747 break;
2748 case P_A64:
2749 valid &= 64;
2750 break;
2751 case P_ASP:
2752 valid &= (addrbits == 32) ? 16 : 32;
2753 break;
2754 default:
2755 break;
2758 for (j = 0; j < ins->operands; j++) {
2759 if (is_class(MEMORY, ins->oprs[j].type)) {
2760 opflags_t i, b;
2762 /* Verify as Register */
2763 if (!is_register(ins->oprs[j].indexreg))
2764 i = 0;
2765 else
2766 i = nasm_reg_flags[ins->oprs[j].indexreg];
2768 /* Verify as Register */
2769 if (!is_register(ins->oprs[j].basereg))
2770 b = 0;
2771 else
2772 b = nasm_reg_flags[ins->oprs[j].basereg];
2774 if (ins->oprs[j].scale == 0)
2775 i = 0;
2777 if (!i && !b) {
2778 int ds = ins->oprs[j].disp_size;
2779 if ((addrbits != 64 && ds > 8) ||
2780 (addrbits == 64 && ds == 16))
2781 valid &= ds;
2782 } else {
2783 if (!(REG16 & ~b))
2784 valid &= 16;
2785 if (!(REG32 & ~b))
2786 valid &= 32;
2787 if (!(REG64 & ~b))
2788 valid &= 64;
2790 if (!(REG16 & ~i))
2791 valid &= 16;
2792 if (!(REG32 & ~i))
2793 valid &= 32;
2794 if (!(REG64 & ~i))
2795 valid &= 64;
2800 if (valid & addrbits) {
2801 ins->addr_size = addrbits;
2802 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
2803 /* Add an address size prefix */
2804 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
2805 ins->addr_size = (addrbits == 32) ? 16 : 32;
2806 } else {
2807 /* Impossible... */
2808 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2809 ins->addr_size = addrbits; /* Error recovery */
2812 defdisp = ins->addr_size == 16 ? 16 : 32;
2814 for (j = 0; j < ins->operands; j++) {
2815 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2816 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2818 * mem_offs sizes must match the address size; if not,
2819 * strip the MEM_OFFS bit and match only EA instructions
2821 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);