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"
176 #include <inttypes.h>
180 #include "assemble.h"
187 * Matching errors. These should be sorted so that more specific
188 * errors come later in the sequence.
201 * Matching success; the conditional ones first
203 MOK_JUMP
, /* Matching OK but needs jmp_match() */
204 MOK_GOOD
/* Matching unconditionally OK */
208 enum ea_type type
; /* what kind of EA is this? */
209 int sib_present
; /* is a SIB byte necessary? */
210 int bytes
; /* # of bytes of offset needed */
211 int size
; /* lazy - this is sib+bytes+1 */
212 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
213 int8_t disp8
; /* compressed displacement for EVEX */
216 #define GEN_SIB(scale, index, base) \
217 (((scale) << 6) | ((index) << 3) | ((base)))
219 #define GEN_MODRM(mod, reg, rm) \
220 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
222 static iflag_t cpu
; /* cpu level received from nasm.c */
223 static efunc errfunc
;
224 static struct ofmt
*outfmt
;
225 static ListGen
*list
;
227 static int64_t calcsize(int32_t, int64_t, int, insn
*,
228 const struct itemplate
*);
229 static void gencode(int32_t segment
, int64_t offset
, int bits
,
230 insn
* ins
, const struct itemplate
*temp
,
232 static enum match_result
find_match(const struct itemplate
**tempp
,
234 int32_t segment
, int64_t offset
, int bits
);
235 static enum match_result
matches(const struct itemplate
*, insn
*, int bits
);
236 static opflags_t
regflag(const operand
*);
237 static int32_t regval(const operand
*);
238 static int rexflags(int, opflags_t
, int);
239 static int op_rexflags(const operand
*, int);
240 static int op_evexflags(const operand
*, int, uint8_t);
241 static void add_asp(insn
*, int);
243 static enum ea_type
process_ea(operand
*, ea
*, int, int, opflags_t
, insn
*);
245 static int has_prefix(insn
* ins
, enum prefix_pos pos
, int prefix
)
247 return ins
->prefixes
[pos
] == prefix
;
250 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
252 if (ins
->prefixes
[pos
])
253 errfunc(ERR_NONFATAL
, "invalid %s prefix",
254 prefix_name(ins
->prefixes
[pos
]));
257 static const char *size_name(int size
)
281 static void warn_overflow(int pass
, int size
)
283 errfunc(ERR_WARNING
| pass
| ERR_WARN_NOV
,
284 "%s data exceeds bounds", size_name(size
));
287 static void warn_overflow_const(int64_t data
, int size
)
289 if (overflow_general(data
, size
))
290 warn_overflow(ERR_PASS1
, size
);
293 static void warn_overflow_opd(const struct operand
*o
, int size
)
295 if (o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
296 if (overflow_general(o
->offset
, size
))
297 warn_overflow(ERR_PASS2
, size
);
302 * This routine wrappers the real output format's output routine,
303 * in order to pass a copy of the data off to the listing file
304 * generator at the same time.
306 static void out(int64_t offset
, int32_t segto
, const void *data
,
307 enum out_type type
, uint64_t size
,
308 int32_t segment
, int32_t wrt
)
310 static int32_t lineno
= 0; /* static!!! */
311 static char *lnfname
= NULL
;
314 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
316 * This is a non-relocated address, and we're going to
317 * convert it into RAWDATA format.
322 errfunc(ERR_PANIC
, "OUT_ADDRESS with size > 8");
326 WRITEADDR(q
, *(int64_t *)data
, size
);
331 list
->output(offset
, data
, type
, size
);
334 * this call to src_get determines when we call the
335 * debug-format-specific "linenum" function
336 * it updates lineno and lnfname to the current values
337 * returning 0 if "same as last time", -2 if lnfname
338 * changed, and the amount by which lineno changed,
339 * if it did. thus, these variables must be static
342 if (src_get(&lineno
, &lnfname
))
343 outfmt
->current_dfmt
->linenum(lnfname
, lineno
, segto
);
345 outfmt
->output(segto
, data
, type
, size
, segment
, wrt
);
348 static void out_imm8(int64_t offset
, int32_t segment
, struct operand
*opx
)
350 if (opx
->segment
!= NO_SEG
) {
351 uint64_t data
= opx
->offset
;
352 out(offset
, segment
, &data
, OUT_ADDRESS
, 1, opx
->segment
, opx
->wrt
);
354 uint8_t byte
= opx
->offset
;
355 out(offset
, segment
, &byte
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
359 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
360 insn
* ins
, const struct itemplate
*temp
)
363 const uint8_t *code
= temp
->code
;
367 if (((c
& ~1) != 0370) || (ins
->oprs
[0].type
& STRICT
))
371 if (optimizing
< 0 && c
== 0371)
374 isize
= calcsize(segment
, offset
, bits
, ins
, temp
);
376 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
377 /* Be optimistic in pass 1 */
380 if (ins
->oprs
[0].segment
!= segment
)
383 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
384 is_byte
= (isize
>= -128 && isize
<= 127); /* is it byte size? */
386 if (is_byte
&& c
== 0371 && ins
->prefixes
[PPS_REP
] == P_BND
) {
387 /* jmp short (opcode eb) cannot be used with bnd prefix. */
388 ins
->prefixes
[PPS_REP
] = P_none
;
389 errfunc(ERR_WARNING
| ERR_WARN_BND
| ERR_PASS2
,
390 "jmp short does not init bnd regs - bnd prefix dropped.");
396 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, iflag_t cp
,
397 insn
* instruction
, struct ofmt
*output
, efunc error
,
400 const struct itemplate
*temp
;
405 int64_t start
= offset
;
406 int64_t wsize
; /* size for DB etc. */
408 errfunc
= error
; /* to pass to other functions */
410 outfmt
= output
; /* likewise */
411 list
= listgen
; /* and again */
413 wsize
= idata_bytes(instruction
->opcode
);
419 int32_t t
= instruction
->times
;
422 "instruction->times < 0 (%ld) in assemble()", t
);
424 while (t
--) { /* repeat TIMES times */
425 list_for_each(e
, instruction
->eops
) {
426 if (e
->type
== EOT_DB_NUMBER
) {
428 errfunc(ERR_NONFATAL
,
429 "integer supplied to a DT, DO or DY"
432 out(offset
, segment
, &e
->offset
,
433 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
436 } else if (e
->type
== EOT_DB_STRING
||
437 e
->type
== EOT_DB_STRING_FREE
) {
440 out(offset
, segment
, e
->stringval
,
441 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
442 align
= e
->stringlen
% wsize
;
445 align
= wsize
- align
;
446 out(offset
, segment
, zero_buffer
,
447 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
449 offset
+= e
->stringlen
+ align
;
452 if (t
> 0 && t
== instruction
->times
- 1) {
454 * Dummy call to list->output to give the offset to the
457 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
458 list
->uplevel(LIST_TIMES
);
461 if (instruction
->times
> 1)
462 list
->downlevel(LIST_TIMES
);
463 return offset
- start
;
466 if (instruction
->opcode
== I_INCBIN
) {
467 const char *fname
= instruction
->eops
->stringval
;
470 fp
= fopen(fname
, "rb");
472 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
474 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
475 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
479 static char buf
[4096];
480 size_t t
= instruction
->times
;
485 if (instruction
->eops
->next
) {
486 base
= instruction
->eops
->next
->offset
;
488 if (instruction
->eops
->next
->next
&&
489 len
> (size_t)instruction
->eops
->next
->next
->offset
)
490 len
= (size_t)instruction
->eops
->next
->next
->offset
;
493 * Dummy call to list->output to give the offset to the
496 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
497 list
->uplevel(LIST_INCBIN
);
501 fseek(fp
, base
, SEEK_SET
);
505 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
508 * This shouldn't happen unless the file
509 * actually changes while we are reading
513 "`incbin': unexpected EOF while"
514 " reading file `%s'", fname
);
515 t
= 0; /* Try to exit cleanly */
518 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
523 list
->downlevel(LIST_INCBIN
);
524 if (instruction
->times
> 1) {
526 * Dummy call to list->output to give the offset to the
529 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
530 list
->uplevel(LIST_TIMES
);
531 list
->downlevel(LIST_TIMES
);
534 return instruction
->times
* len
;
536 return 0; /* if we're here, there's an error */
539 /* Check to see if we need an address-size prefix */
540 add_asp(instruction
, bits
);
542 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
546 int64_t insn_size
= calcsize(segment
, offset
, bits
, instruction
, temp
);
547 itimes
= instruction
->times
;
548 if (insn_size
< 0) /* shouldn't be, on pass two */
549 error(ERR_PANIC
, "errors made it through from pass one");
552 for (j
= 0; j
< MAXPREFIX
; j
++) {
554 switch (instruction
->prefixes
[j
]) {
575 error(ERR_WARNING
| ERR_PASS2
,
576 "cs segment base generated, but will be ignored in 64-bit mode");
582 error(ERR_WARNING
| ERR_PASS2
,
583 "ds segment base generated, but will be ignored in 64-bit mode");
589 error(ERR_WARNING
| ERR_PASS2
,
590 "es segment base generated, but will be ignored in 64-bit mode");
602 error(ERR_WARNING
| ERR_PASS2
,
603 "ss segment base generated, but will be ignored in 64-bit mode");
610 "segr6 and segr7 cannot be used as prefixes");
615 "16-bit addressing is not supported "
617 } else if (bits
!= 16)
627 "64-bit addressing is only supported "
655 error(ERR_PANIC
, "invalid instruction prefix");
658 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
663 insn_end
= offset
+ insn_size
;
664 gencode(segment
, offset
, bits
, instruction
,
667 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
669 * Dummy call to list->output to give the offset to the
672 list
->output(offset
, NULL
, OUT_RAWDATA
, 0);
673 list
->uplevel(LIST_TIMES
);
676 if (instruction
->times
> 1)
677 list
->downlevel(LIST_TIMES
);
678 return offset
- start
;
682 case MERR_OPSIZEMISSING
:
683 error(ERR_NONFATAL
, "operation size not specified");
685 case MERR_OPSIZEMISMATCH
:
686 error(ERR_NONFATAL
, "mismatch in operand sizes");
688 case MERR_BRNUMMISMATCH
:
690 "mismatch in the number of broadcasting elements");
693 error(ERR_NONFATAL
, "no instruction for this cpu level");
696 error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
699 case MERR_ENCMISMATCH
:
700 error(ERR_NONFATAL
, "specific encoding scheme not available");
703 error(ERR_NONFATAL
, "bnd prefix is not allowed");
706 error(ERR_NONFATAL
, "%s prefix is not allowed",
707 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ?
712 "invalid combination of opcode and operands");
719 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, iflag_t cp
,
720 insn
* instruction
, efunc error
)
722 const struct itemplate
*temp
;
725 errfunc
= error
; /* to pass to other functions */
728 if (instruction
->opcode
== I_none
)
731 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
732 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
733 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
734 instruction
->opcode
== I_DY
) {
736 int32_t isize
, osize
, wsize
;
739 wsize
= idata_bytes(instruction
->opcode
);
741 list_for_each(e
, instruction
->eops
) {
745 if (e
->type
== EOT_DB_NUMBER
) {
747 warn_overflow_const(e
->offset
, wsize
);
748 } else if (e
->type
== EOT_DB_STRING
||
749 e
->type
== EOT_DB_STRING_FREE
)
750 osize
= e
->stringlen
;
752 align
= (-osize
) % wsize
;
755 isize
+= osize
+ align
;
757 return isize
* instruction
->times
;
760 if (instruction
->opcode
== I_INCBIN
) {
761 const char *fname
= instruction
->eops
->stringval
;
766 fp
= fopen(fname
, "rb");
768 error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
770 else if (fseek(fp
, 0L, SEEK_END
) < 0)
771 error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
775 if (instruction
->eops
->next
) {
776 len
-= instruction
->eops
->next
->offset
;
777 if (instruction
->eops
->next
->next
&&
778 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
779 len
= (size_t)instruction
->eops
->next
->next
->offset
;
782 val
= instruction
->times
* len
;
789 /* Check to see if we need an address-size prefix */
790 add_asp(instruction
, bits
);
792 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
794 /* we've matched an instruction. */
798 isize
= calcsize(segment
, offset
, bits
, instruction
, temp
);
801 for (j
= 0; j
< MAXPREFIX
; j
++) {
802 switch (instruction
->prefixes
[j
]) {
832 return isize
* instruction
->times
;
834 return -1; /* didn't match any instruction */
838 static void bad_hle_warn(const insn
* ins
, uint8_t hleok
)
840 enum prefixes rep_pfx
= ins
->prefixes
[PPS_REP
];
841 enum whatwarn
{ w_none
, w_lock
, w_inval
} ww
;
842 static const enum whatwarn warn
[2][4] =
844 { w_inval
, w_inval
, w_none
, w_lock
}, /* XACQUIRE */
845 { w_inval
, w_none
, w_none
, w_lock
}, /* XRELEASE */
849 n
= (unsigned int)rep_pfx
- P_XACQUIRE
;
851 return; /* Not XACQUIRE/XRELEASE */
854 if (!is_class(MEMORY
, ins
->oprs
[0].type
))
855 ww
= w_inval
; /* HLE requires operand 0 to be memory */
862 if (ins
->prefixes
[PPS_LOCK
] != P_LOCK
) {
863 errfunc(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
864 "%s with this instruction requires lock",
865 prefix_name(rep_pfx
));
870 errfunc(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
871 "%s invalid with this instruction",
872 prefix_name(rep_pfx
));
877 /* Common construct */
878 #define case3(x) case (x): case (x)+1: case (x)+2
879 #define case4(x) case3(x): case (x)+3
881 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
882 insn
* ins
, const struct itemplate
*temp
)
884 const uint8_t *codes
= temp
->code
;
893 bool lockcheck
= true;
894 enum reg_enum mib_index
= R_none
; /* For a separate index MIB reg form */
896 ins
->rex
= 0; /* Ensure REX is reset */
897 eat
= EA_SCALAR
; /* Expect a scalar EA */
898 memset(ins
->evex_p
, 0, 3); /* Ensure EVEX is reset */
900 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
903 (void)segment
; /* Don't warn that this parameter is unused */
904 (void)offset
; /* Don't warn that this parameter is unused */
908 op1
= (c
& 3) + ((opex
& 1) << 2);
909 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
910 opx
= &ins
->oprs
[op1
];
911 opex
= 0; /* For the next iteration */
915 codes
+= c
, length
+= c
;
924 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
929 /* this is an index reg of MIB operand */
930 mib_index
= opx
->basereg
;
943 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
944 length
+= (opx
->type
& BITS16
) ? 2 : 4;
946 length
+= (bits
== 16) ? 2 : 4;
954 length
+= ins
->addr_size
>> 3;
962 length
+= 8; /* MOV reg64/imm */
970 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
971 length
+= (opx
->type
& BITS16
) ? 2 : 4;
973 length
+= (bits
== 16) ? 2 : 4;
996 ins
->vexreg
= regval(opx
);
997 ins
->evex_p
[2] |= op_evexflags(opx
, EVEX_P2VP
, 2); /* High-16 NDS */
998 ins
->vex_cm
= *codes
++;
999 ins
->vex_wlp
= *codes
++;
1000 ins
->evex_tuple
= (*codes
++ - 0300);
1006 ins
->vex_cm
= *codes
++;
1007 ins
->vex_wlp
= *codes
++;
1008 ins
->evex_tuple
= (*codes
++ - 0300);
1017 ins
->vexreg
= regval(opx
);
1018 ins
->vex_cm
= *codes
++;
1019 ins
->vex_wlp
= *codes
++;
1025 ins
->vex_cm
= *codes
++;
1026 ins
->vex_wlp
= *codes
++;
1043 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
1047 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1054 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1055 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1064 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1068 errfunc(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1070 ins
->prefixes
[PPS_OSIZE
] = P_O16
;
1076 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1080 errfunc(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1082 ins
->prefixes
[PPS_OSIZE
] = P_O32
;
1124 if (!ins
->prefixes
[PPS_REP
])
1125 ins
->prefixes
[PPS_REP
] = P_REP
;
1129 if (!ins
->prefixes
[PPS_REP
])
1130 ins
->prefixes
[PPS_REP
] = P_REPNE
;
1134 if (ins
->oprs
[0].segment
!= NO_SEG
)
1135 errfunc(ERR_NONFATAL
, "attempt to reserve non-constant"
1136 " quantity of BSS space");
1138 length
+= ins
->oprs
[0].offset
;
1142 if (!ins
->prefixes
[PPS_WAIT
])
1143 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1198 struct operand
*opy
= &ins
->oprs
[op2
];
1199 struct operand
*op_er_sae
;
1201 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1204 /* pick rfield from operand b (opx) */
1205 rflags
= regflag(opx
);
1206 rfield
= nasm_regvals
[opx
->basereg
];
1212 /* EVEX.b1 : evex_brerop contains the operand position */
1213 op_er_sae
= (ins
->evex_brerop
>= 0 ?
1214 &ins
->oprs
[ins
->evex_brerop
] : NULL
);
1216 if (op_er_sae
&& (op_er_sae
->decoflags
& (ER
| SAE
))) {
1218 ins
->evex_p
[2] |= EVEX_P2B
;
1219 if (op_er_sae
->decoflags
& ER
) {
1220 /* set EVEX.RC (rounding control) */
1221 ins
->evex_p
[2] |= ((ins
->evex_rm
- BRC_RN
) << 5)
1225 /* set EVEX.L'L (vector length) */
1226 ins
->evex_p
[2] |= ((ins
->vex_wlp
<< (5 - 2)) & EVEX_P2LL
);
1227 ins
->evex_p
[1] |= ((ins
->vex_wlp
<< (7 - 4)) & EVEX_P1W
);
1228 if (opy
->decoflags
& BRDCAST_MASK
) {
1230 ins
->evex_p
[2] |= EVEX_P2B
;
1234 if (itemp_has(temp
, IF_MIB
)) {
1235 opy
->eaflags
|= EAF_MIB
;
1237 * if a separate form of MIB (ICC style) is used,
1238 * the index reg info is merged into mem operand
1240 if (mib_index
!= R_none
) {
1241 opy
->indexreg
= mib_index
;
1243 opy
->hintbase
= mib_index
;
1244 opy
->hinttype
= EAH_NOTBASE
;
1248 if (process_ea(opy
, &ea_data
, bits
,
1249 rfield
, rflags
, ins
) != eat
) {
1250 errfunc(ERR_NONFATAL
, "invalid effective address");
1253 ins
->rex
|= ea_data
.rex
;
1254 length
+= ea_data
.size
;
1260 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1261 ": instruction code \\%o (0x%02X) given", c
, c
);
1266 ins
->rex
&= rex_mask
;
1268 if (ins
->rex
& REX_NH
) {
1269 if (ins
->rex
& REX_H
) {
1270 errfunc(ERR_NONFATAL
, "instruction cannot use high registers");
1273 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1276 switch (ins
->prefixes
[PPS_VEX
]) {
1278 if (!(ins
->rex
& REX_EV
))
1283 if (!(ins
->rex
& REX_V
))
1290 if (ins
->rex
& (REX_V
| REX_EV
)) {
1291 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1293 if (ins
->rex
& REX_H
) {
1294 errfunc(ERR_NONFATAL
, "cannot use high register in AVX instruction");
1297 switch (ins
->vex_wlp
& 060) {
1311 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->vexreg
> 7)) {
1312 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1314 } else if (!(ins
->rex
& REX_EV
) &&
1315 ((ins
->vexreg
> 15) || (ins
->evex_p
[0] & 0xf0))) {
1316 errfunc(ERR_NONFATAL
, "invalid high-16 register in non-AVX-512");
1319 if (ins
->rex
& REX_EV
)
1321 else if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1322 ins
->prefixes
[PPS_VEX
] == P_VEX3
)
1326 } else if (ins
->rex
& REX_REAL
) {
1327 if (ins
->rex
& REX_H
) {
1328 errfunc(ERR_NONFATAL
, "cannot use high register in rex instruction");
1330 } else if (bits
== 64) {
1332 } else if ((ins
->rex
& REX_L
) &&
1333 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1334 iflag_ffs(&cpu
) >= IF_X86_64
) {
1336 assert_no_prefix(ins
, PPS_LOCK
);
1337 lockcheck
= false; /* Already errored, no need for warning */
1340 errfunc(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1345 if (has_prefix(ins
, PPS_LOCK
, P_LOCK
) && lockcheck
&&
1346 (!itemp_has(temp
,IF_LOCK
) || !is_class(MEMORY
, ins
->oprs
[0].type
))) {
1347 errfunc(ERR_WARNING
| ERR_WARN_LOCK
| ERR_PASS2
,
1348 "instruction is not lockable");
1351 bad_hle_warn(ins
, hleok
);
1354 * when BND prefix is set by DEFAULT directive,
1355 * BND prefix is added to every appropriate instruction line
1356 * unless it is overridden by NOBND prefix.
1359 (itemp_has(temp
, IF_BND
) && !has_prefix(ins
, PPS_REP
, P_NOBND
)))
1360 ins
->prefixes
[PPS_REP
] = P_BND
;
1365 static inline unsigned int emit_rex(insn
*ins
, int32_t segment
, int64_t offset
, int bits
)
1368 if ((ins
->rex
& REX_REAL
) &&
1369 !(ins
->rex
& (REX_V
| REX_EV
)) &&
1371 int rex
= (ins
->rex
& REX_REAL
) | REX_P
;
1372 out(offset
, segment
, &rex
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1373 ins
->rex_done
= true;
1381 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1382 insn
* ins
, const struct itemplate
*temp
,
1390 struct operand
*opx
;
1391 const uint8_t *codes
= temp
->code
;
1393 enum ea_type eat
= EA_SCALAR
;
1395 ins
->rex_done
= false;
1399 op1
= (c
& 3) + ((opex
& 1) << 2);
1400 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1401 opx
= &ins
->oprs
[op1
];
1402 opex
= 0; /* For the next iteration */
1409 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1410 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1422 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1423 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1424 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1432 if (opx
->offset
< -256 || opx
->offset
> 255) {
1433 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1434 "byte value exceeds bounds");
1436 out_imm8(offset
, segment
, opx
);
1441 if (opx
->offset
< 0 || opx
->offset
> 255)
1442 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1443 "unsigned byte value exceeds bounds");
1444 out_imm8(offset
, segment
, opx
);
1449 warn_overflow_opd(opx
, 2);
1451 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1452 opx
->segment
, opx
->wrt
);
1457 if (opx
->type
& (BITS16
| BITS32
))
1458 size
= (opx
->type
& BITS16
) ? 2 : 4;
1460 size
= (bits
== 16) ? 2 : 4;
1461 warn_overflow_opd(opx
, size
);
1463 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1464 opx
->segment
, opx
->wrt
);
1469 warn_overflow_opd(opx
, 4);
1471 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1472 opx
->segment
, opx
->wrt
);
1478 size
= ins
->addr_size
>> 3;
1479 warn_overflow_opd(opx
, size
);
1480 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1481 opx
->segment
, opx
->wrt
);
1486 if (opx
->segment
!= segment
) {
1488 out(offset
, segment
, &data
,
1489 OUT_REL1ADR
, insn_end
- offset
,
1490 opx
->segment
, opx
->wrt
);
1492 data
= opx
->offset
- insn_end
;
1493 if (data
> 127 || data
< -128)
1494 errfunc(ERR_NONFATAL
, "short jump is out of range");
1495 out(offset
, segment
, &data
,
1496 OUT_ADDRESS
, 1, NO_SEG
, NO_SEG
);
1502 data
= (int64_t)opx
->offset
;
1503 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1504 opx
->segment
, opx
->wrt
);
1509 if (opx
->segment
!= segment
) {
1511 out(offset
, segment
, &data
,
1512 OUT_REL2ADR
, insn_end
- offset
,
1513 opx
->segment
, opx
->wrt
);
1515 data
= opx
->offset
- insn_end
;
1516 out(offset
, segment
, &data
,
1517 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1523 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1524 size
= (opx
->type
& BITS16
) ? 2 : 4;
1526 size
= (bits
== 16) ? 2 : 4;
1527 if (opx
->segment
!= segment
) {
1529 out(offset
, segment
, &data
,
1530 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1531 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1533 data
= opx
->offset
- insn_end
;
1534 out(offset
, segment
, &data
,
1535 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1541 if (opx
->segment
!= segment
) {
1543 out(offset
, segment
, &data
,
1544 OUT_REL4ADR
, insn_end
- offset
,
1545 opx
->segment
, opx
->wrt
);
1547 data
= opx
->offset
- insn_end
;
1548 out(offset
, segment
, &data
,
1549 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1555 if (opx
->segment
== NO_SEG
)
1556 errfunc(ERR_NONFATAL
, "value referenced by FAR is not"
1559 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1560 outfmt
->segbase(1 + opx
->segment
),
1567 opx
= &ins
->oprs
[c
>> 3];
1568 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1569 opx
= &ins
->oprs
[c
& 7];
1570 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1571 errfunc(ERR_NONFATAL
,
1572 "non-absolute expression not permitted as argument %d",
1575 if (opx
->offset
& ~15) {
1576 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1577 "four-bit argument exceeds bounds");
1579 bytes
[0] |= opx
->offset
& 15;
1581 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1587 opx
= &ins
->oprs
[c
>> 4];
1588 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1590 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1595 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1596 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1602 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1603 (int32_t)data
!= (int64_t)data
) {
1604 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1605 "signed dword immediate exceeds bounds");
1607 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1608 opx
->segment
, opx
->wrt
);
1615 ins
->evex_p
[2] |= op_evexflags(&ins
->oprs
[0],
1616 EVEX_P2Z
| EVEX_P2AAA
, 2);
1617 ins
->evex_p
[2] ^= EVEX_P2VP
; /* 1's complement */
1619 /* EVEX.X can be set by either REX or EVEX for different reasons */
1620 bytes
[1] = ((((ins
->rex
& 7) << 5) |
1621 (ins
->evex_p
[0] & (EVEX_P0X
| EVEX_P0RP
))) ^ 0xf0) |
1623 bytes
[2] = ((ins
->rex
& REX_W
) << (7 - 3)) |
1624 ((~ins
->vexreg
& 15) << 3) |
1625 (1 << 2) | (ins
->vex_wlp
& 3);
1626 bytes
[3] = ins
->evex_p
[2];
1627 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 4, NO_SEG
, NO_SEG
);
1634 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1635 ins
->prefixes
[PPS_VEX
] == P_VEX3
) {
1636 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1637 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1638 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1639 ((~ins
->vexreg
& 15)<< 3) | (ins
->vex_wlp
& 07);
1640 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1644 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1645 ((~ins
->vexreg
& 15) << 3) | (ins
->vex_wlp
& 07);
1646 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1661 if (ins
->rex
& REX_W
)
1663 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1665 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1670 um
= (uint64_t)2 << (s
-1);
1673 if (uv
> 127 && uv
< (uint64_t)-128 &&
1674 (uv
< um
-128 || uv
> um
-1)) {
1675 /* If this wasn't explicitly byte-sized, warn as though we
1676 * had fallen through to the imm16/32/64 case.
1678 errfunc(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1679 "%s value exceeds bounds",
1680 (opx
->type
& BITS8
) ? "signed byte" :
1685 if (opx
->segment
!= NO_SEG
) {
1687 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1688 opx
->segment
, opx
->wrt
);
1691 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1702 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1704 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1711 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1713 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1748 *bytes
= *codes
++ ^ get_cond_opcode(ins
->condition
);
1749 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1758 *bytes
= c
- 0332 + 0xF2;
1759 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1764 if (ins
->rex
& REX_R
) {
1766 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1769 ins
->rex
&= ~(REX_L
|REX_R
);
1780 if (ins
->oprs
[0].segment
!= NO_SEG
)
1781 errfunc(ERR_PANIC
, "non-constant BSS size in pass two");
1783 int64_t size
= ins
->oprs
[0].offset
;
1785 out(offset
, segment
, NULL
,
1786 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1799 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1809 *bytes
= c
- 0366 + 0x66;
1810 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1818 *bytes
= bits
== 16 ? 3 : 5;
1819 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1853 struct operand
*opy
= &ins
->oprs
[op2
];
1856 /* pick rfield from operand b (opx) */
1857 rflags
= regflag(opx
);
1858 rfield
= nasm_regvals
[opx
->basereg
];
1860 /* rfield is constant */
1865 if (process_ea(opy
, &ea_data
, bits
,
1866 rfield
, rflags
, ins
) != eat
)
1867 errfunc(ERR_NONFATAL
, "invalid effective address");
1870 *p
++ = ea_data
.modrm
;
1871 if (ea_data
.sib_present
)
1875 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1878 * Make sure the address gets the right offset in case
1879 * the line breaks in the .lst file (BR 1197827)
1884 switch (ea_data
.bytes
) {
1891 /* use compressed displacement, if available */
1892 data
= ea_data
.disp8
? ea_data
.disp8
: opy
->offset
;
1895 if (opy
->segment
== segment
) {
1897 if (overflow_signed(data
, ea_data
.bytes
))
1898 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1899 out(offset
, segment
, &data
, OUT_ADDRESS
,
1900 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1902 /* overflow check in output/linker? */
1903 out(offset
, segment
, &data
, OUT_REL4ADR
,
1904 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1907 if (overflow_general(data
, ins
->addr_size
>> 3) ||
1908 signed_bits(data
, ins
->addr_size
) !=
1909 signed_bits(data
, ea_data
.bytes
* 8))
1910 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1912 out(offset
, segment
, &data
, OUT_ADDRESS
,
1913 ea_data
.bytes
, opy
->segment
, opy
->wrt
);
1919 "Invalid amount of bytes (%d) for offset?!",
1928 errfunc(ERR_PANIC
, "internal instruction table corrupt"
1929 ": instruction code \\%o (0x%02X) given", c
, c
);
1935 static opflags_t
regflag(const operand
* o
)
1937 if (!is_register(o
->basereg
))
1938 errfunc(ERR_PANIC
, "invalid operand passed to regflag()");
1939 return nasm_reg_flags
[o
->basereg
];
1942 static int32_t regval(const operand
* o
)
1944 if (!is_register(o
->basereg
))
1945 errfunc(ERR_PANIC
, "invalid operand passed to regval()");
1946 return nasm_regvals
[o
->basereg
];
1949 static int op_rexflags(const operand
* o
, int mask
)
1954 if (!is_register(o
->basereg
))
1955 errfunc(ERR_PANIC
, "invalid operand passed to op_rexflags()");
1957 flags
= nasm_reg_flags
[o
->basereg
];
1958 val
= nasm_regvals
[o
->basereg
];
1960 return rexflags(val
, flags
, mask
);
1963 static int rexflags(int val
, opflags_t flags
, int mask
)
1967 if (val
>= 0 && (val
& 8))
1968 rex
|= REX_B
|REX_X
|REX_R
;
1971 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
1973 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
1979 static int evexflags(int val
, decoflags_t deco
,
1980 int mask
, uint8_t byte
)
1986 if (val
>= 0 && (val
& 16))
1987 evex
|= (EVEX_P0RP
| EVEX_P0X
);
1990 if (val
>= 0 && (val
& 16))
1994 if (deco
& OPMASK_MASK
)
1995 evex
|= deco
& EVEX_P2AAA
;
2001 static int op_evexflags(const operand
* o
, int mask
, uint8_t byte
)
2005 val
= nasm_regvals
[o
->basereg
];
2007 return evexflags(val
, o
->decoflags
, mask
, byte
);
2010 static enum match_result
find_match(const struct itemplate
**tempp
,
2012 int32_t segment
, int64_t offset
, int bits
)
2014 const struct itemplate
*temp
;
2015 enum match_result m
, merr
;
2016 opflags_t xsizeflags
[MAX_OPERANDS
];
2017 bool opsizemissing
= false;
2018 int8_t broadcast
= instruction
->evex_brerop
;
2021 /* broadcasting uses a different data element size */
2022 for (i
= 0; i
< instruction
->operands
; i
++)
2024 xsizeflags
[i
] = instruction
->oprs
[i
].decoflags
& BRSIZE_MASK
;
2026 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
2028 merr
= MERR_INVALOP
;
2030 for (temp
= nasm_instructions
[instruction
->opcode
];
2031 temp
->opcode
!= I_none
; temp
++) {
2032 m
= matches(temp
, instruction
, bits
);
2033 if (m
== MOK_JUMP
) {
2034 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2038 } else if (m
== MERR_OPSIZEMISSING
&& !itemp_has(temp
, IF_SX
)) {
2040 * Missing operand size and a candidate for fuzzy matching...
2042 for (i
= 0; i
< temp
->operands
; i
++)
2044 xsizeflags
[i
] |= temp
->deco
[i
] & BRSIZE_MASK
;
2046 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
2047 opsizemissing
= true;
2051 if (merr
== MOK_GOOD
)
2055 /* No match, but see if we can get a fuzzy operand size match... */
2059 for (i
= 0; i
< instruction
->operands
; i
++) {
2061 * We ignore extrinsic operand sizes on registers, so we should
2062 * never try to fuzzy-match on them. This also resolves the case
2063 * when we have e.g. "xmmrm128" in two different positions.
2065 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
2068 /* This tests if xsizeflags[i] has more than one bit set */
2069 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
2070 goto done
; /* No luck */
2072 if (i
== broadcast
) {
2073 instruction
->oprs
[i
].decoflags
|= xsizeflags
[i
];
2074 instruction
->oprs
[i
].type
|= (xsizeflags
[i
] == BR_BITS32
?
2077 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
2081 /* Try matching again... */
2082 for (temp
= nasm_instructions
[instruction
->opcode
];
2083 temp
->opcode
!= I_none
; temp
++) {
2084 m
= matches(temp
, instruction
, bits
);
2085 if (m
== MOK_JUMP
) {
2086 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2093 if (merr
== MOK_GOOD
)
2102 static enum match_result
matches(const struct itemplate
*itemp
,
2103 insn
*instruction
, int bits
)
2105 opflags_t size
[MAX_OPERANDS
], asize
;
2106 bool opsizemissing
= false;
2112 if (itemp
->opcode
!= instruction
->opcode
)
2113 return MERR_INVALOP
;
2116 * Count the operands
2118 if (itemp
->operands
!= instruction
->operands
)
2119 return MERR_INVALOP
;
2124 if (!(optimizing
> 0) && itemp_has(itemp
, IF_OPT
))
2125 return MERR_INVALOP
;
2130 switch (instruction
->prefixes
[PPS_VEX
]) {
2132 if (!itemp_has(itemp
, IF_EVEX
))
2133 return MERR_ENCMISMATCH
;
2137 if (!itemp_has(itemp
, IF_VEX
))
2138 return MERR_ENCMISMATCH
;
2145 * Check that no spurious colons or TOs are present
2147 for (i
= 0; i
< itemp
->operands
; i
++)
2148 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2149 return MERR_INVALOP
;
2152 * Process size flags
2154 switch (itemp_smask(itemp
)) {
2155 case IF_GENBIT(IF_SB
):
2158 case IF_GENBIT(IF_SW
):
2161 case IF_GENBIT(IF_SD
):
2164 case IF_GENBIT(IF_SQ
):
2167 case IF_GENBIT(IF_SO
):
2170 case IF_GENBIT(IF_SY
):
2173 case IF_GENBIT(IF_SZ
):
2176 case IF_GENBIT(IF_SIZE
):
2197 if (itemp_armask(itemp
)) {
2198 /* S- flags only apply to a specific operand */
2199 i
= itemp_arg(itemp
);
2200 memset(size
, 0, sizeof size
);
2203 /* S- flags apply to all operands */
2204 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2209 * Check that the operand flags all match up,
2210 * it's a bit tricky so lets be verbose:
2212 * 1) Find out the size of operand. If instruction
2213 * doesn't have one specified -- we're trying to
2214 * guess it either from template (IF_S* flag) or
2217 * 2) If template operand do not match the instruction OR
2218 * template has an operand size specified AND this size differ
2219 * from which instruction has (perhaps we got it from code bits)
2221 * a) Check that only size of instruction and operand is differ
2222 * other characteristics do match
2223 * b) Perhaps it's a register specified in instruction so
2224 * for such a case we just mark that operand as "size
2225 * missing" and this will turn on fuzzy operand size
2226 * logic facility (handled by a caller)
2228 for (i
= 0; i
< itemp
->operands
; i
++) {
2229 opflags_t type
= instruction
->oprs
[i
].type
;
2230 decoflags_t deco
= instruction
->oprs
[i
].decoflags
;
2231 bool is_broadcast
= deco
& BRDCAST_MASK
;
2232 uint8_t brcast_num
= 0;
2233 opflags_t template_opsize
, insn_opsize
;
2235 if (!(type
& SIZE_MASK
))
2238 insn_opsize
= type
& SIZE_MASK
;
2239 if (!is_broadcast
) {
2240 template_opsize
= itemp
->opd
[i
] & SIZE_MASK
;
2242 decoflags_t deco_brsize
= itemp
->deco
[i
] & BRSIZE_MASK
;
2244 * when broadcasting, the element size depends on
2245 * the instruction type. decorator flag should match.
2249 template_opsize
= (deco_brsize
== BR_BITS32
? BITS32
: BITS64
);
2250 /* calculate the proper number : {1to<brcast_num>} */
2251 brcast_num
= (itemp
->opd
[i
] & SIZE_MASK
) / BITS128
*
2252 BITS64
/ template_opsize
* 2;
2254 template_opsize
= 0;
2258 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2259 (deco
& ~itemp
->deco
[i
] & ~BRNUM_MASK
)) {
2260 return MERR_INVALOP
;
2261 } else if (template_opsize
) {
2262 if (template_opsize
!= insn_opsize
) {
2264 return MERR_INVALOP
;
2265 } else if (!is_class(REGISTER
, type
)) {
2267 * Note: we don't honor extrinsic operand sizes for registers,
2268 * so "missing operand size" for a register should be
2269 * considered a wildcard match rather than an error.
2271 opsizemissing
= true;
2273 } else if (is_broadcast
&&
2275 (8U << ((deco
& BRNUM_MASK
) >> BRNUM_SHIFT
)))) {
2277 * broadcasting opsize matches but the number of repeated memory
2278 * element does not match.
2279 * if 64b double precision float is broadcasted to zmm (512b),
2280 * broadcasting decorator must be {1to8}.
2282 return MERR_BRNUMMISMATCH
;
2288 return MERR_OPSIZEMISSING
;
2291 * Check operand sizes
2293 if (itemp_has(itemp
, IF_SM
) || itemp_has(itemp
, IF_SM2
)) {
2294 oprs
= (itemp_has(itemp
, IF_SM2
) ? 2 : itemp
->operands
);
2295 for (i
= 0; i
< oprs
; i
++) {
2296 asize
= itemp
->opd
[i
] & SIZE_MASK
;
2298 for (i
= 0; i
< oprs
; i
++)
2304 oprs
= itemp
->operands
;
2307 for (i
= 0; i
< itemp
->operands
; i
++) {
2308 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2309 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2310 return MERR_OPSIZEMISMATCH
;
2314 * Check template is okay at the set cpu level
2316 if (iflag_cmp_cpu_level(&insns_flags
[itemp
->iflag_idx
], &cpu
) > 0)
2320 * Verify the appropriate long mode flag.
2322 if (itemp_has(itemp
, (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2323 return MERR_BADMODE
;
2326 * If we have a HLE prefix, look for the NOHLE flag
2328 if (itemp_has(itemp
, IF_NOHLE
) &&
2329 (has_prefix(instruction
, PPS_REP
, P_XACQUIRE
) ||
2330 has_prefix(instruction
, PPS_REP
, P_XRELEASE
)))
2334 * Check if special handling needed for Jumps
2336 if ((itemp
->code
[0] & ~1) == 0370)
2340 * Check if BND prefix is allowed.
2341 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
2343 if (!itemp_has(itemp
, IF_BND
) &&
2344 (has_prefix(instruction
, PPS_REP
, P_BND
) ||
2345 has_prefix(instruction
, PPS_REP
, P_NOBND
)))
2347 else if (itemp_has(itemp
, IF_BND
) &&
2348 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ||
2349 has_prefix(instruction
, PPS_REP
, P_REPNZ
)))
2350 return MERR_BADREPNE
;
2356 * Check if ModR/M.mod should/can be 01.
2357 * - EAF_BYTEOFFS is set
2358 * - offset can fit in a byte when EVEX is not used
2359 * - offset can be compressed when EVEX is used
2361 #define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2362 (o >= -128 && o <= 127 && \
2363 seg == NO_SEG && !forw_ref && \
2364 !(input->eaflags & EAF_WORDOFFS) && \
2365 !(ins->rex & REX_EV)) || \
2366 (ins->rex & REX_EV && \
2367 is_disp8n(input, ins, &output->disp8)))
2369 static enum ea_type
process_ea(operand
*input
, ea
*output
, int bits
,
2370 int rfield
, opflags_t rflags
, insn
*ins
)
2372 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2373 int addrbits
= ins
->addr_size
;
2374 int eaflags
= input
->eaflags
;
2376 output
->type
= EA_SCALAR
;
2377 output
->rip
= false;
2380 /* REX flags for the rfield operand */
2381 output
->rex
|= rexflags(rfield
, rflags
, REX_R
| REX_P
| REX_W
| REX_H
);
2382 /* EVEX.R' flag for the REG operand */
2383 ins
->evex_p
[0] |= evexflags(rfield
, 0, EVEX_P0RP
, 0);
2385 if (is_class(REGISTER
, input
->type
)) {
2387 * It's a direct register.
2389 if (!is_register(input
->basereg
))
2392 if (!is_reg_class(REG_EA
, input
->basereg
))
2395 /* broadcasting is not available with a direct register operand. */
2396 if (input
->decoflags
& BRDCAST_MASK
) {
2397 nasm_error(ERR_NONFATAL
, "Broadcasting not allowed from a register");
2401 output
->rex
|= op_rexflags(input
, REX_B
| REX_P
| REX_W
| REX_H
);
2402 ins
->evex_p
[0] |= op_evexflags(input
, EVEX_P0X
, 0);
2403 output
->sib_present
= false; /* no SIB necessary */
2404 output
->bytes
= 0; /* no offset necessary either */
2405 output
->modrm
= GEN_MODRM(3, rfield
, nasm_regvals
[input
->basereg
]);
2408 * It's a memory reference.
2411 /* Embedded rounding or SAE is not available with a mem ref operand. */
2412 if (input
->decoflags
& (ER
| SAE
)) {
2413 nasm_error(ERR_NONFATAL
,
2414 "Embedded rounding is available only with reg-reg op.");
2418 if (input
->basereg
== -1 &&
2419 (input
->indexreg
== -1 || input
->scale
== 0)) {
2421 * It's a pure offset.
2423 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
) &&
2424 input
->segment
== NO_SEG
) {
2425 nasm_error(ERR_WARNING
| ERR_PASS1
, "absolute address can not be RIP-relative");
2426 input
->type
&= ~IP_REL
;
2427 input
->type
|= MEMORY
;
2431 !(IP_REL
& ~input
->type
) && (eaflags
& EAF_MIB
)) {
2432 nasm_error(ERR_NONFATAL
, "RIP-relative addressing is prohibited for mib.");
2436 if (eaflags
& EAF_BYTEOFFS
||
2437 (eaflags
& EAF_WORDOFFS
&&
2438 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16))) {
2439 nasm_error(ERR_WARNING
| ERR_PASS1
, "displacement size ignored on absolute address");
2442 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2443 output
->sib_present
= true;
2444 output
->sib
= GEN_SIB(0, 4, 5);
2446 output
->modrm
= GEN_MODRM(0, rfield
, 4);
2447 output
->rip
= false;
2449 output
->sib_present
= false;
2450 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2451 output
->modrm
= GEN_MODRM(0, rfield
, (addrbits
!= 16 ? 5 : 6));
2452 output
->rip
= bits
== 64;
2456 * It's an indirection.
2458 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2459 int32_t seg
= input
->segment
;
2460 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2461 int t
, it
, bt
; /* register numbers */
2462 opflags_t x
, ix
, bx
; /* register flags */
2465 i
= -1; /* make this easy, at least */
2467 if (is_register(i
)) {
2468 it
= nasm_regvals
[i
];
2469 ix
= nasm_reg_flags
[i
];
2475 if (is_register(b
)) {
2476 bt
= nasm_regvals
[b
];
2477 bx
= nasm_reg_flags
[b
];
2483 /* if either one are a vector register... */
2484 if ((ix
|bx
) & (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
) {
2485 opflags_t sok
= BITS32
| BITS64
;
2486 int32_t o
= input
->offset
;
2487 int mod
, scale
, index
, base
;
2490 * For a vector SIB, one has to be a vector and the other,
2491 * if present, a GPR. The vector must be the index operand.
2493 if (it
== -1 || (bx
& (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
)) {
2499 t
= bt
, bt
= it
, it
= t
;
2500 x
= bx
, bx
= ix
, ix
= x
;
2506 if (!(REG64
& ~bx
) || !(REG32
& ~bx
))
2513 * While we're here, ensure the user didn't specify
2516 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2519 if (addrbits
== 16 ||
2520 (addrbits
== 32 && !(sok
& BITS32
)) ||
2521 (addrbits
== 64 && !(sok
& BITS64
)))
2524 output
->type
= ((ix
& ZMMREG
& ~REG_EA
) ? EA_ZMMVSIB
2525 : ((ix
& YMMREG
& ~REG_EA
)
2526 ? EA_YMMVSIB
: EA_XMMVSIB
));
2528 output
->rex
|= rexflags(it
, ix
, REX_X
);
2529 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2530 ins
->evex_p
[2] |= evexflags(it
, 0, EVEX_P2VP
, 2);
2532 index
= it
& 7; /* it is known to be != -1 */
2547 default: /* then what the smeg is it? */
2548 goto err
; /* panic */
2556 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2557 seg
== NO_SEG
&& !forw_ref
&&
2558 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2560 else if (IS_MOD_01())
2566 output
->sib_present
= true;
2567 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2568 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2569 output
->sib
= GEN_SIB(scale
, index
, base
);
2570 } else if ((ix
|bx
) & (BITS32
|BITS64
)) {
2572 * it must be a 32/64-bit memory reference. Firstly we have
2573 * to check that all registers involved are type E/Rxx.
2575 opflags_t sok
= BITS32
| BITS64
;
2576 int32_t o
= input
->offset
;
2579 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2587 goto err
; /* Invalid register */
2588 if (~sok
& bx
& SIZE_MASK
)
2589 goto err
; /* Invalid size */
2594 * While we're here, ensure the user didn't specify
2597 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2600 if (addrbits
== 16 ||
2601 (addrbits
== 32 && !(sok
& BITS32
)) ||
2602 (addrbits
== 64 && !(sok
& BITS64
)))
2605 /* now reorganize base/index */
2606 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2607 ((hb
== b
&& ht
== EAH_NOTBASE
) ||
2608 (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2609 /* swap if hints say so */
2610 t
= bt
, bt
= it
, it
= t
;
2611 x
= bx
, bx
= ix
, ix
= x
;
2614 if (bt
== -1 && s
== 1 && !(hb
== i
&& ht
== EAH_NOTBASE
)) {
2615 /* make single reg base, unless hint */
2616 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2618 if (eaflags
& EAF_MIB
) {
2619 /* only for mib operands */
2620 if (it
== -1 && (hb
== b
&& ht
== EAH_NOTBASE
)) {
2622 * make a single reg index [reg*1].
2623 * gas uses this form for an explicit index register.
2625 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2627 if ((ht
== EAH_SUMMED
) && bt
== -1) {
2628 /* separate once summed index into [base, index] */
2629 bt
= it
, bx
= ix
, s
--;
2632 if (((s
== 2 && it
!= REG_NUM_ESP
&&
2633 (!(eaflags
& EAF_TIMESTWO
) || (ht
== EAH_SUMMED
))) ||
2634 s
== 3 || s
== 5 || s
== 9) && bt
== -1) {
2635 /* convert 3*EAX to EAX+2*EAX */
2636 bt
= it
, bx
= ix
, s
--;
2638 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
&&
2639 (eaflags
& EAF_TIMESTWO
) &&
2640 (hb
== b
&& ht
== EAH_NOTBASE
)) {
2642 * convert [NOSPLIT EAX*1]
2643 * to sib format with 0x0 displacement - [EAX*1+0].
2645 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2648 if (s
== 1 && it
== REG_NUM_ESP
) {
2649 /* swap ESP into base if scale is 1 */
2650 t
= it
, it
= bt
, bt
= t
;
2651 x
= ix
, ix
= bx
, bx
= x
;
2653 if (it
== REG_NUM_ESP
||
2654 (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2655 goto err
; /* wrong, for various reasons */
2657 output
->rex
|= rexflags(it
, ix
, REX_X
);
2658 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2660 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2669 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2670 seg
== NO_SEG
&& !forw_ref
&&
2671 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2673 else if (IS_MOD_01())
2679 output
->sib_present
= false;
2680 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2681 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2684 int mod
, scale
, index
, base
;
2704 default: /* then what the smeg is it? */
2705 goto err
; /* panic */
2713 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2714 seg
== NO_SEG
&& !forw_ref
&&
2715 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2717 else if (IS_MOD_01())
2723 output
->sib_present
= true;
2724 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2725 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2726 output
->sib
= GEN_SIB(scale
, index
, base
);
2728 } else { /* it's 16-bit */
2730 int16_t o
= input
->offset
;
2732 /* check for 64-bit long mode */
2736 /* check all registers are BX, BP, SI or DI */
2737 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
&& b
!= R_DI
) ||
2738 (i
!= -1 && i
!= R_BP
&& i
!= R_BX
&& i
!= R_SI
&& i
!= R_DI
))
2741 /* ensure the user didn't specify DWORD/QWORD */
2742 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2745 if (s
!= 1 && i
!= -1)
2746 goto err
; /* no can do, in 16-bit EA */
2747 if (b
== -1 && i
!= -1) {
2752 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2757 /* have BX/BP as base, SI/DI index */
2759 goto err
; /* shouldn't ever happen, in theory */
2760 if (i
!= -1 && b
!= -1 &&
2761 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2762 goto err
; /* invalid combinations */
2763 if (b
== -1) /* pure offset: handled above */
2764 goto err
; /* so if it gets to here, panic! */
2768 switch (i
* 256 + b
) {
2769 case R_SI
* 256 + R_BX
:
2772 case R_DI
* 256 + R_BX
:
2775 case R_SI
* 256 + R_BP
:
2778 case R_DI
* 256 + R_BP
:
2796 if (rm
== -1) /* can't happen, in theory */
2797 goto err
; /* so panic if it does */
2799 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2800 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2802 else if (IS_MOD_01())
2807 output
->sib_present
= false; /* no SIB - it's 16-bit */
2808 output
->bytes
= mod
; /* bytes of offset needed */
2809 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2814 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2815 return output
->type
;
2818 return output
->type
= EA_INVALID
;
2821 static void add_asp(insn
*ins
, int addrbits
)
2826 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2828 switch (ins
->prefixes
[PPS_ASIZE
]) {
2839 valid
&= (addrbits
== 32) ? 16 : 32;
2845 for (j
= 0; j
< ins
->operands
; j
++) {
2846 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
2849 /* Verify as Register */
2850 if (!is_register(ins
->oprs
[j
].indexreg
))
2853 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2855 /* Verify as Register */
2856 if (!is_register(ins
->oprs
[j
].basereg
))
2859 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2861 if (ins
->oprs
[j
].scale
== 0)
2865 int ds
= ins
->oprs
[j
].disp_size
;
2866 if ((addrbits
!= 64 && ds
> 8) ||
2867 (addrbits
== 64 && ds
== 16))
2887 if (valid
& addrbits
) {
2888 ins
->addr_size
= addrbits
;
2889 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2890 /* Add an address size prefix */
2891 ins
->prefixes
[PPS_ASIZE
] = (addrbits
== 32) ? P_A16
: P_A32
;;
2892 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2895 errfunc(ERR_NONFATAL
, "impossible combination of address sizes");
2896 ins
->addr_size
= addrbits
; /* Error recovery */
2899 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2901 for (j
= 0; j
< ins
->operands
; j
++) {
2902 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2903 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
) != ins
->addr_size
) {
2905 * mem_offs sizes must match the address size; if not,
2906 * strip the MEM_OFFS bit and match only EA instructions
2908 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);