changes.src: update for a 2.12.03 release
[nasm.git] / assemble.c
blob0971fa3ac7ac7d8a587ae20d5b1985caf2709f1d
1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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 * Bytecode specification
38 * ----------------------
41 * Codes Mnemonic Explanation
43 * \0 terminates the code. (Unless it's a literal of course.)
44 * \1..\4 that many literal bytes follow in the code stream
45 * \5 add 4 to the primary operand number (b, low octdigit)
46 * \6 add 4 to the secondary operand number (a, middle octdigit)
47 * \7 add 4 to both the primary and the secondary operand number
48 * \10..\13 a literal byte follows in the code stream, to be added
49 * to the register value of operand 0..3
50 * \14..\17 the position of index register operand in MIB (BND insns)
51 * \20..\23 ib a byte immediate operand, from operand 0..3
52 * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
53 * \30..\33 iw a word immediate operand, from operand 0..3
54 * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
55 * assembly mode or the operand-size override on the operand
56 * \40..\43 id a long immediate operand, from operand 0..3
57 * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
58 * depending on the address size of the instruction.
59 * \50..\53 rel8 a byte relative operand, from operand 0..3
60 * \54..\57 iq a qword immediate operand, from operand 0..3
61 * \60..\63 rel16 a word relative operand, from operand 0..3
62 * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
63 * assembly mode or the operand-size override on the operand
64 * \70..\73 rel32 a long relative operand, from operand 0..3
65 * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
66 * \1ab a ModRM, calculated on EA in operand a, with the spare
67 * field the register value of operand b.
68 * \172\ab the register number from operand a in bits 7..4, with
69 * the 4-bit immediate from operand b in bits 3..0.
70 * \173\xab the register number from operand a in bits 7..4, with
71 * the value b in bits 3..0.
72 * \174..\177 the register number from operand 0..3 in bits 7..4, and
73 * an arbitrary value in bits 3..0 (assembled as zero.)
74 * \2ab a ModRM, calculated on EA in operand a, with the spare
75 * field equal to digit b.
77 * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
78 * V field taken from operand 0..3.
79 * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
80 * V field set to 1111b.
82 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
84 * cc 000 0mm
85 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
86 * and wlp is:
87 * 00 wwl lpp
88 * [l0] ll = 0 (.128, .lz)
89 * [l1] ll = 1 (.256)
90 * [l2] ll = 2 (.512)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
93 * [w0] ww = 0 for W = 0
94 * [w1] ww = 1 for W = 1
95 * [wig] ww = 2 for W don't care (always assembled as 0)
96 * [ww] ww = 3 for W used as REX.W
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
100 * [f3] pp = 2
101 * [f2] pp = 3
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
106 * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
107 * \260..\263 this instruction uses VEX/XOP rather than REX, with the
108 * V field taken from operand 0..3.
109 * \270 this instruction uses VEX/XOP rather than REX, with the
110 * V field set to 1111b.
112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
114 * 00 wwl lpp
115 * [l0] ll = 0 for L = 0 (.128, .lz)
116 * [l1] ll = 1 for L = 1 (.256)
117 * [lig] ll = 2 for L don't care (always assembled as 0)
119 * [w0] ww = 0 for W = 0
120 * [w1 ] ww = 1 for W = 1
121 * [wig] ww = 2 for W don't care (always assembled as 0)
122 * [ww] ww = 3 for W used as REX.W
124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
126 * \271 hlexr instruction takes XRELEASE (F3) with or without lock
127 * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
128 * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
129 * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
130 * to the operand size (if o16/o32/o64 present) or the bit size
131 * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
132 * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
133 * \312 adf (disassembler only) invalid with non-default address size.
134 * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
135 * \314 norexb (disassembler only) invalid with REX.B
136 * \315 norexx (disassembler only) invalid with REX.X
137 * \316 norexr (disassembler only) invalid with REX.R
138 * \317 norexw (disassembler only) invalid with REX.W
139 * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
140 * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
141 * \322 odf indicates that this instruction is only valid when the
142 * operand size is the default (instruction to disassembler,
143 * generates no code in the assembler)
144 * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
145 * \324 o64 indicates 64-bit operand size requiring REX prefix.
146 * \325 nohi instruction which always uses spl/bpl/sil/dil
147 * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
148 disassembler only; for SSE instructions.
149 * \330 a literal byte follows in the code stream, to be added
150 * to the condition code value of the instruction.
151 * \331 norep instruction not valid with REP prefix. Hint for
152 * disassembler only; for SSE instructions.
153 * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
154 * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
155 * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
156 * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
157 * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
158 * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
159 * \336-\337 are still listed as prefixes in the disassembler.
160 * \340 resb reserve <operand 0> bytes of uninitialized storage.
161 * Operand 0 had better be a segmentless constant.
162 * \341 wait this instruction needs a WAIT "prefix"
163 * \360 np no SSE prefix (== \364\331)
164 * \361 66 SSE prefix (== \366\331)
165 * \364 !osp operand-size prefix (0x66) not permitted
166 * \365 !asp address-size prefix (0x67) not permitted
167 * \366 operand-size prefix (0x66) used as opcode extension
168 * \367 address-size prefix (0x67) used as opcode extension
169 * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
170 * jmp8 370 is used for Jcc, 371 is used for JMP.
171 * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
172 * used for conditional jump over longer jump
173 * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
174 * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
175 * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
178 #include "compiler.h"
180 #include <stdio.h>
181 #include <string.h>
182 #include <stdlib.h>
183 #include <inttypes.h>
185 #include "nasm.h"
186 #include "nasmlib.h"
187 #include "assemble.h"
188 #include "insns.h"
189 #include "tables.h"
190 #include "disp8.h"
191 #include "listing.h"
193 enum match_result {
195 * Matching errors. These should be sorted so that more specific
196 * errors come later in the sequence.
198 MERR_INVALOP,
199 MERR_OPSIZEMISSING,
200 MERR_OPSIZEMISMATCH,
201 MERR_BRNUMMISMATCH,
202 MERR_BADCPU,
203 MERR_BADMODE,
204 MERR_BADHLE,
205 MERR_ENCMISMATCH,
206 MERR_BADBND,
207 MERR_BADREPNE,
209 * Matching success; the conditional ones first
211 MOK_JUMP, /* Matching OK but needs jmp_match() */
212 MOK_GOOD /* Matching unconditionally OK */
215 typedef struct {
216 enum ea_type type; /* what kind of EA is this? */
217 int sib_present; /* is a SIB byte necessary? */
218 int bytes; /* # of bytes of offset needed */
219 int size; /* lazy - this is sib+bytes+1 */
220 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
221 int8_t disp8; /* compressed displacement for EVEX */
222 } ea;
224 #define GEN_SIB(scale, index, base) \
225 (((scale) << 6) | ((index) << 3) | ((base)))
227 #define GEN_MODRM(mod, reg, rm) \
228 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
230 static iflag_t cpu; /* cpu level received from nasm.c */
232 static int64_t calcsize(int32_t, int64_t, int, insn *,
233 const struct itemplate *);
234 static void gencode(int32_t segment, int64_t offset, int bits,
235 insn * ins, const struct itemplate *temp,
236 int64_t insn_end);
237 static enum match_result find_match(const struct itemplate **tempp,
238 insn *instruction,
239 int32_t segment, int64_t offset, int bits);
240 static enum match_result matches(const struct itemplate *, insn *, int bits);
241 static opflags_t regflag(const operand *);
242 static int32_t regval(const operand *);
243 static int rexflags(int, opflags_t, int);
244 static int op_rexflags(const operand *, int);
245 static int op_evexflags(const operand *, int, uint8_t);
246 static void add_asp(insn *, int);
248 static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
250 static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
252 return ins->prefixes[pos] == prefix;
255 static void assert_no_prefix(insn * ins, enum prefix_pos pos)
257 if (ins->prefixes[pos])
258 nasm_error(ERR_NONFATAL, "invalid %s prefix",
259 prefix_name(ins->prefixes[pos]));
262 static const char *size_name(int size)
264 switch (size) {
265 case 1:
266 return "byte";
267 case 2:
268 return "word";
269 case 4:
270 return "dword";
271 case 8:
272 return "qword";
273 case 10:
274 return "tword";
275 case 16:
276 return "oword";
277 case 32:
278 return "yword";
279 case 64:
280 return "zword";
281 default:
282 return "???";
286 static void warn_overflow(int pass, int size)
288 nasm_error(ERR_WARNING | pass | ERR_WARN_NOV,
289 "%s data exceeds bounds", size_name(size));
292 static void warn_overflow_const(int64_t data, int size)
294 if (overflow_general(data, size))
295 warn_overflow(ERR_PASS1, size);
298 static void warn_overflow_opd(const struct operand *o, int size)
300 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
301 if (overflow_general(o->offset, size))
302 warn_overflow(ERR_PASS2, size);
307 * Size of an address relocation, or zero if not an address
309 static int addrsize(enum out_type type, uint64_t size)
311 switch (type) {
312 case OUT_ADDRESS:
313 return abs((int)size);
314 case OUT_REL1ADR:
315 return 1;
316 case OUT_REL2ADR:
317 return 2;
318 case OUT_REL4ADR:
319 return 4;
320 case OUT_REL8ADR:
321 return 8;
322 default:
323 return 0;
328 * This routine wrappers the real output format's output routine,
329 * in order to pass a copy of the data off to the listing file
330 * generator at the same time, flatten unnecessary relocations,
331 * and verify backend compatibility.
333 static void out(int64_t offset, int32_t segto, const void *data,
334 enum out_type type, uint64_t size,
335 int32_t segment, int32_t wrt)
337 static int32_t lineno = 0; /* static!!! */
338 static const char *lnfname = NULL;
339 uint8_t p[8];
340 int asize = addrsize(type, size); /* Address size in bytes */
341 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
343 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
345 * This is a non-relocated address, and we're going to
346 * convert it into RAWDATA format.
348 uint8_t *q = p;
350 if (asize > 8) {
351 nasm_panic(0, "OUT_ADDRESS with size > 8");
352 return;
355 WRITEADDR(q, *(int64_t *)data, asize);
356 data = p;
357 type = OUT_RAWDATA;
358 size = asize;
359 asize = 0; /* No longer an address */
362 lfmt->output(offset, data, type, size);
365 * this call to src_get determines when we call the
366 * debug-format-specific "linenum" function
367 * it updates lineno and lnfname to the current values
368 * returning 0 if "same as last time", -2 if lnfname
369 * changed, and the amount by which lineno changed,
370 * if it did. thus, these variables must be static
373 if (src_get(&lineno, &lnfname))
374 dfmt->linenum(lnfname, lineno, segto);
376 if (asize && asize > amax) {
377 if (type != OUT_ADDRESS || (int)size < 0) {
378 nasm_error(ERR_NONFATAL,
379 "%d-bit signed relocation unsupported by output format %s\n",
380 asize << 3, ofmt->shortname);
381 size = asize;
382 } else {
383 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
384 "%d-bit unsigned relocation zero-extended from %d bits\n",
385 asize << 3, ofmt->maxbits);
386 ofmt->output(segto, data, type, amax, segment, wrt);
387 size = asize - amax;
389 data = zero_buffer;
390 type = OUT_RAWDATA;
391 segment = wrt = NO_SEG;
394 ofmt->output(segto, data, type, size, segment, wrt);
397 static void out_imm8(int64_t offset, int32_t segment,
398 struct operand *opx, int asize)
400 if (opx->segment != NO_SEG) {
401 uint64_t data = opx->offset;
402 out(offset, segment, &data, OUT_ADDRESS, asize, opx->segment, opx->wrt);
403 } else {
404 uint8_t byte = opx->offset;
405 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
409 static bool jmp_match(int32_t segment, int64_t offset, int bits,
410 insn * ins, const struct itemplate *temp)
412 int64_t isize;
413 const uint8_t *code = temp->code;
414 uint8_t c = code[0];
415 bool is_byte;
417 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
418 return false;
419 if (!optimizing)
420 return false;
421 if (optimizing < 0 && c == 0371)
422 return false;
424 isize = calcsize(segment, offset, bits, ins, temp);
426 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
427 /* Be optimistic in pass 1 */
428 return true;
430 if (ins->oprs[0].segment != segment)
431 return false;
433 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
434 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
436 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
437 /* jmp short (opcode eb) cannot be used with bnd prefix. */
438 ins->prefixes[PPS_REP] = P_none;
439 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
440 "jmp short does not init bnd regs - bnd prefix dropped.");
443 return is_byte;
446 int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
447 insn * instruction)
449 const struct itemplate *temp;
450 int j;
451 enum match_result m;
452 int64_t insn_end;
453 int32_t itimes;
454 int64_t start = offset;
455 int64_t wsize; /* size for DB etc. */
457 cpu = cp;
459 wsize = idata_bytes(instruction->opcode);
460 if (wsize == -1)
461 return 0;
463 if (wsize) {
464 extop *e;
465 int32_t t = instruction->times;
466 if (t < 0)
467 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
469 while (t--) { /* repeat TIMES times */
470 list_for_each(e, instruction->eops) {
471 if (e->type == EOT_DB_NUMBER) {
472 if (wsize > 8) {
473 nasm_error(ERR_NONFATAL,
474 "integer supplied to a DT, DO or DY"
475 " instruction");
476 } else {
477 out(offset, segment, &e->offset,
478 OUT_ADDRESS, wsize, e->segment, e->wrt);
479 offset += wsize;
481 } else if (e->type == EOT_DB_STRING ||
482 e->type == EOT_DB_STRING_FREE) {
483 int align;
485 out(offset, segment, e->stringval,
486 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
487 align = e->stringlen % wsize;
489 if (align) {
490 align = wsize - align;
491 out(offset, segment, zero_buffer,
492 OUT_RAWDATA, align, NO_SEG, NO_SEG);
494 offset += e->stringlen + align;
497 if (t > 0 && t == instruction->times - 1) {
499 * Dummy call to lfmt->output to give the offset to the
500 * listing module.
502 lfmt->output(offset, NULL, OUT_RAWDATA, 0);
503 lfmt->uplevel(LIST_TIMES);
506 if (instruction->times > 1)
507 lfmt->downlevel(LIST_TIMES);
508 return offset - start;
511 if (instruction->opcode == I_INCBIN) {
512 const char *fname = instruction->eops->stringval;
513 FILE *fp;
515 fp = fopen(fname, "rb");
516 if (!fp) {
517 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
518 fname);
519 } else if (fseek(fp, 0L, SEEK_END) < 0) {
520 nasm_error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
521 fname);
522 fclose(fp);
523 } else {
524 static char buf[4096];
525 size_t t = instruction->times;
526 size_t base = 0;
527 size_t len;
529 len = ftell(fp);
530 if (instruction->eops->next) {
531 base = instruction->eops->next->offset;
532 len -= base;
533 if (instruction->eops->next->next &&
534 len > (size_t)instruction->eops->next->next->offset)
535 len = (size_t)instruction->eops->next->next->offset;
538 * Dummy call to lfmt->output to give the offset to the
539 * listing module.
541 lfmt->output(offset, NULL, OUT_RAWDATA, 0);
542 lfmt->uplevel(LIST_INCBIN);
543 while (t--) {
544 size_t l;
546 fseek(fp, base, SEEK_SET);
547 l = len;
548 while (l > 0) {
549 int32_t m;
550 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
551 if (!m) {
553 * This shouldn't happen unless the file
554 * actually changes while we are reading
555 * it.
557 nasm_error(ERR_NONFATAL,
558 "`incbin': unexpected EOF while"
559 " reading file `%s'", fname);
560 t = 0; /* Try to exit cleanly */
561 break;
563 out(offset, segment, buf, OUT_RAWDATA, m,
564 NO_SEG, NO_SEG);
565 l -= m;
568 lfmt->downlevel(LIST_INCBIN);
569 if (instruction->times > 1) {
571 * Dummy call to lfmt->output to give the offset to the
572 * listing module.
574 lfmt->output(offset, NULL, OUT_RAWDATA, 0);
575 lfmt->uplevel(LIST_TIMES);
576 lfmt->downlevel(LIST_TIMES);
578 fclose(fp);
579 return instruction->times * len;
581 return 0; /* if we're here, there's an error */
584 /* Check to see if we need an address-size prefix */
585 add_asp(instruction, bits);
587 m = find_match(&temp, instruction, segment, offset, bits);
589 if (m == MOK_GOOD) {
590 /* Matches! */
591 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
592 itimes = instruction->times;
593 if (insn_size < 0) /* shouldn't be, on pass two */
594 nasm_panic(0, "errors made it through from pass one");
595 else
596 while (itimes--) {
597 for (j = 0; j < MAXPREFIX; j++) {
598 uint8_t c = 0;
599 switch (instruction->prefixes[j]) {
600 case P_WAIT:
601 c = 0x9B;
602 break;
603 case P_LOCK:
604 c = 0xF0;
605 break;
606 case P_REPNE:
607 case P_REPNZ:
608 case P_XACQUIRE:
609 case P_BND:
610 c = 0xF2;
611 break;
612 case P_REPE:
613 case P_REPZ:
614 case P_REP:
615 case P_XRELEASE:
616 c = 0xF3;
617 break;
618 case R_CS:
619 if (bits == 64) {
620 nasm_error(ERR_WARNING | ERR_PASS2,
621 "cs segment base generated, but will be ignored in 64-bit mode");
623 c = 0x2E;
624 break;
625 case R_DS:
626 if (bits == 64) {
627 nasm_error(ERR_WARNING | ERR_PASS2,
628 "ds segment base generated, but will be ignored in 64-bit mode");
630 c = 0x3E;
631 break;
632 case R_ES:
633 if (bits == 64) {
634 nasm_error(ERR_WARNING | ERR_PASS2,
635 "es segment base generated, but will be ignored in 64-bit mode");
637 c = 0x26;
638 break;
639 case R_FS:
640 c = 0x64;
641 break;
642 case R_GS:
643 c = 0x65;
644 break;
645 case R_SS:
646 if (bits == 64) {
647 nasm_error(ERR_WARNING | ERR_PASS2,
648 "ss segment base generated, but will be ignored in 64-bit mode");
650 c = 0x36;
651 break;
652 case R_SEGR6:
653 case R_SEGR7:
654 nasm_error(ERR_NONFATAL,
655 "segr6 and segr7 cannot be used as prefixes");
656 break;
657 case P_A16:
658 if (bits == 64) {
659 nasm_error(ERR_NONFATAL,
660 "16-bit addressing is not supported "
661 "in 64-bit mode");
662 } else if (bits != 16)
663 c = 0x67;
664 break;
665 case P_A32:
666 if (bits != 32)
667 c = 0x67;
668 break;
669 case P_A64:
670 if (bits != 64) {
671 nasm_error(ERR_NONFATAL,
672 "64-bit addressing is only supported "
673 "in 64-bit mode");
675 break;
676 case P_ASP:
677 c = 0x67;
678 break;
679 case P_O16:
680 if (bits != 16)
681 c = 0x66;
682 break;
683 case P_O32:
684 if (bits == 16)
685 c = 0x66;
686 break;
687 case P_O64:
688 /* REX.W */
689 break;
690 case P_OSP:
691 c = 0x66;
692 break;
693 case P_EVEX:
694 case P_VEX3:
695 case P_VEX2:
696 case P_NOBND:
697 case P_none:
698 break;
699 default:
700 nasm_panic(0, "invalid instruction prefix");
702 if (c != 0) {
703 out(offset, segment, &c, OUT_RAWDATA, 1,
704 NO_SEG, NO_SEG);
705 offset++;
708 insn_end = offset + insn_size;
709 gencode(segment, offset, bits, instruction,
710 temp, insn_end);
711 offset += insn_size;
712 if (itimes > 0 && itimes == instruction->times - 1) {
714 * Dummy call to lfmt->output to give the offset to the
715 * listing module.
717 lfmt->output(offset, NULL, OUT_RAWDATA, 0);
718 lfmt->uplevel(LIST_TIMES);
721 if (instruction->times > 1)
722 lfmt->downlevel(LIST_TIMES);
723 return offset - start;
724 } else {
725 /* No match */
726 switch (m) {
727 case MERR_OPSIZEMISSING:
728 nasm_error(ERR_NONFATAL, "operation size not specified");
729 break;
730 case MERR_OPSIZEMISMATCH:
731 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
732 break;
733 case MERR_BRNUMMISMATCH:
734 nasm_error(ERR_NONFATAL,
735 "mismatch in the number of broadcasting elements");
736 break;
737 case MERR_BADCPU:
738 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
739 break;
740 case MERR_BADMODE:
741 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
742 bits);
743 break;
744 case MERR_ENCMISMATCH:
745 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
746 break;
747 case MERR_BADBND:
748 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
749 break;
750 case MERR_BADREPNE:
751 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
752 (has_prefix(instruction, PPS_REP, P_REPNE) ?
753 "repne" : "repnz"));
754 break;
755 default:
756 nasm_error(ERR_NONFATAL,
757 "invalid combination of opcode and operands");
758 break;
761 return 0;
764 int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
765 insn * instruction)
767 const struct itemplate *temp;
768 enum match_result m;
770 cpu = cp;
772 if (instruction->opcode == I_none)
773 return 0;
775 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
776 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
777 instruction->opcode == I_DT || instruction->opcode == I_DO ||
778 instruction->opcode == I_DY) {
779 extop *e;
780 int32_t isize, osize, wsize;
782 isize = 0;
783 wsize = idata_bytes(instruction->opcode);
785 list_for_each(e, instruction->eops) {
786 int32_t align;
788 osize = 0;
789 if (e->type == EOT_DB_NUMBER) {
790 osize = 1;
791 warn_overflow_const(e->offset, wsize);
792 } else if (e->type == EOT_DB_STRING ||
793 e->type == EOT_DB_STRING_FREE)
794 osize = e->stringlen;
796 align = (-osize) % wsize;
797 if (align < 0)
798 align += wsize;
799 isize += osize + align;
801 return isize * instruction->times;
804 if (instruction->opcode == I_INCBIN) {
805 const char *fname = instruction->eops->stringval;
806 FILE *fp;
807 int64_t val = 0;
808 size_t len;
810 fp = fopen(fname, "rb");
811 if (!fp)
812 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
813 fname);
814 else if (fseek(fp, 0L, SEEK_END) < 0)
815 nasm_error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
816 fname);
817 else {
818 len = ftell(fp);
819 if (instruction->eops->next) {
820 len -= instruction->eops->next->offset;
821 if (instruction->eops->next->next &&
822 len > (size_t)instruction->eops->next->next->offset) {
823 len = (size_t)instruction->eops->next->next->offset;
826 val = instruction->times * len;
828 if (fp)
829 fclose(fp);
830 return val;
833 /* Check to see if we need an address-size prefix */
834 add_asp(instruction, bits);
836 m = find_match(&temp, instruction, segment, offset, bits);
837 if (m == MOK_GOOD) {
838 /* we've matched an instruction. */
839 int64_t isize;
840 int j;
842 isize = calcsize(segment, offset, bits, instruction, temp);
843 if (isize < 0)
844 return -1;
845 for (j = 0; j < MAXPREFIX; j++) {
846 switch (instruction->prefixes[j]) {
847 case P_A16:
848 if (bits != 16)
849 isize++;
850 break;
851 case P_A32:
852 if (bits != 32)
853 isize++;
854 break;
855 case P_O16:
856 if (bits != 16)
857 isize++;
858 break;
859 case P_O32:
860 if (bits == 16)
861 isize++;
862 break;
863 case P_A64:
864 case P_O64:
865 case P_EVEX:
866 case P_VEX3:
867 case P_VEX2:
868 case P_NOBND:
869 case P_none:
870 break;
871 default:
872 isize++;
873 break;
876 return isize * instruction->times;
877 } else {
878 return -1; /* didn't match any instruction */
882 static void bad_hle_warn(const insn * ins, uint8_t hleok)
884 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
885 enum whatwarn { w_none, w_lock, w_inval } ww;
886 static const enum whatwarn warn[2][4] =
888 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
889 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
891 unsigned int n;
893 n = (unsigned int)rep_pfx - P_XACQUIRE;
894 if (n > 1)
895 return; /* Not XACQUIRE/XRELEASE */
897 ww = warn[n][hleok];
898 if (!is_class(MEMORY, ins->oprs[0].type))
899 ww = w_inval; /* HLE requires operand 0 to be memory */
901 switch (ww) {
902 case w_none:
903 break;
905 case w_lock:
906 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
907 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
908 "%s with this instruction requires lock",
909 prefix_name(rep_pfx));
911 break;
913 case w_inval:
914 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
915 "%s invalid with this instruction",
916 prefix_name(rep_pfx));
917 break;
921 /* Common construct */
922 #define case3(x) case (x): case (x)+1: case (x)+2
923 #define case4(x) case3(x): case (x)+3
925 static int64_t calcsize(int32_t segment, int64_t offset, int bits,
926 insn * ins, const struct itemplate *temp)
928 const uint8_t *codes = temp->code;
929 int64_t length = 0;
930 uint8_t c;
931 int rex_mask = ~0;
932 int op1, op2;
933 struct operand *opx;
934 uint8_t opex = 0;
935 enum ea_type eat;
936 uint8_t hleok = 0;
937 bool lockcheck = true;
938 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
940 ins->rex = 0; /* Ensure REX is reset */
941 eat = EA_SCALAR; /* Expect a scalar EA */
942 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
944 if (ins->prefixes[PPS_OSIZE] == P_O64)
945 ins->rex |= REX_W;
947 (void)segment; /* Don't warn that this parameter is unused */
948 (void)offset; /* Don't warn that this parameter is unused */
950 while (*codes) {
951 c = *codes++;
952 op1 = (c & 3) + ((opex & 1) << 2);
953 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
954 opx = &ins->oprs[op1];
955 opex = 0; /* For the next iteration */
957 switch (c) {
958 case4(01):
959 codes += c, length += c;
960 break;
962 case3(05):
963 opex = c;
964 break;
966 case4(010):
967 ins->rex |=
968 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
969 codes++, length++;
970 break;
972 case4(014):
973 /* this is an index reg of MIB operand */
974 mib_index = opx->basereg;
975 break;
977 case4(020):
978 case4(024):
979 length++;
980 break;
982 case4(030):
983 length += 2;
984 break;
986 case4(034):
987 if (opx->type & (BITS16 | BITS32 | BITS64))
988 length += (opx->type & BITS16) ? 2 : 4;
989 else
990 length += (bits == 16) ? 2 : 4;
991 break;
993 case4(040):
994 length += 4;
995 break;
997 case4(044):
998 length += ins->addr_size >> 3;
999 break;
1001 case4(050):
1002 length++;
1003 break;
1005 case4(054):
1006 length += 8; /* MOV reg64/imm */
1007 break;
1009 case4(060):
1010 length += 2;
1011 break;
1013 case4(064):
1014 if (opx->type & (BITS16 | BITS32 | BITS64))
1015 length += (opx->type & BITS16) ? 2 : 4;
1016 else
1017 length += (bits == 16) ? 2 : 4;
1018 break;
1020 case4(070):
1021 length += 4;
1022 break;
1024 case4(074):
1025 length += 2;
1026 break;
1028 case 0172:
1029 case 0173:
1030 codes++;
1031 length++;
1032 break;
1034 case4(0174):
1035 length++;
1036 break;
1038 case4(0240):
1039 ins->rex |= REX_EV;
1040 ins->vexreg = regval(opx);
1041 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1042 ins->vex_cm = *codes++;
1043 ins->vex_wlp = *codes++;
1044 ins->evex_tuple = (*codes++ - 0300);
1045 break;
1047 case 0250:
1048 ins->rex |= REX_EV;
1049 ins->vexreg = 0;
1050 ins->vex_cm = *codes++;
1051 ins->vex_wlp = *codes++;
1052 ins->evex_tuple = (*codes++ - 0300);
1053 break;
1055 case4(0254):
1056 length += 4;
1057 break;
1059 case4(0260):
1060 ins->rex |= REX_V;
1061 ins->vexreg = regval(opx);
1062 ins->vex_cm = *codes++;
1063 ins->vex_wlp = *codes++;
1064 break;
1066 case 0270:
1067 ins->rex |= REX_V;
1068 ins->vexreg = 0;
1069 ins->vex_cm = *codes++;
1070 ins->vex_wlp = *codes++;
1071 break;
1073 case3(0271):
1074 hleok = c & 3;
1075 break;
1077 case4(0274):
1078 length++;
1079 break;
1081 case4(0300):
1082 break;
1084 case 0310:
1085 if (bits == 64)
1086 return -1;
1087 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
1088 break;
1090 case 0311:
1091 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
1092 break;
1094 case 0312:
1095 break;
1097 case 0313:
1098 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1099 has_prefix(ins, PPS_ASIZE, P_A32))
1100 return -1;
1101 break;
1103 case4(0314):
1104 break;
1106 case 0320:
1108 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1109 if (pfx == P_O16)
1110 break;
1111 if (pfx != P_none)
1112 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1113 else
1114 ins->prefixes[PPS_OSIZE] = P_O16;
1115 break;
1118 case 0321:
1120 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1121 if (pfx == P_O32)
1122 break;
1123 if (pfx != P_none)
1124 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1125 else
1126 ins->prefixes[PPS_OSIZE] = P_O32;
1127 break;
1130 case 0322:
1131 break;
1133 case 0323:
1134 rex_mask &= ~REX_W;
1135 break;
1137 case 0324:
1138 ins->rex |= REX_W;
1139 break;
1141 case 0325:
1142 ins->rex |= REX_NH;
1143 break;
1145 case 0326:
1146 break;
1148 case 0330:
1149 codes++, length++;
1150 break;
1152 case 0331:
1153 break;
1155 case 0332:
1156 case 0333:
1157 length++;
1158 break;
1160 case 0334:
1161 ins->rex |= REX_L;
1162 break;
1164 case 0335:
1165 break;
1167 case 0336:
1168 if (!ins->prefixes[PPS_REP])
1169 ins->prefixes[PPS_REP] = P_REP;
1170 break;
1172 case 0337:
1173 if (!ins->prefixes[PPS_REP])
1174 ins->prefixes[PPS_REP] = P_REPNE;
1175 break;
1177 case 0340:
1178 if (ins->oprs[0].segment != NO_SEG)
1179 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
1180 " quantity of BSS space");
1181 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1182 nasm_error(ERR_WARNING | ERR_PASS1,
1183 "forward reference in RESx can have unpredictable results");
1184 else
1185 length += ins->oprs[0].offset;
1186 break;
1188 case 0341:
1189 if (!ins->prefixes[PPS_WAIT])
1190 ins->prefixes[PPS_WAIT] = P_WAIT;
1191 break;
1193 case 0360:
1194 break;
1196 case 0361:
1197 length++;
1198 break;
1200 case 0364:
1201 case 0365:
1202 break;
1204 case 0366:
1205 case 0367:
1206 length++;
1207 break;
1209 case 0370:
1210 case 0371:
1211 break;
1213 case 0373:
1214 length++;
1215 break;
1217 case 0374:
1218 eat = EA_XMMVSIB;
1219 break;
1221 case 0375:
1222 eat = EA_YMMVSIB;
1223 break;
1225 case 0376:
1226 eat = EA_ZMMVSIB;
1227 break;
1229 case4(0100):
1230 case4(0110):
1231 case4(0120):
1232 case4(0130):
1233 case4(0200):
1234 case4(0204):
1235 case4(0210):
1236 case4(0214):
1237 case4(0220):
1238 case4(0224):
1239 case4(0230):
1240 case4(0234):
1242 ea ea_data;
1243 int rfield;
1244 opflags_t rflags;
1245 struct operand *opy = &ins->oprs[op2];
1246 struct operand *op_er_sae;
1248 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
1250 if (c <= 0177) {
1251 /* pick rfield from operand b (opx) */
1252 rflags = regflag(opx);
1253 rfield = nasm_regvals[opx->basereg];
1254 } else {
1255 rflags = 0;
1256 rfield = c & 7;
1259 /* EVEX.b1 : evex_brerop contains the operand position */
1260 op_er_sae = (ins->evex_brerop >= 0 ?
1261 &ins->oprs[ins->evex_brerop] : NULL);
1263 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1264 /* set EVEX.b */
1265 ins->evex_p[2] |= EVEX_P2B;
1266 if (op_er_sae->decoflags & ER) {
1267 /* set EVEX.RC (rounding control) */
1268 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1269 & EVEX_P2RC;
1271 } else {
1272 /* set EVEX.L'L (vector length) */
1273 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
1274 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
1275 if (opy->decoflags & BRDCAST_MASK) {
1276 /* set EVEX.b */
1277 ins->evex_p[2] |= EVEX_P2B;
1281 if (itemp_has(temp, IF_MIB)) {
1282 opy->eaflags |= EAF_MIB;
1284 * if a separate form of MIB (ICC style) is used,
1285 * the index reg info is merged into mem operand
1287 if (mib_index != R_none) {
1288 opy->indexreg = mib_index;
1289 opy->scale = 1;
1290 opy->hintbase = mib_index;
1291 opy->hinttype = EAH_NOTBASE;
1295 if (process_ea(opy, &ea_data, bits,
1296 rfield, rflags, ins) != eat) {
1297 nasm_error(ERR_NONFATAL, "invalid effective address");
1298 return -1;
1299 } else {
1300 ins->rex |= ea_data.rex;
1301 length += ea_data.size;
1304 break;
1306 default:
1307 nasm_panic(0, "internal instruction table corrupt"
1308 ": instruction code \\%o (0x%02X) given", c, c);
1309 break;
1313 ins->rex &= rex_mask;
1315 if (ins->rex & REX_NH) {
1316 if (ins->rex & REX_H) {
1317 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
1318 return -1;
1320 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
1323 switch (ins->prefixes[PPS_VEX]) {
1324 case P_EVEX:
1325 if (!(ins->rex & REX_EV))
1326 return -1;
1327 break;
1328 case P_VEX3:
1329 case P_VEX2:
1330 if (!(ins->rex & REX_V))
1331 return -1;
1332 break;
1333 default:
1334 break;
1337 if (ins->rex & (REX_V | REX_EV)) {
1338 int bad32 = REX_R|REX_W|REX_X|REX_B;
1340 if (ins->rex & REX_H) {
1341 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
1342 return -1;
1344 switch (ins->vex_wlp & 060) {
1345 case 000:
1346 case 040:
1347 ins->rex &= ~REX_W;
1348 break;
1349 case 020:
1350 ins->rex |= REX_W;
1351 bad32 &= ~REX_W;
1352 break;
1353 case 060:
1354 /* Follow REX_W */
1355 break;
1358 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
1359 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1360 return -1;
1361 } else if (!(ins->rex & REX_EV) &&
1362 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1363 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1364 return -1;
1366 if (ins->rex & REX_EV)
1367 length += 4;
1368 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1369 ins->prefixes[PPS_VEX] == P_VEX3)
1370 length += 3;
1371 else
1372 length += 2;
1373 } else if (ins->rex & REX_MASK) {
1374 if (ins->rex & REX_H) {
1375 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
1376 return -1;
1377 } else if (bits == 64) {
1378 length++;
1379 } else if ((ins->rex & REX_L) &&
1380 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1381 iflag_ffs(&cpu) >= IF_X86_64) {
1382 /* LOCK-as-REX.R */
1383 assert_no_prefix(ins, PPS_LOCK);
1384 lockcheck = false; /* Already errored, no need for warning */
1385 length++;
1386 } else {
1387 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1388 return -1;
1392 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
1393 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
1394 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
1395 "instruction is not lockable");
1398 bad_hle_warn(ins, hleok);
1401 * when BND prefix is set by DEFAULT directive,
1402 * BND prefix is added to every appropriate instruction line
1403 * unless it is overridden by NOBND prefix.
1405 if (globalbnd &&
1406 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1407 ins->prefixes[PPS_REP] = P_BND;
1409 return length;
1412 static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1414 if (bits == 64) {
1415 if ((ins->rex & REX_MASK) &&
1416 !(ins->rex & (REX_V | REX_EV)) &&
1417 !ins->rex_done) {
1418 int rex = (ins->rex & REX_MASK) | REX_P;
1419 out(offset, segment, &rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1420 ins->rex_done = true;
1421 return 1;
1425 return 0;
1428 static void gencode(int32_t segment, int64_t offset, int bits,
1429 insn * ins, const struct itemplate *temp,
1430 int64_t insn_end)
1432 uint8_t c;
1433 uint8_t bytes[4];
1434 int64_t size;
1435 int64_t data;
1436 int op1, op2;
1437 struct operand *opx;
1438 const uint8_t *codes = temp->code;
1439 uint8_t opex = 0;
1440 enum ea_type eat = EA_SCALAR;
1441 int r;
1443 ins->rex_done = false;
1445 while (*codes) {
1446 c = *codes++;
1447 op1 = (c & 3) + ((opex & 1) << 2);
1448 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1449 opx = &ins->oprs[op1];
1450 opex = 0; /* For the next iteration */
1452 switch (c) {
1453 case 01:
1454 case 02:
1455 case 03:
1456 case 04:
1457 offset += emit_rex(ins, segment, offset, bits);
1458 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
1459 codes += c;
1460 offset += c;
1461 break;
1463 case 05:
1464 case 06:
1465 case 07:
1466 opex = c;
1467 break;
1469 case4(010):
1470 offset += emit_rex(ins, segment, offset, bits);
1471 bytes[0] = *codes++ + (regval(opx) & 7);
1472 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1473 offset += 1;
1474 break;
1476 case4(014):
1477 break;
1479 case4(020):
1480 if (opx->offset < -256 || opx->offset > 255) {
1481 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1482 "byte value exceeds bounds");
1484 out_imm8(offset, segment, opx, -1);
1485 offset += 1;
1486 break;
1488 case4(024):
1489 if (opx->offset < 0 || opx->offset > 255)
1490 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1491 "unsigned byte value exceeds bounds");
1492 out_imm8(offset, segment, opx, 1);
1493 offset += 1;
1494 break;
1496 case4(030):
1497 warn_overflow_opd(opx, 2);
1498 data = opx->offset;
1499 out(offset, segment, &data, OUT_ADDRESS, 2,
1500 opx->segment, opx->wrt);
1501 offset += 2;
1502 break;
1504 case4(034):
1505 if (opx->type & (BITS16 | BITS32))
1506 size = (opx->type & BITS16) ? 2 : 4;
1507 else
1508 size = (bits == 16) ? 2 : 4;
1509 warn_overflow_opd(opx, size);
1510 data = opx->offset;
1511 out(offset, segment, &data, OUT_ADDRESS, size,
1512 opx->segment, opx->wrt);
1513 offset += size;
1514 break;
1516 case4(040):
1517 warn_overflow_opd(opx, 4);
1518 data = opx->offset;
1519 out(offset, segment, &data, OUT_ADDRESS, 4,
1520 opx->segment, opx->wrt);
1521 offset += 4;
1522 break;
1524 case4(044):
1525 data = opx->offset;
1526 size = ins->addr_size >> 3;
1527 warn_overflow_opd(opx, size);
1528 out(offset, segment, &data, OUT_ADDRESS, size,
1529 opx->segment, opx->wrt);
1530 offset += size;
1531 break;
1533 case4(050):
1534 if (opx->segment != segment) {
1535 data = opx->offset;
1536 out(offset, segment, &data,
1537 OUT_REL1ADR, insn_end - offset,
1538 opx->segment, opx->wrt);
1539 } else {
1540 data = opx->offset - insn_end;
1541 if (data > 127 || data < -128)
1542 nasm_error(ERR_NONFATAL, "short jump is out of range");
1543 out(offset, segment, &data,
1544 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1546 offset += 1;
1547 break;
1549 case4(054):
1550 data = (int64_t)opx->offset;
1551 out(offset, segment, &data, OUT_ADDRESS, 8,
1552 opx->segment, opx->wrt);
1553 offset += 8;
1554 break;
1556 case4(060):
1557 if (opx->segment != segment) {
1558 data = opx->offset;
1559 out(offset, segment, &data,
1560 OUT_REL2ADR, insn_end - offset,
1561 opx->segment, opx->wrt);
1562 } else {
1563 data = opx->offset - insn_end;
1564 out(offset, segment, &data,
1565 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
1567 offset += 2;
1568 break;
1570 case4(064):
1571 if (opx->type & (BITS16 | BITS32 | BITS64))
1572 size = (opx->type & BITS16) ? 2 : 4;
1573 else
1574 size = (bits == 16) ? 2 : 4;
1575 if (opx->segment != segment) {
1576 data = opx->offset;
1577 out(offset, segment, &data,
1578 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1579 insn_end - offset, opx->segment, opx->wrt);
1580 } else {
1581 data = opx->offset - insn_end;
1582 out(offset, segment, &data,
1583 OUT_ADDRESS, size, NO_SEG, NO_SEG);
1585 offset += size;
1586 break;
1588 case4(070):
1589 if (opx->segment != segment) {
1590 data = opx->offset;
1591 out(offset, segment, &data,
1592 OUT_REL4ADR, insn_end - offset,
1593 opx->segment, opx->wrt);
1594 } else {
1595 data = opx->offset - insn_end;
1596 out(offset, segment, &data,
1597 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
1599 offset += 4;
1600 break;
1602 case4(074):
1603 if (opx->segment == NO_SEG)
1604 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
1605 " relocatable");
1606 data = 0;
1607 out(offset, segment, &data, OUT_ADDRESS, 2,
1608 ofmt->segbase(1 + opx->segment),
1609 opx->wrt);
1610 offset += 2;
1611 break;
1613 case 0172:
1615 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1616 const struct operand *opy;
1618 c = *codes++;
1619 opx = &ins->oprs[c >> 3];
1620 opy = &ins->oprs[c & 7];
1621 if (opy->segment != NO_SEG || opy->wrt != NO_SEG) {
1622 nasm_error(ERR_NONFATAL,
1623 "non-absolute expression not permitted as argument %d",
1624 c & 7);
1625 } else if (opy->offset & ~mask) {
1626 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1627 "is4 argument exceeds bounds");
1629 c = opy->offset & mask;
1630 goto emit_is4;
1633 case 0173:
1634 c = *codes++;
1635 opx = &ins->oprs[c >> 4];
1636 c &= 15;
1637 goto emit_is4;
1639 case4(0174):
1640 c = 0;
1641 emit_is4:
1642 r = nasm_regvals[opx->basereg];
1643 bytes[0] = (r << 4) | ((r & 0x10) >> 1) | c;
1644 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1645 offset++;
1646 break;
1648 case4(0254):
1649 data = opx->offset;
1650 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1651 (int32_t)data != (int64_t)data) {
1652 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1653 "signed dword immediate exceeds bounds");
1655 out(offset, segment, &data, OUT_ADDRESS, -4,
1656 opx->segment, opx->wrt);
1657 offset += 4;
1658 break;
1660 case4(0240):
1661 case 0250:
1662 codes += 3;
1663 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1664 EVEX_P2Z | EVEX_P2AAA, 2);
1665 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1666 bytes[0] = 0x62;
1667 /* EVEX.X can be set by either REX or EVEX for different reasons */
1668 bytes[1] = ((((ins->rex & 7) << 5) |
1669 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1670 (ins->vex_cm & 3);
1671 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1672 ((~ins->vexreg & 15) << 3) |
1673 (1 << 2) | (ins->vex_wlp & 3);
1674 bytes[3] = ins->evex_p[2];
1675 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1676 offset += 4;
1677 break;
1679 case4(0260):
1680 case 0270:
1681 codes += 2;
1682 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1683 ins->prefixes[PPS_VEX] == P_VEX3) {
1684 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1685 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1686 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
1687 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
1688 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1689 offset += 3;
1690 } else {
1691 bytes[0] = 0xc5;
1692 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
1693 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
1694 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1695 offset += 2;
1697 break;
1699 case 0271:
1700 case 0272:
1701 case 0273:
1702 break;
1704 case4(0274):
1706 uint64_t uv, um;
1707 int s;
1709 if (ins->rex & REX_W)
1710 s = 64;
1711 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1712 s = 16;
1713 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1714 s = 32;
1715 else
1716 s = bits;
1718 um = (uint64_t)2 << (s-1);
1719 uv = opx->offset;
1721 if (uv > 127 && uv < (uint64_t)-128 &&
1722 (uv < um-128 || uv > um-1)) {
1723 /* If this wasn't explicitly byte-sized, warn as though we
1724 * had fallen through to the imm16/32/64 case.
1726 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1727 "%s value exceeds bounds",
1728 (opx->type & BITS8) ? "signed byte" :
1729 s == 16 ? "word" :
1730 s == 32 ? "dword" :
1731 "signed dword");
1733 if (opx->segment != NO_SEG) {
1734 data = uv;
1735 out(offset, segment, &data, OUT_ADDRESS, 1,
1736 opx->segment, opx->wrt);
1737 } else {
1738 bytes[0] = uv;
1739 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1740 NO_SEG);
1742 offset += 1;
1743 break;
1746 case4(0300):
1747 break;
1749 case 0310:
1750 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
1751 *bytes = 0x67;
1752 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1753 offset += 1;
1754 } else
1755 offset += 0;
1756 break;
1758 case 0311:
1759 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
1760 *bytes = 0x67;
1761 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1762 offset += 1;
1763 } else
1764 offset += 0;
1765 break;
1767 case 0312:
1768 break;
1770 case 0313:
1771 ins->rex = 0;
1772 break;
1774 case4(0314):
1775 break;
1777 case 0320:
1778 case 0321:
1779 break;
1781 case 0322:
1782 case 0323:
1783 break;
1785 case 0324:
1786 ins->rex |= REX_W;
1787 break;
1789 case 0325:
1790 break;
1792 case 0326:
1793 break;
1795 case 0330:
1796 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
1797 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1798 offset += 1;
1799 break;
1801 case 0331:
1802 break;
1804 case 0332:
1805 case 0333:
1806 *bytes = c - 0332 + 0xF2;
1807 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1808 offset += 1;
1809 break;
1811 case 0334:
1812 if (ins->rex & REX_R) {
1813 *bytes = 0xF0;
1814 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1815 offset += 1;
1817 ins->rex &= ~(REX_L|REX_R);
1818 break;
1820 case 0335:
1821 break;
1823 case 0336:
1824 case 0337:
1825 break;
1827 case 0340:
1828 if (ins->oprs[0].segment != NO_SEG)
1829 nasm_panic(0, "non-constant BSS size in pass two");
1830 else {
1831 int64_t size = ins->oprs[0].offset;
1832 if (size > 0)
1833 out(offset, segment, NULL,
1834 OUT_RESERVE, size, NO_SEG, NO_SEG);
1835 offset += size;
1837 break;
1839 case 0341:
1840 break;
1842 case 0360:
1843 break;
1845 case 0361:
1846 bytes[0] = 0x66;
1847 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1848 offset += 1;
1849 break;
1851 case 0364:
1852 case 0365:
1853 break;
1855 case 0366:
1856 case 0367:
1857 *bytes = c - 0366 + 0x66;
1858 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1859 offset += 1;
1860 break;
1862 case3(0370):
1863 break;
1865 case 0373:
1866 *bytes = bits == 16 ? 3 : 5;
1867 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1868 offset += 1;
1869 break;
1871 case 0374:
1872 eat = EA_XMMVSIB;
1873 break;
1875 case 0375:
1876 eat = EA_YMMVSIB;
1877 break;
1879 case 0376:
1880 eat = EA_ZMMVSIB;
1881 break;
1883 case4(0100):
1884 case4(0110):
1885 case4(0120):
1886 case4(0130):
1887 case4(0200):
1888 case4(0204):
1889 case4(0210):
1890 case4(0214):
1891 case4(0220):
1892 case4(0224):
1893 case4(0230):
1894 case4(0234):
1896 ea ea_data;
1897 int rfield;
1898 opflags_t rflags;
1899 uint8_t *p;
1900 int32_t s;
1901 struct operand *opy = &ins->oprs[op2];
1903 if (c <= 0177) {
1904 /* pick rfield from operand b (opx) */
1905 rflags = regflag(opx);
1906 rfield = nasm_regvals[opx->basereg];
1907 } else {
1908 /* rfield is constant */
1909 rflags = 0;
1910 rfield = c & 7;
1913 if (process_ea(opy, &ea_data, bits,
1914 rfield, rflags, ins) != eat)
1915 nasm_error(ERR_NONFATAL, "invalid effective address");
1917 p = bytes;
1918 *p++ = ea_data.modrm;
1919 if (ea_data.sib_present)
1920 *p++ = ea_data.sib;
1922 s = p - bytes;
1923 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
1926 * Make sure the address gets the right offset in case
1927 * the line breaks in the .lst file (BR 1197827)
1929 offset += s;
1930 s = 0;
1932 if (ea_data.bytes) {
1933 /* use compressed displacement, if available */
1934 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
1935 s += ea_data.bytes;
1936 if (ea_data.rip) {
1937 if (opy->segment == segment) {
1938 data -= insn_end;
1939 if (overflow_signed(data, ea_data.bytes))
1940 warn_overflow(ERR_PASS2, ea_data.bytes);
1941 out(offset, segment, &data, OUT_ADDRESS,
1942 ea_data.bytes, NO_SEG, NO_SEG);
1943 } else {
1944 /* overflow check in output/linker? */
1945 out(offset, segment, &data, OUT_REL4ADR,
1946 insn_end - offset, opy->segment, opy->wrt);
1948 } else {
1949 int asize = ins->addr_size >> 3;
1950 int atype = ea_data.bytes;
1952 if (overflow_general(data, asize) ||
1953 signed_bits(data, ins->addr_size) !=
1954 signed_bits(data, ea_data.bytes << 3))
1955 warn_overflow(ERR_PASS2, ea_data.bytes);
1957 if (asize > ea_data.bytes) {
1959 * If the address isn't the full width of
1960 * the address size, treat is as signed...
1962 atype = -atype;
1965 out(offset, segment, &data, OUT_ADDRESS,
1966 atype, opy->segment, opy->wrt);
1969 offset += s;
1971 break;
1973 default:
1974 nasm_panic(0, "internal instruction table corrupt"
1975 ": instruction code \\%o (0x%02X) given", c, c);
1976 break;
1981 static opflags_t regflag(const operand * o)
1983 if (!is_register(o->basereg))
1984 nasm_panic(0, "invalid operand passed to regflag()");
1985 return nasm_reg_flags[o->basereg];
1988 static int32_t regval(const operand * o)
1990 if (!is_register(o->basereg))
1991 nasm_panic(0, "invalid operand passed to regval()");
1992 return nasm_regvals[o->basereg];
1995 static int op_rexflags(const operand * o, int mask)
1997 opflags_t flags;
1998 int val;
2000 if (!is_register(o->basereg))
2001 nasm_panic(0, "invalid operand passed to op_rexflags()");
2003 flags = nasm_reg_flags[o->basereg];
2004 val = nasm_regvals[o->basereg];
2006 return rexflags(val, flags, mask);
2009 static int rexflags(int val, opflags_t flags, int mask)
2011 int rex = 0;
2013 if (val >= 0 && (val & 8))
2014 rex |= REX_B|REX_X|REX_R;
2015 if (flags & BITS64)
2016 rex |= REX_W;
2017 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2018 rex |= REX_H;
2019 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2020 rex |= REX_P;
2022 return rex & mask;
2025 static int evexflags(int val, decoflags_t deco,
2026 int mask, uint8_t byte)
2028 int evex = 0;
2030 switch (byte) {
2031 case 0:
2032 if (val >= 0 && (val & 16))
2033 evex |= (EVEX_P0RP | EVEX_P0X);
2034 break;
2035 case 2:
2036 if (val >= 0 && (val & 16))
2037 evex |= EVEX_P2VP;
2038 if (deco & Z)
2039 evex |= EVEX_P2Z;
2040 if (deco & OPMASK_MASK)
2041 evex |= deco & EVEX_P2AAA;
2042 break;
2044 return evex & mask;
2047 static int op_evexflags(const operand * o, int mask, uint8_t byte)
2049 int val;
2051 val = nasm_regvals[o->basereg];
2053 return evexflags(val, o->decoflags, mask, byte);
2056 static enum match_result find_match(const struct itemplate **tempp,
2057 insn *instruction,
2058 int32_t segment, int64_t offset, int bits)
2060 const struct itemplate *temp;
2061 enum match_result m, merr;
2062 opflags_t xsizeflags[MAX_OPERANDS];
2063 bool opsizemissing = false;
2064 int8_t broadcast = instruction->evex_brerop;
2065 int i;
2067 /* broadcasting uses a different data element size */
2068 for (i = 0; i < instruction->operands; i++)
2069 if (i == broadcast)
2070 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2071 else
2072 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
2074 merr = MERR_INVALOP;
2076 for (temp = nasm_instructions[instruction->opcode];
2077 temp->opcode != I_none; temp++) {
2078 m = matches(temp, instruction, bits);
2079 if (m == MOK_JUMP) {
2080 if (jmp_match(segment, offset, bits, instruction, temp))
2081 m = MOK_GOOD;
2082 else
2083 m = MERR_INVALOP;
2084 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
2086 * Missing operand size and a candidate for fuzzy matching...
2088 for (i = 0; i < temp->operands; i++)
2089 if (i == broadcast)
2090 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2091 else
2092 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
2093 opsizemissing = true;
2095 if (m > merr)
2096 merr = m;
2097 if (merr == MOK_GOOD)
2098 goto done;
2101 /* No match, but see if we can get a fuzzy operand size match... */
2102 if (!opsizemissing)
2103 goto done;
2105 for (i = 0; i < instruction->operands; i++) {
2107 * We ignore extrinsic operand sizes on registers, so we should
2108 * never try to fuzzy-match on them. This also resolves the case
2109 * when we have e.g. "xmmrm128" in two different positions.
2111 if (is_class(REGISTER, instruction->oprs[i].type))
2112 continue;
2114 /* This tests if xsizeflags[i] has more than one bit set */
2115 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2116 goto done; /* No luck */
2118 if (i == broadcast) {
2119 instruction->oprs[i].decoflags |= xsizeflags[i];
2120 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2121 BITS32 : BITS64);
2122 } else {
2123 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
2127 /* Try matching again... */
2128 for (temp = nasm_instructions[instruction->opcode];
2129 temp->opcode != I_none; temp++) {
2130 m = matches(temp, instruction, bits);
2131 if (m == MOK_JUMP) {
2132 if (jmp_match(segment, offset, bits, instruction, temp))
2133 m = MOK_GOOD;
2134 else
2135 m = MERR_INVALOP;
2137 if (m > merr)
2138 merr = m;
2139 if (merr == MOK_GOOD)
2140 goto done;
2143 done:
2144 *tempp = temp;
2145 return merr;
2148 static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2150 opflags_t opsize = opflags & SIZE_MASK;
2151 uint8_t brcast_num;
2154 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2155 * this cannot be a simple arithmetic calculation.
2157 if (brsize > BITS64)
2158 nasm_error(ERR_FATAL,
2159 "size of broadcasting element is greater than 64 bits");
2161 switch (opsize) {
2162 case BITS64:
2163 brcast_num = BITS64 / brsize;
2164 break;
2165 default:
2166 brcast_num = (opsize / BITS128) * (BITS64 / brsize) * 2;
2167 break;
2170 return brcast_num;
2173 static enum match_result matches(const struct itemplate *itemp,
2174 insn *instruction, int bits)
2176 opflags_t size[MAX_OPERANDS], asize;
2177 bool opsizemissing = false;
2178 int i, oprs;
2181 * Check the opcode
2183 if (itemp->opcode != instruction->opcode)
2184 return MERR_INVALOP;
2187 * Count the operands
2189 if (itemp->operands != instruction->operands)
2190 return MERR_INVALOP;
2193 * Is it legal?
2195 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
2196 return MERR_INVALOP;
2199 * {evex} available?
2201 switch (instruction->prefixes[PPS_VEX]) {
2202 case P_EVEX:
2203 if (!itemp_has(itemp, IF_EVEX))
2204 return MERR_ENCMISMATCH;
2205 break;
2206 case P_VEX3:
2207 case P_VEX2:
2208 if (!itemp_has(itemp, IF_VEX))
2209 return MERR_ENCMISMATCH;
2210 break;
2211 default:
2212 break;
2216 * Check that no spurious colons or TOs are present
2218 for (i = 0; i < itemp->operands; i++)
2219 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
2220 return MERR_INVALOP;
2223 * Process size flags
2225 switch (itemp_smask(itemp)) {
2226 case IF_GENBIT(IF_SB):
2227 asize = BITS8;
2228 break;
2229 case IF_GENBIT(IF_SW):
2230 asize = BITS16;
2231 break;
2232 case IF_GENBIT(IF_SD):
2233 asize = BITS32;
2234 break;
2235 case IF_GENBIT(IF_SQ):
2236 asize = BITS64;
2237 break;
2238 case IF_GENBIT(IF_SO):
2239 asize = BITS128;
2240 break;
2241 case IF_GENBIT(IF_SY):
2242 asize = BITS256;
2243 break;
2244 case IF_GENBIT(IF_SZ):
2245 asize = BITS512;
2246 break;
2247 case IF_GENBIT(IF_SIZE):
2248 switch (bits) {
2249 case 16:
2250 asize = BITS16;
2251 break;
2252 case 32:
2253 asize = BITS32;
2254 break;
2255 case 64:
2256 asize = BITS64;
2257 break;
2258 default:
2259 asize = 0;
2260 break;
2262 break;
2263 default:
2264 asize = 0;
2265 break;
2268 if (itemp_armask(itemp)) {
2269 /* S- flags only apply to a specific operand */
2270 i = itemp_arg(itemp);
2271 memset(size, 0, sizeof size);
2272 size[i] = asize;
2273 } else {
2274 /* S- flags apply to all operands */
2275 for (i = 0; i < MAX_OPERANDS; i++)
2276 size[i] = asize;
2280 * Check that the operand flags all match up,
2281 * it's a bit tricky so lets be verbose:
2283 * 1) Find out the size of operand. If instruction
2284 * doesn't have one specified -- we're trying to
2285 * guess it either from template (IF_S* flag) or
2286 * from code bits.
2288 * 2) If template operand do not match the instruction OR
2289 * template has an operand size specified AND this size differ
2290 * from which instruction has (perhaps we got it from code bits)
2291 * we are:
2292 * a) Check that only size of instruction and operand is differ
2293 * other characteristics do match
2294 * b) Perhaps it's a register specified in instruction so
2295 * for such a case we just mark that operand as "size
2296 * missing" and this will turn on fuzzy operand size
2297 * logic facility (handled by a caller)
2299 for (i = 0; i < itemp->operands; i++) {
2300 opflags_t type = instruction->oprs[i].type;
2301 decoflags_t deco = instruction->oprs[i].decoflags;
2302 bool is_broadcast = deco & BRDCAST_MASK;
2303 uint8_t brcast_num = 0;
2304 opflags_t template_opsize, insn_opsize;
2306 if (!(type & SIZE_MASK))
2307 type |= size[i];
2309 insn_opsize = type & SIZE_MASK;
2310 if (!is_broadcast) {
2311 template_opsize = itemp->opd[i] & SIZE_MASK;
2312 } else {
2313 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2315 * when broadcasting, the element size depends on
2316 * the instruction type. decorator flag should match.
2319 if (deco_brsize) {
2320 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
2321 /* calculate the proper number : {1to<brcast_num>} */
2322 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
2323 } else {
2324 template_opsize = 0;
2328 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
2329 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
2330 return MERR_INVALOP;
2331 } else if (template_opsize) {
2332 if (template_opsize != insn_opsize) {
2333 if (insn_opsize) {
2334 return MERR_INVALOP;
2335 } else if (!is_class(REGISTER, type)) {
2337 * Note: we don't honor extrinsic operand sizes for registers,
2338 * so "missing operand size" for a register should be
2339 * considered a wildcard match rather than an error.
2341 opsizemissing = true;
2343 } else if (is_broadcast &&
2344 (brcast_num !=
2345 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
2347 * broadcasting opsize matches but the number of repeated memory
2348 * element does not match.
2349 * if 64b double precision float is broadcasted to ymm (256b),
2350 * broadcasting decorator must be {1to4}.
2352 return MERR_BRNUMMISMATCH;
2357 if (opsizemissing)
2358 return MERR_OPSIZEMISSING;
2361 * Check operand sizes
2363 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2364 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
2365 for (i = 0; i < oprs; i++) {
2366 asize = itemp->opd[i] & SIZE_MASK;
2367 if (asize) {
2368 for (i = 0; i < oprs; i++)
2369 size[i] = asize;
2370 break;
2373 } else {
2374 oprs = itemp->operands;
2377 for (i = 0; i < itemp->operands; i++) {
2378 if (!(itemp->opd[i] & SIZE_MASK) &&
2379 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
2380 return MERR_OPSIZEMISMATCH;
2384 * Check template is okay at the set cpu level
2386 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
2387 return MERR_BADCPU;
2390 * Verify the appropriate long mode flag.
2392 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
2393 return MERR_BADMODE;
2396 * If we have a HLE prefix, look for the NOHLE flag
2398 if (itemp_has(itemp, IF_NOHLE) &&
2399 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2400 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2401 return MERR_BADHLE;
2404 * Check if special handling needed for Jumps
2406 if ((itemp->code[0] & ~1) == 0370)
2407 return MOK_JUMP;
2410 * Check if BND prefix is allowed.
2411 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
2413 if (!itemp_has(itemp, IF_BND) &&
2414 (has_prefix(instruction, PPS_REP, P_BND) ||
2415 has_prefix(instruction, PPS_REP, P_NOBND)))
2416 return MERR_BADBND;
2417 else if (itemp_has(itemp, IF_BND) &&
2418 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2419 has_prefix(instruction, PPS_REP, P_REPNZ)))
2420 return MERR_BADREPNE;
2422 return MOK_GOOD;
2426 * Check if ModR/M.mod should/can be 01.
2427 * - EAF_BYTEOFFS is set
2428 * - offset can fit in a byte when EVEX is not used
2429 * - offset can be compressed when EVEX is used
2431 #define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2432 (o >= -128 && o <= 127 && \
2433 seg == NO_SEG && !forw_ref && \
2434 !(input->eaflags & EAF_WORDOFFS) && \
2435 !(ins->rex & REX_EV)) || \
2436 (ins->rex & REX_EV && \
2437 is_disp8n(input, ins, &output->disp8)))
2439 static enum ea_type process_ea(operand *input, ea *output, int bits,
2440 int rfield, opflags_t rflags, insn *ins)
2442 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
2443 int addrbits = ins->addr_size;
2444 int eaflags = input->eaflags;
2446 output->type = EA_SCALAR;
2447 output->rip = false;
2448 output->disp8 = 0;
2450 /* REX flags for the rfield operand */
2451 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
2452 /* EVEX.R' flag for the REG operand */
2453 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
2455 if (is_class(REGISTER, input->type)) {
2457 * It's a direct register.
2459 if (!is_register(input->basereg))
2460 goto err;
2462 if (!is_reg_class(REG_EA, input->basereg))
2463 goto err;
2465 /* broadcasting is not available with a direct register operand. */
2466 if (input->decoflags & BRDCAST_MASK) {
2467 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2468 goto err;
2471 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
2472 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
2473 output->sib_present = false; /* no SIB necessary */
2474 output->bytes = 0; /* no offset necessary either */
2475 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2476 } else {
2478 * It's a memory reference.
2481 /* Embedded rounding or SAE is not available with a mem ref operand. */
2482 if (input->decoflags & (ER | SAE)) {
2483 nasm_error(ERR_NONFATAL,
2484 "Embedded rounding is available only with reg-reg op.");
2485 return -1;
2488 if (input->basereg == -1 &&
2489 (input->indexreg == -1 || input->scale == 0)) {
2491 * It's a pure offset.
2493 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2494 input->segment == NO_SEG) {
2495 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2496 input->type &= ~IP_REL;
2497 input->type |= MEMORY;
2500 if (bits == 64 &&
2501 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2502 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2503 return -1;
2506 if (eaflags & EAF_BYTEOFFS ||
2507 (eaflags & EAF_WORDOFFS &&
2508 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2509 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2512 if (bits == 64 && (~input->type & IP_REL)) {
2513 output->sib_present = true;
2514 output->sib = GEN_SIB(0, 4, 5);
2515 output->bytes = 4;
2516 output->modrm = GEN_MODRM(0, rfield, 4);
2517 output->rip = false;
2518 } else {
2519 output->sib_present = false;
2520 output->bytes = (addrbits != 16 ? 4 : 2);
2521 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2522 output->rip = bits == 64;
2524 } else {
2526 * It's an indirection.
2528 int i = input->indexreg, b = input->basereg, s = input->scale;
2529 int32_t seg = input->segment;
2530 int hb = input->hintbase, ht = input->hinttype;
2531 int t, it, bt; /* register numbers */
2532 opflags_t x, ix, bx; /* register flags */
2534 if (s == 0)
2535 i = -1; /* make this easy, at least */
2537 if (is_register(i)) {
2538 it = nasm_regvals[i];
2539 ix = nasm_reg_flags[i];
2540 } else {
2541 it = -1;
2542 ix = 0;
2545 if (is_register(b)) {
2546 bt = nasm_regvals[b];
2547 bx = nasm_reg_flags[b];
2548 } else {
2549 bt = -1;
2550 bx = 0;
2553 /* if either one are a vector register... */
2554 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
2555 opflags_t sok = BITS32 | BITS64;
2556 int32_t o = input->offset;
2557 int mod, scale, index, base;
2560 * For a vector SIB, one has to be a vector and the other,
2561 * if present, a GPR. The vector must be the index operand.
2563 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
2564 if (s == 0)
2565 s = 1;
2566 else if (s != 1)
2567 goto err;
2569 t = bt, bt = it, it = t;
2570 x = bx, bx = ix, ix = x;
2573 if (bt != -1) {
2574 if (REG_GPR & ~bx)
2575 goto err;
2576 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2577 sok &= bx;
2578 else
2579 goto err;
2583 * While we're here, ensure the user didn't specify
2584 * WORD or QWORD
2586 if (input->disp_size == 16 || input->disp_size == 64)
2587 goto err;
2589 if (addrbits == 16 ||
2590 (addrbits == 32 && !(sok & BITS32)) ||
2591 (addrbits == 64 && !(sok & BITS64)))
2592 goto err;
2594 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2595 : ((ix & YMMREG & ~REG_EA)
2596 ? EA_YMMVSIB : EA_XMMVSIB));
2598 output->rex |= rexflags(it, ix, REX_X);
2599 output->rex |= rexflags(bt, bx, REX_B);
2600 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
2602 index = it & 7; /* it is known to be != -1 */
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 !(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);
2640 } else if ((ix|bx) & (BITS32|BITS64)) {
2642 * it must be a 32/64-bit memory reference. Firstly we have
2643 * to check that all registers involved are type E/Rxx.
2645 opflags_t sok = BITS32 | BITS64;
2646 int32_t o = input->offset;
2648 if (it != -1) {
2649 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2650 sok &= ix;
2651 else
2652 goto err;
2655 if (bt != -1) {
2656 if (REG_GPR & ~bx)
2657 goto err; /* Invalid register */
2658 if (~sok & bx & SIZE_MASK)
2659 goto err; /* Invalid size */
2660 sok &= bx;
2664 * While we're here, ensure the user didn't specify
2665 * WORD or QWORD
2667 if (input->disp_size == 16 || input->disp_size == 64)
2668 goto err;
2670 if (addrbits == 16 ||
2671 (addrbits == 32 && !(sok & BITS32)) ||
2672 (addrbits == 64 && !(sok & BITS64)))
2673 goto err;
2675 /* now reorganize base/index */
2676 if (s == 1 && bt != it && bt != -1 && it != -1 &&
2677 ((hb == b && ht == EAH_NOTBASE) ||
2678 (hb == i && ht == EAH_MAKEBASE))) {
2679 /* swap if hints say so */
2680 t = bt, bt = it, it = t;
2681 x = bx, bx = ix, ix = x;
2684 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
2685 /* make single reg base, unless hint */
2686 bt = it, bx = ix, it = -1, ix = 0;
2688 if (eaflags & EAF_MIB) {
2689 /* only for mib operands */
2690 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2692 * make a single reg index [reg*1].
2693 * gas uses this form for an explicit index register.
2695 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2697 if ((ht == EAH_SUMMED) && bt == -1) {
2698 /* separate once summed index into [base, index] */
2699 bt = it, bx = ix, s--;
2701 } else {
2702 if (((s == 2 && it != REG_NUM_ESP &&
2703 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
2704 s == 3 || s == 5 || s == 9) && bt == -1) {
2705 /* convert 3*EAX to EAX+2*EAX */
2706 bt = it, bx = ix, s--;
2708 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2709 (eaflags & EAF_TIMESTWO) &&
2710 (hb == b && ht == EAH_NOTBASE)) {
2712 * convert [NOSPLIT EAX*1]
2713 * to sib format with 0x0 displacement - [EAX*1+0].
2715 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2718 if (s == 1 && it == REG_NUM_ESP) {
2719 /* swap ESP into base if scale is 1 */
2720 t = it, it = bt, bt = t;
2721 x = ix, ix = bx, bx = x;
2723 if (it == REG_NUM_ESP ||
2724 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
2725 goto err; /* wrong, for various reasons */
2727 output->rex |= rexflags(it, ix, REX_X);
2728 output->rex |= rexflags(bt, bx, REX_B);
2730 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
2731 /* no SIB needed */
2732 int mod, rm;
2734 if (bt == -1) {
2735 rm = 5;
2736 mod = 0;
2737 } else {
2738 rm = (bt & 7);
2739 if (rm != REG_NUM_EBP && o == 0 &&
2740 seg == NO_SEG && !forw_ref &&
2741 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2742 mod = 0;
2743 else if (IS_MOD_01())
2744 mod = 1;
2745 else
2746 mod = 2;
2749 output->sib_present = false;
2750 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2751 output->modrm = GEN_MODRM(mod, rfield, rm);
2752 } else {
2753 /* we need a SIB */
2754 int mod, scale, index, base;
2756 if (it == -1)
2757 index = 4, s = 1;
2758 else
2759 index = (it & 7);
2761 switch (s) {
2762 case 1:
2763 scale = 0;
2764 break;
2765 case 2:
2766 scale = 1;
2767 break;
2768 case 4:
2769 scale = 2;
2770 break;
2771 case 8:
2772 scale = 3;
2773 break;
2774 default: /* then what the smeg is it? */
2775 goto err; /* panic */
2778 if (bt == -1) {
2779 base = 5;
2780 mod = 0;
2781 } else {
2782 base = (bt & 7);
2783 if (base != REG_NUM_EBP && o == 0 &&
2784 seg == NO_SEG && !forw_ref &&
2785 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2786 mod = 0;
2787 else if (IS_MOD_01())
2788 mod = 1;
2789 else
2790 mod = 2;
2793 output->sib_present = true;
2794 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2795 output->modrm = GEN_MODRM(mod, rfield, 4);
2796 output->sib = GEN_SIB(scale, index, base);
2798 } else { /* it's 16-bit */
2799 int mod, rm;
2800 int16_t o = input->offset;
2802 /* check for 64-bit long mode */
2803 if (addrbits == 64)
2804 goto err;
2806 /* check all registers are BX, BP, SI or DI */
2807 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2808 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
2809 goto err;
2811 /* ensure the user didn't specify DWORD/QWORD */
2812 if (input->disp_size == 32 || input->disp_size == 64)
2813 goto err;
2815 if (s != 1 && i != -1)
2816 goto err; /* no can do, in 16-bit EA */
2817 if (b == -1 && i != -1) {
2818 int tmp = b;
2819 b = i;
2820 i = tmp;
2821 } /* swap */
2822 if ((b == R_SI || b == R_DI) && i != -1) {
2823 int tmp = b;
2824 b = i;
2825 i = tmp;
2827 /* have BX/BP as base, SI/DI index */
2828 if (b == i)
2829 goto err; /* shouldn't ever happen, in theory */
2830 if (i != -1 && b != -1 &&
2831 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
2832 goto err; /* invalid combinations */
2833 if (b == -1) /* pure offset: handled above */
2834 goto err; /* so if it gets to here, panic! */
2836 rm = -1;
2837 if (i != -1)
2838 switch (i * 256 + b) {
2839 case R_SI * 256 + R_BX:
2840 rm = 0;
2841 break;
2842 case R_DI * 256 + R_BX:
2843 rm = 1;
2844 break;
2845 case R_SI * 256 + R_BP:
2846 rm = 2;
2847 break;
2848 case R_DI * 256 + R_BP:
2849 rm = 3;
2850 break;
2851 } else
2852 switch (b) {
2853 case R_SI:
2854 rm = 4;
2855 break;
2856 case R_DI:
2857 rm = 5;
2858 break;
2859 case R_BP:
2860 rm = 6;
2861 break;
2862 case R_BX:
2863 rm = 7;
2864 break;
2866 if (rm == -1) /* can't happen, in theory */
2867 goto err; /* so panic if it does */
2869 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2870 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2871 mod = 0;
2872 else if (IS_MOD_01())
2873 mod = 1;
2874 else
2875 mod = 2;
2877 output->sib_present = false; /* no SIB - it's 16-bit */
2878 output->bytes = mod; /* bytes of offset needed */
2879 output->modrm = GEN_MODRM(mod, rfield, rm);
2884 output->size = 1 + output->sib_present + output->bytes;
2885 return output->type;
2887 err:
2888 return output->type = EA_INVALID;
2891 static void add_asp(insn *ins, int addrbits)
2893 int j, valid;
2894 int defdisp;
2896 valid = (addrbits == 64) ? 64|32 : 32|16;
2898 switch (ins->prefixes[PPS_ASIZE]) {
2899 case P_A16:
2900 valid &= 16;
2901 break;
2902 case P_A32:
2903 valid &= 32;
2904 break;
2905 case P_A64:
2906 valid &= 64;
2907 break;
2908 case P_ASP:
2909 valid &= (addrbits == 32) ? 16 : 32;
2910 break;
2911 default:
2912 break;
2915 for (j = 0; j < ins->operands; j++) {
2916 if (is_class(MEMORY, ins->oprs[j].type)) {
2917 opflags_t i, b;
2919 /* Verify as Register */
2920 if (!is_register(ins->oprs[j].indexreg))
2921 i = 0;
2922 else
2923 i = nasm_reg_flags[ins->oprs[j].indexreg];
2925 /* Verify as Register */
2926 if (!is_register(ins->oprs[j].basereg))
2927 b = 0;
2928 else
2929 b = nasm_reg_flags[ins->oprs[j].basereg];
2931 if (ins->oprs[j].scale == 0)
2932 i = 0;
2934 if (!i && !b) {
2935 int ds = ins->oprs[j].disp_size;
2936 if ((addrbits != 64 && ds > 8) ||
2937 (addrbits == 64 && ds == 16))
2938 valid &= ds;
2939 } else {
2940 if (!(REG16 & ~b))
2941 valid &= 16;
2942 if (!(REG32 & ~b))
2943 valid &= 32;
2944 if (!(REG64 & ~b))
2945 valid &= 64;
2947 if (!(REG16 & ~i))
2948 valid &= 16;
2949 if (!(REG32 & ~i))
2950 valid &= 32;
2951 if (!(REG64 & ~i))
2952 valid &= 64;
2957 if (valid & addrbits) {
2958 ins->addr_size = addrbits;
2959 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
2960 /* Add an address size prefix */
2961 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
2962 ins->addr_size = (addrbits == 32) ? 16 : 32;
2963 } else {
2964 /* Impossible... */
2965 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
2966 ins->addr_size = addrbits; /* Error recovery */
2969 defdisp = ins->addr_size == 16 ? 16 : 32;
2971 for (j = 0; j < ins->operands; j++) {
2972 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2973 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2975 * mem_offs sizes must match the address size; if not,
2976 * strip the MEM_OFFS bit and match only EA instructions
2978 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);