1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2014 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"
177 #include <inttypes.h>
181 #include "assemble.h"
188 * Matching errors. These should be sorted so that more specific
189 * errors come later in the sequence.
202 * Matching success; the conditional ones first
204 MOK_JUMP
, /* Matching OK but needs jmp_match() */
205 MOK_GOOD
/* Matching unconditionally OK */
209 enum ea_type type
; /* what kind of EA is this? */
210 int sib_present
; /* is a SIB byte necessary? */
211 int bytes
; /* # of bytes of offset needed */
212 int size
; /* lazy - this is sib+bytes+1 */
213 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
214 int8_t disp8
; /* compressed displacement for EVEX */
217 #define GEN_SIB(scale, index, base) \
218 (((scale) << 6) | ((index) << 3) | ((base)))
220 #define GEN_MODRM(mod, reg, rm) \
221 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
223 static iflag_t cpu
; /* cpu level received from nasm.c */
224 static efunc errfunc
;
225 static struct ofmt
*outfmt
;
226 static ListGen
*list
;
228 static int64_t calcsize(int32_t, int64_t, int, insn
*,
229 const struct itemplate
*);
230 static void gencode(int32_t segment
, int64_t offset
, int bits
,
231 insn
* ins
, const struct itemplate
*temp
,
233 static enum match_result
find_match(const struct itemplate
**tempp
,
235 int32_t segment
, int64_t offset
, int bits
);
236 static enum match_result
matches(const struct itemplate
*, insn
*, int bits
);
237 static opflags_t
regflag(const operand
*);
238 static int32_t regval(const operand
*);
239 static int rexflags(int, opflags_t
, int);
240 static int op_rexflags(const operand
*, int);
241 static int op_evexflags(const operand
*, int, uint8_t);
242 static void add_asp(insn
*, int);
244 static enum ea_type
process_ea(operand
*, ea
*, int, int, opflags_t
, insn
*);
246 static int has_prefix(insn
* ins
, enum prefix_pos pos
, int prefix
)
248 return ins
->prefixes
[pos
] == prefix
;
251 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
253 if (ins
->prefixes
[pos
])
254 errfunc(ERR_NONFATAL
, "invalid %s prefix",
255 prefix_name(ins
->prefixes
[pos
]));
258 static const char *size_name(int size
)
282 static void warn_overflow(int pass
, int size
)
284 errfunc(ERR_WARNING
| pass
| ERR_WARN_NOV
,
285 "%s data exceeds bounds", size_name(size
));
288 static void warn_overflow_const(int64_t data
, int size
)
290 if (overflow_general(data
, size
))
291 warn_overflow(ERR_PASS1
, size
);
294 static void warn_overflow_opd(const struct operand
*o
, int size
)
296 if (o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
297 if (overflow_general(o
->offset
, size
))
298 warn_overflow(ERR_PASS2
, size
);
303 * This routine wrappers the real output format's output routine,
304 * in order to pass a copy of the data off to the listing file
305 * generator at the same time.
307 static void out(int64_t offset
, int32_t segto
, const void *data
,
308 enum out_type type
, uint64_t size
,
309 int32_t segment
, int32_t wrt
)
311 static int32_t lineno
= 0; /* static!!! */
312 static char *lnfname
= NULL
;
315 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
317 * This is a non-relocated address, and we're going to
318 * convert it into RAWDATA format.
322 size
= abs((int)size
);
324 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
328 WRITEADDR(q
, *(int64_t *)data
, size
);
333 list
->output(offset
, data
, type
, size
);
336 * this call to src_get determines when we call the
337 * debug-format-specific "linenum" function
338 * it updates lineno and lnfname to the current values
339 * returning 0 if "same as last time", -2 if lnfname
340 * changed, and the amount by which lineno changed,
341 * if it did. thus, these variables must be static
344 if (src_get(&lineno
, &lnfname
))
345 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
347 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
350 static void out_imm8(int64_t offset
, int32_t segment
,
351 struct operand
*opx
, int asize
)
353 if (opx
->segment
!= NO_SEG
) {
354 uint64_t data
= opx
->offset
;
355 out(offset
, segment
, &data
, OUT_ADDRESS
, asize
, opx
->segment
, opx
->wrt
);
357 uint8_t byte
= opx
->offset
;
358 out(offset
, segment
, &byte
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
362 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
363 insn
* ins
, const struct itemplate
*temp
)
366 const uint8_t *code
= temp
->code
;
370 if (((c
& ~1) != 0370) || (ins
->oprs
[0].type
& STRICT
))
374 if (optimizing
< 0 && c
== 0371)
377 isize
= calcsize(segment
, offset
, bits
, ins
, temp
);
379 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
380 /* Be optimistic in pass 1 */
383 if (ins
->oprs
[0].segment
!= segment
)
386 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
387 is_byte
= (isize
>= -128 && isize
<= 127); /* is it byte size? */
389 if (is_byte
&& c
== 0371 && ins
->prefixes
[PPS_REP
] == P_BND
) {
390 /* jmp short (opcode eb) cannot be used with bnd prefix. */
391 ins
->prefixes
[PPS_REP
] = P_none
;
392 errfunc(ERR_WARNING
| ERR_WARN_BND
| ERR_PASS2
,
393 "jmp short does not init bnd regs - bnd prefix dropped.");
399 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, iflag_t cp
,
400 insn
* instruction
, struct ofmt
*output
, efunc error
,
403 const struct itemplate
*temp
;
408 int64_t start
= offset
;
409 int64_t wsize
; /* size for DB etc. */
411 errfunc
= error
; /* to pass to other functions */
413 outfmt
= output
; /* likewise */
414 list
= listgen
; /* and again */
416 wsize
= idata_bytes(instruction
->opcode
);
422 int32_t t
= instruction
->times
;
425 "instruction->times < 0 (%ld) in assemble()", t
);
427 while (t
--) { /* repeat TIMES times */
428 list_for_each(e
, instruction
->eops
) {
429 if (e
->type
== EOT_DB_NUMBER
) {
431 errfunc(ERR_NONFATAL
,
432 "integer supplied to a DT, DO or DY"
435 out(offset
, segment
, &e
->offset
,
436 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
439 } else if (e
->type
== EOT_DB_STRING
||
440 e
->type
== EOT_DB_STRING_FREE
) {
443 out(offset
, segment
, e
->stringval
,
444 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
445 align
= e
->stringlen
% wsize
;
448 align
= wsize
- align
;
449 out(offset
, segment
, zero_buffer
,
450 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
452 offset
+= e
->stringlen
+ align
;
455 if (t
> 0 && t
== instruction
->times
- 1) {
457 * Dummy call to list->output to give the offset to the
460 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
461 list
->uplevel(LIST_TIMES
);
464 if (instruction
->times
> 1)
465 list
->downlevel(LIST_TIMES
);
466 return offset
- start
;
469 if (instruction
->opcode
== I_INCBIN
) {
470 const char *fname
= instruction
->eops
->stringval
;
473 fp
= fopen(fname
, "rb");
475 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
477 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
478 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
482 static char buf
[4096];
483 size_t t
= instruction
->times
;
488 if (instruction
->eops
->next
) {
489 base
= instruction
->eops
->next
->offset
;
491 if (instruction
->eops
->next
->next
&&
492 len
> (size_t)instruction
->eops
->next
->next
->offset
)
493 len
= (size_t)instruction
->eops
->next
->next
->offset
;
496 * Dummy call to list->output to give the offset to the
499 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
500 list
->uplevel(LIST_INCBIN
);
504 fseek(fp
, base
, SEEK_SET
);
508 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
511 * This shouldn't happen unless the file
512 * actually changes while we are reading
516 "`incbin': unexpected EOF while"
517 " reading file `%s'", fname
);
518 t
= 0; /* Try to exit cleanly */
521 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
526 list
->downlevel(LIST_INCBIN
);
527 if (instruction
->times
> 1) {
529 * Dummy call to list->output to give the offset to the
532 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
533 list
->uplevel(LIST_TIMES
);
534 list
->downlevel(LIST_TIMES
);
537 return instruction
->times
* len
;
539 return 0; /* if we're here, there's an error */
542 /* Check to see if we need an address-size prefix */
543 add_asp(instruction
, bits
);
545 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
549 int64_t insn_size
= calcsize(segment
, offset
, bits
, instruction
, temp
);
550 itimes
= instruction
->times
;
551 if (insn_size
< 0) /* shouldn't be, on pass two */
552 error(ERR_PANIC
, "errors made it through from pass one");
555 for (j
= 0; j
< MAXPREFIX
; j
++) {
557 switch (instruction
->prefixes
[j
]) {
578 error(ERR_WARNING
| ERR_PASS2
,
579 "cs segment base generated, but will be ignored in 64-bit mode");
585 error(ERR_WARNING
| ERR_PASS2
,
586 "ds segment base generated, but will be ignored in 64-bit mode");
592 error(ERR_WARNING
| ERR_PASS2
,
593 "es segment base generated, but will be ignored in 64-bit mode");
605 error(ERR_WARNING
| ERR_PASS2
,
606 "ss segment base generated, but will be ignored in 64-bit mode");
613 "segr6 and segr7 cannot be used as prefixes");
618 "16-bit addressing is not supported "
620 } else if (bits
!= 16)
630 "64-bit addressing is only supported "
658 error(ERR_PANIC
, "invalid instruction prefix");
661 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
666 insn_end
= offset
+ insn_size
;
667 gencode(segment
, offset
, bits
, instruction
,
670 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
672 * Dummy call to list->output to give the offset to the
675 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
676 list
->uplevel(LIST_TIMES
);
679 if (instruction
->times
> 1)
680 list
->downlevel(LIST_TIMES
);
681 return offset
- start
;
685 case MERR_OPSIZEMISSING
:
686 error(ERR_NONFATAL
, "operation size not specified");
688 case MERR_OPSIZEMISMATCH
:
689 error(ERR_NONFATAL
, "mismatch in operand sizes");
691 case MERR_BRNUMMISMATCH
:
693 "mismatch in the number of broadcasting elements");
696 error(ERR_NONFATAL
, "no instruction for this cpu level");
699 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
702 case MERR_ENCMISMATCH
:
703 error(ERR_NONFATAL
, "specific encoding scheme not available");
706 error(ERR_NONFATAL
, "bnd prefix is not allowed");
709 error(ERR_NONFATAL
, "%s prefix is not allowed",
710 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ?
715 "invalid combination of opcode and operands");
722 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, iflag_t cp
,
723 insn
* instruction
, efunc error
)
725 const struct itemplate
*temp
;
728 errfunc
= error
; /* to pass to other functions */
731 if (instruction
->opcode
== I_none
)
734 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
735 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
736 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
737 instruction
->opcode
== I_DY
) {
739 int32_t isize
, osize
, wsize
;
742 wsize
= idata_bytes(instruction
->opcode
);
744 list_for_each(e
, instruction
->eops
) {
748 if (e
->type
== EOT_DB_NUMBER
) {
750 warn_overflow_const(e
->offset
, wsize
);
751 } else if (e
->type
== EOT_DB_STRING
||
752 e
->type
== EOT_DB_STRING_FREE
)
753 osize
= e
->stringlen
;
755 align
= (-osize
) % wsize
;
758 isize
+= osize
+ align
;
760 return isize
* instruction
->times
;
763 if (instruction
->opcode
== I_INCBIN
) {
764 const char *fname
= instruction
->eops
->stringval
;
769 fp
= fopen(fname
, "rb");
771 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
773 else if (fseek(fp
, 0L, SEEK_END
) < 0)
774 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
778 if (instruction
->eops
->next
) {
779 len
-= instruction
->eops
->next
->offset
;
780 if (instruction
->eops
->next
->next
&&
781 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
782 len
= (size_t)instruction
->eops
->next
->next
->offset
;
785 val
= instruction
->times
* len
;
792 /* Check to see if we need an address-size prefix */
793 add_asp(instruction
, bits
);
795 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
797 /* we've matched an instruction. */
801 isize
= calcsize(segment
, offset
, bits
, instruction
, temp
);
804 for (j
= 0; j
< MAXPREFIX
; j
++) {
805 switch (instruction
->prefixes
[j
]) {
835 return isize
* instruction
->times
;
837 return -1; /* didn't match any instruction */
841 static void bad_hle_warn(const insn
* ins
, uint8_t hleok
)
843 enum prefixes rep_pfx
= ins
->prefixes
[PPS_REP
];
844 enum whatwarn
{ w_none
, w_lock
, w_inval
} ww
;
845 static const enum whatwarn warn
[2][4] =
847 { w_inval
, w_inval
, w_none
, w_lock
}, /* XACQUIRE */
848 { w_inval
, w_none
, w_none
, w_lock
}, /* XRELEASE */
852 n
= (unsigned int)rep_pfx
- P_XACQUIRE
;
854 return; /* Not XACQUIRE/XRELEASE */
857 if (!is_class(MEMORY
, ins
->oprs
[0].type
))
858 ww
= w_inval
; /* HLE requires operand 0 to be memory */
865 if (ins
->prefixes
[PPS_LOCK
] != P_LOCK
) {
866 errfunc(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
867 "%s with this instruction requires lock",
868 prefix_name(rep_pfx
));
873 errfunc(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
874 "%s invalid with this instruction",
875 prefix_name(rep_pfx
));
880 /* Common construct */
881 #define case3(x) case (x): case (x)+1: case (x)+2
882 #define case4(x) case3(x): case (x)+3
884 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
885 insn
* ins
, const struct itemplate
*temp
)
887 const uint8_t *codes
= temp
->code
;
896 bool lockcheck
= true;
897 enum reg_enum mib_index
= R_none
; /* For a separate index MIB reg form */
899 ins
->rex
= 0; /* Ensure REX is reset */
900 eat
= EA_SCALAR
; /* Expect a scalar EA */
901 memset(ins
->evex_p
, 0, 3); /* Ensure EVEX is reset */
903 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
906 (void)segment
; /* Don't warn that this parameter is unused */
907 (void)offset
; /* Don't warn that this parameter is unused */
911 op1
= (c
& 3) + ((opex
& 1) << 2);
912 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
913 opx
= &ins
->oprs
[op1
];
914 opex
= 0; /* For the next iteration */
918 codes
+= c
, length
+= c
;
927 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
932 /* this is an index reg of MIB operand */
933 mib_index
= opx
->basereg
;
946 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
947 length
+= (opx
->type
& BITS16
) ? 2 : 4;
949 length
+= (bits
== 16) ? 2 : 4;
957 length
+= ins
->addr_size
>> 3;
965 length
+= 8; /* MOV reg64/imm */
973 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
974 length
+= (opx
->type
& BITS16
) ? 2 : 4;
976 length
+= (bits
== 16) ? 2 : 4;
999 ins
->vexreg
= regval(opx
);
1000 ins
->evex_p
[2] |= op_evexflags(opx
, EVEX_P2VP
, 2); /* High-16 NDS */
1001 ins
->vex_cm
= *codes
++;
1002 ins
->vex_wlp
= *codes
++;
1003 ins
->evex_tuple
= (*codes
++ - 0300);
1009 ins
->vex_cm
= *codes
++;
1010 ins
->vex_wlp
= *codes
++;
1011 ins
->evex_tuple
= (*codes
++ - 0300);
1020 ins
->vexreg
= regval(opx
);
1021 ins
->vex_cm
= *codes
++;
1022 ins
->vex_wlp
= *codes
++;
1028 ins
->vex_cm
= *codes
++;
1029 ins
->vex_wlp
= *codes
++;
1046 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
1050 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1057 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1058 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1067 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1071 errfunc(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1073 ins
->prefixes
[PPS_OSIZE
] = P_O16
;
1079 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1083 errfunc(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1085 ins
->prefixes
[PPS_OSIZE
] = P_O32
;
1127 if (!ins
->prefixes
[PPS_REP
])
1128 ins
->prefixes
[PPS_REP
] = P_REP
;
1132 if (!ins
->prefixes
[PPS_REP
])
1133 ins
->prefixes
[PPS_REP
] = P_REPNE
;
1137 if (ins
->oprs
[0].segment
!= NO_SEG
)
1138 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1139 " quantity of BSS space");
1141 length
+= ins
->oprs
[0].offset
;
1145 if (!ins
->prefixes
[PPS_WAIT
])
1146 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1201 struct operand
*opy
= &ins
->oprs
[op2
];
1202 struct operand
*op_er_sae
;
1204 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1207 /* pick rfield from operand b (opx) */
1208 rflags
= regflag(opx
);
1209 rfield
= nasm_regvals
[opx
->basereg
];
1215 /* EVEX.b1 : evex_brerop contains the operand position */
1216 op_er_sae
= (ins
->evex_brerop
>= 0 ?
1217 &ins
->oprs
[ins
->evex_brerop
] : NULL
);
1219 if (op_er_sae
&& (op_er_sae
->decoflags
& (ER
| SAE
))) {
1221 ins
->evex_p
[2] |= EVEX_P2B
;
1222 if (op_er_sae
->decoflags
& ER
) {
1223 /* set EVEX.RC (rounding control) */
1224 ins
->evex_p
[2] |= ((ins
->evex_rm
- BRC_RN
) << 5)
1228 /* set EVEX.L'L (vector length) */
1229 ins
->evex_p
[2] |= ((ins
->vex_wlp
<< (5 - 2)) & EVEX_P2LL
);
1230 ins
->evex_p
[1] |= ((ins
->vex_wlp
<< (7 - 4)) & EVEX_P1W
);
1231 if (opy
->decoflags
& BRDCAST_MASK
) {
1233 ins
->evex_p
[2] |= EVEX_P2B
;
1237 if (itemp_has(temp
, IF_MIB
)) {
1238 opy
->eaflags
|= EAF_MIB
;
1240 * if a separate form of MIB (ICC style) is used,
1241 * the index reg info is merged into mem operand
1243 if (mib_index
!= R_none
) {
1244 opy
->indexreg
= mib_index
;
1246 opy
->hintbase
= mib_index
;
1247 opy
->hinttype
= EAH_NOTBASE
;
1251 if (process_ea(opy
, &ea_data
, bits
,
1252 rfield
, rflags
, ins
) != eat
) {
1253 errfunc(ERR_NONFATAL
, "invalid effective address");
1256 ins
->rex
|= ea_data
.rex
;
1257 length
+= ea_data
.size
;
1263 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1264 ": instruction code \\%o (0x%02X) given", c
, c
);
1269 ins
->rex
&= rex_mask
;
1271 if (ins
->rex
& REX_NH
) {
1272 if (ins
->rex
& REX_H
) {
1273 errfunc(ERR_NONFATAL
, "instruction cannot use high registers");
1276 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1279 switch (ins
->prefixes
[PPS_VEX
]) {
1281 if (!(ins
->rex
& REX_EV
))
1286 if (!(ins
->rex
& REX_V
))
1293 if (ins
->rex
& (REX_V
| REX_EV
)) {
1294 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1296 if (ins
->rex
& REX_H
) {
1297 errfunc(ERR_NONFATAL
, "cannot use high register in AVX instruction");
1300 switch (ins
->vex_wlp
& 060) {
1314 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->vexreg
> 7)) {
1315 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1317 } else if (!(ins
->rex
& REX_EV
) &&
1318 ((ins
->vexreg
> 15) || (ins
->evex_p
[0] & 0xf0))) {
1319 errfunc(ERR_NONFATAL
, "invalid high-16 register in non-AVX-512");
1322 if (ins
->rex
& REX_EV
)
1324 else if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1325 ins
->prefixes
[PPS_VEX
] == P_VEX3
)
1329 } else if (ins
->rex
& REX_MASK
) {
1330 if (ins
->rex
& REX_H
) {
1331 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1333 } else if (bits
== 64) {
1335 } else if ((ins
->rex
& REX_L
) &&
1336 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1337 iflag_ffs(&cpu
) >= IF_X86_64
) {
1339 assert_no_prefix(ins
, PPS_LOCK
);
1340 lockcheck
= false; /* Already errored, no need for warning */
1343 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1348 if (has_prefix(ins
, PPS_LOCK
, P_LOCK
) && lockcheck
&&
1349 (!itemp_has(temp
,IF_LOCK
) || !is_class(MEMORY
, ins
->oprs
[0].type
))) {
1350 errfunc(ERR_WARNING
| ERR_WARN_LOCK
| ERR_PASS2
,
1351 "instruction is not lockable");
1354 bad_hle_warn(ins
, hleok
);
1357 * when BND prefix is set by DEFAULT directive,
1358 * BND prefix is added to every appropriate instruction line
1359 * unless it is overridden by NOBND prefix.
1362 (itemp_has(temp
, IF_BND
) && !has_prefix(ins
, PPS_REP
, P_NOBND
)))
1363 ins
->prefixes
[PPS_REP
] = P_BND
;
1368 static inline unsigned int emit_rex(insn
*ins
, int32_t segment
, int64_t offset
, int bits
)
1371 if ((ins
->rex
& REX_MASK
) &&
1372 !(ins
->rex
& (REX_V
| REX_EV
)) &&
1374 int rex
= (ins
->rex
& REX_MASK
) | REX_P
;
1375 out(offset
, segment
, &rex
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1376 ins
->rex_done
= true;
1384 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1385 insn
* ins
, const struct itemplate
*temp
,
1393 struct operand
*opx
;
1394 const uint8_t *codes
= temp
->code
;
1396 enum ea_type eat
= EA_SCALAR
;
1398 ins
->rex_done
= false;
1402 op1
= (c
& 3) + ((opex
& 1) << 2);
1403 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1404 opx
= &ins
->oprs
[op1
];
1405 opex
= 0; /* For the next iteration */
1412 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1413 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1425 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1426 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1427 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1435 if (opx
->offset
< -256 || opx
->offset
> 255) {
1436 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1437 "byte value exceeds bounds");
1439 out_imm8(offset
, segment
, opx
, -1);
1444 if (opx
->offset
< 0 || opx
->offset
> 255)
1445 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1446 "unsigned byte value exceeds bounds");
1447 out_imm8(offset
, segment
, opx
, 1);
1452 warn_overflow_opd(opx
, 2);
1454 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1455 opx
->segment
, opx
->wrt
);
1460 if (opx
->type
& (BITS16
| BITS32
))
1461 size
= (opx
->type
& BITS16
) ? 2 : 4;
1463 size
= (bits
== 16) ? 2 : 4;
1464 warn_overflow_opd(opx
, size
);
1466 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1467 opx
->segment
, opx
->wrt
);
1472 warn_overflow_opd(opx
, 4);
1474 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1475 opx
->segment
, opx
->wrt
);
1481 size
= ins
->addr_size
>> 3;
1482 warn_overflow_opd(opx
, size
);
1483 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1484 opx
->segment
, opx
->wrt
);
1489 if (opx
->segment
!= segment
) {
1491 out(offset
, segment
, &data
,
1492 OUT_REL1ADR
, insn_end
- offset
,
1493 opx
->segment
, opx
->wrt
);
1495 data
= opx
->offset
- insn_end
;
1496 if (data
> 127 || data
< -128)
1497 errfunc(ERR_NONFATAL
, "short jump is out of range");
1498 out(offset
, segment
, &data
,
1499 OUT_ADDRESS
, 1, NO_SEG
, NO_SEG
);
1505 data
= (int64_t)opx
->offset
;
1506 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1507 opx
->segment
, opx
->wrt
);
1512 if (opx
->segment
!= segment
) {
1514 out(offset
, segment
, &data
,
1515 OUT_REL2ADR
, insn_end
- offset
,
1516 opx
->segment
, opx
->wrt
);
1518 data
= opx
->offset
- insn_end
;
1519 out(offset
, segment
, &data
,
1520 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1526 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1527 size
= (opx
->type
& BITS16
) ? 2 : 4;
1529 size
= (bits
== 16) ? 2 : 4;
1530 if (opx
->segment
!= segment
) {
1532 out(offset
, segment
, &data
,
1533 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1534 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1536 data
= opx
->offset
- insn_end
;
1537 out(offset
, segment
, &data
,
1538 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1544 if (opx
->segment
!= segment
) {
1546 out(offset
, segment
, &data
,
1547 OUT_REL4ADR
, insn_end
- offset
,
1548 opx
->segment
, opx
->wrt
);
1550 data
= opx
->offset
- insn_end
;
1551 out(offset
, segment
, &data
,
1552 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1558 if (opx
->segment
== NO_SEG
)
1559 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1562 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1563 outfmt
->segbase(1 + opx
->segment
),
1570 opx
= &ins
->oprs
[c
>> 3];
1571 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1572 opx
= &ins
->oprs
[c
& 7];
1573 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1574 errfunc(ERR_NONFATAL
,
1575 "non-absolute expression not permitted as argument %d",
1578 if (opx
->offset
& ~15) {
1579 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1580 "four-bit argument exceeds bounds");
1582 bytes
[0] |= opx
->offset
& 15;
1584 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1590 opx
= &ins
->oprs
[c
>> 4];
1591 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1593 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1598 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1599 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1605 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1606 (int32_t)data
!= (int64_t)data
) {
1607 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1608 "signed dword immediate exceeds bounds");
1610 out(offset
, segment
, &data
, OUT_ADDRESS
, -4,
1611 opx
->segment
, opx
->wrt
);
1618 ins
->evex_p
[2] |= op_evexflags(&ins
->oprs
[0],
1619 EVEX_P2Z
| EVEX_P2AAA
, 2);
1620 ins
->evex_p
[2] ^= EVEX_P2VP
; /* 1's complement */
1622 /* EVEX.X can be set by either REX or EVEX for different reasons */
1623 bytes
[1] = ((((ins
->rex
& 7) << 5) |
1624 (ins
->evex_p
[0] & (EVEX_P0X
| EVEX_P0RP
))) ^ 0xf0) |
1626 bytes
[2] = ((ins
->rex
& REX_W
) << (7 - 3)) |
1627 ((~ins
->vexreg
& 15) << 3) |
1628 (1 << 2) | (ins
->vex_wlp
& 3);
1629 bytes
[3] = ins
->evex_p
[2];
1630 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 4, NO_SEG
, NO_SEG
);
1637 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1638 ins
->prefixes
[PPS_VEX
] == P_VEX3
) {
1639 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1640 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1641 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1642 ((~ins
->vexreg
& 15)<< 3) | (ins
->vex_wlp
& 07);
1643 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1647 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1648 ((~ins
->vexreg
& 15) << 3) | (ins
->vex_wlp
& 07);
1649 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1664 if (ins
->rex
& REX_W
)
1666 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1668 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1673 um
= (uint64_t)2 << (s
-1);
1676 if (uv
> 127 && uv
< (uint64_t)-128 &&
1677 (uv
< um
-128 || uv
> um
-1)) {
1678 /* If this wasn't explicitly byte-sized, warn as though we
1679 * had fallen through to the imm16/32/64 case.
1681 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1682 "%s value exceeds bounds",
1683 (opx
->type
& BITS8
) ? "signed byte" :
1688 if (opx
->segment
!= NO_SEG
) {
1690 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1691 opx
->segment
, opx
->wrt
);
1694 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1705 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1707 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1714 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1716 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1751 *bytes
= *codes
++ ^ get_cond_opcode(ins
->condition
);
1752 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1761 *bytes
= c
- 0332 + 0xF2;
1762 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1767 if (ins
->rex
& REX_R
) {
1769 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1772 ins
->rex
&= ~(REX_L
|REX_R
);
1783 if (ins
->oprs
[0].segment
!= NO_SEG
)
1784 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1786 int64_t size
= ins
->oprs
[0].offset
;
1788 out(offset
, segment
, NULL
,
1789 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1802 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1812 *bytes
= c
- 0366 + 0x66;
1813 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1821 *bytes
= bits
== 16 ? 3 : 5;
1822 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1856 struct operand
*opy
= &ins
->oprs
[op2
];
1859 /* pick rfield from operand b (opx) */
1860 rflags
= regflag(opx
);
1861 rfield
= nasm_regvals
[opx
->basereg
];
1863 /* rfield is constant */
1868 if (process_ea(opy
, &ea_data
, bits
,
1869 rfield
, rflags
, ins
) != eat
)
1870 errfunc(ERR_NONFATAL
, "invalid effective address");
1873 *p
++ = ea_data
.modrm
;
1874 if (ea_data
.sib_present
)
1878 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1881 * Make sure the address gets the right offset in case
1882 * the line breaks in the .lst file (BR 1197827)
1887 if (ea_data
.bytes
) {
1888 /* use compressed displacement, if available */
1889 data
= ea_data
.disp8
? ea_data
.disp8
: opy
->offset
;
1892 if (opy
->segment
== segment
) {
1894 if (overflow_signed(data
, ea_data
.bytes
))
1895 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1896 out(offset
, segment
, &data
, OUT_ADDRESS
,
1897 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1899 /* overflow check in output/linker? */
1900 out(offset
, segment
, &data
, OUT_REL4ADR
,
1901 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1904 int asize
= ins
->addr_size
>> 3;
1905 int atype
= ea_data
.bytes
;
1907 if (overflow_general(data
, asize
) ||
1908 signed_bits(data
, ins
->addr_size
) !=
1909 signed_bits(data
, ea_data
.bytes
<< 3))
1910 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1912 if (asize
> ea_data
.bytes
) {
1914 * If the address isn't the full width of
1915 * the address size, treat is as signed...
1920 out(offset
, segment
, &data
, OUT_ADDRESS
,
1921 atype
, opy
->segment
, opy
->wrt
);
1929 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1930 ": instruction code \\%o (0x%02X) given", c
, c
);
1936 static opflags_t
regflag(const operand
* o
)
1938 if (!is_register(o
->basereg
))
1939 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1940 return nasm_reg_flags
[o
->basereg
];
1943 static int32_t regval(const operand
* o
)
1945 if (!is_register(o
->basereg
))
1946 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1947 return nasm_regvals
[o
->basereg
];
1950 static int op_rexflags(const operand
* o
, int mask
)
1955 if (!is_register(o
->basereg
))
1956 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1958 flags
= nasm_reg_flags
[o
->basereg
];
1959 val
= nasm_regvals
[o
->basereg
];
1961 return rexflags(val
, flags
, mask
);
1964 static int rexflags(int val
, opflags_t flags
, int mask
)
1968 if (val
>= 0 && (val
& 8))
1969 rex
|= REX_B
|REX_X
|REX_R
;
1972 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1974 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1980 static int evexflags(int val
, decoflags_t deco
,
1981 int mask
, uint8_t byte
)
1987 if (val
>= 0 && (val
& 16))
1988 evex
|= (EVEX_P0RP
| EVEX_P0X
);
1991 if (val
>= 0 && (val
& 16))
1995 if (deco
& OPMASK_MASK
)
1996 evex
|= deco
& EVEX_P2AAA
;
2002 static int op_evexflags(const operand
* o
, int mask
, uint8_t byte
)
2006 val
= nasm_regvals
[o
->basereg
];
2008 return evexflags(val
, o
->decoflags
, mask
, byte
);
2011 static enum match_result
find_match(const struct itemplate
**tempp
,
2013 int32_t segment
, int64_t offset
, int bits
)
2015 const struct itemplate
*temp
;
2016 enum match_result m
, merr
;
2017 opflags_t xsizeflags
[MAX_OPERANDS
];
2018 bool opsizemissing
= false;
2019 int8_t broadcast
= instruction
->evex_brerop
;
2022 /* broadcasting uses a different data element size */
2023 for (i
= 0; i
< instruction
->operands
; i
++)
2025 xsizeflags
[i
] = instruction
->oprs
[i
].decoflags
& BRSIZE_MASK
;
2027 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
2029 merr
= MERR_INVALOP
;
2031 for (temp
= nasm_instructions
[instruction
->opcode
];
2032 temp
->opcode
!= I_none
; temp
++) {
2033 m
= matches(temp
, instruction
, bits
);
2034 if (m
== MOK_JUMP
) {
2035 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2039 } else if (m
== MERR_OPSIZEMISSING
&& !itemp_has(temp
, IF_SX
)) {
2041 * Missing operand size and a candidate for fuzzy matching...
2043 for (i
= 0; i
< temp
->operands
; i
++)
2045 xsizeflags
[i
] |= temp
->deco
[i
] & BRSIZE_MASK
;
2047 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
2048 opsizemissing
= true;
2052 if (merr
== MOK_GOOD
)
2056 /* No match, but see if we can get a fuzzy operand size match... */
2060 for (i
= 0; i
< instruction
->operands
; i
++) {
2062 * We ignore extrinsic operand sizes on registers, so we should
2063 * never try to fuzzy-match on them. This also resolves the case
2064 * when we have e.g. "xmmrm128" in two different positions.
2066 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
2069 /* This tests if xsizeflags[i] has more than one bit set */
2070 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
2071 goto done
; /* No luck */
2073 if (i
== broadcast
) {
2074 instruction
->oprs
[i
].decoflags
|= xsizeflags
[i
];
2075 instruction
->oprs
[i
].type
|= (xsizeflags
[i
] == BR_BITS32
?
2078 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
2082 /* Try matching again... */
2083 for (temp
= nasm_instructions
[instruction
->opcode
];
2084 temp
->opcode
!= I_none
; temp
++) {
2085 m
= matches(temp
, instruction
, bits
);
2086 if (m
== MOK_JUMP
) {
2087 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2094 if (merr
== MOK_GOOD
)
2103 static enum match_result
matches(const struct itemplate
*itemp
,
2104 insn
*instruction
, int bits
)
2106 opflags_t size
[MAX_OPERANDS
], asize
;
2107 bool opsizemissing
= false;
2113 if (itemp
->opcode
!= instruction
->opcode
)
2114 return MERR_INVALOP
;
2117 * Count the operands
2119 if (itemp
->operands
!= instruction
->operands
)
2120 return MERR_INVALOP
;
2125 if (!(optimizing
> 0) && itemp_has(itemp
, IF_OPT
))
2126 return MERR_INVALOP
;
2131 switch (instruction
->prefixes
[PPS_VEX
]) {
2133 if (!itemp_has(itemp
, IF_EVEX
))
2134 return MERR_ENCMISMATCH
;
2138 if (!itemp_has(itemp
, IF_VEX
))
2139 return MERR_ENCMISMATCH
;
2146 * Check that no spurious colons or TOs are present
2148 for (i
= 0; i
< itemp
->operands
; i
++)
2149 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2150 return MERR_INVALOP
;
2153 * Process size flags
2155 switch (itemp_smask(itemp
)) {
2156 case IF_GENBIT(IF_SB
):
2159 case IF_GENBIT(IF_SW
):
2162 case IF_GENBIT(IF_SD
):
2165 case IF_GENBIT(IF_SQ
):
2168 case IF_GENBIT(IF_SO
):
2171 case IF_GENBIT(IF_SY
):
2174 case IF_GENBIT(IF_SZ
):
2177 case IF_GENBIT(IF_SIZE
):
2198 if (itemp_armask(itemp
)) {
2199 /* S- flags only apply to a specific operand */
2200 i
= itemp_arg(itemp
);
2201 memset(size
, 0, sizeof size
);
2204 /* S- flags apply to all operands */
2205 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2210 * Check that the operand flags all match up,
2211 * it's a bit tricky so lets be verbose:
2213 * 1) Find out the size of operand. If instruction
2214 * doesn't have one specified -- we're trying to
2215 * guess it either from template (IF_S* flag) or
2218 * 2) If template operand do not match the instruction OR
2219 * template has an operand size specified AND this size differ
2220 * from which instruction has (perhaps we got it from code bits)
2222 * a) Check that only size of instruction and operand is differ
2223 * other characteristics do match
2224 * b) Perhaps it's a register specified in instruction so
2225 * for such a case we just mark that operand as "size
2226 * missing" and this will turn on fuzzy operand size
2227 * logic facility (handled by a caller)
2229 for (i
= 0; i
< itemp
->operands
; i
++) {
2230 opflags_t type
= instruction
->oprs
[i
].type
;
2231 decoflags_t deco
= instruction
->oprs
[i
].decoflags
;
2232 bool is_broadcast
= deco
& BRDCAST_MASK
;
2233 uint8_t brcast_num
= 0;
2234 opflags_t template_opsize
, insn_opsize
;
2236 if (!(type
& SIZE_MASK
))
2239 insn_opsize
= type
& SIZE_MASK
;
2240 if (!is_broadcast
) {
2241 template_opsize
= itemp
->opd
[i
] & SIZE_MASK
;
2243 decoflags_t deco_brsize
= itemp
->deco
[i
] & BRSIZE_MASK
;
2245 * when broadcasting, the element size depends on
2246 * the instruction type. decorator flag should match.
2250 template_opsize
= (deco_brsize
== BR_BITS32
? BITS32
: BITS64
);
2251 /* calculate the proper number : {1to<brcast_num>} */
2252 brcast_num
= (itemp
->opd
[i
] & SIZE_MASK
) / BITS128
*
2253 BITS64
/ template_opsize
* 2;
2255 template_opsize
= 0;
2259 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2260 (deco
& ~itemp
->deco
[i
] & ~BRNUM_MASK
)) {
2261 return MERR_INVALOP
;
2262 } else if (template_opsize
) {
2263 if (template_opsize
!= insn_opsize
) {
2265 return MERR_INVALOP
;
2266 } else if (!is_class(REGISTER
, type
)) {
2268 * Note: we don't honor extrinsic operand sizes for registers,
2269 * so "missing operand size" for a register should be
2270 * considered a wildcard match rather than an error.
2272 opsizemissing
= true;
2274 } else if (is_broadcast
&&
2276 (8U << ((deco
& BRNUM_MASK
) >> BRNUM_SHIFT
)))) {
2278 * broadcasting opsize matches but the number of repeated memory
2279 * element does not match.
2280 * if 64b double precision float is broadcasted to zmm (512b),
2281 * broadcasting decorator must be {1to8}.
2283 return MERR_BRNUMMISMATCH
;
2289 return MERR_OPSIZEMISSING
;
2292 * Check operand sizes
2294 if (itemp_has(itemp
, IF_SM
) || itemp_has(itemp
, IF_SM2
)) {
2295 oprs
= (itemp_has(itemp
, IF_SM2
) ? 2 : itemp
->operands
);
2296 for (i
= 0; i
< oprs
; i
++) {
2297 asize
= itemp
->opd
[i
] & SIZE_MASK
;
2299 for (i
= 0; i
< oprs
; i
++)
2305 oprs
= itemp
->operands
;
2308 for (i
= 0; i
< itemp
->operands
; i
++) {
2309 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2310 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2311 return MERR_OPSIZEMISMATCH
;
2315 * Check template is okay at the set cpu level
2317 if (iflag_cmp_cpu_level(&insns_flags
[itemp
->iflag_idx
], &cpu
) > 0)
2321 * Verify the appropriate long mode flag.
2323 if (itemp_has(itemp
, (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2324 return MERR_BADMODE
;
2327 * If we have a HLE prefix, look for the NOHLE flag
2329 if (itemp_has(itemp
, IF_NOHLE
) &&
2330 (has_prefix(instruction
, PPS_REP
, P_XACQUIRE
) ||
2331 has_prefix(instruction
, PPS_REP
, P_XRELEASE
)))
2335 * Check if special handling needed for Jumps
2337 if ((itemp
->code
[0] & ~1) == 0370)
2341 * Check if BND prefix is allowed.
2342 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
2344 if (!itemp_has(itemp
, IF_BND
) &&
2345 (has_prefix(instruction
, PPS_REP
, P_BND
) ||
2346 has_prefix(instruction
, PPS_REP
, P_NOBND
)))
2348 else if (itemp_has(itemp
, IF_BND
) &&
2349 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ||
2350 has_prefix(instruction
, PPS_REP
, P_REPNZ
)))
2351 return MERR_BADREPNE
;
2357 * Check if ModR/M.mod should/can be 01.
2358 * - EAF_BYTEOFFS is set
2359 * - offset can fit in a byte when EVEX is not used
2360 * - offset can be compressed when EVEX is used
2362 #define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2363 (o >= -128 && o <= 127 && \
2364 seg == NO_SEG && !forw_ref && \
2365 !(input->eaflags & EAF_WORDOFFS) && \
2366 !(ins->rex & REX_EV)) || \
2367 (ins->rex & REX_EV && \
2368 is_disp8n(input, ins, &output->disp8)))
2370 static enum ea_type
process_ea(operand
*input
, ea
*output
, int bits
,
2371 int rfield
, opflags_t rflags
, insn
*ins
)
2373 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2374 int addrbits
= ins
->addr_size
;
2375 int eaflags
= input
->eaflags
;
2377 output
->type
= EA_SCALAR
;
2378 output
->rip
= false;
2381 /* REX flags for the rfield operand */
2382 output
->rex
|= rexflags(rfield
, rflags
, REX_R
| REX_P
| REX_W
| REX_H
);
2383 /* EVEX.R' flag for the REG operand */
2384 ins
->evex_p
[0] |= evexflags(rfield
, 0, EVEX_P0RP
, 0);
2386 if (is_class(REGISTER
, input
->type
)) {
2388 * It's a direct register.
2390 if (!is_register(input
->basereg
))
2393 if (!is_reg_class(REG_EA
, input
->basereg
))
2396 /* broadcasting is not available with a direct register operand. */
2397 if (input
->decoflags
& BRDCAST_MASK
) {
2398 nasm_error(ERR_NONFATAL
, "Broadcasting not allowed from a register");
2402 output
->rex
|= op_rexflags(input
, REX_B
| REX_P
| REX_W
| REX_H
);
2403 ins
->evex_p
[0] |= op_evexflags(input
, EVEX_P0X
, 0);
2404 output
->sib_present
= false; /* no SIB necessary */
2405 output
->bytes
= 0; /* no offset necessary either */
2406 output
->modrm
= GEN_MODRM(3, rfield
, nasm_regvals
[input
->basereg
]);
2409 * It's a memory reference.
2412 /* Embedded rounding or SAE is not available with a mem ref operand. */
2413 if (input
->decoflags
& (ER
| SAE
)) {
2414 nasm_error(ERR_NONFATAL
,
2415 "Embedded rounding is available only with reg-reg op.");
2419 if (input
->basereg
== -1 &&
2420 (input
->indexreg
== -1 || input
->scale
== 0)) {
2422 * It's a pure offset.
2424 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
) &&
2425 input
->segment
== NO_SEG
) {
2426 nasm_error(ERR_WARNING
| ERR_PASS1
, "absolute address can not be RIP-relative");
2427 input
->type
&= ~IP_REL
;
2428 input
->type
|= MEMORY
;
2432 !(IP_REL
& ~input
->type
) && (eaflags
& EAF_MIB
)) {
2433 nasm_error(ERR_NONFATAL
, "RIP-relative addressing is prohibited for mib.");
2437 if (eaflags
& EAF_BYTEOFFS
||
2438 (eaflags
& EAF_WORDOFFS
&&
2439 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16))) {
2440 nasm_error(ERR_WARNING
| ERR_PASS1
, "displacement size ignored on absolute address");
2443 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2444 output
->sib_present
= true;
2445 output
->sib
= GEN_SIB(0, 4, 5);
2447 output
->modrm
= GEN_MODRM(0, rfield
, 4);
2448 output
->rip
= false;
2450 output
->sib_present
= false;
2451 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2452 output
->modrm
= GEN_MODRM(0, rfield
, (addrbits
!= 16 ? 5 : 6));
2453 output
->rip
= bits
== 64;
2457 * It's an indirection.
2459 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2460 int32_t seg
= input
->segment
;
2461 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2462 int t
, it
, bt
; /* register numbers */
2463 opflags_t x
, ix
, bx
; /* register flags */
2466 i
= -1; /* make this easy, at least */
2468 if (is_register(i
)) {
2469 it
= nasm_regvals
[i
];
2470 ix
= nasm_reg_flags
[i
];
2476 if (is_register(b
)) {
2477 bt
= nasm_regvals
[b
];
2478 bx
= nasm_reg_flags
[b
];
2484 /* if either one are a vector register... */
2485 if ((ix
|bx
) & (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
) {
2486 opflags_t sok
= BITS32
| BITS64
;
2487 int32_t o
= input
->offset
;
2488 int mod
, scale
, index
, base
;
2491 * For a vector SIB, one has to be a vector and the other,
2492 * if present, a GPR. The vector must be the index operand.
2494 if (it
== -1 || (bx
& (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
)) {
2500 t
= bt
, bt
= it
, it
= t
;
2501 x
= bx
, bx
= ix
, ix
= x
;
2507 if (!(REG64
& ~bx
) || !(REG32
& ~bx
))
2514 * While we're here, ensure the user didn't specify
2517 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2520 if (addrbits
== 16 ||
2521 (addrbits
== 32 && !(sok
& BITS32
)) ||
2522 (addrbits
== 64 && !(sok
& BITS64
)))
2525 output
->type
= ((ix
& ZMMREG
& ~REG_EA
) ? EA_ZMMVSIB
2526 : ((ix
& YMMREG
& ~REG_EA
)
2527 ? EA_YMMVSIB
: EA_XMMVSIB
));
2529 output
->rex
|= rexflags(it
, ix
, REX_X
);
2530 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2531 ins
->evex_p
[2] |= evexflags(it
, 0, EVEX_P2VP
, 2);
2533 index
= it
& 7; /* it is known to be != -1 */
2548 default: /* then what the smeg is it? */
2549 goto err
; /* panic */
2557 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2558 seg
== NO_SEG
&& !forw_ref
&&
2559 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2561 else if (IS_MOD_01())
2567 output
->sib_present
= true;
2568 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2569 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2570 output
->sib
= GEN_SIB(scale
, index
, base
);
2571 } else if ((ix
|bx
) & (BITS32
|BITS64
)) {
2573 * it must be a 32/64-bit memory reference. Firstly we have
2574 * to check that all registers involved are type E/Rxx.
2576 opflags_t sok
= BITS32
| BITS64
;
2577 int32_t o
= input
->offset
;
2580 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2588 goto err
; /* Invalid register */
2589 if (~sok
& bx
& SIZE_MASK
)
2590 goto err
; /* Invalid size */
2595 * While we're here, ensure the user didn't specify
2598 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2601 if (addrbits
== 16 ||
2602 (addrbits
== 32 && !(sok
& BITS32
)) ||
2603 (addrbits
== 64 && !(sok
& BITS64
)))
2606 /* now reorganize base/index */
2607 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2608 ((hb
== b
&& ht
== EAH_NOTBASE
) ||
2609 (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2610 /* swap if hints say so */
2611 t
= bt
, bt
= it
, it
= t
;
2612 x
= bx
, bx
= ix
, ix
= x
;
2615 if (bt
== -1 && s
== 1 && !(hb
== i
&& ht
== EAH_NOTBASE
)) {
2616 /* make single reg base, unless hint */
2617 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2619 if (eaflags
& EAF_MIB
) {
2620 /* only for mib operands */
2621 if (it
== -1 && (hb
== b
&& ht
== EAH_NOTBASE
)) {
2623 * make a single reg index [reg*1].
2624 * gas uses this form for an explicit index register.
2626 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2628 if ((ht
== EAH_SUMMED
) && bt
== -1) {
2629 /* separate once summed index into [base, index] */
2630 bt
= it
, bx
= ix
, s
--;
2633 if (((s
== 2 && it
!= REG_NUM_ESP
&&
2634 (!(eaflags
& EAF_TIMESTWO
) || (ht
== EAH_SUMMED
))) ||
2635 s
== 3 || s
== 5 || s
== 9) && bt
== -1) {
2636 /* convert 3*EAX to EAX+2*EAX */
2637 bt
= it
, bx
= ix
, s
--;
2639 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
&&
2640 (eaflags
& EAF_TIMESTWO
) &&
2641 (hb
== b
&& ht
== EAH_NOTBASE
)) {
2643 * convert [NOSPLIT EAX*1]
2644 * to sib format with 0x0 displacement - [EAX*1+0].
2646 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2649 if (s
== 1 && it
== REG_NUM_ESP
) {
2650 /* swap ESP into base if scale is 1 */
2651 t
= it
, it
= bt
, bt
= t
;
2652 x
= ix
, ix
= bx
, bx
= x
;
2654 if (it
== REG_NUM_ESP
||
2655 (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2656 goto err
; /* wrong, for various reasons */
2658 output
->rex
|= rexflags(it
, ix
, REX_X
);
2659 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2661 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2670 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2671 seg
== NO_SEG
&& !forw_ref
&&
2672 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2674 else if (IS_MOD_01())
2680 output
->sib_present
= false;
2681 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2682 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2685 int mod
, scale
, index
, base
;
2705 default: /* then what the smeg is it? */
2706 goto err
; /* panic */
2714 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2715 seg
== NO_SEG
&& !forw_ref
&&
2716 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2718 else if (IS_MOD_01())
2724 output
->sib_present
= true;
2725 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2726 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2727 output
->sib
= GEN_SIB(scale
, index
, base
);
2729 } else { /* it's 16-bit */
2731 int16_t o
= input
->offset
;
2733 /* check for 64-bit long mode */
2737 /* check all registers are BX, BP, SI or DI */
2738 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
&& b
!= R_DI
) ||
2739 (i
!= -1 && i
!= R_BP
&& i
!= R_BX
&& i
!= R_SI
&& i
!= R_DI
))
2742 /* ensure the user didn't specify DWORD/QWORD */
2743 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2746 if (s
!= 1 && i
!= -1)
2747 goto err
; /* no can do, in 16-bit EA */
2748 if (b
== -1 && i
!= -1) {
2753 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2758 /* have BX/BP as base, SI/DI index */
2760 goto err
; /* shouldn't ever happen, in theory */
2761 if (i
!= -1 && b
!= -1 &&
2762 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2763 goto err
; /* invalid combinations */
2764 if (b
== -1) /* pure offset: handled above */
2765 goto err
; /* so if it gets to here, panic! */
2769 switch (i
* 256 + b
) {
2770 case R_SI
* 256 + R_BX
:
2773 case R_DI
* 256 + R_BX
:
2776 case R_SI
* 256 + R_BP
:
2779 case R_DI
* 256 + R_BP
:
2797 if (rm
== -1) /* can't happen, in theory */
2798 goto err
; /* so panic if it does */
2800 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2801 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2803 else if (IS_MOD_01())
2808 output
->sib_present
= false; /* no SIB - it's 16-bit */
2809 output
->bytes
= mod
; /* bytes of offset needed */
2810 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2815 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2816 return output
->type
;
2819 return output
->type
= EA_INVALID
;
2822 static void add_asp(insn
*ins
, int addrbits
)
2827 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2829 switch (ins
->prefixes
[PPS_ASIZE
]) {
2840 valid
&= (addrbits
== 32) ? 16 : 32;
2846 for (j
= 0; j
< ins
->operands
; j
++) {
2847 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
2850 /* Verify as Register */
2851 if (!is_register(ins
->oprs
[j
].indexreg
))
2854 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2856 /* Verify as Register */
2857 if (!is_register(ins
->oprs
[j
].basereg
))
2860 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2862 if (ins
->oprs
[j
].scale
== 0)
2866 int ds
= ins
->oprs
[j
].disp_size
;
2867 if ((addrbits
!= 64 && ds
> 8) ||
2868 (addrbits
== 64 && ds
== 16))
2888 if (valid
& addrbits
) {
2889 ins
->addr_size
= addrbits
;
2890 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2891 /* Add an address size prefix */
2892 ins
->prefixes
[PPS_ASIZE
] = (addrbits
== 32) ? P_A16
: P_A32
;;
2893 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2896 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2897 ins
->addr_size
= addrbits
; /* Error recovery */
2900 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2902 for (j
= 0; j
< ins
->operands
; j
++) {
2903 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2904 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
) != ins
->addr_size
) {
2906 * mem_offs sizes must match the address size; if not,
2907 * strip the MEM_OFFS bit and match only EA instructions
2909 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);