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.
196 * Matching success; the conditional ones first
198 MOK_JUMP
, /* Matching OK but needs jmp_match() */
199 MOK_GOOD
/* Matching unconditionally OK */
203 enum ea_type type
; /* what kind of EA is this? */
204 int sib_present
; /* is a SIB byte necessary? */
205 int bytes
; /* # of bytes of offset needed */
206 int size
; /* lazy - this is sib+bytes+1 */
207 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
208 int8_t disp8
; /* compressed displacement for EVEX */
211 #define GEN_SIB(scale, index, base) \
212 (((scale) << 6) | ((index) << 3) | ((base)))
214 #define GEN_MODRM(mod, reg, rm) \
215 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
217 static iflags_t cpu
; /* cpu level received from nasm.c */
218 static efunc errfunc
;
219 static struct ofmt
*outfmt
;
220 static ListGen
*list
;
222 static int64_t calcsize(int32_t, int64_t, int, insn
*,
223 const struct itemplate
*);
224 static void gencode(int32_t segment
, int64_t offset
, int bits
,
225 insn
* ins
, const struct itemplate
*temp
,
227 static enum match_result
find_match(const struct itemplate
**tempp
,
229 int32_t segment
, int64_t offset
, int bits
);
230 static enum match_result
matches(const struct itemplate
*, insn
*, int bits
);
231 static opflags_t
regflag(const operand
*);
232 static int32_t regval(const operand
*);
233 static int rexflags(int, opflags_t
, int);
234 static int op_rexflags(const operand
*, int);
235 static int op_evexflags(const operand
*, int, uint8_t);
236 static void add_asp(insn
*, int);
238 static enum ea_type
process_ea(operand
*, ea
*, int, int, opflags_t
, insn
*);
240 static int has_prefix(insn
* ins
, enum prefix_pos pos
, int prefix
)
242 return ins
->prefixes
[pos
] == prefix
;
245 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
247 if (ins
->prefixes
[pos
])
248 errfunc(ERR_NONFATAL
, "invalid %s prefix",
249 prefix_name(ins
->prefixes
[pos
]));
252 static const char *size_name(int size
)
276 static void warn_overflow(int pass
, int size
)
278 errfunc(ERR_WARNING
| pass
| ERR_WARN_NOV
,
279 "%s data exceeds bounds", size_name(size
));
282 static void warn_overflow_const(int64_t data
, int size
)
284 if (overflow_general(data
, size
))
285 warn_overflow(ERR_PASS1
, size
);
288 static void warn_overflow_opd(const struct operand
*o
, int size
)
290 if (o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
291 if (overflow_general(o
->offset
, size
))
292 warn_overflow(ERR_PASS2
, size
);
297 * This routine wrappers the real output format's output routine,
298 * in order to pass a copy of the data off to the listing file
299 * generator at the same time.
301 static void out(int64_t offset
, int32_t segto
, const void *data
,
302 enum out_type type
, uint64_t size
,
303 int32_t segment
, int32_t wrt
)
305 static int32_t lineno
= 0; /* static!!! */
306 static char *lnfname
= NULL
;
309 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
311 * This is a non-relocated address, and we're going to
312 * convert it into RAWDATA format.
317 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
321 WRITEADDR(q
, *(int64_t *)data
, size
);
326 list
->output(offset
, data
, type
, size
);
329 * this call to src_get determines when we call the
330 * debug-format-specific "linenum" function
331 * it updates lineno and lnfname to the current values
332 * returning 0 if "same as last time", -2 if lnfname
333 * changed, and the amount by which lineno changed,
334 * if it did. thus, these variables must be static
337 if (src_get(&lineno
, &lnfname
))
338 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
340 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
343 static void out_imm8(int64_t offset
, int32_t segment
, struct operand
*opx
)
345 if (opx
->segment
!= NO_SEG
) {
346 uint64_t data
= opx
->offset
;
347 out(offset
, segment
, &data
, OUT_ADDRESS
, 1, opx
->segment
, opx
->wrt
);
349 uint8_t byte
= opx
->offset
;
350 out(offset
, segment
, &byte
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
354 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
355 insn
* ins
, const struct itemplate
*temp
)
358 const uint8_t *code
= temp
->code
;
361 if (((c
& ~1) != 0370) || (ins
->oprs
[0].type
& STRICT
))
365 if (optimizing
< 0 && c
== 0371)
368 isize
= calcsize(segment
, offset
, bits
, ins
, temp
);
370 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
371 /* Be optimistic in pass 1 */
374 if (ins
->oprs
[0].segment
!= segment
)
377 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
378 return (isize
>= -128 && isize
<= 127); /* is it byte size? */
381 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, iflags_t cp
,
382 insn
* instruction
, struct ofmt
*output
, efunc error
,
385 const struct itemplate
*temp
;
390 int64_t start
= offset
;
391 int64_t wsize
; /* size for DB etc. */
393 errfunc
= error
; /* to pass to other functions */
395 outfmt
= output
; /* likewise */
396 list
= listgen
; /* and again */
398 wsize
= idata_bytes(instruction
->opcode
);
404 int32_t t
= instruction
->times
;
407 "instruction->times < 0 (%ld) in assemble()", t
);
409 while (t
--) { /* repeat TIMES times */
410 list_for_each(e
, instruction
->eops
) {
411 if (e
->type
== EOT_DB_NUMBER
) {
413 errfunc(ERR_NONFATAL
,
414 "integer supplied to a DT, DO or DY"
417 out(offset
, segment
, &e
->offset
,
418 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
421 } else if (e
->type
== EOT_DB_STRING
||
422 e
->type
== EOT_DB_STRING_FREE
) {
425 out(offset
, segment
, e
->stringval
,
426 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
427 align
= e
->stringlen
% wsize
;
430 align
= wsize
- align
;
431 out(offset
, segment
, zero_buffer
,
432 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
434 offset
+= e
->stringlen
+ align
;
437 if (t
> 0 && t
== instruction
->times
- 1) {
439 * Dummy call to list->output to give the offset to the
442 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
443 list
->uplevel(LIST_TIMES
);
446 if (instruction
->times
> 1)
447 list
->downlevel(LIST_TIMES
);
448 return offset
- start
;
451 if (instruction
->opcode
== I_INCBIN
) {
452 const char *fname
= instruction
->eops
->stringval
;
455 fp
= fopen(fname
, "rb");
457 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
459 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
460 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
464 static char buf
[4096];
465 size_t t
= instruction
->times
;
470 if (instruction
->eops
->next
) {
471 base
= instruction
->eops
->next
->offset
;
473 if (instruction
->eops
->next
->next
&&
474 len
> (size_t)instruction
->eops
->next
->next
->offset
)
475 len
= (size_t)instruction
->eops
->next
->next
->offset
;
478 * Dummy call to list->output to give the offset to the
481 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
482 list
->uplevel(LIST_INCBIN
);
486 fseek(fp
, base
, SEEK_SET
);
490 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
493 * This shouldn't happen unless the file
494 * actually changes while we are reading
498 "`incbin': unexpected EOF while"
499 " reading file `%s'", fname
);
500 t
= 0; /* Try to exit cleanly */
503 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
508 list
->downlevel(LIST_INCBIN
);
509 if (instruction
->times
> 1) {
511 * Dummy call to list->output to give the offset to the
514 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
515 list
->uplevel(LIST_TIMES
);
516 list
->downlevel(LIST_TIMES
);
519 return instruction
->times
* len
;
521 return 0; /* if we're here, there's an error */
524 /* Check to see if we need an address-size prefix */
525 add_asp(instruction
, bits
);
527 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
531 int64_t insn_size
= calcsize(segment
, offset
, bits
, instruction
, temp
);
532 itimes
= instruction
->times
;
533 if (insn_size
< 0) /* shouldn't be, on pass two */
534 error(ERR_PANIC
, "errors made it through from pass one");
537 for (j
= 0; j
< MAXPREFIX
; j
++) {
539 switch (instruction
->prefixes
[j
]) {
559 error(ERR_WARNING
| ERR_PASS2
,
560 "cs segment base generated, but will be ignored in 64-bit mode");
566 error(ERR_WARNING
| ERR_PASS2
,
567 "ds segment base generated, but will be ignored in 64-bit mode");
573 error(ERR_WARNING
| ERR_PASS2
,
574 "es segment base generated, but will be ignored in 64-bit mode");
586 error(ERR_WARNING
| ERR_PASS2
,
587 "ss segment base generated, but will be ignored in 64-bit mode");
594 "segr6 and segr7 cannot be used as prefixes");
599 "16-bit addressing is not supported "
601 } else if (bits
!= 16)
611 "64-bit addressing is only supported "
635 error(ERR_PANIC
, "invalid instruction prefix");
638 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
643 insn_end
= offset
+ insn_size
;
644 gencode(segment
, offset
, bits
, instruction
,
647 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
649 * Dummy call to list->output to give the offset to the
652 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
653 list
->uplevel(LIST_TIMES
);
656 if (instruction
->times
> 1)
657 list
->downlevel(LIST_TIMES
);
658 return offset
- start
;
662 case MERR_OPSIZEMISSING
:
663 error(ERR_NONFATAL
, "operation size not specified");
665 case MERR_OPSIZEMISMATCH
:
666 error(ERR_NONFATAL
, "mismatch in operand sizes");
669 error(ERR_NONFATAL
, "no instruction for this cpu level");
672 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
677 "invalid combination of opcode and operands");
684 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, iflags_t cp
,
685 insn
* instruction
, efunc error
)
687 const struct itemplate
*temp
;
690 errfunc
= error
; /* to pass to other functions */
693 if (instruction
->opcode
== I_none
)
696 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
697 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
698 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
699 instruction
->opcode
== I_DY
) {
701 int32_t isize
, osize
, wsize
;
704 wsize
= idata_bytes(instruction
->opcode
);
706 list_for_each(e
, instruction
->eops
) {
710 if (e
->type
== EOT_DB_NUMBER
) {
712 warn_overflow_const(e
->offset
, wsize
);
713 } else if (e
->type
== EOT_DB_STRING
||
714 e
->type
== EOT_DB_STRING_FREE
)
715 osize
= e
->stringlen
;
717 align
= (-osize
) % wsize
;
720 isize
+= osize
+ align
;
722 return isize
* instruction
->times
;
725 if (instruction
->opcode
== I_INCBIN
) {
726 const char *fname
= instruction
->eops
->stringval
;
731 fp
= fopen(fname
, "rb");
733 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
735 else if (fseek(fp
, 0L, SEEK_END
) < 0)
736 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
740 if (instruction
->eops
->next
) {
741 len
-= instruction
->eops
->next
->offset
;
742 if (instruction
->eops
->next
->next
&&
743 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
744 len
= (size_t)instruction
->eops
->next
->next
->offset
;
747 val
= instruction
->times
* len
;
754 /* Check to see if we need an address-size prefix */
755 add_asp(instruction
, bits
);
757 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
759 /* we've matched an instruction. */
763 isize
= calcsize(segment
, offset
, bits
, instruction
, temp
);
766 for (j
= 0; j
< MAXPREFIX
; j
++) {
767 switch (instruction
->prefixes
[j
]) {
793 return isize
* instruction
->times
;
795 return -1; /* didn't match any instruction */
799 static void bad_hle_warn(const insn
* ins
, uint8_t hleok
)
801 enum prefixes rep_pfx
= ins
->prefixes
[PPS_REP
];
802 enum whatwarn
{ w_none
, w_lock
, w_inval
} ww
;
803 static const enum whatwarn warn
[2][4] =
805 { w_inval
, w_inval
, w_none
, w_lock
}, /* XACQUIRE */
806 { w_inval
, w_none
, w_none
, w_lock
}, /* XRELEASE */
810 n
= (unsigned int)rep_pfx
- P_XACQUIRE
;
812 return; /* Not XACQUIRE/XRELEASE */
815 if (!is_class(MEMORY
, ins
->oprs
[0].type
))
816 ww
= w_inval
; /* HLE requires operand 0 to be memory */
823 if (ins
->prefixes
[PPS_LOCK
] != P_LOCK
) {
824 errfunc(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
825 "%s with this instruction requires lock",
826 prefix_name(rep_pfx
));
831 errfunc(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
832 "%s invalid with this instruction",
833 prefix_name(rep_pfx
));
838 /* Common construct */
839 #define case3(x) case (x): case (x)+1: case (x)+2
840 #define case4(x) case3(x): case (x)+3
842 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
843 insn
* ins
, const struct itemplate
*temp
)
845 const uint8_t *codes
= temp
->code
;
854 bool lockcheck
= true;
856 ins
->rex
= 0; /* Ensure REX is reset */
857 eat
= EA_SCALAR
; /* Expect a scalar EA */
858 memset(ins
->evex_p
, 0, 3); /* Ensure EVEX is reset */
860 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
863 (void)segment
; /* Don't warn that this parameter is unused */
864 (void)offset
; /* Don't warn that this parameter is unused */
868 op1
= (c
& 3) + ((opex
& 1) << 2);
869 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
870 opx
= &ins
->oprs
[op1
];
871 opex
= 0; /* For the next iteration */
875 codes
+= c
, length
+= c
;
884 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
898 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
899 length
+= (opx
->type
& BITS16
) ? 2 : 4;
901 length
+= (bits
== 16) ? 2 : 4;
909 length
+= ins
->addr_size
>> 3;
917 length
+= 8; /* MOV reg64/imm */
925 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
926 length
+= (opx
->type
& BITS16
) ? 2 : 4;
928 length
+= (bits
== 16) ? 2 : 4;
951 ins
->vexreg
= regval(opx
);
952 ins
->evex_p
[2] |= op_evexflags(opx
, EVEX_P2VP
, 2); /* High-16 NDS */
953 ins
->vex_cm
= *codes
++;
954 ins
->vex_wlp
= *codes
++;
955 ins
->evex_tuple
= (*codes
++ - 0300);
961 ins
->vex_cm
= *codes
++;
962 ins
->vex_wlp
= *codes
++;
963 ins
->evex_tuple
= (*codes
++ - 0300);
972 ins
->vexreg
= regval(opx
);
973 ins
->vex_cm
= *codes
++;
974 ins
->vex_wlp
= *codes
++;
980 ins
->vex_cm
= *codes
++;
981 ins
->vex_wlp
= *codes
++;
998 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
1002 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1009 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1010 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1019 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1023 errfunc(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1025 ins
->prefixes
[PPS_OSIZE
] = P_O16
;
1031 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1035 errfunc(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1037 ins
->prefixes
[PPS_OSIZE
] = P_O32
;
1079 if (!ins
->prefixes
[PPS_REP
])
1080 ins
->prefixes
[PPS_REP
] = P_REP
;
1084 if (!ins
->prefixes
[PPS_REP
])
1085 ins
->prefixes
[PPS_REP
] = P_REPNE
;
1089 if (ins
->oprs
[0].segment
!= NO_SEG
)
1090 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1091 " quantity of BSS space");
1093 length
+= ins
->oprs
[0].offset
;
1097 if (!ins
->prefixes
[PPS_WAIT
])
1098 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1152 struct operand
*opy
= &ins
->oprs
[op2
];
1153 struct operand
*op_er_sae
;
1155 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1158 /* pick rfield from operand b (opx) */
1159 rflags
= regflag(opx
);
1160 rfield
= nasm_regvals
[opx
->basereg
];
1166 /* EVEX.b1 : evex_brerop contains the operand position */
1167 op_er_sae
= (ins
->evex_brerop
>= 0 ?
1168 &ins
->oprs
[ins
->evex_brerop
] : NULL
);
1170 if (op_er_sae
&& (op_er_sae
->decoflags
& ER
)) {
1171 /* set EVEX.RC (rounding control) and b */
1172 ins
->evex_p
[2] |= (((ins
->evex_rm
- BRC_RN
) << 5) & EVEX_P2LL
) |
1175 /* set EVEX.L'L (vector length) */
1176 ins
->evex_p
[2] |= ((ins
->vex_wlp
<< (5 - 2)) & EVEX_P2LL
);
1177 if ((op_er_sae
&& (op_er_sae
->decoflags
& SAE
)) ||
1178 (opy
->decoflags
& BRDCAST_MASK
)) {
1180 ins
->evex_p
[2] |= EVEX_P2B
;
1184 if (process_ea(opy
, &ea_data
, bits
,
1185 rfield
, rflags
, ins
) != eat
) {
1186 errfunc(ERR_NONFATAL
, "invalid effective address");
1189 ins
->rex
|= ea_data
.rex
;
1190 length
+= ea_data
.size
;
1196 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1197 ": instruction code \\%o (0x%02X) given", c
, c
);
1202 ins
->rex
&= rex_mask
;
1204 if (ins
->rex
& REX_NH
) {
1205 if (ins
->rex
& REX_H
) {
1206 errfunc(ERR_NONFATAL
, "instruction cannot use high registers");
1209 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1212 if (ins
->rex
& (REX_V
| REX_EV
)) {
1213 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1215 if (ins
->rex
& REX_H
) {
1216 errfunc(ERR_NONFATAL
, "cannot use high register in AVX instruction");
1219 switch (ins
->vex_wlp
& 060) {
1233 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->vexreg
> 7)) {
1234 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1236 } else if (!(ins
->rex
& REX_EV
) &&
1237 ((ins
->vexreg
> 15) || (ins
->evex_p
[0] & 0xf0))) {
1238 errfunc(ERR_NONFATAL
, "invalid high-16 register in non-AVX-512");
1241 if (ins
->rex
& REX_EV
)
1243 else if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)))
1247 } else if (ins
->rex
& REX_REAL
) {
1248 if (ins
->rex
& REX_H
) {
1249 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1251 } else if (bits
== 64) {
1253 } else if ((ins
->rex
& REX_L
) &&
1254 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1257 assert_no_prefix(ins
, PPS_LOCK
);
1258 lockcheck
= false; /* Already errored, no need for warning */
1261 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1266 if (has_prefix(ins
, PPS_LOCK
, P_LOCK
) && lockcheck
&&
1267 (!(temp
->flags
& IF_LOCK
) || !is_class(MEMORY
, ins
->oprs
[0].type
))) {
1268 errfunc(ERR_WARNING
| ERR_WARN_LOCK
| ERR_PASS2
,
1269 "instruction is not lockable");
1272 bad_hle_warn(ins
, hleok
);
1277 static inline unsigned int emit_rex(insn
*ins
, int32_t segment
, int64_t offset
, int bits
)
1280 if ((ins
->rex
& REX_REAL
) && !(ins
->rex
& (REX_V
| REX_EV
))) {
1281 ins
->rex
= (ins
->rex
& REX_REAL
) | REX_P
;
1282 out(offset
, segment
, &ins
->rex
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1291 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1292 insn
* ins
, const struct itemplate
*temp
,
1300 struct operand
*opx
;
1301 const uint8_t *codes
= temp
->code
;
1303 enum ea_type eat
= EA_SCALAR
;
1307 op1
= (c
& 3) + ((opex
& 1) << 2);
1308 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1309 opx
= &ins
->oprs
[op1
];
1310 opex
= 0; /* For the next iteration */
1317 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1318 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1330 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1331 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1332 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1337 if (opx
->offset
< -256 || opx
->offset
> 255) {
1338 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1339 "byte value exceeds bounds");
1341 out_imm8(offset
, segment
, opx
);
1346 if (opx
->offset
< 0 || opx
->offset
> 255)
1347 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1348 "unsigned byte value exceeds bounds");
1349 out_imm8(offset
, segment
, opx
);
1354 warn_overflow_opd(opx
, 2);
1356 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1357 opx
->segment
, opx
->wrt
);
1362 if (opx
->type
& (BITS16
| BITS32
))
1363 size
= (opx
->type
& BITS16
) ? 2 : 4;
1365 size
= (bits
== 16) ? 2 : 4;
1366 warn_overflow_opd(opx
, size
);
1368 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1369 opx
->segment
, opx
->wrt
);
1374 warn_overflow_opd(opx
, 4);
1376 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1377 opx
->segment
, opx
->wrt
);
1383 size
= ins
->addr_size
>> 3;
1384 warn_overflow_opd(opx
, size
);
1385 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1386 opx
->segment
, opx
->wrt
);
1391 if (opx
->segment
!= segment
) {
1393 out(offset
, segment
, &data
,
1394 OUT_REL1ADR
, insn_end
- offset
,
1395 opx
->segment
, opx
->wrt
);
1397 data
= opx
->offset
- insn_end
;
1398 if (data
> 127 || data
< -128)
1399 errfunc(ERR_NONFATAL
, "short jump is out of range");
1400 out(offset
, segment
, &data
,
1401 OUT_ADDRESS
, 1, NO_SEG
, NO_SEG
);
1407 data
= (int64_t)opx
->offset
;
1408 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1409 opx
->segment
, opx
->wrt
);
1414 if (opx
->segment
!= segment
) {
1416 out(offset
, segment
, &data
,
1417 OUT_REL2ADR
, insn_end
- offset
,
1418 opx
->segment
, opx
->wrt
);
1420 data
= opx
->offset
- insn_end
;
1421 out(offset
, segment
, &data
,
1422 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1428 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1429 size
= (opx
->type
& BITS16
) ? 2 : 4;
1431 size
= (bits
== 16) ? 2 : 4;
1432 if (opx
->segment
!= segment
) {
1434 out(offset
, segment
, &data
,
1435 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1436 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1438 data
= opx
->offset
- insn_end
;
1439 out(offset
, segment
, &data
,
1440 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1446 if (opx
->segment
!= segment
) {
1448 out(offset
, segment
, &data
,
1449 OUT_REL4ADR
, insn_end
- offset
,
1450 opx
->segment
, opx
->wrt
);
1452 data
= opx
->offset
- insn_end
;
1453 out(offset
, segment
, &data
,
1454 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1460 if (opx
->segment
== NO_SEG
)
1461 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1464 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1465 outfmt
->segbase(1 + opx
->segment
),
1472 opx
= &ins
->oprs
[c
>> 3];
1473 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1474 opx
= &ins
->oprs
[c
& 7];
1475 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1476 errfunc(ERR_NONFATAL
,
1477 "non-absolute expression not permitted as argument %d",
1480 if (opx
->offset
& ~15) {
1481 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1482 "four-bit argument exceeds bounds");
1484 bytes
[0] |= opx
->offset
& 15;
1486 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1492 opx
= &ins
->oprs
[c
>> 4];
1493 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1495 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1500 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1501 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1507 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1508 (int32_t)data
!= (int64_t)data
) {
1509 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1510 "signed dword immediate exceeds bounds");
1512 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1513 opx
->segment
, opx
->wrt
);
1520 ins
->evex_p
[2] |= op_evexflags(&ins
->oprs
[0],
1521 EVEX_P2Z
| EVEX_P2AAA
, 2);
1522 ins
->evex_p
[2] ^= EVEX_P2VP
; /* 1's complement */
1524 /* EVEX.X can be set by either REX or EVEX for different reasons */
1525 bytes
[1] = (~(((ins
->rex
& 7) << 5) |
1526 (ins
->evex_p
[0] & (EVEX_P0X
| EVEX_P0RP
))) & 0xf0) |
1528 bytes
[2] = ((ins
->rex
& REX_W
) << (7 - 3)) |
1529 ((~ins
->vexreg
& 15) << 3) |
1530 (1 << 2) | (ins
->vex_wlp
& 3);
1531 bytes
[3] = ins
->evex_p
[2];
1532 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 4, NO_SEG
, NO_SEG
);
1539 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
))) {
1540 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1541 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1542 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1543 ((~ins
->vexreg
& 15)<< 3) | (ins
->vex_wlp
& 07);
1544 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1548 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1549 ((~ins
->vexreg
& 15) << 3) | (ins
->vex_wlp
& 07);
1550 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1565 if (ins
->rex
& REX_W
)
1567 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1569 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1574 um
= (uint64_t)2 << (s
-1);
1577 if (uv
> 127 && uv
< (uint64_t)-128 &&
1578 (uv
< um
-128 || uv
> um
-1)) {
1579 /* If this wasn't explicitly byte-sized, warn as though we
1580 * had fallen through to the imm16/32/64 case.
1582 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1583 "%s value exceeds bounds",
1584 (opx
->type
& BITS8
) ? "signed byte" :
1589 if (opx
->segment
!= NO_SEG
) {
1591 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1592 opx
->segment
, opx
->wrt
);
1595 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1606 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1608 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1615 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1617 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1652 *bytes
= *codes
++ ^ get_cond_opcode(ins
->condition
);
1653 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1662 *bytes
= c
- 0332 + 0xF2;
1663 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1668 if (ins
->rex
& REX_R
) {
1670 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1673 ins
->rex
&= ~(REX_L
|REX_R
);
1684 if (ins
->oprs
[0].segment
!= NO_SEG
)
1685 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1687 int64_t size
= ins
->oprs
[0].offset
;
1689 out(offset
, segment
, NULL
,
1690 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1703 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1713 *bytes
= c
- 0366 + 0x66;
1714 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1723 *bytes
= bits
== 16 ? 3 : 5;
1724 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1758 struct operand
*opy
= &ins
->oprs
[op2
];
1761 /* pick rfield from operand b (opx) */
1762 rflags
= regflag(opx
);
1763 rfield
= nasm_regvals
[opx
->basereg
];
1765 /* rfield is constant */
1770 if (process_ea(opy
, &ea_data
, bits
,
1771 rfield
, rflags
, ins
) != eat
)
1772 errfunc(ERR_NONFATAL
, "invalid effective address");
1775 *p
++ = ea_data
.modrm
;
1776 if (ea_data
.sib_present
)
1780 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1783 * Make sure the address gets the right offset in case
1784 * the line breaks in the .lst file (BR 1197827)
1789 switch (ea_data
.bytes
) {
1796 /* use compressed displacement, if available */
1797 data
= ea_data
.disp8
? ea_data
.disp8
: opy
->offset
;
1800 if (opy
->segment
== segment
) {
1802 if (overflow_signed(data
, ea_data
.bytes
))
1803 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1804 out(offset
, segment
, &data
, OUT_ADDRESS
,
1805 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1807 /* overflow check in output/linker? */
1808 out(offset
, segment
, &data
, OUT_REL4ADR
,
1809 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1812 if (overflow_general(data
, ins
->addr_size
>> 3) ||
1813 signed_bits(data
, ins
->addr_size
) !=
1814 signed_bits(data
, ea_data
.bytes
* 8))
1815 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1817 out(offset
, segment
, &data
, OUT_ADDRESS
,
1818 ea_data
.bytes
, opy
->segment
, opy
->wrt
);
1824 "Invalid amount of bytes (%d) for offset?!",
1833 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1834 ": instruction code \\%o (0x%02X) given", c
, c
);
1840 static opflags_t
regflag(const operand
* o
)
1842 if (!is_register(o
->basereg
))
1843 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1844 return nasm_reg_flags
[o
->basereg
];
1847 static int32_t regval(const operand
* o
)
1849 if (!is_register(o
->basereg
))
1850 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1851 return nasm_regvals
[o
->basereg
];
1854 static int op_rexflags(const operand
* o
, int mask
)
1859 if (!is_register(o
->basereg
))
1860 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1862 flags
= nasm_reg_flags
[o
->basereg
];
1863 val
= nasm_regvals
[o
->basereg
];
1865 return rexflags(val
, flags
, mask
);
1868 static int rexflags(int val
, opflags_t flags
, int mask
)
1873 rex
|= REX_B
|REX_X
|REX_R
;
1876 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1878 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1884 static int evexflags(int val
, decoflags_t deco
,
1885 int mask
, uint8_t byte
)
1892 evex
|= (EVEX_P0RP
| EVEX_P0X
);
1899 if (deco
& OPMASK_MASK
)
1900 evex
|= deco
& EVEX_P2AAA
;
1906 static int op_evexflags(const operand
* o
, int mask
, uint8_t byte
)
1910 if (!is_register(o
->basereg
))
1911 errfunc(ERR_PANIC
, "invalid operand passed to op_evexflags()");
1913 val
= nasm_regvals
[o
->basereg
];
1915 return evexflags(val
, o
->decoflags
, mask
, byte
);
1918 static enum match_result
find_match(const struct itemplate
**tempp
,
1920 int32_t segment
, int64_t offset
, int bits
)
1922 const struct itemplate
*temp
;
1923 enum match_result m
, merr
;
1924 opflags_t xsizeflags
[MAX_OPERANDS
];
1925 bool opsizemissing
= false;
1926 int8_t broadcast
= instruction
->evex_brerop
;
1929 /* broadcasting uses a different data element size */
1930 for (i
= 0; i
< instruction
->operands
; i
++)
1932 xsizeflags
[i
] = instruction
->oprs
[i
].decoflags
& BRSIZE_MASK
;
1934 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
1936 merr
= MERR_INVALOP
;
1938 for (temp
= nasm_instructions
[instruction
->opcode
];
1939 temp
->opcode
!= I_none
; temp
++) {
1940 m
= matches(temp
, instruction
, bits
);
1941 if (m
== MOK_JUMP
) {
1942 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
1946 } else if (m
== MERR_OPSIZEMISSING
&&
1947 (temp
->flags
& IF_SMASK
) != IF_SX
) {
1949 * Missing operand size and a candidate for fuzzy matching...
1951 for (i
= 0; i
< temp
->operands
; i
++)
1953 xsizeflags
[i
] |= temp
->deco
[i
] & BRSIZE_MASK
;
1955 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
1956 opsizemissing
= true;
1960 if (merr
== MOK_GOOD
)
1964 /* No match, but see if we can get a fuzzy operand size match... */
1968 for (i
= 0; i
< instruction
->operands
; i
++) {
1970 * We ignore extrinsic operand sizes on registers, so we should
1971 * never try to fuzzy-match on them. This also resolves the case
1972 * when we have e.g. "xmmrm128" in two different positions.
1974 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
1977 /* This tests if xsizeflags[i] has more than one bit set */
1978 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
1979 goto done
; /* No luck */
1982 instruction
->oprs
[i
].decoflags
|= xsizeflags
[i
];
1984 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
1987 /* Try matching again... */
1988 for (temp
= nasm_instructions
[instruction
->opcode
];
1989 temp
->opcode
!= I_none
; temp
++) {
1990 m
= matches(temp
, instruction
, bits
);
1991 if (m
== MOK_JUMP
) {
1992 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
1999 if (merr
== MOK_GOOD
)
2008 static enum match_result
matches(const struct itemplate
*itemp
,
2009 insn
*instruction
, int bits
)
2011 opflags_t size
[MAX_OPERANDS
], asize
;
2012 bool opsizemissing
= false;
2018 if (itemp
->opcode
!= instruction
->opcode
)
2019 return MERR_INVALOP
;
2022 * Count the operands
2024 if (itemp
->operands
!= instruction
->operands
)
2025 return MERR_INVALOP
;
2030 if (!(optimizing
> 0) && (itemp
->flags
& IF_OPT
))
2031 return MERR_INVALOP
;
2034 * Check that no spurious colons or TOs are present
2036 for (i
= 0; i
< itemp
->operands
; i
++)
2037 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2038 return MERR_INVALOP
;
2041 * Process size flags
2043 switch (itemp
->flags
& IF_SMASK
) {
2086 if (itemp
->flags
& IF_ARMASK
) {
2087 /* S- flags only apply to a specific operand */
2088 i
= ((itemp
->flags
& IF_ARMASK
) >> IF_ARSHFT
) - 1;
2089 memset(size
, 0, sizeof size
);
2092 /* S- flags apply to all operands */
2093 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2098 * Check that the operand flags all match up,
2099 * it's a bit tricky so lets be verbose:
2101 * 1) Find out the size of operand. If instruction
2102 * doesn't have one specified -- we're trying to
2103 * guess it either from template (IF_S* flag) or
2106 * 2) If template operand do not match the instruction OR
2107 * template has an operand size specified AND this size differ
2108 * from which instruction has (perhaps we got it from code bits)
2110 * a) Check that only size of instruction and operand is differ
2111 * other characteristics do match
2112 * b) Perhaps it's a register specified in instruction so
2113 * for such a case we just mark that operand as "size
2114 * missing" and this will turn on fuzzy operand size
2115 * logic facility (handled by a caller)
2117 for (i
= 0; i
< itemp
->operands
; i
++) {
2118 opflags_t type
= instruction
->oprs
[i
].type
;
2119 decoflags_t deco
= instruction
->oprs
[i
].decoflags
;
2120 if (!(type
& SIZE_MASK
))
2123 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2124 (itemp
->deco
[i
] & deco
) != deco
) {
2125 return MERR_INVALOP
;
2126 } else if ((itemp
->opd
[i
] & SIZE_MASK
) &&
2127 (itemp
->opd
[i
] & SIZE_MASK
) != (type
& SIZE_MASK
)) {
2128 if (type
& SIZE_MASK
) {
2130 * when broadcasting, the element size depends on
2131 * the instruction type. decorator flag should match.
2133 #define MATCH_BRSZ(bits) (((type & SIZE_MASK) == BITS##bits) && \
2134 ((itemp->deco[i] & BRSIZE_MASK) == BR_BITS##bits))
2135 if (!((deco
& BRDCAST_MASK
) &&
2136 (MATCH_BRSZ(32) || MATCH_BRSZ(64)))) {
2137 return MERR_INVALOP
;
2139 } else if (!is_class(REGISTER
, type
)) {
2141 * Note: we don't honor extrinsic operand sizes for registers,
2142 * so "missing operand size" for a register should be
2143 * considered a wildcard match rather than an error.
2145 opsizemissing
= true;
2147 } else if (nasm_regvals
[instruction
->oprs
[i
].basereg
] >= 16 &&
2148 (itemp
->flags
& IF_INSMASK
) != IF_AVX512
) {
2149 return MERR_ENCMISMATCH
;
2154 return MERR_OPSIZEMISSING
;
2157 * Check operand sizes
2159 if (itemp
->flags
& (IF_SM
| IF_SM2
)) {
2160 oprs
= (itemp
->flags
& IF_SM2
? 2 : itemp
->operands
);
2161 for (i
= 0; i
< oprs
; i
++) {
2162 asize
= itemp
->opd
[i
] & SIZE_MASK
;
2164 for (i
= 0; i
< oprs
; i
++)
2170 oprs
= itemp
->operands
;
2173 for (i
= 0; i
< itemp
->operands
; i
++) {
2174 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2175 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2176 return MERR_OPSIZEMISMATCH
;
2180 * Check template is okay at the set cpu level
2182 if (((itemp
->flags
& IF_PLEVEL
) > cpu
))
2186 * Verify the appropriate long mode flag.
2188 if ((itemp
->flags
& (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2189 return MERR_BADMODE
;
2192 * If we have a HLE prefix, look for the NOHLE flag
2194 if ((itemp
->flags
& IF_NOHLE
) &&
2195 (has_prefix(instruction
, PPS_REP
, P_XACQUIRE
) ||
2196 has_prefix(instruction
, PPS_REP
, P_XRELEASE
)))
2200 * Check if special handling needed for Jumps
2202 if ((itemp
->code
[0] & ~1) == 0370)
2209 * Check if offset is a multiple of N with corresponding tuple type
2210 * if Disp8*N is available, compressed displacement is stored in compdisp
2212 static bool is_disp8n(operand
*input
, insn
*ins
, int8_t *compdisp
)
2214 const uint8_t fv_n
[2][2][VLMAX
] = {{{16, 32, 64}, {4, 4, 4}},
2215 {{16, 32, 64}, {8, 8, 8}}};
2216 const uint8_t hv_n
[2][VLMAX
] = {{8, 16, 32}, {4, 4, 4}};
2217 const uint8_t dup_n
[VLMAX
] = {8, 32, 64};
2219 bool evex_b
= input
->decoflags
& BRDCAST_MASK
;
2220 enum ttypes tuple
= ins
->evex_tuple
;
2221 /* vex_wlp composed as [wwllpp] */
2222 enum vectlens vectlen
= (ins
->vex_wlp
& 0x0c) >> 2;
2223 /* wig(=2) is treated as w0(=0) */
2224 bool evex_w
= (ins
->vex_wlp
& 0x10) >> 4;
2225 int32_t off
= input
->offset
;
2231 n
= fv_n
[evex_w
][evex_b
][vectlen
];
2234 n
= hv_n
[evex_b
][vectlen
];
2238 /* 16, 32, 64 for VL 128, 256, 512 respectively*/
2239 n
= 1 << (vectlen
+ 4);
2241 case T1S8
: /* N = 1 */
2242 case T1S16
: /* N = 2 */
2243 n
= tuple
- T1S8
+ 1;
2246 /* N = 4 for 32bit, 8 for 64bit */
2251 /* N = 4 for 32bit, 8 for 64bit */
2252 n
= (tuple
== T1F32
? 4 : 8);
2257 if (vectlen
+ 7 <= (evex_w
+ 5) + (tuple
- T2
+ 1))
2260 n
= 1 << (tuple
- T2
+ evex_w
+ 3);
2265 n
= 1 << (OVM
- tuple
+ vectlen
+ 1);
2278 if (n
&& !(off
& (n
- 1))) {
2280 /* if it fits in Disp8 */
2281 if (disp8
>= -128 && disp8
<= 127) {
2292 * Check if ModR/M.mod should/can be 01.
2293 * - EAF_BYTEOFFS is set
2294 * - offset can fit in a byte when EVEX is not used
2295 * - offset can be compressed when EVEX is used
2297 #define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2298 (o >= -128 && o <= 127 && \
2299 seg == NO_SEG && !forw_ref && \
2300 !(input->eaflags & EAF_WORDOFFS) && \
2301 !(ins->rex & REX_EV)) || \
2302 (ins->rex & REX_EV && \
2303 is_disp8n(input, ins, &output->disp8)))
2305 static enum ea_type
process_ea(operand
*input
, ea
*output
, int bits
,
2306 int rfield
, opflags_t rflags
, insn
*ins
)
2308 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2309 int addrbits
= ins
->addr_size
;
2311 output
->type
= EA_SCALAR
;
2312 output
->rip
= false;
2314 /* REX flags for the rfield operand */
2315 output
->rex
|= rexflags(rfield
, rflags
, REX_R
| REX_P
| REX_W
| REX_H
);
2316 /* EVEX.R' flag for the REG operand */
2317 ins
->evex_p
[0] |= evexflags(rfield
, 0, EVEX_P0RP
, 0);
2319 if (is_class(REGISTER
, input
->type
)) {
2321 * It's a direct register.
2323 if (!is_register(input
->basereg
))
2326 if (!is_reg_class(REG_EA
, input
->basereg
))
2329 /* broadcasting is not available with a direct register operand. */
2330 if (input
->decoflags
& BRDCAST_MASK
) {
2331 nasm_error(ERR_NONFATAL
, "Broadcasting not allowed from a register");
2335 output
->rex
|= op_rexflags(input
, REX_B
| REX_P
| REX_W
| REX_H
);
2336 ins
->evex_p
[0] |= op_evexflags(input
, EVEX_P0X
, 0);
2337 output
->sib_present
= false; /* no SIB necessary */
2338 output
->bytes
= 0; /* no offset necessary either */
2339 output
->modrm
= GEN_MODRM(3, rfield
, nasm_regvals
[input
->basereg
]);
2342 * It's a memory reference.
2345 /* Embedded rounding or SAE is not available with a mem ref operand. */
2346 if (input
->decoflags
& (ER
| SAE
)) {
2347 nasm_error(ERR_NONFATAL
,
2348 "Embedded rounding is available only with reg-reg op.");
2352 if (input
->basereg
== -1 &&
2353 (input
->indexreg
== -1 || input
->scale
== 0)) {
2355 * It's a pure offset.
2357 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
) &&
2358 input
->segment
== NO_SEG
) {
2359 nasm_error(ERR_WARNING
| ERR_PASS1
, "absolute address can not be RIP-relative");
2360 input
->type
&= ~IP_REL
;
2361 input
->type
|= MEMORY
;
2364 if (input
->eaflags
& EAF_BYTEOFFS
||
2365 (input
->eaflags
& EAF_WORDOFFS
&&
2366 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16))) {
2367 nasm_error(ERR_WARNING
| ERR_PASS1
, "displacement size ignored on absolute address");
2370 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2371 output
->sib_present
= true;
2372 output
->sib
= GEN_SIB(0, 4, 5);
2374 output
->modrm
= GEN_MODRM(0, rfield
, 4);
2375 output
->rip
= false;
2377 output
->sib_present
= false;
2378 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2379 output
->modrm
= GEN_MODRM(0, rfield
, (addrbits
!= 16 ? 5 : 6));
2380 output
->rip
= bits
== 64;
2384 * It's an indirection.
2386 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2387 int32_t seg
= input
->segment
;
2388 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2389 int t
, it
, bt
; /* register numbers */
2390 opflags_t x
, ix
, bx
; /* register flags */
2393 i
= -1; /* make this easy, at least */
2395 if (is_register(i
)) {
2396 it
= nasm_regvals
[i
];
2397 ix
= nasm_reg_flags
[i
];
2403 if (is_register(b
)) {
2404 bt
= nasm_regvals
[b
];
2405 bx
= nasm_reg_flags
[b
];
2411 /* if either one are a vector register... */
2412 if ((ix
|bx
) & (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
) {
2413 opflags_t sok
= BITS32
| BITS64
;
2414 int32_t o
= input
->offset
;
2415 int mod
, scale
, index
, base
;
2418 * For a vector SIB, one has to be a vector and the other,
2419 * if present, a GPR. The vector must be the index operand.
2421 if (it
== -1 || (bx
& (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
)) {
2427 t
= bt
, bt
= it
, it
= t
;
2428 x
= bx
, bx
= ix
, ix
= x
;
2434 if (!(REG64
& ~bx
) || !(REG32
& ~bx
))
2441 * While we're here, ensure the user didn't specify
2444 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2447 if (addrbits
== 16 ||
2448 (addrbits
== 32 && !(sok
& BITS32
)) ||
2449 (addrbits
== 64 && !(sok
& BITS64
)))
2452 output
->type
= ((ix
& ZMMREG
& ~REG_EA
) ? EA_ZMMVSIB
2453 : ((ix
& YMMREG
& ~REG_EA
)
2454 ? EA_YMMVSIB
: EA_XMMVSIB
));
2456 output
->rex
|= rexflags(it
, ix
, REX_X
);
2457 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2458 ins
->evex_p
[2] |= evexflags(it
, 0, EVEX_P2VP
, 2);
2460 index
= it
& 7; /* it is known to be != -1 */
2475 default: /* then what the smeg is it? */
2476 goto err
; /* panic */
2484 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2485 seg
== NO_SEG
&& !forw_ref
&&
2486 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2488 else if (IS_MOD_01())
2494 output
->sib_present
= true;
2495 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2496 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2497 output
->sib
= GEN_SIB(scale
, index
, base
);
2498 } else if ((ix
|bx
) & (BITS32
|BITS64
)) {
2500 * it must be a 32/64-bit memory reference. Firstly we have
2501 * to check that all registers involved are type E/Rxx.
2503 opflags_t sok
= BITS32
| BITS64
;
2504 int32_t o
= input
->offset
;
2507 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2515 goto err
; /* Invalid register */
2516 if (~sok
& bx
& SIZE_MASK
)
2517 goto err
; /* Invalid size */
2522 * While we're here, ensure the user didn't specify
2525 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2528 if (addrbits
== 16 ||
2529 (addrbits
== 32 && !(sok
& BITS32
)) ||
2530 (addrbits
== 64 && !(sok
& BITS64
)))
2533 /* now reorganize base/index */
2534 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2535 ((hb
== b
&& ht
== EAH_NOTBASE
) ||
2536 (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2537 /* swap if hints say so */
2538 t
= bt
, bt
= it
, it
= t
;
2539 x
= bx
, bx
= ix
, ix
= x
;
2541 if (bt
== it
) /* convert EAX+2*EAX to 3*EAX */
2542 bt
= -1, bx
= 0, s
++;
2543 if (bt
== -1 && s
== 1 && !(hb
== it
&& ht
== EAH_NOTBASE
)) {
2544 /* make single reg base, unless hint */
2545 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2547 if (((s
== 2 && it
!= REG_NUM_ESP
&& !(input
->eaflags
& EAF_TIMESTWO
)) ||
2548 s
== 3 || s
== 5 || s
== 9) && bt
== -1)
2549 bt
= it
, bx
= ix
, s
--; /* convert 3*EAX to EAX+2*EAX */
2550 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
&&
2551 (input
->eaflags
& EAF_TIMESTWO
))
2552 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2553 /* convert [NOSPLIT EAX] to sib format with 0x0 displacement */
2554 if (s
== 1 && it
== REG_NUM_ESP
) {
2555 /* swap ESP into base if scale is 1 */
2556 t
= it
, it
= bt
, bt
= t
;
2557 x
= ix
, ix
= bx
, bx
= x
;
2559 if (it
== REG_NUM_ESP
||
2560 (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2561 goto err
; /* wrong, for various reasons */
2563 output
->rex
|= rexflags(it
, ix
, REX_X
);
2564 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2566 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2575 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2576 seg
== NO_SEG
&& !forw_ref
&&
2577 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2579 else if (IS_MOD_01())
2585 output
->sib_present
= false;
2586 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2587 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2590 int mod
, scale
, index
, base
;
2610 default: /* then what the smeg is it? */
2611 goto err
; /* panic */
2619 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2620 seg
== NO_SEG
&& !forw_ref
&&
2621 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2623 else if (IS_MOD_01())
2629 output
->sib_present
= true;
2630 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2631 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2632 output
->sib
= GEN_SIB(scale
, index
, base
);
2634 } else { /* it's 16-bit */
2636 int16_t o
= input
->offset
;
2638 /* check for 64-bit long mode */
2642 /* check all registers are BX, BP, SI or DI */
2643 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
&& b
!= R_DI
) ||
2644 (i
!= -1 && i
!= R_BP
&& i
!= R_BX
&& i
!= R_SI
&& i
!= R_DI
))
2647 /* ensure the user didn't specify DWORD/QWORD */
2648 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2651 if (s
!= 1 && i
!= -1)
2652 goto err
; /* no can do, in 16-bit EA */
2653 if (b
== -1 && i
!= -1) {
2658 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2663 /* have BX/BP as base, SI/DI index */
2665 goto err
; /* shouldn't ever happen, in theory */
2666 if (i
!= -1 && b
!= -1 &&
2667 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2668 goto err
; /* invalid combinations */
2669 if (b
== -1) /* pure offset: handled above */
2670 goto err
; /* so if it gets to here, panic! */
2674 switch (i
* 256 + b
) {
2675 case R_SI
* 256 + R_BX
:
2678 case R_DI
* 256 + R_BX
:
2681 case R_SI
* 256 + R_BP
:
2684 case R_DI
* 256 + R_BP
:
2702 if (rm
== -1) /* can't happen, in theory */
2703 goto err
; /* so panic if it does */
2705 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2706 !(input
->eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2708 else if (IS_MOD_01())
2713 output
->sib_present
= false; /* no SIB - it's 16-bit */
2714 output
->bytes
= mod
; /* bytes of offset needed */
2715 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2720 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2721 return output
->type
;
2724 return output
->type
= EA_INVALID
;
2727 static void add_asp(insn
*ins
, int addrbits
)
2732 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2734 switch (ins
->prefixes
[PPS_ASIZE
]) {
2745 valid
&= (addrbits
== 32) ? 16 : 32;
2751 for (j
= 0; j
< ins
->operands
; j
++) {
2752 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
2755 /* Verify as Register */
2756 if (!is_register(ins
->oprs
[j
].indexreg
))
2759 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2761 /* Verify as Register */
2762 if (!is_register(ins
->oprs
[j
].basereg
))
2765 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2767 if (ins
->oprs
[j
].scale
== 0)
2771 int ds
= ins
->oprs
[j
].disp_size
;
2772 if ((addrbits
!= 64 && ds
> 8) ||
2773 (addrbits
== 64 && ds
== 16))
2793 if (valid
& addrbits
) {
2794 ins
->addr_size
= addrbits
;
2795 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2796 /* Add an address size prefix */
2797 ins
->prefixes
[PPS_ASIZE
] = (addrbits
== 32) ? P_A16
: P_A32
;;
2798 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2801 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2802 ins
->addr_size
= addrbits
; /* Error recovery */
2805 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2807 for (j
= 0; j
< ins
->operands
; j
++) {
2808 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2809 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
) != ins
->addr_size
) {
2811 * mem_offs sizes must match the address size; if not,
2812 * strip the MEM_OFFS bit and match only EA instructions
2814 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);