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 * \14..\17 - the position of index register operand in MIB (BND insns)
46 * \20..\23 - a byte immediate operand, from operand 0..3
47 * \24..\27 - a zero-extended byte immediate operand, from operand 0..3
48 * \30..\33 - a word immediate operand, from operand 0..3
49 * \34..\37 - select between \3[0-3] and \4[0-3] depending on 16/32 bit
50 * assembly mode or the operand-size override on the operand
51 * \40..\43 - a long immediate operand, from operand 0..3
52 * \44..\47 - select between \3[0-3], \4[0-3] and \5[4-7]
53 * depending on the address size of the instruction.
54 * \50..\53 - a byte relative operand, from operand 0..3
55 * \54..\57 - a qword immediate operand, from operand 0..3
56 * \60..\63 - a word relative operand, from operand 0..3
57 * \64..\67 - select between \6[0-3] and \7[0-3] depending on 16/32 bit
58 * assembly mode or the operand-size override on the operand
59 * \70..\73 - a long relative operand, from operand 0..3
60 * \74..\77 - a word constant, from the _segment_ part of operand 0..3
61 * \1ab - a ModRM, calculated on EA in operand a, with the spare
62 * field the register value of operand b.
63 * \172\ab - the register number from operand a in bits 7..4, with
64 * the 4-bit immediate from operand b in bits 3..0.
65 * \173\xab - the register number from operand a in bits 7..4, with
66 * the value b in bits 3..0.
67 * \174..\177 - the register number from operand 0..3 in bits 7..4, and
68 * an arbitrary value in bits 3..0 (assembled as zero.)
69 * \2ab - a ModRM, calculated on EA in operand a, with the spare
70 * field equal to digit b.
72 * \240..\243 - this instruction uses EVEX rather than REX or VEX/XOP, with the
73 * V field taken from operand 0..3.
74 * \250 - this instruction uses EVEX rather than REX or VEX/XOP, with the
75 * V field set to 1111b.
76 * EVEX prefixes are followed by the sequence:
77 * \cm\wlp\tup where cm is:
79 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
82 * [l0] ll = 0 (.128, .lz)
85 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
87 * [w0] ww = 0 for W = 0
88 * [w1] ww = 1 for W = 1
89 * [wig] ww = 2 for W don't care (always assembled as 0)
90 * [ww] ww = 3 for W used as REX.W
92 * [p0] pp = 0 for no prefix
93 * [60] pp = 1 for legacy prefix 60
97 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
98 * (compressed displacement encoding)
100 * \254..\257 - a signed 32-bit operand to be extended to 64 bits.
101 * \260..\263 - this instruction uses VEX/XOP rather than REX, with the
102 * V field taken from operand 0..3.
103 * \270 - this instruction uses VEX/XOP rather than REX, with the
104 * V field set to 1111b.
106 * VEX/XOP prefixes are followed by the sequence:
107 * \tmm\wlp where mm is the M field; and wlp is:
109 * [l0] ll = 0 for L = 0 (.128, .lz)
110 * [l1] ll = 1 for L = 1 (.256)
111 * [lig] ll = 2 for L don't care (always assembled as 0)
113 * [w0] ww = 0 for W = 0
114 * [w1 ] ww = 1 for W = 1
115 * [wig] ww = 2 for W don't care (always assembled as 0)
116 * [ww] ww = 3 for W used as REX.W
118 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
120 * \271 - instruction takes XRELEASE (F3) with or without lock
121 * \272 - instruction takes XACQUIRE/XRELEASE with or without lock
122 * \273 - instruction takes XACQUIRE/XRELEASE with lock only
123 * \274..\277 - a byte immediate operand, from operand 0..3, sign-extended
124 * to the operand size (if o16/o32/o64 present) or the bit size
125 * \310 - indicates fixed 16-bit address size, i.e. optional 0x67.
126 * \311 - indicates fixed 32-bit address size, i.e. optional 0x67.
127 * \312 - (disassembler only) invalid with non-default address size.
128 * \313 - indicates fixed 64-bit address size, 0x67 invalid.
129 * \314 - (disassembler only) invalid with REX.B
130 * \315 - (disassembler only) invalid with REX.X
131 * \316 - (disassembler only) invalid with REX.R
132 * \317 - (disassembler only) invalid with REX.W
133 * \320 - indicates fixed 16-bit operand size, i.e. optional 0x66.
134 * \321 - indicates fixed 32-bit operand size, i.e. optional 0x66.
135 * \322 - indicates that this instruction is only valid when the
136 * operand size is the default (instruction to disassembler,
137 * generates no code in the assembler)
138 * \323 - indicates fixed 64-bit operand size, REX on extensions only.
139 * \324 - indicates 64-bit operand size requiring REX prefix.
140 * \325 - instruction which always uses spl/bpl/sil/dil
141 * \326 - instruction not valid with 0xF3 REP prefix. Hint for
142 disassembler only; for SSE instructions.
143 * \330 - a literal byte follows in the code stream, to be added
144 * to the condition code value of the instruction.
145 * \331 - instruction not valid with REP prefix. Hint for
146 * disassembler only; for SSE instructions.
147 * \332 - REP prefix (0xF2 byte) used as opcode extension.
148 * \333 - REP prefix (0xF3 byte) used as opcode extension.
149 * \334 - LOCK prefix used as REX.R (used in non-64-bit mode)
150 * \335 - disassemble a rep (0xF3 byte) prefix as repe not rep.
151 * \336 - force a REP(E) prefix (0xF3) even if not specified.
152 * \337 - force a REPNE prefix (0xF2) even if not specified.
153 * \336-\337 are still listed as prefixes in the disassembler.
154 * \340 - reserve <operand 0> bytes of uninitialized storage.
155 * Operand 0 had better be a segmentless constant.
156 * \341 - this instruction needs a WAIT "prefix"
157 * \360 - no SSE prefix (== \364\331)
158 * \361 - 66 SSE prefix (== \366\331)
159 * \364 - operand-size prefix (0x66) not permitted
160 * \365 - address-size prefix (0x67) not permitted
161 * \366 - operand-size prefix (0x66) used as opcode extension
162 * \367 - address-size prefix (0x67) used as opcode extension
163 * \370,\371 - match only if operand 0 meets byte jump criteria.
164 * 370 is used for Jcc, 371 is used for JMP.
165 * \373 - assemble 0x03 if bits==16, 0x05 if bits==32;
166 * used for conditional jump over longer jump
167 * \374 - this instruction takes an XMM VSIB memory EA
168 * \375 - this instruction takes an YMM VSIB memory EA
169 * \376 - this instruction takes an ZMM VSIB memory EA
172 #include "compiler.h"
176 #include <inttypes.h>
180 #include "assemble.h"
186 * Matching errors. These should be sorted so that more specific
187 * errors come later in the sequence.
197 * Matching success; the conditional ones first
199 MOK_JUMP
, /* Matching OK but needs jmp_match() */
200 MOK_GOOD
/* Matching unconditionally OK */
204 enum ea_type type
; /* what kind of EA is this? */
205 int sib_present
; /* is a SIB byte necessary? */
206 int bytes
; /* # of bytes of offset needed */
207 int size
; /* lazy - this is sib+bytes+1 */
208 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
209 int8_t disp8
; /* compressed displacement for EVEX */
212 #define GEN_SIB(scale, index, base) \
213 (((scale) << 6) | ((index) << 3) | ((base)))
215 #define GEN_MODRM(mod, reg, rm) \
216 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
218 static iflags_t cpu
; /* cpu level received from nasm.c */
219 static efunc errfunc
;
220 static struct ofmt
*outfmt
;
221 static ListGen
*list
;
223 static int64_t calcsize(int32_t, int64_t, int, insn
*,
224 const struct itemplate
*);
225 static void gencode(int32_t segment
, int64_t offset
, int bits
,
226 insn
* ins
, const struct itemplate
*temp
,
228 static enum match_result
find_match(const struct itemplate
**tempp
,
230 int32_t segment
, int64_t offset
, int bits
);
231 static enum match_result
matches(const struct itemplate
*, insn
*, int bits
);
232 static opflags_t
regflag(const operand
*);
233 static int32_t regval(const operand
*);
234 static int rexflags(int, opflags_t
, int);
235 static int op_rexflags(const operand
*, int);
236 static int op_evexflags(const operand
*, int, uint8_t);
237 static void add_asp(insn
*, int);
239 static enum ea_type
process_ea(operand
*, ea
*, int, int, opflags_t
, insn
*);
241 static int has_prefix(insn
* ins
, enum prefix_pos pos
, int prefix
)
243 return ins
->prefixes
[pos
] == prefix
;
246 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
248 if (ins
->prefixes
[pos
])
249 errfunc(ERR_NONFATAL
, "invalid %s prefix",
250 prefix_name(ins
->prefixes
[pos
]));
253 static const char *size_name(int size
)
277 static void warn_overflow(int pass
, int size
)
279 errfunc(ERR_WARNING
| pass
| ERR_WARN_NOV
,
280 "%s data exceeds bounds", size_name(size
));
283 static void warn_overflow_const(int64_t data
, int size
)
285 if (overflow_general(data
, size
))
286 warn_overflow(ERR_PASS1
, size
);
289 static void warn_overflow_opd(const struct operand
*o
, int size
)
291 if (o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
292 if (overflow_general(o
->offset
, size
))
293 warn_overflow(ERR_PASS2
, size
);
298 * This routine wrappers the real output format's output routine,
299 * in order to pass a copy of the data off to the listing file
300 * generator at the same time.
302 static void out(int64_t offset
, int32_t segto
, const void *data
,
303 enum out_type type
, uint64_t size
,
304 int32_t segment
, int32_t wrt
)
306 static int32_t lineno
= 0; /* static!!! */
307 static char *lnfname
= NULL
;
310 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
312 * This is a non-relocated address, and we're going to
313 * convert it into RAWDATA format.
318 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
322 WRITEADDR(q
, *(int64_t *)data
, size
);
327 list
->output(offset
, data
, type
, size
);
330 * this call to src_get determines when we call the
331 * debug-format-specific "linenum" function
332 * it updates lineno and lnfname to the current values
333 * returning 0 if "same as last time", -2 if lnfname
334 * changed, and the amount by which lineno changed,
335 * if it did. thus, these variables must be static
338 if (src_get(&lineno
, &lnfname
))
339 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
341 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
344 static void out_imm8(int64_t offset
, int32_t segment
, struct operand
*opx
)
346 if (opx
->segment
!= NO_SEG
) {
347 uint64_t data
= opx
->offset
;
348 out(offset
, segment
, &data
, OUT_ADDRESS
, 1, opx
->segment
, opx
->wrt
);
350 uint8_t byte
= opx
->offset
;
351 out(offset
, segment
, &byte
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
355 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
356 insn
* ins
, const struct itemplate
*temp
)
359 const uint8_t *code
= temp
->code
;
362 if (((c
& ~1) != 0370) || (ins
->oprs
[0].type
& STRICT
))
366 if (optimizing
< 0 && c
== 0371)
369 isize
= calcsize(segment
, offset
, bits
, ins
, temp
);
371 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
372 /* Be optimistic in pass 1 */
375 if (ins
->oprs
[0].segment
!= segment
)
378 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
379 return (isize
>= -128 && isize
<= 127); /* is it byte size? */
382 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, iflags_t cp
,
383 insn
* instruction
, struct ofmt
*output
, efunc error
,
386 const struct itemplate
*temp
;
391 int64_t start
= offset
;
392 int64_t wsize
; /* size for DB etc. */
394 errfunc
= error
; /* to pass to other functions */
396 outfmt
= output
; /* likewise */
397 list
= listgen
; /* and again */
399 wsize
= idata_bytes(instruction
->opcode
);
405 int32_t t
= instruction
->times
;
408 "instruction->times < 0 (%ld) in assemble()", t
);
410 while (t
--) { /* repeat TIMES times */
411 list_for_each(e
, instruction
->eops
) {
412 if (e
->type
== EOT_DB_NUMBER
) {
414 errfunc(ERR_NONFATAL
,
415 "integer supplied to a DT, DO or DY"
418 out(offset
, segment
, &e
->offset
,
419 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
422 } else if (e
->type
== EOT_DB_STRING
||
423 e
->type
== EOT_DB_STRING_FREE
) {
426 out(offset
, segment
, e
->stringval
,
427 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
428 align
= e
->stringlen
% wsize
;
431 align
= wsize
- align
;
432 out(offset
, segment
, zero_buffer
,
433 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
435 offset
+= e
->stringlen
+ align
;
438 if (t
> 0 && t
== instruction
->times
- 1) {
440 * Dummy call to list->output to give the offset to the
443 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
444 list
->uplevel(LIST_TIMES
);
447 if (instruction
->times
> 1)
448 list
->downlevel(LIST_TIMES
);
449 return offset
- start
;
452 if (instruction
->opcode
== I_INCBIN
) {
453 const char *fname
= instruction
->eops
->stringval
;
456 fp
= fopen(fname
, "rb");
458 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
460 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
461 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
465 static char buf
[4096];
466 size_t t
= instruction
->times
;
471 if (instruction
->eops
->next
) {
472 base
= instruction
->eops
->next
->offset
;
474 if (instruction
->eops
->next
->next
&&
475 len
> (size_t)instruction
->eops
->next
->next
->offset
)
476 len
= (size_t)instruction
->eops
->next
->next
->offset
;
479 * Dummy call to list->output to give the offset to the
482 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
483 list
->uplevel(LIST_INCBIN
);
487 fseek(fp
, base
, SEEK_SET
);
491 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
494 * This shouldn't happen unless the file
495 * actually changes while we are reading
499 "`incbin': unexpected EOF while"
500 " reading file `%s'", fname
);
501 t
= 0; /* Try to exit cleanly */
504 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
509 list
->downlevel(LIST_INCBIN
);
510 if (instruction
->times
> 1) {
512 * Dummy call to list->output to give the offset to the
515 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
516 list
->uplevel(LIST_TIMES
);
517 list
->downlevel(LIST_TIMES
);
520 return instruction
->times
* len
;
522 return 0; /* if we're here, there's an error */
525 /* Check to see if we need an address-size prefix */
526 add_asp(instruction
, bits
);
528 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
532 int64_t insn_size
= calcsize(segment
, offset
, bits
, instruction
, temp
);
533 itimes
= instruction
->times
;
534 if (insn_size
< 0) /* shouldn't be, on pass two */
535 error(ERR_PANIC
, "errors made it through from pass one");
538 for (j
= 0; j
< MAXPREFIX
; j
++) {
540 switch (instruction
->prefixes
[j
]) {
560 error(ERR_WARNING
| ERR_PASS2
,
561 "cs segment base generated, but will be ignored in 64-bit mode");
567 error(ERR_WARNING
| ERR_PASS2
,
568 "ds segment base generated, but will be ignored in 64-bit mode");
574 error(ERR_WARNING
| ERR_PASS2
,
575 "es segment base generated, but will be ignored in 64-bit mode");
587 error(ERR_WARNING
| ERR_PASS2
,
588 "ss segment base generated, but will be ignored in 64-bit mode");
595 "segr6 and segr7 cannot be used as prefixes");
600 "16-bit addressing is not supported "
602 } else if (bits
!= 16)
612 "64-bit addressing is only supported "
636 error(ERR_PANIC
, "invalid instruction prefix");
639 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
644 insn_end
= offset
+ insn_size
;
645 gencode(segment
, offset
, bits
, instruction
,
648 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
650 * Dummy call to list->output to give the offset to the
653 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
654 list
->uplevel(LIST_TIMES
);
657 if (instruction
->times
> 1)
658 list
->downlevel(LIST_TIMES
);
659 return offset
- start
;
663 case MERR_OPSIZEMISSING
:
664 error(ERR_NONFATAL
, "operation size not specified");
666 case MERR_OPSIZEMISMATCH
:
667 error(ERR_NONFATAL
, "mismatch in operand sizes");
670 error(ERR_NONFATAL
, "no instruction for this cpu level");
673 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
678 "invalid combination of opcode and operands");
685 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, iflags_t cp
,
686 insn
* instruction
, efunc error
)
688 const struct itemplate
*temp
;
691 errfunc
= error
; /* to pass to other functions */
694 if (instruction
->opcode
== I_none
)
697 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
698 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
699 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
700 instruction
->opcode
== I_DY
) {
702 int32_t isize
, osize
, wsize
;
705 wsize
= idata_bytes(instruction
->opcode
);
707 list_for_each(e
, instruction
->eops
) {
711 if (e
->type
== EOT_DB_NUMBER
) {
713 warn_overflow_const(e
->offset
, wsize
);
714 } else if (e
->type
== EOT_DB_STRING
||
715 e
->type
== EOT_DB_STRING_FREE
)
716 osize
= e
->stringlen
;
718 align
= (-osize
) % wsize
;
721 isize
+= osize
+ align
;
723 return isize
* instruction
->times
;
726 if (instruction
->opcode
== I_INCBIN
) {
727 const char *fname
= instruction
->eops
->stringval
;
732 fp
= fopen(fname
, "rb");
734 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
736 else if (fseek(fp
, 0L, SEEK_END
) < 0)
737 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
741 if (instruction
->eops
->next
) {
742 len
-= instruction
->eops
->next
->offset
;
743 if (instruction
->eops
->next
->next
&&
744 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
745 len
= (size_t)instruction
->eops
->next
->next
->offset
;
748 val
= instruction
->times
* len
;
755 /* Check to see if we need an address-size prefix */
756 add_asp(instruction
, bits
);
758 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
760 /* we've matched an instruction. */
764 isize
= calcsize(segment
, offset
, bits
, instruction
, temp
);
767 for (j
= 0; j
< MAXPREFIX
; j
++) {
768 switch (instruction
->prefixes
[j
]) {
794 return isize
* instruction
->times
;
796 return -1; /* didn't match any instruction */
800 static void bad_hle_warn(const insn
* ins
, uint8_t hleok
)
802 enum prefixes rep_pfx
= ins
->prefixes
[PPS_REP
];
803 enum whatwarn
{ w_none
, w_lock
, w_inval
} ww
;
804 static const enum whatwarn warn
[2][4] =
806 { w_inval
, w_inval
, w_none
, w_lock
}, /* XACQUIRE */
807 { w_inval
, w_none
, w_none
, w_lock
}, /* XRELEASE */
811 n
= (unsigned int)rep_pfx
- P_XACQUIRE
;
813 return; /* Not XACQUIRE/XRELEASE */
816 if (!is_class(MEMORY
, ins
->oprs
[0].type
))
817 ww
= w_inval
; /* HLE requires operand 0 to be memory */
824 if (ins
->prefixes
[PPS_LOCK
] != P_LOCK
) {
825 errfunc(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
826 "%s with this instruction requires lock",
827 prefix_name(rep_pfx
));
832 errfunc(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
833 "%s invalid with this instruction",
834 prefix_name(rep_pfx
));
839 /* Common construct */
840 #define case3(x) case (x): case (x)+1: case (x)+2
841 #define case4(x) case3(x): case (x)+3
843 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
844 insn
* ins
, const struct itemplate
*temp
)
846 const uint8_t *codes
= temp
->code
;
855 bool lockcheck
= true;
856 enum reg_enum mib_index
= R_none
; /* For a separate index MIB reg form */
858 ins
->rex
= 0; /* Ensure REX is reset */
859 eat
= EA_SCALAR
; /* Expect a scalar EA */
860 memset(ins
->evex_p
, 0, 3); /* Ensure EVEX is reset */
862 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
865 (void)segment
; /* Don't warn that this parameter is unused */
866 (void)offset
; /* Don't warn that this parameter is unused */
870 op1
= (c
& 3) + ((opex
& 1) << 2);
871 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
872 opx
= &ins
->oprs
[op1
];
873 opex
= 0; /* For the next iteration */
877 codes
+= c
, length
+= c
;
886 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
891 /* this is an index reg of MIB operand */
892 mib_index
= opx
->basereg
;
905 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
906 length
+= (opx
->type
& BITS16
) ? 2 : 4;
908 length
+= (bits
== 16) ? 2 : 4;
916 length
+= ins
->addr_size
>> 3;
924 length
+= 8; /* MOV reg64/imm */
932 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
933 length
+= (opx
->type
& BITS16
) ? 2 : 4;
935 length
+= (bits
== 16) ? 2 : 4;
958 ins
->vexreg
= regval(opx
);
959 ins
->evex_p
[2] |= op_evexflags(opx
, EVEX_P2VP
, 2); /* High-16 NDS */
960 ins
->vex_cm
= *codes
++;
961 ins
->vex_wlp
= *codes
++;
962 ins
->evex_tuple
= (*codes
++ - 0300);
968 ins
->vex_cm
= *codes
++;
969 ins
->vex_wlp
= *codes
++;
970 ins
->evex_tuple
= (*codes
++ - 0300);
979 ins
->vexreg
= regval(opx
);
980 ins
->vex_cm
= *codes
++;
981 ins
->vex_wlp
= *codes
++;
987 ins
->vex_cm
= *codes
++;
988 ins
->vex_wlp
= *codes
++;
1005 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
1009 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1016 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1017 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1026 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1030 errfunc(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1032 ins
->prefixes
[PPS_OSIZE
] = P_O16
;
1038 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1042 errfunc(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1044 ins
->prefixes
[PPS_OSIZE
] = P_O32
;
1086 if (!ins
->prefixes
[PPS_REP
])
1087 ins
->prefixes
[PPS_REP
] = P_REP
;
1091 if (!ins
->prefixes
[PPS_REP
])
1092 ins
->prefixes
[PPS_REP
] = P_REPNE
;
1096 if (ins
->oprs
[0].segment
!= NO_SEG
)
1097 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1098 " quantity of BSS space");
1100 length
+= ins
->oprs
[0].offset
;
1104 if (!ins
->prefixes
[PPS_WAIT
])
1105 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1159 struct operand
*opy
= &ins
->oprs
[op2
];
1160 struct operand
*op_er_sae
;
1162 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1165 /* pick rfield from operand b (opx) */
1166 rflags
= regflag(opx
);
1167 rfield
= nasm_regvals
[opx
->basereg
];
1173 /* EVEX.b1 : evex_brerop contains the operand position */
1174 op_er_sae
= (ins
->evex_brerop
>= 0 ?
1175 &ins
->oprs
[ins
->evex_brerop
] : NULL
);
1177 if (op_er_sae
&& (op_er_sae
->decoflags
& (ER
| SAE
))) {
1179 ins
->evex_p
[2] |= EVEX_P2B
;
1180 if (op_er_sae
->decoflags
& ER
) {
1181 /* set EVEX.RC (rounding control) */
1182 ins
->evex_p
[2] |= ((ins
->evex_rm
- BRC_RN
) << 5)
1186 /* set EVEX.L'L (vector length) */
1187 ins
->evex_p
[2] |= ((ins
->vex_wlp
<< (5 - 2)) & EVEX_P2LL
);
1188 if (opy
->decoflags
& BRDCAST_MASK
) {
1190 ins
->evex_p
[2] |= EVEX_P2B
;
1195 * if a separate form of MIB (ICC style) is used,
1196 * the index reg info is merged into mem operand
1198 if (mib_index
!= R_none
) {
1199 opy
->indexreg
= mib_index
;
1201 opy
->hintbase
= mib_index
;
1202 opy
->hinttype
= EAH_NOTBASE
;
1205 if (process_ea(opy
, &ea_data
, bits
,
1206 rfield
, rflags
, ins
) != eat
) {
1207 errfunc(ERR_NONFATAL
, "invalid effective address");
1210 ins
->rex
|= ea_data
.rex
;
1211 length
+= ea_data
.size
;
1217 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1218 ": instruction code \\%o (0x%02X) given", c
, c
);
1223 ins
->rex
&= rex_mask
;
1225 if (ins
->rex
& REX_NH
) {
1226 if (ins
->rex
& REX_H
) {
1227 errfunc(ERR_NONFATAL
, "instruction cannot use high registers");
1230 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1233 if (ins
->rex
& (REX_V
| REX_EV
)) {
1234 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1236 if (ins
->rex
& REX_H
) {
1237 errfunc(ERR_NONFATAL
, "cannot use high register in AVX instruction");
1240 switch (ins
->vex_wlp
& 060) {
1254 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->vexreg
> 7)) {
1255 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1257 } else if (!(ins
->rex
& REX_EV
) &&
1258 ((ins
->vexreg
> 15) || (ins
->evex_p
[0] & 0xf0))) {
1259 errfunc(ERR_NONFATAL
, "invalid high-16 register in non-AVX-512");
1262 if (ins
->rex
& REX_EV
)
1264 else if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)))
1268 } else if (ins
->rex
& REX_REAL
) {
1269 if (ins
->rex
& REX_H
) {
1270 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1272 } else if (bits
== 64) {
1274 } else if ((ins
->rex
& REX_L
) &&
1275 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1278 assert_no_prefix(ins
, PPS_LOCK
);
1279 lockcheck
= false; /* Already errored, no need for warning */
1282 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1287 if (has_prefix(ins
, PPS_LOCK
, P_LOCK
) && lockcheck
&&
1288 (!(temp
->flags
& IF_LOCK
) || !is_class(MEMORY
, ins
->oprs
[0].type
))) {
1289 errfunc(ERR_WARNING
| ERR_WARN_LOCK
| ERR_PASS2
,
1290 "instruction is not lockable");
1293 bad_hle_warn(ins
, hleok
);
1298 static inline unsigned int emit_rex(insn
*ins
, int32_t segment
, int64_t offset
, int bits
)
1301 if ((ins
->rex
& REX_REAL
) && !(ins
->rex
& (REX_V
| REX_EV
))) {
1302 ins
->rex
= (ins
->rex
& REX_REAL
) | REX_P
;
1303 out(offset
, segment
, &ins
->rex
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1312 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1313 insn
* ins
, const struct itemplate
*temp
,
1321 struct operand
*opx
;
1322 const uint8_t *codes
= temp
->code
;
1324 enum ea_type eat
= EA_SCALAR
;
1328 op1
= (c
& 3) + ((opex
& 1) << 2);
1329 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1330 opx
= &ins
->oprs
[op1
];
1331 opex
= 0; /* For the next iteration */
1338 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1339 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1351 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1352 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1353 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1361 if (opx
->offset
< -256 || opx
->offset
> 255) {
1362 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1363 "byte value exceeds bounds");
1365 out_imm8(offset
, segment
, opx
);
1370 if (opx
->offset
< 0 || opx
->offset
> 255)
1371 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1372 "unsigned byte value exceeds bounds");
1373 out_imm8(offset
, segment
, opx
);
1378 warn_overflow_opd(opx
, 2);
1380 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1381 opx
->segment
, opx
->wrt
);
1386 if (opx
->type
& (BITS16
| BITS32
))
1387 size
= (opx
->type
& BITS16
) ? 2 : 4;
1389 size
= (bits
== 16) ? 2 : 4;
1390 warn_overflow_opd(opx
, size
);
1392 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1393 opx
->segment
, opx
->wrt
);
1398 warn_overflow_opd(opx
, 4);
1400 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1401 opx
->segment
, opx
->wrt
);
1407 size
= ins
->addr_size
>> 3;
1408 warn_overflow_opd(opx
, size
);
1409 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1410 opx
->segment
, opx
->wrt
);
1415 if (opx
->segment
!= segment
) {
1417 out(offset
, segment
, &data
,
1418 OUT_REL1ADR
, insn_end
- offset
,
1419 opx
->segment
, opx
->wrt
);
1421 data
= opx
->offset
- insn_end
;
1422 if (data
> 127 || data
< -128)
1423 errfunc(ERR_NONFATAL
, "short jump is out of range");
1424 out(offset
, segment
, &data
,
1425 OUT_ADDRESS
, 1, NO_SEG
, NO_SEG
);
1431 data
= (int64_t)opx
->offset
;
1432 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1433 opx
->segment
, opx
->wrt
);
1438 if (opx
->segment
!= segment
) {
1440 out(offset
, segment
, &data
,
1441 OUT_REL2ADR
, insn_end
- offset
,
1442 opx
->segment
, opx
->wrt
);
1444 data
= opx
->offset
- insn_end
;
1445 out(offset
, segment
, &data
,
1446 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1452 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1453 size
= (opx
->type
& BITS16
) ? 2 : 4;
1455 size
= (bits
== 16) ? 2 : 4;
1456 if (opx
->segment
!= segment
) {
1458 out(offset
, segment
, &data
,
1459 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1460 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1462 data
= opx
->offset
- insn_end
;
1463 out(offset
, segment
, &data
,
1464 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1470 if (opx
->segment
!= segment
) {
1472 out(offset
, segment
, &data
,
1473 OUT_REL4ADR
, insn_end
- offset
,
1474 opx
->segment
, opx
->wrt
);
1476 data
= opx
->offset
- insn_end
;
1477 out(offset
, segment
, &data
,
1478 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1484 if (opx
->segment
== NO_SEG
)
1485 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1488 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1489 outfmt
->segbase(1 + opx
->segment
),
1496 opx
= &ins
->oprs
[c
>> 3];
1497 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1498 opx
= &ins
->oprs
[c
& 7];
1499 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1500 errfunc(ERR_NONFATAL
,
1501 "non-absolute expression not permitted as argument %d",
1504 if (opx
->offset
& ~15) {
1505 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1506 "four-bit argument exceeds bounds");
1508 bytes
[0] |= opx
->offset
& 15;
1510 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1516 opx
= &ins
->oprs
[c
>> 4];
1517 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1519 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1524 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1525 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1531 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1532 (int32_t)data
!= (int64_t)data
) {
1533 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1534 "signed dword immediate exceeds bounds");
1536 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1537 opx
->segment
, opx
->wrt
);
1544 ins
->evex_p
[2] |= op_evexflags(&ins
->oprs
[0],
1545 EVEX_P2Z
| EVEX_P2AAA
, 2);
1546 ins
->evex_p
[2] ^= EVEX_P2VP
; /* 1's complement */
1548 /* EVEX.X can be set by either REX or EVEX for different reasons */
1549 bytes
[1] = (~(((ins
->rex
& 7) << 5) |
1550 (ins
->evex_p
[0] & (EVEX_P0X
| EVEX_P0RP
))) & 0xf0) |
1552 bytes
[2] = ((ins
->rex
& REX_W
) << (7 - 3)) |
1553 ((~ins
->vexreg
& 15) << 3) |
1554 (1 << 2) | (ins
->vex_wlp
& 3);
1555 bytes
[3] = ins
->evex_p
[2];
1556 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 4, NO_SEG
, NO_SEG
);
1563 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
))) {
1564 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1565 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1566 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1567 ((~ins
->vexreg
& 15)<< 3) | (ins
->vex_wlp
& 07);
1568 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1572 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1573 ((~ins
->vexreg
& 15) << 3) | (ins
->vex_wlp
& 07);
1574 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1589 if (ins
->rex
& REX_W
)
1591 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1593 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1598 um
= (uint64_t)2 << (s
-1);
1601 if (uv
> 127 && uv
< (uint64_t)-128 &&
1602 (uv
< um
-128 || uv
> um
-1)) {
1603 /* If this wasn't explicitly byte-sized, warn as though we
1604 * had fallen through to the imm16/32/64 case.
1606 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1607 "%s value exceeds bounds",
1608 (opx
->type
& BITS8
) ? "signed byte" :
1613 if (opx
->segment
!= NO_SEG
) {
1615 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1616 opx
->segment
, opx
->wrt
);
1619 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1630 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1632 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1639 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1641 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1676 *bytes
= *codes
++ ^ get_cond_opcode(ins
->condition
);
1677 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1686 *bytes
= c
- 0332 + 0xF2;
1687 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1692 if (ins
->rex
& REX_R
) {
1694 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1697 ins
->rex
&= ~(REX_L
|REX_R
);
1708 if (ins
->oprs
[0].segment
!= NO_SEG
)
1709 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1711 int64_t size
= ins
->oprs
[0].offset
;
1713 out(offset
, segment
, NULL
,
1714 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1727 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1737 *bytes
= c
- 0366 + 0x66;
1738 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1747 *bytes
= bits
== 16 ? 3 : 5;
1748 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1782 struct operand
*opy
= &ins
->oprs
[op2
];
1785 /* pick rfield from operand b (opx) */
1786 rflags
= regflag(opx
);
1787 rfield
= nasm_regvals
[opx
->basereg
];
1789 /* rfield is constant */
1794 if (process_ea(opy
, &ea_data
, bits
,
1795 rfield
, rflags
, ins
) != eat
)
1796 errfunc(ERR_NONFATAL
, "invalid effective address");
1799 *p
++ = ea_data
.modrm
;
1800 if (ea_data
.sib_present
)
1804 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1807 * Make sure the address gets the right offset in case
1808 * the line breaks in the .lst file (BR 1197827)
1813 switch (ea_data
.bytes
) {
1820 /* use compressed displacement, if available */
1821 data
= ea_data
.disp8
? ea_data
.disp8
: opy
->offset
;
1824 if (opy
->segment
== segment
) {
1826 if (overflow_signed(data
, ea_data
.bytes
))
1827 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1828 out(offset
, segment
, &data
, OUT_ADDRESS
,
1829 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1831 /* overflow check in output/linker? */
1832 out(offset
, segment
, &data
, OUT_REL4ADR
,
1833 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1836 if (overflow_general(data
, ins
->addr_size
>> 3) ||
1837 signed_bits(data
, ins
->addr_size
) !=
1838 signed_bits(data
, ea_data
.bytes
* 8))
1839 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1841 out(offset
, segment
, &data
, OUT_ADDRESS
,
1842 ea_data
.bytes
, opy
->segment
, opy
->wrt
);
1848 "Invalid amount of bytes (%d) for offset?!",
1857 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1858 ": instruction code \\%o (0x%02X) given", c
, c
);
1864 static opflags_t
regflag(const operand
* o
)
1866 if (!is_register(o
->basereg
))
1867 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1868 return nasm_reg_flags
[o
->basereg
];
1871 static int32_t regval(const operand
* o
)
1873 if (!is_register(o
->basereg
))
1874 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1875 return nasm_regvals
[o
->basereg
];
1878 static int op_rexflags(const operand
* o
, int mask
)
1883 if (!is_register(o
->basereg
))
1884 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1886 flags
= nasm_reg_flags
[o
->basereg
];
1887 val
= nasm_regvals
[o
->basereg
];
1889 return rexflags(val
, flags
, mask
);
1892 static int rexflags(int val
, opflags_t flags
, int mask
)
1897 rex
|= REX_B
|REX_X
|REX_R
;
1900 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1902 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1908 static int evexflags(int val
, decoflags_t deco
,
1909 int mask
, uint8_t byte
)
1916 evex
|= (EVEX_P0RP
| EVEX_P0X
);
1923 if (deco
& OPMASK_MASK
)
1924 evex
|= deco
& EVEX_P2AAA
;
1930 static int op_evexflags(const operand
* o
, int mask
, uint8_t byte
)
1934 if (!is_register(o
->basereg
))
1935 errfunc(ERR_PANIC
, "invalid operand passed to op_evexflags()");
1937 val
= nasm_regvals
[o
->basereg
];
1939 return evexflags(val
, o
->decoflags
, mask
, byte
);
1942 static enum match_result
find_match(const struct itemplate
**tempp
,
1944 int32_t segment
, int64_t offset
, int bits
)
1946 const struct itemplate
*temp
;
1947 enum match_result m
, merr
;
1948 opflags_t xsizeflags
[MAX_OPERANDS
];
1949 bool opsizemissing
= false;
1950 int8_t broadcast
= instruction
->evex_brerop
;
1953 /* broadcasting uses a different data element size */
1954 for (i
= 0; i
< instruction
->operands
; i
++)
1956 xsizeflags
[i
] = instruction
->oprs
[i
].decoflags
& BRSIZE_MASK
;
1958 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
1960 merr
= MERR_INVALOP
;
1962 for (temp
= nasm_instructions
[instruction
->opcode
];
1963 temp
->opcode
!= I_none
; temp
++) {
1964 m
= matches(temp
, instruction
, bits
);
1965 if (m
== MOK_JUMP
) {
1966 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
1970 } else if (m
== MERR_OPSIZEMISSING
&&
1971 (temp
->flags
& IF_SMASK
) != IF_SX
) {
1973 * Missing operand size and a candidate for fuzzy matching...
1975 for (i
= 0; i
< temp
->operands
; i
++)
1977 xsizeflags
[i
] |= temp
->deco
[i
] & BRSIZE_MASK
;
1979 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
1980 opsizemissing
= true;
1984 if (merr
== MOK_GOOD
)
1988 /* No match, but see if we can get a fuzzy operand size match... */
1992 for (i
= 0; i
< instruction
->operands
; i
++) {
1994 * We ignore extrinsic operand sizes on registers, so we should
1995 * never try to fuzzy-match on them. This also resolves the case
1996 * when we have e.g. "xmmrm128" in two different positions.
1998 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
2001 /* This tests if xsizeflags[i] has more than one bit set */
2002 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
2003 goto done
; /* No luck */
2006 instruction
->oprs
[i
].decoflags
|= xsizeflags
[i
];
2008 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
2011 /* Try matching again... */
2012 for (temp
= nasm_instructions
[instruction
->opcode
];
2013 temp
->opcode
!= I_none
; temp
++) {
2014 m
= matches(temp
, instruction
, bits
);
2015 if (m
== MOK_JUMP
) {
2016 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2023 if (merr
== MOK_GOOD
)
2032 static enum match_result
matches(const struct itemplate
*itemp
,
2033 insn
*instruction
, int bits
)
2035 opflags_t size
[MAX_OPERANDS
], asize
;
2036 bool opsizemissing
= false;
2042 if (itemp
->opcode
!= instruction
->opcode
)
2043 return MERR_INVALOP
;
2046 * Count the operands
2048 if (itemp
->operands
!= instruction
->operands
)
2049 return MERR_INVALOP
;
2054 if (!(optimizing
> 0) && (itemp
->flags
& IF_OPT
))
2055 return MERR_INVALOP
;
2058 * Check that no spurious colons or TOs are present
2060 for (i
= 0; i
< itemp
->operands
; i
++)
2061 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2062 return MERR_INVALOP
;
2065 * Process size flags
2067 switch (itemp
->flags
& IF_SMASK
) {
2110 if (itemp
->flags
& IF_ARMASK
) {
2111 /* S- flags only apply to a specific operand */
2112 i
= ((itemp
->flags
& IF_ARMASK
) >> IF_ARSHFT
) - 1;
2113 memset(size
, 0, sizeof size
);
2116 /* S- flags apply to all operands */
2117 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2122 * Check that the operand flags all match up,
2123 * it's a bit tricky so lets be verbose:
2125 * 1) Find out the size of operand. If instruction
2126 * doesn't have one specified -- we're trying to
2127 * guess it either from template (IF_S* flag) or
2130 * 2) If template operand do not match the instruction OR
2131 * template has an operand size specified AND this size differ
2132 * from which instruction has (perhaps we got it from code bits)
2134 * a) Check that only size of instruction and operand is differ
2135 * other characteristics do match
2136 * b) Perhaps it's a register specified in instruction so
2137 * for such a case we just mark that operand as "size
2138 * missing" and this will turn on fuzzy operand size
2139 * logic facility (handled by a caller)
2141 for (i
= 0; i
< itemp
->operands
; i
++) {
2142 opflags_t type
= instruction
->oprs
[i
].type
;
2143 decoflags_t deco
= instruction
->oprs
[i
].decoflags
;
2144 if (!(type
& SIZE_MASK
))
2147 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2148 (itemp
->deco
[i
] & deco
) != deco
) {
2149 return MERR_INVALOP
;
2150 } else if ((itemp
->opd
[i
] & SIZE_MASK
) &&
2151 (itemp
->opd
[i
] & SIZE_MASK
) != (type
& SIZE_MASK
)) {
2152 if (type
& SIZE_MASK
) {
2154 * when broadcasting, the element size depends on
2155 * the instruction type. decorator flag should match.
2157 #define MATCH_BRSZ(bits) (((type & SIZE_MASK) == BITS##bits) && \
2158 ((itemp->deco[i] & BRSIZE_MASK) == BR_BITS##bits))
2159 if (!((deco
& BRDCAST_MASK
) &&
2160 (MATCH_BRSZ(32) || MATCH_BRSZ(64)))) {
2161 return MERR_INVALOP
;
2163 } else if (!is_class(REGISTER
, type
)) {
2165 * Note: we don't honor extrinsic operand sizes for registers,
2166 * so "missing operand size" for a register should be
2167 * considered a wildcard match rather than an error.
2169 opsizemissing
= true;
2171 } else if (is_register(instruction
->oprs
[i
].basereg
) &&
2172 nasm_regvals
[instruction
->oprs
[i
].basereg
] >= 16 &&
2173 !(itemp
->flags
& IF_AVX512
)) {
2174 return MERR_ENCMISMATCH
;
2179 return MERR_OPSIZEMISSING
;
2182 * Check operand sizes
2184 if (itemp
->flags
& (IF_SM
| IF_SM2
)) {
2185 oprs
= (itemp
->flags
& IF_SM2
? 2 : itemp
->operands
);
2186 for (i
= 0; i
< oprs
; i
++) {
2187 asize
= itemp
->opd
[i
] & SIZE_MASK
;
2189 for (i
= 0; i
< oprs
; i
++)
2195 oprs
= itemp
->operands
;
2198 for (i
= 0; i
< itemp
->operands
; i
++) {
2199 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2200 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2201 return MERR_OPSIZEMISMATCH
;
2205 * Check template is okay at the set cpu level
2207 if (((itemp
->flags
& IF_PLEVEL
) > cpu
))
2211 * Verify the appropriate long mode flag.
2213 if ((itemp
->flags
& (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2214 return MERR_BADMODE
;
2217 * If we have a HLE prefix, look for the NOHLE flag
2219 if ((itemp
->flags
& IF_NOHLE
) &&
2220 (has_prefix(instruction
, PPS_REP
, P_XACQUIRE
) ||
2221 has_prefix(instruction
, PPS_REP
, P_XRELEASE
)))
2225 * Check if special handling needed for Jumps
2227 if ((itemp
->code
[0] & ~1) == 0370)
2234 * Check if offset is a multiple of N with corresponding tuple type
2235 * if Disp8*N is available, compressed displacement is stored in compdisp
2237 static bool is_disp8n(operand
*input
, insn
*ins
, int8_t *compdisp
)
2239 const uint8_t fv_n
[2][2][VLMAX
] = {{{16, 32, 64}, {4, 4, 4}},
2240 {{16, 32, 64}, {8, 8, 8}}};
2241 const uint8_t hv_n
[2][VLMAX
] = {{8, 16, 32}, {4, 4, 4}};
2242 const uint8_t dup_n
[VLMAX
] = {8, 32, 64};
2244 bool evex_b
= input
->decoflags
& BRDCAST_MASK
;
2245 enum ttypes tuple
= ins
->evex_tuple
;
2246 /* vex_wlp composed as [wwllpp] */
2247 enum vectlens vectlen
= (ins
->vex_wlp
& 0x0c) >> 2;
2248 /* wig(=2) is treated as w0(=0) */
2249 bool evex_w
= (ins
->vex_wlp
& 0x10) >> 4;
2250 int32_t off
= input
->offset
;
2256 n
= fv_n
[evex_w
][evex_b
][vectlen
];
2259 n
= hv_n
[evex_b
][vectlen
];
2263 /* 16, 32, 64 for VL 128, 256, 512 respectively*/
2264 n
= 1 << (vectlen
+ 4);
2266 case T1S8
: /* N = 1 */
2267 case T1S16
: /* N = 2 */
2268 n
= tuple
- T1S8
+ 1;
2271 /* N = 4 for 32bit, 8 for 64bit */
2276 /* N = 4 for 32bit, 8 for 64bit */
2277 n
= (tuple
== T1F32
? 4 : 8);
2282 if (vectlen
+ 7 <= (evex_w
+ 5) + (tuple
- T2
+ 1))
2285 n
= 1 << (tuple
- T2
+ evex_w
+ 3);
2290 n
= 1 << (OVM
- tuple
+ vectlen
+ 1);
2303 if (n
&& !(off
& (n
- 1))) {
2305 /* if it fits in Disp8 */
2306 if (disp8
>= -128 && disp8
<= 127) {
2317 * Check if ModR/M.mod should/can be 01.
2318 * - EAF_BYTEOFFS is set
2319 * - offset can fit in a byte when EVEX is not used
2320 * - offset can be compressed when EVEX is used
2322 #define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2323 (o >= -128 && o <= 127 && \
2324 seg == NO_SEG && !forw_ref && \
2325 !(input->eaflags & EAF_WORDOFFS) && \
2326 !(ins->rex & REX_EV)) || \
2327 (ins->rex & REX_EV && \
2328 is_disp8n(input, ins, &output->disp8)))
2330 static enum ea_type
process_ea(operand
*input
, ea
*output
, int bits
,
2331 int rfield
, opflags_t rflags
, insn
*ins
)
2333 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2334 int addrbits
= ins
->addr_size
;
2336 output
->type
= EA_SCALAR
;
2337 output
->rip
= false;
2340 /* REX flags for the rfield operand */
2341 output
->rex
|= rexflags(rfield
, rflags
, REX_R
| REX_P
| REX_W
| REX_H
);
2342 /* EVEX.R' flag for the REG operand */
2343 ins
->evex_p
[0] |= evexflags(rfield
, 0, EVEX_P0RP
, 0);
2345 if (is_class(REGISTER
, input
->type
)) {
2347 * It's a direct register.
2349 if (!is_register(input
->basereg
))
2352 if (!is_reg_class(REG_EA
, input
->basereg
))
2355 /* broadcasting is not available with a direct register operand. */
2356 if (input
->decoflags
& BRDCAST_MASK
) {
2357 nasm_error(ERR_NONFATAL
, "Broadcasting not allowed from a register");
2361 output
->rex
|= op_rexflags(input
, REX_B
| REX_P
| REX_W
| REX_H
);
2362 ins
->evex_p
[0] |= op_evexflags(input
, EVEX_P0X
, 0);
2363 output
->sib_present
= false; /* no SIB necessary */
2364 output
->bytes
= 0; /* no offset necessary either */
2365 output
->modrm
= GEN_MODRM(3, rfield
, nasm_regvals
[input
->basereg
]);
2368 * It's a memory reference.
2371 /* Embedded rounding or SAE is not available with a mem ref operand. */
2372 if (input
->decoflags
& (ER
| SAE
)) {
2373 nasm_error(ERR_NONFATAL
,
2374 "Embedded rounding is available only with reg-reg op.");
2378 if (input
->basereg
== -1 &&
2379 (input
->indexreg
== -1 || input
->scale
== 0)) {
2381 * It's a pure offset.
2383 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
) &&
2384 input
->segment
== NO_SEG
) {
2385 nasm_error(ERR_WARNING
| ERR_PASS1
, "absolute address can not be RIP-relative");
2386 input
->type
&= ~IP_REL
;
2387 input
->type
|= MEMORY
;
2390 if (input
->eaflags
& EAF_BYTEOFFS
||
2391 (input
->eaflags
& EAF_WORDOFFS
&&
2392 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16))) {
2393 nasm_error(ERR_WARNING
| ERR_PASS1
, "displacement size ignored on absolute address");
2396 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2397 output
->sib_present
= true;
2398 output
->sib
= GEN_SIB(0, 4, 5);
2400 output
->modrm
= GEN_MODRM(0, rfield
, 4);
2401 output
->rip
= false;
2403 output
->sib_present
= false;
2404 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2405 output
->modrm
= GEN_MODRM(0, rfield
, (addrbits
!= 16 ? 5 : 6));
2406 output
->rip
= bits
== 64;
2410 * It's an indirection.
2412 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2413 int32_t seg
= input
->segment
;
2414 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2415 int t
, it
, bt
; /* register numbers */
2416 opflags_t x
, ix
, bx
; /* register flags */
2419 i
= -1; /* make this easy, at least */
2421 if (is_register(i
)) {
2422 it
= nasm_regvals
[i
];
2423 ix
= nasm_reg_flags
[i
];
2429 if (is_register(b
)) {
2430 bt
= nasm_regvals
[b
];
2431 bx
= nasm_reg_flags
[b
];
2437 /* if either one are a vector register... */
2438 if ((ix
|bx
) & (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
) {
2439 opflags_t sok
= BITS32
| BITS64
;
2440 int32_t o
= input
->offset
;
2441 int mod
, scale
, index
, base
;
2444 * For a vector SIB, one has to be a vector and the other,
2445 * if present, a GPR. The vector must be the index operand.
2447 if (it
== -1 || (bx
& (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
)) {
2453 t
= bt
, bt
= it
, it
= t
;
2454 x
= bx
, bx
= ix
, ix
= x
;
2460 if (!(REG64
& ~bx
) || !(REG32
& ~bx
))
2467 * While we're here, ensure the user didn't specify
2470 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2473 if (addrbits
== 16 ||
2474 (addrbits
== 32 && !(sok
& BITS32
)) ||
2475 (addrbits
== 64 && !(sok
& BITS64
)))
2478 output
->type
= ((ix
& ZMMREG
& ~REG_EA
) ? EA_ZMMVSIB
2479 : ((ix
& YMMREG
& ~REG_EA
)
2480 ? EA_YMMVSIB
: EA_XMMVSIB
));
2482 output
->rex
|= rexflags(it
, ix
, REX_X
);
2483 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2484 ins
->evex_p
[2] |= evexflags(it
, 0, EVEX_P2VP
, 2);
2486 index
= it
& 7; /* it is known to be != -1 */
2501 default: /* then what the smeg is it? */
2502 goto err
; /* panic */
2510 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2511 seg
== NO_SEG
&& !forw_ref
&&
2512 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2514 else if (IS_MOD_01())
2520 output
->sib_present
= true;
2521 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2522 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2523 output
->sib
= GEN_SIB(scale
, index
, base
);
2524 } else if ((ix
|bx
) & (BITS32
|BITS64
)) {
2526 * it must be a 32/64-bit memory reference. Firstly we have
2527 * to check that all registers involved are type E/Rxx.
2529 opflags_t sok
= BITS32
| BITS64
;
2530 int32_t o
= input
->offset
;
2533 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2541 goto err
; /* Invalid register */
2542 if (~sok
& bx
& SIZE_MASK
)
2543 goto err
; /* Invalid size */
2548 * While we're here, ensure the user didn't specify
2551 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2554 if (addrbits
== 16 ||
2555 (addrbits
== 32 && !(sok
& BITS32
)) ||
2556 (addrbits
== 64 && !(sok
& BITS64
)))
2559 /* now reorganize base/index */
2560 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2561 ((hb
== b
&& ht
== EAH_NOTBASE
) ||
2562 (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2563 /* swap if hints say so */
2564 t
= bt
, bt
= it
, it
= t
;
2565 x
= bx
, bx
= ix
, ix
= x
;
2567 if (bt
== it
) /* convert EAX+2*EAX to 3*EAX */
2568 bt
= -1, bx
= 0, s
++;
2569 if (bt
== -1 && s
== 1 && !(hb
== i
&& ht
== EAH_NOTBASE
)) {
2570 /* make single reg base, unless hint */
2571 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2573 if (((s
== 2 && it
!= REG_NUM_ESP
&& !(input
->eaflags
& EAF_TIMESTWO
)) ||
2574 s
== 3 || s
== 5 || s
== 9) && bt
== -1)
2575 bt
= it
, bx
= ix
, s
--; /* convert 3*EAX to EAX+2*EAX */
2576 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
&&
2577 (input
->eaflags
& EAF_TIMESTWO
))
2578 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2579 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2580 if (s
== 1 && it
== REG_NUM_ESP
) {
2581 /* swap ESP into base if scale is 1 */
2582 t
= it
, it
= bt
, bt
= t
;
2583 x
= ix
, ix
= bx
, bx
= x
;
2585 if (it
== REG_NUM_ESP
||
2586 (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2587 goto err
; /* wrong, for various reasons */
2589 output
->rex
|= rexflags(it
, ix
, REX_X
);
2590 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2592 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2601 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2602 seg
== NO_SEG
&& !forw_ref
&&
2603 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2605 else if (IS_MOD_01())
2611 output
->sib_present
= false;
2612 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2613 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2616 int mod
, scale
, index
, base
;
2636 default: /* then what the smeg is it? */
2637 goto err
; /* panic */
2645 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2646 seg
== NO_SEG
&& !forw_ref
&&
2647 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2649 else if (IS_MOD_01())
2655 output
->sib_present
= true;
2656 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2657 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2658 output
->sib
= GEN_SIB(scale
, index
, base
);
2660 } else { /* it's 16-bit */
2662 int16_t o
= input
->offset
;
2664 /* check for 64-bit long mode */
2668 /* check all registers are BX, BP, SI or DI */
2669 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
&& b
!= R_DI
) ||
2670 (i
!= -1 && i
!= R_BP
&& i
!= R_BX
&& i
!= R_SI
&& i
!= R_DI
))
2673 /* ensure the user didn't specify DWORD/QWORD */
2674 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2677 if (s
!= 1 && i
!= -1)
2678 goto err
; /* no can do, in 16-bit EA */
2679 if (b
== -1 && i
!= -1) {
2684 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2689 /* have BX/BP as base, SI/DI index */
2691 goto err
; /* shouldn't ever happen, in theory */
2692 if (i
!= -1 && b
!= -1 &&
2693 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2694 goto err
; /* invalid combinations */
2695 if (b
== -1) /* pure offset: handled above */
2696 goto err
; /* so if it gets to here, panic! */
2700 switch (i
* 256 + b
) {
2701 case R_SI
* 256 + R_BX
:
2704 case R_DI
* 256 + R_BX
:
2707 case R_SI
* 256 + R_BP
:
2710 case R_DI
* 256 + R_BP
:
2728 if (rm
== -1) /* can't happen, in theory */
2729 goto err
; /* so panic if it does */
2731 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2732 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2734 else if (IS_MOD_01())
2739 output
->sib_present
= false; /* no SIB - it's 16-bit */
2740 output
->bytes
= mod
; /* bytes of offset needed */
2741 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2746 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2747 return output
->type
;
2750 return output
->type
= EA_INVALID
;
2753 static void add_asp(insn
*ins
, int addrbits
)
2758 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2760 switch (ins
->prefixes
[PPS_ASIZE
]) {
2771 valid
&= (addrbits
== 32) ? 16 : 32;
2777 for (j
= 0; j
< ins
->operands
; j
++) {
2778 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
2781 /* Verify as Register */
2782 if (!is_register(ins
->oprs
[j
].indexreg
))
2785 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2787 /* Verify as Register */
2788 if (!is_register(ins
->oprs
[j
].basereg
))
2791 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2793 if (ins
->oprs
[j
].scale
== 0)
2797 int ds
= ins
->oprs
[j
].disp_size
;
2798 if ((addrbits
!= 64 && ds
> 8) ||
2799 (addrbits
== 64 && ds
== 16))
2819 if (valid
& addrbits
) {
2820 ins
->addr_size
= addrbits
;
2821 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2822 /* Add an address size prefix */
2823 ins
->prefixes
[PPS_ASIZE
] = (addrbits
== 32) ? P_A16
: P_A32
;;
2824 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2827 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2828 ins
->addr_size
= addrbits
; /* Error recovery */
2831 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2833 for (j
= 0; j
< ins
->operands
; j
++) {
2834 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2835 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
) != ins
->addr_size
) {
2837 * mem_offs sizes must match the address size; if not,
2838 * strip the MEM_OFFS bit and match only EA instructions
2840 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);