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
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 * \20..\23 - a byte immediate operand, from operand 0..3
46 * \24..\27 - a zero-extended byte immediate operand, from operand 0..3
47 * \30..\33 - a word immediate operand, from operand 0..3
48 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
49 * assembly mode or the operand-size override on the operand
50 * \40..\43 - a long immediate operand, from operand 0..3
51 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
52 * depending on the address size of the instruction.
53 * \50..\53 - a byte relative operand, from operand 0..3
54 * \54..\57 - a qword immediate operand, from operand 0..3
55 * \60..\63 - a word relative operand, from operand 0..3
56 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
57 * assembly mode or the operand-size override on the operand
58 * \70..\73 - a long relative operand, from operand 0..3
59 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
60 * \1ab - a ModRM, calculated on EA in operand a, with the spare
61 * field the register value of operand b.
62 * \172\ab - the register number from operand a in bits 7..4, with
63 * the 4-bit immediate from operand b in bits 3..0.
64 * \173\xab - the register number from operand a in bits 7..4, with
65 * the value b in bits 3..0.
66 * \174..\177 - the register number from operand 0..3 in bits 7..4, and
67 * an arbitrary value in bits 3..0 (assembled as zero.)
68 * \2ab - a ModRM, calculated on EA in operand a, with the spare
69 * field equal to digit b.
71 * \240..\243 - this instruction uses EVEX rather than REX or VEX/XOP, with the
72 * V field taken from operand 0..3.
73 * \250 - this instruction uses EVEX rather than REX or VEX/XOP, with the
74 * V field set to 1111b.
75 * EVEX prefixes are followed by the sequence:
76 * \cm\wlp\tup where cm is:
78 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
81 * [l0] ll = 0 (.128, .lz)
84 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
86 * [w0] ww = 0 for W = 0
87 * [w1] ww = 1 for W = 1
88 * [wig] ww = 2 for W don't care (always assembled as 0)
89 * [ww] ww = 3 for W used as REX.W
91 * [p0] pp = 0 for no prefix
92 * [60] pp = 1 for legacy prefix 60
96 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
97 * (compressed displacement encoding)
99 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
100 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
101 * V field taken from operand 0..3.
102 * \270 - this instruction uses VEX/XOP rather than REX, with the
103 * V field set to 1111b.
105 * VEX/XOP prefixes are followed by the sequence:
106 * \tmm\wlp where mm is the M field; and wlp is:
108 * [l0] ll = 0 for L = 0 (.128, .lz)
109 * [l1] ll = 1 for L = 1 (.256)
110 * [lig] ll = 2 for L don't care (always assembled as 0)
112 * [w0] ww = 0 for W = 0
113 * [w1 ] ww = 1 for W = 1
114 * [wig] ww = 2 for W don't care (always assembled as 0)
115 * [ww] ww = 3 for W used as REX.W
117 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
119 * \271 - instruction takes XRELEASE (F3) with or without lock
120 * \272 - instruction takes XACQUIRE/XRELEASE with or without lock
121 * \273 - instruction takes XACQUIRE/XRELEASE with lock only
122 * \274..\277 - a byte immediate operand, from operand 0..3, sign-extended
123 * to the operand size (if o16/o32/o64 present) or the bit size
124 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
125 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
126 * \312 - (disassembler only) invalid with non-default address size.
127 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
128 * \314 - (disassembler only) invalid with REX.B
129 * \315 - (disassembler only) invalid with REX.X
130 * \316 - (disassembler only) invalid with REX.R
131 * \317 - (disassembler only) invalid with REX.W
132 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
133 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
134 * \322 - indicates that this instruction is only valid when the
135 * operand size is the default (instruction to disassembler,
136 * generates no code in the assembler)
137 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
138 * \324 - indicates 64-bit operand size requiring REX prefix.
139 * \325 - instruction which always uses spl/bpl/sil/dil
140 * \326 - instruction not valid with 0xF3 REP prefix. Hint for
141 disassembler only; for SSE instructions.
142 * \330 - a literal byte follows in the code stream, to be added
143 * to the condition code value of the instruction.
144 * \331 - instruction not valid with REP prefix. Hint for
145 * disassembler only; for SSE instructions.
146 * \332 - REP prefix (0xF2 byte) used as opcode extension.
147 * \333 - REP prefix (0xF3 byte) used as opcode extension.
148 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
149 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
150 * \336 - force a REP(E) prefix (0xF3) even if not specified.
151 * \337 - force a REPNE prefix (0xF2) even if not specified.
152 * \336-\337 are still listed as prefixes in the disassembler.
153 * \340 - reserve <operand 0> bytes of uninitialized storage.
154 * Operand 0 had better be a segmentless constant.
155 * \341 - this instruction needs a WAIT "prefix"
156 * \360 - no SSE prefix (== \364\331)
157 * \361 - 66 SSE prefix (== \366\331)
158 * \364 - operand-size prefix (0x66) not permitted
159 * \365 - address-size prefix (0x67) not permitted
160 * \366 - operand-size prefix (0x66) used as opcode extension
161 * \367 - address-size prefix (0x67) used as opcode extension
162 * \370,\371 - match only if operand 0 meets byte jump criteria.
163 * 370 is used for Jcc, 371 is used for JMP.
164 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
165 * used for conditional jump over longer jump
166 * \374 - this instruction takes an XMM VSIB memory EA
167 * \375 - this instruction takes an YMM VSIB memory EA
168 * \376 - this instruction takes an ZMM VSIB memory EA
171 #include "compiler.h"
175 #include <inttypes.h>
179 #include "assemble.h"
185 * Matching errors. These should be sorted so that more specific
186 * errors come later in the sequence.
195 * Matching success; the conditional ones first
197 MOK_JUMP, /* Matching OK but needs jmp_match() */
198 MOK_GOOD /* Matching unconditionally OK */
202 enum ea_type type; /* what kind of EA is this? */
203 int sib_present; /* is a SIB byte necessary? */
204 int bytes; /* # of bytes of offset needed */
205 int size; /* lazy - this is sib+bytes+1 */
206 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
207 int8_t disp8; /* compressed displacement for EVEX */
210 #define GEN_SIB(scale, index, base) \
211 (((scale) << 6) | ((index) << 3) | ((base)))
213 #define GEN_MODRM(mod, reg, rm) \
214 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
216 static uint32_t cpu; /* cpu level received from nasm.c */
217 static efunc errfunc;
218 static struct ofmt *outfmt;
219 static ListGen *list;
221 static int64_t calcsize(int32_t, int64_t, int, insn *,
222 const struct itemplate *);
223 static void gencode(int32_t segment, int64_t offset, int bits,
224 insn * ins, const struct itemplate *temp,
226 static enum match_result find_match(const struct itemplate **tempp,
228 int32_t segment, int64_t offset, int bits);
229 static enum match_result matches(const struct itemplate *, insn *, int bits);
230 static opflags_t regflag(const operand *);
231 static int32_t regval(const operand *);
232 static int rexflags(int, opflags_t, int);
233 static int op_rexflags(const operand *, int);
234 static int op_evexflags(const operand *, int, uint8_t);
235 static void add_asp(insn *, int);
237 static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
239 static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
241 return ins->prefixes[pos] == prefix;
244 static void assert_no_prefix(insn * ins, enum prefix_pos pos)
246 if (ins->prefixes[pos])
247 errfunc(ERR_NONFATAL, "invalid %s prefix",
248 prefix_name(ins->prefixes[pos]));
251 static const char *size_name(int size)
275 static void warn_overflow(int pass, int size)
277 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
278 "%s data exceeds bounds", size_name(size));
281 static void warn_overflow_const(int64_t data, int size)
283 if (overflow_general(data, size))
284 warn_overflow(ERR_PASS1, size);
287 static void warn_overflow_opd(const struct operand *o, int size)
289 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
290 if (overflow_general(o->offset, size))
291 warn_overflow(ERR_PASS2, size);
296 * This routine wrappers the real output format's output routine,
297 * in order to pass a copy of the data off to the listing file
298 * generator at the same time.
300 static void out(int64_t offset, int32_t segto, const void *data,
301 enum out_type type, uint64_t size,
302 int32_t segment, int32_t wrt)
304 static int32_t lineno = 0; /* static!!! */
305 static char *lnfname = NULL;
308 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
310 * This is a non-relocated address, and we're going to
311 * convert it into RAWDATA format.
316 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
320 WRITEADDR(q, *(int64_t *)data, size);
325 list->output(offset, data, type, size);
328 * this call to src_get determines when we call the
329 * debug-format-specific "linenum" function
330 * it updates lineno and lnfname to the current values
331 * returning 0 if "same as last time", -2 if lnfname
332 * changed, and the amount by which lineno changed,
333 * if it did. thus, these variables must be static
336 if (src_get(&lineno, &lnfname))
337 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
339 outfmt->output(segto, data, type, size, segment, wrt);
342 static void out_imm8(int64_t offset, int32_t segment, struct operand *opx)
344 if (opx->segment != NO_SEG) {
345 uint64_t data = opx->offset;
346 out(offset, segment, &data, OUT_ADDRESS, 1, opx->segment, opx->wrt);
348 uint8_t byte = opx->offset;
349 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
353 static bool jmp_match(int32_t segment, int64_t offset, int bits,
354 insn * ins, const struct itemplate *temp)
357 const uint8_t *code = temp->code;
360 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
364 if (optimizing < 0 && c == 0371)
367 isize = calcsize(segment, offset, bits, ins, temp);
369 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
370 /* Be optimistic in pass 1 */
373 if (ins->oprs[0].segment != segment)
376 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
377 return (isize >= -128 && isize <= 127); /* is it byte size? */
380 int64_t assemble(int32_t segment, int64_t offset, int bits, uint32_t cp,
381 insn * instruction, struct ofmt *output, efunc error,
384 const struct itemplate *temp;
389 int64_t start = offset;
390 int64_t wsize; /* size for DB etc. */
392 errfunc = error; /* to pass to other functions */
394 outfmt = output; /* likewise */
395 list = listgen; /* and again */
397 wsize = idata_bytes(instruction->opcode);
403 int32_t t = instruction->times;
406 "instruction->times < 0 (%ld) in assemble()", t);
408 while (t--) { /* repeat TIMES times */
409 list_for_each(e, instruction->eops) {
410 if (e->type == EOT_DB_NUMBER) {
412 errfunc(ERR_NONFATAL,
413 "integer supplied to a DT, DO or DY"
416 out(offset, segment, &e->offset,
417 OUT_ADDRESS, wsize, e->segment, e->wrt);
420 } else if (e->type == EOT_DB_STRING ||
421 e->type == EOT_DB_STRING_FREE) {
424 out(offset, segment, e->stringval,
425 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
426 align = e->stringlen % wsize;
429 align = wsize - align;
430 out(offset, segment, zero_buffer,
431 OUT_RAWDATA, align, NO_SEG, NO_SEG);
433 offset += e->stringlen + align;
436 if (t > 0 && t == instruction->times - 1) {
438 * Dummy call to list->output to give the offset to the
441 list->output(offset, NULL, OUT_RAWDATA, 0);
442 list->uplevel(LIST_TIMES);
445 if (instruction->times > 1)
446 list->downlevel(LIST_TIMES);
447 return offset - start;
450 if (instruction->opcode == I_INCBIN) {
451 const char *fname = instruction->eops->stringval;
454 fp = fopen(fname, "rb");
456 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
458 } else if (fseek(fp, 0L, SEEK_END) < 0) {
459 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
463 static char buf[4096];
464 size_t t = instruction->times;
469 if (instruction->eops->next) {
470 base = instruction->eops->next->offset;
472 if (instruction->eops->next->next &&
473 len > (size_t)instruction->eops->next->next->offset)
474 len = (size_t)instruction->eops->next->next->offset;
477 * Dummy call to list->output to give the offset to the
480 list->output(offset, NULL, OUT_RAWDATA, 0);
481 list->uplevel(LIST_INCBIN);
485 fseek(fp, base, SEEK_SET);
489 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
492 * This shouldn't happen unless the file
493 * actually changes while we are reading
497 "`incbin': unexpected EOF while"
498 " reading file `%s'", fname);
499 t = 0; /* Try to exit cleanly */
502 out(offset, segment, buf, OUT_RAWDATA, m,
507 list->downlevel(LIST_INCBIN);
508 if (instruction->times > 1) {
510 * Dummy call to list->output to give the offset to the
513 list->output(offset, NULL, OUT_RAWDATA, 0);
514 list->uplevel(LIST_TIMES);
515 list->downlevel(LIST_TIMES);
518 return instruction->times * len;
520 return 0; /* if we're here, there's an error */
523 /* Check to see if we need an address-size prefix */
524 add_asp(instruction, bits);
526 m = find_match(&temp, instruction, segment, offset, bits);
530 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
531 itimes = instruction->times;
532 if (insn_size < 0) /* shouldn't be, on pass two */
533 error(ERR_PANIC, "errors made it through from pass one");
536 for (j = 0; j < MAXPREFIX; j++) {
538 switch (instruction->prefixes[j]) {
558 error(ERR_WARNING | ERR_PASS2,
559 "cs segment base generated, but will be ignored in 64-bit mode");
565 error(ERR_WARNING | ERR_PASS2,
566 "ds segment base generated, but will be ignored in 64-bit mode");
572 error(ERR_WARNING | ERR_PASS2,
573 "es segment base generated, but will be ignored in 64-bit mode");
585 error(ERR_WARNING | ERR_PASS2,
586 "ss segment base generated, but will be ignored in 64-bit mode");
593 "segr6 and segr7 cannot be used as prefixes");
598 "16-bit addressing is not supported "
600 } else if (bits != 16)
610 "64-bit addressing is only supported "
634 error(ERR_PANIC, "invalid instruction prefix");
637 out(offset, segment, &c, OUT_RAWDATA, 1,
642 insn_end = offset + insn_size;
643 gencode(segment, offset, bits, instruction,
646 if (itimes > 0 && itimes == instruction->times - 1) {
648 * Dummy call to list->output to give the offset to the
651 list->output(offset, NULL, OUT_RAWDATA, 0);
652 list->uplevel(LIST_TIMES);
655 if (instruction->times > 1)
656 list->downlevel(LIST_TIMES);
657 return offset - start;
661 case MERR_OPSIZEMISSING:
662 error(ERR_NONFATAL, "operation size not specified");
664 case MERR_OPSIZEMISMATCH:
665 error(ERR_NONFATAL, "mismatch in operand sizes");
668 error(ERR_NONFATAL, "no instruction for this cpu level");
671 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
676 "invalid combination of opcode and operands");
683 int64_t insn_size(int32_t segment, int64_t offset, int bits, uint32_t cp,
684 insn * instruction, efunc error)
686 const struct itemplate *temp;
689 errfunc = error; /* to pass to other functions */
692 if (instruction->opcode == I_none)
695 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
696 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
697 instruction->opcode == I_DT || instruction->opcode == I_DO ||
698 instruction->opcode == I_DY) {
700 int32_t isize, osize, wsize;
703 wsize = idata_bytes(instruction->opcode);
705 list_for_each(e, instruction->eops) {
709 if (e->type == EOT_DB_NUMBER) {
711 warn_overflow_const(e->offset, wsize);
712 } else if (e->type == EOT_DB_STRING ||
713 e->type == EOT_DB_STRING_FREE)
714 osize = e->stringlen;
716 align = (-osize) % wsize;
719 isize += osize + align;
721 return isize * instruction->times;
724 if (instruction->opcode == I_INCBIN) {
725 const char *fname = instruction->eops->stringval;
730 fp = fopen(fname, "rb");
732 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
734 else if (fseek(fp, 0L, SEEK_END) < 0)
735 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
739 if (instruction->eops->next) {
740 len -= instruction->eops->next->offset;
741 if (instruction->eops->next->next &&
742 len > (size_t)instruction->eops->next->next->offset) {
743 len = (size_t)instruction->eops->next->next->offset;
746 val = instruction->times * len;
753 /* Check to see if we need an address-size prefix */
754 add_asp(instruction, bits);
756 m = find_match(&temp, instruction, segment, offset, bits);
758 /* we've matched an instruction. */
762 isize = calcsize(segment, offset, bits, instruction, temp);
765 for (j = 0; j < MAXPREFIX; j++) {
766 switch (instruction->prefixes[j]) {
792 return isize * instruction->times;
794 return -1; /* didn't match any instruction */
798 static void bad_hle_warn(const insn * ins, uint8_t hleok)
800 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
801 enum whatwarn { w_none, w_lock, w_inval } ww;
802 static const enum whatwarn warn[2][4] =
804 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
805 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
809 n = (unsigned int)rep_pfx - P_XACQUIRE;
811 return; /* Not XACQUIRE/XRELEASE */
814 if (!is_class(MEMORY, ins->oprs[0].type))
815 ww = w_inval; /* HLE requires operand 0 to be memory */
822 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
823 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
824 "%s with this instruction requires lock",
825 prefix_name(rep_pfx));
830 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
831 "%s invalid with this instruction",
832 prefix_name(rep_pfx));
837 /* Common construct */
838 #define case3(x) case (x): case (x)+1: case (x)+2
839 #define case4(x) case3(x): case (x)+3
841 static int64_t calcsize(int32_t segment, int64_t offset, int bits,
842 insn * ins, const struct itemplate *temp)
844 const uint8_t *codes = temp->code;
853 bool lockcheck = true;
855 ins->rex = 0; /* Ensure REX is reset */
856 eat = EA_SCALAR; /* Expect a scalar EA */
857 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
859 if (ins->prefixes[PPS_OSIZE] == P_O64)
862 (void)segment; /* Don't warn that this parameter is unused */
863 (void)offset; /* Don't warn that this parameter is unused */
867 op1 = (c & 3) + ((opex & 1) << 2);
868 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
869 opx = &ins->oprs[op1];
870 opex = 0; /* For the next iteration */
874 codes += c, length += c;
883 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
897 if (opx->type & (BITS16 | BITS32 | BITS64))
898 length += (opx->type & BITS16) ? 2 : 4;
900 length += (bits == 16) ? 2 : 4;
908 length += ins->addr_size >> 3;
916 length += 8; /* MOV reg64/imm */
924 if (opx->type & (BITS16 | BITS32 | BITS64))
925 length += (opx->type & BITS16) ? 2 : 4;
927 length += (bits == 16) ? 2 : 4;
950 ins->vexreg = regval(opx);
951 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
952 ins->vex_cm = *codes++;
953 ins->vex_wlp = *codes++;
954 ins->evex_tuple = (*codes++ - 0300);
960 ins->vex_cm = *codes++;
961 ins->vex_wlp = *codes++;
962 ins->evex_tuple = (*codes++ - 0300);
971 ins->vexreg = regval(opx);
972 ins->vex_cm = *codes++;
973 ins->vex_wlp = *codes++;
979 ins->vex_cm = *codes++;
980 ins->vex_wlp = *codes++;
997 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
1001 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
1008 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1009 has_prefix(ins, PPS_ASIZE, P_A32))
1018 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1022 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1024 ins->prefixes[PPS_OSIZE] = P_O16;
1030 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1034 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1036 ins->prefixes[PPS_OSIZE] = P_O32;
1078 if (!ins->prefixes[PPS_REP])
1079 ins->prefixes[PPS_REP] = P_REP;
1083 if (!ins->prefixes[PPS_REP])
1084 ins->prefixes[PPS_REP] = P_REPNE;
1088 if (ins->oprs[0].segment != NO_SEG)
1089 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1090 " quantity of BSS space");
1092 length += ins->oprs[0].offset;
1096 if (!ins->prefixes[PPS_WAIT])
1097 ins->prefixes[PPS_WAIT] = P_WAIT;
1151 struct operand *opy = &ins->oprs[op2];
1152 struct operand *oplast;
1154 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
1157 /* pick rfield from operand b (opx) */
1158 rflags = regflag(opx);
1159 rfield = nasm_regvals[opx->basereg];
1160 /* find the last SIMD operand where ER decorator resides */
1161 oplast = &ins->oprs[op1 > op2 ? op1 : op2];
1168 if (oplast->decoflags & ER) {
1169 /* set EVEX.RC (rounding control) and b */
1170 ins->evex_p[2] |= (((ins->evex_rm - BRC_RN) << 5) & EVEX_P2LL) |
1173 /* set EVEX.L'L (vector length) */
1174 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
1175 if ((oplast->decoflags & SAE) ||
1176 (opy->decoflags & BRDCAST_MASK)) {
1178 ins->evex_p[2] |= EVEX_P2B;
1182 if (process_ea(opy, &ea_data, bits,
1183 rfield, rflags, ins) != eat) {
1184 errfunc(ERR_NONFATAL, "invalid effective address");
1187 ins->rex |= ea_data.rex;
1188 length += ea_data.size;
1194 errfunc(ERR_PANIC, "internal instruction table corrupt"
1195 ": instruction code \\%o (0x%02X) given", c, c);
1200 ins->rex &= rex_mask;
1202 if (ins->rex & REX_NH) {
1203 if (ins->rex & REX_H) {
1204 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1207 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
1210 if (ins->rex & (REX_V | REX_EV)) {
1211 int bad32 = REX_R|REX_W|REX_X|REX_B;
1213 if (ins->rex & REX_H) {
1214 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
1217 switch (ins->vex_wlp & 060) {
1231 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
1232 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1235 if (ins->rex & REX_EV)
1237 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)))
1241 } else if (ins->rex & REX_REAL) {
1242 if (ins->rex & REX_H) {
1243 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1245 } else if (bits == 64) {
1247 } else if ((ins->rex & REX_L) &&
1248 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
1251 assert_no_prefix(ins, PPS_LOCK);
1252 lockcheck = false; /* Already errored, no need for warning */
1255 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1260 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
1261 (!(temp->flags & IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
1262 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
1263 "instruction is not lockable");
1266 bad_hle_warn(ins, hleok);
1271 static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1274 if ((ins->rex & REX_REAL) && !(ins->rex & (REX_V | REX_EV))) {
1275 ins->rex = (ins->rex & REX_REAL) | REX_P;
1276 out(offset, segment, &ins->rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1285 static void gencode(int32_t segment, int64_t offset, int bits,
1286 insn * ins, const struct itemplate *temp,
1294 struct operand *opx;
1295 const uint8_t *codes = temp->code;
1297 enum ea_type eat = EA_SCALAR;
1301 op1 = (c & 3) + ((opex & 1) << 2);
1302 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1303 opx = &ins->oprs[op1];
1304 opex = 0; /* For the next iteration */
1311 offset += emit_rex(ins, segment, offset, bits);
1312 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
1324 offset += emit_rex(ins, segment, offset, bits);
1325 bytes[0] = *codes++ + (regval(opx) & 7);
1326 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1331 if (opx->offset < -256 || opx->offset > 255) {
1332 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1333 "byte value exceeds bounds");
1335 out_imm8(offset, segment, opx);
1340 if (opx->offset < 0 || opx->offset > 255)
1341 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1342 "unsigned byte value exceeds bounds");
1343 out_imm8(offset, segment, opx);
1348 warn_overflow_opd(opx, 2);
1350 out(offset, segment, &data, OUT_ADDRESS, 2,
1351 opx->segment, opx->wrt);
1356 if (opx->type & (BITS16 | BITS32))
1357 size = (opx->type & BITS16) ? 2 : 4;
1359 size = (bits == 16) ? 2 : 4;
1360 warn_overflow_opd(opx, size);
1362 out(offset, segment, &data, OUT_ADDRESS, size,
1363 opx->segment, opx->wrt);
1368 warn_overflow_opd(opx, 4);
1370 out(offset, segment, &data, OUT_ADDRESS, 4,
1371 opx->segment, opx->wrt);
1377 size = ins->addr_size >> 3;
1378 warn_overflow_opd(opx, size);
1379 out(offset, segment, &data, OUT_ADDRESS, size,
1380 opx->segment, opx->wrt);
1385 if (opx->segment != segment) {
1387 out(offset, segment, &data,
1388 OUT_REL1ADR, insn_end - offset,
1389 opx->segment, opx->wrt);
1391 data = opx->offset - insn_end;
1392 if (data > 127 || data < -128)
1393 errfunc(ERR_NONFATAL, "short jump is out of range");
1394 out(offset, segment, &data,
1395 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1401 data = (int64_t)opx->offset;
1402 out(offset, segment, &data, OUT_ADDRESS, 8,
1403 opx->segment, opx->wrt);
1408 if (opx->segment != segment) {
1410 out(offset, segment, &data,
1411 OUT_REL2ADR, insn_end - offset,
1412 opx->segment, opx->wrt);
1414 data = opx->offset - insn_end;
1415 out(offset, segment, &data,
1416 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
1422 if (opx->type & (BITS16 | BITS32 | BITS64))
1423 size = (opx->type & BITS16) ? 2 : 4;
1425 size = (bits == 16) ? 2 : 4;
1426 if (opx->segment != segment) {
1428 out(offset, segment, &data,
1429 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1430 insn_end - offset, opx->segment, opx->wrt);
1432 data = opx->offset - insn_end;
1433 out(offset, segment, &data,
1434 OUT_ADDRESS, size, NO_SEG, NO_SEG);
1440 if (opx->segment != segment) {
1442 out(offset, segment, &data,
1443 OUT_REL4ADR, insn_end - offset,
1444 opx->segment, opx->wrt);
1446 data = opx->offset - insn_end;
1447 out(offset, segment, &data,
1448 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
1454 if (opx->segment == NO_SEG)
1455 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1458 out(offset, segment, &data, OUT_ADDRESS, 2,
1459 outfmt->segbase(1 + opx->segment),
1466 opx = &ins->oprs[c >> 3];
1467 bytes[0] = nasm_regvals[opx->basereg] << 4;
1468 opx = &ins->oprs[c & 7];
1469 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1470 errfunc(ERR_NONFATAL,
1471 "non-absolute expression not permitted as argument %d",
1474 if (opx->offset & ~15) {
1475 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1476 "four-bit argument exceeds bounds");
1478 bytes[0] |= opx->offset & 15;
1480 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1486 opx = &ins->oprs[c >> 4];
1487 bytes[0] = nasm_regvals[opx->basereg] << 4;
1489 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1494 bytes[0] = nasm_regvals[opx->basereg] << 4;
1495 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1501 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1502 (int32_t)data != (int64_t)data) {
1503 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1504 "signed dword immediate exceeds bounds");
1506 out(offset, segment, &data, OUT_ADDRESS, 4,
1507 opx->segment, opx->wrt);
1514 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1515 EVEX_P2Z | EVEX_P2AAA, 2);
1516 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1518 /* EVEX.X can be set by either REX or EVEX for different reasons */
1519 bytes[1] = (~(((ins->rex & 7) << 5) |
1520 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) & 0xf0) |
1522 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1523 ((~ins->vexreg & 15) << 3) |
1524 (1 << 2) | (ins->vex_wlp & 3);
1525 bytes[3] = ins->evex_p[2];
1526 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1533 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B))) {
1534 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1535 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1536 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
1537 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
1538 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1542 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
1543 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
1544 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1559 if (ins->rex & REX_W)
1561 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1563 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1568 um = (uint64_t)2 << (s-1);
1571 if (uv > 127 && uv < (uint64_t)-128 &&
1572 (uv < um-128 || uv > um-1)) {
1573 /* If this wasn't explicitly byte-sized, warn as though we
1574 * had fallen through to the imm16/32/64 case.
1576 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1577 "%s value exceeds bounds",
1578 (opx->type & BITS8) ? "signed byte" :
1583 if (opx->segment != NO_SEG) {
1585 out(offset, segment, &data, OUT_ADDRESS, 1,
1586 opx->segment, opx->wrt);
1589 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1600 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
1602 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1609 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
1611 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1646 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
1647 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1656 *bytes = c - 0332 + 0xF2;
1657 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1662 if (ins->rex & REX_R) {
1664 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1667 ins->rex &= ~(REX_L|REX_R);
1678 if (ins->oprs[0].segment != NO_SEG)
1679 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1681 int64_t size = ins->oprs[0].offset;
1683 out(offset, segment, NULL,
1684 OUT_RESERVE, size, NO_SEG, NO_SEG);
1697 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1707 *bytes = c - 0366 + 0x66;
1708 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1717 *bytes = bits == 16 ? 3 : 5;
1718 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1752 struct operand *opy = &ins->oprs[op2];
1755 /* pick rfield from operand b (opx) */
1756 rflags = regflag(opx);
1757 rfield = nasm_regvals[opx->basereg];
1759 /* rfield is constant */
1764 if (process_ea(opy, &ea_data, bits,
1765 rfield, rflags, ins) != eat)
1766 errfunc(ERR_NONFATAL, "invalid effective address");
1769 *p++ = ea_data.modrm;
1770 if (ea_data.sib_present)
1774 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
1777 * Make sure the address gets the right offset in case
1778 * the line breaks in the .lst file (BR 1197827)
1783 switch (ea_data.bytes) {
1790 /* use compressed displacement, if available */
1791 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
1794 if (opy->segment == segment) {
1796 if (overflow_signed(data, ea_data.bytes))
1797 warn_overflow(ERR_PASS2, ea_data.bytes);
1798 out(offset, segment, &data, OUT_ADDRESS,
1799 ea_data.bytes, NO_SEG, NO_SEG);
1801 /* overflow check in output/linker? */
1802 out(offset, segment, &data, OUT_REL4ADR,
1803 insn_end - offset, opy->segment, opy->wrt);
1806 if (overflow_general(data, ins->addr_size >> 3) ||
1807 signed_bits(data, ins->addr_size) !=
1808 signed_bits(data, ea_data.bytes * 8))
1809 warn_overflow(ERR_PASS2, ea_data.bytes);
1811 out(offset, segment, &data, OUT_ADDRESS,
1812 ea_data.bytes, opy->segment, opy->wrt);
1818 "Invalid amount of bytes (%d) for offset?!",
1827 errfunc(ERR_PANIC, "internal instruction table corrupt"
1828 ": instruction code \\%o (0x%02X) given", c, c);
1834 static opflags_t regflag(const operand * o)
1836 if (!is_register(o->basereg))
1837 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
1838 return nasm_reg_flags[o->basereg];
1841 static int32_t regval(const operand * o)
1843 if (!is_register(o->basereg))
1844 errfunc(ERR_PANIC, "invalid operand passed to regval()");
1845 return nasm_regvals[o->basereg];
1848 static int op_rexflags(const operand * o, int mask)
1853 if (!is_register(o->basereg))
1854 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
1856 flags = nasm_reg_flags[o->basereg];
1857 val = nasm_regvals[o->basereg];
1859 return rexflags(val, flags, mask);
1862 static int rexflags(int val, opflags_t flags, int mask)
1867 rex |= REX_B|REX_X|REX_R;
1870 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1872 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1878 static int evexflags(int val, decoflags_t deco,
1879 int mask, uint8_t byte)
1886 evex |= (EVEX_P0RP | EVEX_P0X);
1893 if (deco & OPMASK_MASK)
1894 evex |= deco & EVEX_P2AAA;
1900 static int op_evexflags(const operand * o, int mask, uint8_t byte)
1904 if (!is_register(o->basereg))
1905 errfunc(ERR_PANIC, "invalid operand passed to op_evexflags()");
1907 val = nasm_regvals[o->basereg];
1909 return evexflags(val, o->decoflags, mask, byte);
1912 static enum match_result find_match(const struct itemplate **tempp,
1914 int32_t segment, int64_t offset, int bits)
1916 const struct itemplate *temp;
1917 enum match_result m, merr;
1918 opflags_t xsizeflags[MAX_OPERANDS];
1919 bool opsizemissing = false;
1920 int8_t broadcast = -1;
1923 /* find the position of broadcasting operand */
1924 for (i = 0; i < instruction->operands; i++)
1925 if (instruction->oprs[i].decoflags & BRDCAST_MASK) {
1930 /* broadcasting uses a different data element size */
1931 for (i = 0; i < instruction->operands; i++)
1933 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
1935 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
1937 merr = MERR_INVALOP;
1939 for (temp = nasm_instructions[instruction->opcode];
1940 temp->opcode != I_none; temp++) {
1941 m = matches(temp, instruction, bits);
1942 if (m == MOK_JUMP) {
1943 if (jmp_match(segment, offset, bits, instruction, temp))
1947 } else if (m == MERR_OPSIZEMISSING &&
1948 (temp->flags & IF_SMASK) != IF_SX) {
1950 * Missing operand size and a candidate for fuzzy matching...
1952 for (i = 0; i < temp->operands; i++)
1954 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
1956 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
1957 opsizemissing = true;
1961 if (merr == MOK_GOOD)
1965 /* No match, but see if we can get a fuzzy operand size match... */
1969 for (i = 0; i < instruction->operands; i++) {
1971 * We ignore extrinsic operand sizes on registers, so we should
1972 * never try to fuzzy-match on them. This also resolves the case
1973 * when we have e.g. "xmmrm128" in two different positions.
1975 if (is_class(REGISTER, instruction->oprs[i].type))
1978 /* This tests if xsizeflags[i] has more than one bit set */
1979 if ((xsizeflags[i] & (xsizeflags[i]-1)))
1980 goto done; /* No luck */
1983 instruction->oprs[i].decoflags |= xsizeflags[i];
1985 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
1988 /* Try matching again... */
1989 for (temp = nasm_instructions[instruction->opcode];
1990 temp->opcode != I_none; temp++) {
1991 m = matches(temp, instruction, bits);
1992 if (m == MOK_JUMP) {
1993 if (jmp_match(segment, offset, bits, instruction, temp))
2000 if (merr == MOK_GOOD)
2009 static enum match_result matches(const struct itemplate *itemp,
2010 insn *instruction, int bits)
2012 opflags_t size[MAX_OPERANDS], asize;
2013 bool opsizemissing = false;
2019 if (itemp->opcode != instruction->opcode)
2020 return MERR_INVALOP;
2023 * Count the operands
2025 if (itemp->operands != instruction->operands)
2026 return MERR_INVALOP;
2031 if (!(optimizing > 0) && (itemp->flags & IF_OPT))
2032 return MERR_INVALOP;
2035 * Check that no spurious colons or TOs are present
2037 for (i = 0; i < itemp->operands; i++)
2038 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
2039 return MERR_INVALOP;
2042 * Process size flags
2044 switch (itemp->flags & IF_SMASK) {
2087 if (itemp->flags & IF_ARMASK) {
2088 /* S- flags only apply to a specific operand */
2089 i = ((itemp->flags & IF_ARMASK) >> IF_ARSHFT) - 1;
2090 memset(size, 0, sizeof size);
2093 /* S- flags apply to all operands */
2094 for (i = 0; i < MAX_OPERANDS; i++)
2099 * Check that the operand flags all match up,
2100 * it's a bit tricky so lets be verbose:
2102 * 1) Find out the size of operand. If instruction
2103 * doesn't have one specified -- we're trying to
2104 * guess it either from template (IF_S* flag) or
2107 * 2) If template operand do not match the instruction OR
2108 * template has an operand size specified AND this size differ
2109 * from which instruction has (perhaps we got it from code bits)
2111 * a) Check that only size of instruction and operand is differ
2112 * other characteristics do match
2113 * b) Perhaps it's a register specified in instruction so
2114 * for such a case we just mark that operand as "size
2115 * missing" and this will turn on fuzzy operand size
2116 * logic facility (handled by a caller)
2118 for (i = 0; i < itemp->operands; i++) {
2119 opflags_t type = instruction->oprs[i].type;
2120 decoflags_t deco = instruction->oprs[i].decoflags;
2121 if (!(type & SIZE_MASK))
2124 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
2125 (itemp->deco[i] & deco) != deco) {
2126 return MERR_INVALOP;
2127 } else if ((itemp->opd[i] & SIZE_MASK) &&
2128 (itemp->opd[i] & SIZE_MASK) != (type & SIZE_MASK)) {
2129 if (type & SIZE_MASK) {
2131 * when broadcasting, the element size depends on
2132 * the instruction type. decorator flag should match.
2134 #define MATCH_BRSZ(bits) (((type & SIZE_MASK) == BITS##bits) && \
2135 ((itemp->deco[i] & BRSIZE_MASK) == BR_BITS##bits))
2136 if (!((deco & BRDCAST_MASK) &&
2137 (MATCH_BRSZ(32) || MATCH_BRSZ(64)))) {
2138 return MERR_INVALOP;
2140 } else if (!is_class(REGISTER, type)) {
2142 * Note: we don't honor extrinsic operand sizes for registers,
2143 * so "missing operand size" for a register should be
2144 * considered a wildcard match rather than an error.
2146 opsizemissing = true;
2152 return MERR_OPSIZEMISSING;
2155 * Check operand sizes
2157 if (itemp->flags & (IF_SM | IF_SM2)) {
2158 oprs = (itemp->flags & IF_SM2 ? 2 : itemp->operands);
2159 for (i = 0; i < oprs; i++) {
2160 asize = itemp->opd[i] & SIZE_MASK;
2162 for (i = 0; i < oprs; i++)
2168 oprs = itemp->operands;
2171 for (i = 0; i < itemp->operands; i++) {
2172 if (!(itemp->opd[i] & SIZE_MASK) &&
2173 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
2174 return MERR_OPSIZEMISMATCH;
2178 * Check template is okay at the set cpu level
2180 if (((itemp->flags & IF_PLEVEL) > cpu))
2184 * Verify the appropriate long mode flag.
2186 if ((itemp->flags & (bits == 64 ? IF_NOLONG : IF_LONG)))
2187 return MERR_BADMODE;
2190 * If we have a HLE prefix, look for the NOHLE flag
2192 if ((itemp->flags & IF_NOHLE) &&
2193 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2194 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2198 * Check if special handling needed for Jumps
2200 if ((itemp->code[0] & ~1) == 0370)
2207 * Check if offset is a multiple of N with corresponding tuple type
2208 * if Disp8*N is available, compressed displacement is stored in compdisp
2210 static bool is_disp8n(operand *input, insn *ins, int8_t *compdisp)
2212 const uint8_t fv_n[2][2][VLMAX] = {{{16, 32, 64}, {4, 4, 4}},
2213 {{16, 32, 64}, {8, 8, 8}}};
2214 const uint8_t hv_n[2][VLMAX] = {{8, 16, 32}, {4, 4, 4}};
2215 const uint8_t dup_n[VLMAX] = {8, 32, 64};
2217 bool evex_b = input->decoflags & BRDCAST_MASK;
2218 enum ttypes tuple = ins->evex_tuple;
2219 /* vex_wlp composed as [wwllpp] */
2220 enum vectlens vectlen = (ins->vex_wlp & 0x0c) >> 2;
2221 /* wig(=2) is treated as w0(=0) */
2222 bool evex_w = (ins->vex_wlp & 0x10) >> 4;
2223 int32_t off = input->offset;
2229 n = fv_n[evex_w][evex_b][vectlen];
2232 n = hv_n[evex_b][vectlen];
2236 /* 16, 32, 64 for VL 128, 256, 512 respectively*/
2237 n = 1 << (vectlen + 4);
2239 case T1S8: /* N = 1 */
2240 case T1S16: /* N = 2 */
2241 n = tuple - T1S8 + 1;
2244 /* N = 4 for 32bit, 8 for 64bit */
2249 /* N = 4 for 32bit, 8 for 64bit */
2250 n = (tuple == T1F32 ? 4 : 8);
2255 if (vectlen + 7 <= (evex_w + 5) + (tuple - T2 + 1))
2258 n = 1 << (tuple - T2 + evex_w + 4);
2263 n = 1 << (OVM - tuple + vectlen + 1);
2276 if (n && !(off & (n - 1))) {
2278 /* if it fits in Disp8 */
2279 if (disp8 >= -128 && disp8 <= 127) {
2290 * Check if ModR/M.mod should/can be 01.
2291 * - EAF_BYTEOFFS is set
2292 * - offset can fit in a byte when EVEX is not used
2293 * - offset can be compressed when EVEX is used
2295 #define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2296 (o >= -128 && o <= 127 && \
2297 seg == NO_SEG && !forw_ref && \
2298 !(input->eaflags & EAF_WORDOFFS) && \
2299 !(ins->rex & REX_EV)) || \
2300 (ins->rex & REX_EV && \
2301 is_disp8n(input, ins, &output->disp8)))
2303 static enum ea_type process_ea(operand *input, ea *output, int bits,
2304 int rfield, opflags_t rflags, insn *ins)
2306 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
2307 int addrbits = ins->addr_size;
2309 output->type = EA_SCALAR;
2310 output->rip = false;
2312 /* REX flags for the rfield operand */
2313 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
2314 /* EVEX.R' flag for the REG operand */
2315 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
2317 if (is_class(REGISTER, input->type)) {
2319 * It's a direct register.
2321 if (!is_register(input->basereg))
2324 if (!is_reg_class(REG_EA, input->basereg))
2327 /* broadcasting is not available with a direct register operand. */
2328 if (input->decoflags & BRDCAST_MASK) {
2329 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2333 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
2334 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
2335 output->sib_present = false; /* no SIB necessary */
2336 output->bytes = 0; /* no offset necessary either */
2337 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2340 * It's a memory reference.
2343 /* Embedded rounding or SAE is not available with a mem ref operand. */
2344 if (input->decoflags & (ER | SAE)) {
2345 nasm_error(ERR_NONFATAL,
2346 "Embedded rounding is available only with reg-reg op.");
2350 if (input->basereg == -1 &&
2351 (input->indexreg == -1 || input->scale == 0)) {
2353 * It's a pure offset.
2355 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2356 input->segment == NO_SEG) {
2357 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2358 input->type &= ~IP_REL;
2359 input->type |= MEMORY;
2362 if (input->eaflags & EAF_BYTEOFFS ||
2363 (input->eaflags & EAF_WORDOFFS &&
2364 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2365 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2368 if (bits == 64 && (~input->type & IP_REL)) {
2369 output->sib_present = true;
2370 output->sib = GEN_SIB(0, 4, 5);
2372 output->modrm = GEN_MODRM(0, rfield, 4);
2373 output->rip = false;
2375 output->sib_present = false;
2376 output->bytes = (addrbits != 16 ? 4 : 2);
2377 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2378 output->rip = bits == 64;
2382 * It's an indirection.
2384 int i = input->indexreg, b = input->basereg, s = input->scale;
2385 int32_t seg = input->segment;
2386 int hb = input->hintbase, ht = input->hinttype;
2387 int t, it, bt; /* register numbers */
2388 opflags_t x, ix, bx; /* register flags */
2391 i = -1; /* make this easy, at least */
2393 if (is_register(i)) {
2394 it = nasm_regvals[i];
2395 ix = nasm_reg_flags[i];
2401 if (is_register(b)) {
2402 bt = nasm_regvals[b];
2403 bx = nasm_reg_flags[b];
2409 /* if either one are a vector register... */
2410 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
2411 opflags_t sok = BITS32 | BITS64;
2412 int32_t o = input->offset;
2413 int mod, scale, index, base;
2416 * For a vector SIB, one has to be a vector and the other,
2417 * if present, a GPR. The vector must be the index operand.
2419 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
2425 t = bt, bt = it, it = t;
2426 x = bx, bx = ix, ix = x;
2432 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2439 * While we're here, ensure the user didn't specify
2442 if (input->disp_size == 16 || input->disp_size == 64)
2445 if (addrbits == 16 ||
2446 (addrbits == 32 && !(sok & BITS32)) ||
2447 (addrbits == 64 && !(sok & BITS64)))
2450 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2451 : ((ix & YMMREG & ~REG_EA)
2452 ? EA_YMMVSIB : EA_XMMVSIB));
2454 output->rex |= rexflags(it, ix, REX_X);
2455 output->rex |= rexflags(bt, bx, REX_B);
2456 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
2458 index = it & 7; /* it is known to be != -1 */
2473 default: /* then what the smeg is it? */
2474 goto err; /* panic */
2482 if (base != REG_NUM_EBP && o == 0 &&
2483 seg == NO_SEG && !forw_ref &&
2484 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2486 else if (IS_MOD_01())
2492 output->sib_present = true;
2493 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2494 output->modrm = GEN_MODRM(mod, rfield, 4);
2495 output->sib = GEN_SIB(scale, index, base);
2496 } else if ((ix|bx) & (BITS32|BITS64)) {
2498 * it must be a 32/64-bit memory reference. Firstly we have
2499 * to check that all registers involved are type E/Rxx.
2501 opflags_t sok = BITS32 | BITS64;
2502 int32_t o = input->offset;
2505 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2513 goto err; /* Invalid register */
2514 if (~sok & bx & SIZE_MASK)
2515 goto err; /* Invalid size */
2520 * While we're here, ensure the user didn't specify
2523 if (input->disp_size == 16 || input->disp_size == 64)
2526 if (addrbits == 16 ||
2527 (addrbits == 32 && !(sok & BITS32)) ||
2528 (addrbits == 64 && !(sok & BITS64)))
2531 /* now reorganize base/index */
2532 if (s == 1 && bt != it && bt != -1 && it != -1 &&
2533 ((hb == b && ht == EAH_NOTBASE) ||
2534 (hb == i && ht == EAH_MAKEBASE))) {
2535 /* swap if hints say so */
2536 t = bt, bt = it, it = t;
2537 x = bx, bx = ix, ix = x;
2539 if (bt == it) /* convert EAX+2*EAX to 3*EAX */
2540 bt = -1, bx = 0, s++;
2541 if (bt == -1 && s == 1 && !(hb == it && ht == EAH_NOTBASE)) {
2542 /* make single reg base, unless hint */
2543 bt = it, bx = ix, it = -1, ix = 0;
2545 if (((s == 2 && it != REG_NUM_ESP && !(input->eaflags & EAF_TIMESTWO)) ||
2546 s == 3 || s == 5 || s == 9) && bt == -1)
2547 bt = it, bx = ix, s--; /* convert 3*EAX to EAX+2*EAX */
2548 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
2549 (input->eaflags & EAF_TIMESTWO))
2550 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2551 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2552 if (s == 1 && it == REG_NUM_ESP) {
2553 /* swap ESP into base if scale is 1 */
2554 t = it, it = bt, bt = t;
2555 x = ix, ix = bx, bx = x;
2557 if (it == REG_NUM_ESP ||
2558 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
2559 goto err; /* wrong, for various reasons */
2561 output->rex |= rexflags(it, ix, REX_X);
2562 output->rex |= rexflags(bt, bx, REX_B);
2564 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
2573 if (rm != REG_NUM_EBP && o == 0 &&
2574 seg == NO_SEG && !forw_ref &&
2575 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2577 else if (IS_MOD_01())
2583 output->sib_present = false;
2584 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2585 output->modrm = GEN_MODRM(mod, rfield, rm);
2588 int mod, scale, index, base;
2608 default: /* then what the smeg is it? */
2609 goto err; /* panic */
2617 if (base != REG_NUM_EBP && o == 0 &&
2618 seg == NO_SEG && !forw_ref &&
2619 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2621 else if (IS_MOD_01())
2627 output->sib_present = true;
2628 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2629 output->modrm = GEN_MODRM(mod, rfield, 4);
2630 output->sib = GEN_SIB(scale, index, base);
2632 } else { /* it's 16-bit */
2634 int16_t o = input->offset;
2636 /* check for 64-bit long mode */
2640 /* check all registers are BX, BP, SI or DI */
2641 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2642 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
2645 /* ensure the user didn't specify DWORD/QWORD */
2646 if (input->disp_size == 32 || input->disp_size == 64)
2649 if (s != 1 && i != -1)
2650 goto err; /* no can do, in 16-bit EA */
2651 if (b == -1 && i != -1) {
2656 if ((b == R_SI || b == R_DI) && i != -1) {
2661 /* have BX/BP as base, SI/DI index */
2663 goto err; /* shouldn't ever happen, in theory */
2664 if (i != -1 && b != -1 &&
2665 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
2666 goto err; /* invalid combinations */
2667 if (b == -1) /* pure offset: handled above */
2668 goto err; /* so if it gets to here, panic! */
2672 switch (i * 256 + b) {
2673 case R_SI * 256 + R_BX:
2676 case R_DI * 256 + R_BX:
2679 case R_SI * 256 + R_BP:
2682 case R_DI * 256 + R_BP:
2700 if (rm == -1) /* can't happen, in theory */
2701 goto err; /* so panic if it does */
2703 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
2704 !(input->eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
2706 else if (IS_MOD_01())
2711 output->sib_present = false; /* no SIB - it's 16-bit */
2712 output->bytes = mod; /* bytes of offset needed */
2713 output->modrm = GEN_MODRM(mod, rfield, rm);
2718 output->size = 1 + output->sib_present + output->bytes;
2719 return output->type;
2722 return output->type = EA_INVALID;
2725 static void add_asp(insn *ins, int addrbits)
2730 valid = (addrbits == 64) ? 64|32 : 32|16;
2732 switch (ins->prefixes[PPS_ASIZE]) {
2743 valid &= (addrbits == 32) ? 16 : 32;
2749 for (j = 0; j < ins->operands; j++) {
2750 if (is_class(MEMORY, ins->oprs[j].type)) {
2753 /* Verify as Register */
2754 if (!is_register(ins->oprs[j].indexreg))
2757 i = nasm_reg_flags[ins->oprs[j].indexreg];
2759 /* Verify as Register */
2760 if (!is_register(ins->oprs[j].basereg))
2763 b = nasm_reg_flags[ins->oprs[j].basereg];
2765 if (ins->oprs[j].scale == 0)
2769 int ds = ins->oprs[j].disp_size;
2770 if ((addrbits != 64 && ds > 8) ||
2771 (addrbits == 64 && ds == 16))
2791 if (valid & addrbits) {
2792 ins->addr_size = addrbits;
2793 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
2794 /* Add an address size prefix */
2795 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
2796 ins->addr_size = (addrbits == 32) ? 16 : 32;
2799 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2800 ins->addr_size = addrbits; /* Error recovery */
2803 defdisp = ins->addr_size == 16 ? 16 : 32;
2805 for (j = 0; j < ins->operands; j++) {
2806 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2807 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2809 * mem_offs sizes must match the address size; if not,
2810 * strip the MEM_OFFS bit and match only EA instructions
2812 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);