1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2016 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 * Bytecode specification
38 * ----------------------
41 * Codes Mnemonic Explanation
43 * \0 terminates the code. (Unless it's a literal of course.)
44 * \1..\4 that many literal bytes follow in the code stream
45 * \5 add 4 to the primary operand number (b, low octdigit)
46 * \6 add 4 to the secondary operand number (a, middle octdigit)
47 * \7 add 4 to both the primary and the secondary operand number
48 * \10..\13 a literal byte follows in the code stream, to be added
49 * to the register value of operand 0..3
50 * \14..\17 the position of index register operand in MIB (BND insns)
51 * \20..\23 ib a byte immediate operand, from operand 0..3
52 * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
53 * \30..\33 iw a word immediate operand, from operand 0..3
54 * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
55 * assembly mode or the operand-size override on the operand
56 * \40..\43 id a long immediate operand, from operand 0..3
57 * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
58 * depending on the address size of the instruction.
59 * \50..\53 rel8 a byte relative operand, from operand 0..3
60 * \54..\57 iq a qword immediate operand, from operand 0..3
61 * \60..\63 rel16 a word relative operand, from operand 0..3
62 * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
63 * assembly mode or the operand-size override on the operand
64 * \70..\73 rel32 a long relative operand, from operand 0..3
65 * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
66 * \1ab a ModRM, calculated on EA in operand a, with the spare
67 * field the register value of operand b.
68 * \172\ab the register number from operand a in bits 7..4, with
69 * the 4-bit immediate from operand b in bits 3..0.
70 * \173\xab the register number from operand a in bits 7..4, with
71 * the value b in bits 3..0.
72 * \174..\177 the register number from operand 0..3 in bits 7..4, and
73 * an arbitrary value in bits 3..0 (assembled as zero.)
74 * \2ab a ModRM, calculated on EA in operand a, with the spare
75 * field equal to digit b.
77 * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
78 * V field taken from operand 0..3.
79 * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
80 * V field set to 1111b.
82 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
85 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
88 * [l0] ll = 0 (.128, .lz)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
93 * [w0] ww = 0 for W = 0
94 * [w1] ww = 1 for W = 1
95 * [wig] ww = 2 for W don't care (always assembled as 0)
96 * [ww] ww = 3 for W used as REX.W
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
106 * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
107 * \260..\263 this instruction uses VEX/XOP rather than REX, with the
108 * V field taken from operand 0..3.
109 * \270 this instruction uses VEX/XOP rather than REX, with the
110 * V field set to 1111b.
112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
115 * [l0] ll = 0 for L = 0 (.128, .lz)
116 * [l1] ll = 1 for L = 1 (.256)
117 * [lig] ll = 2 for L don't care (always assembled as 0)
119 * [w0] ww = 0 for W = 0
120 * [w1 ] ww = 1 for W = 1
121 * [wig] ww = 2 for W don't care (always assembled as 0)
122 * [ww] ww = 3 for W used as REX.W
124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
126 * \271 hlexr instruction takes XRELEASE (F3) with or without lock
127 * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
128 * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
129 * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
130 * to the operand size (if o16/o32/o64 present) or the bit size
131 * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
132 * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
133 * \312 adf (disassembler only) invalid with non-default address size.
134 * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
135 * \314 norexb (disassembler only) invalid with REX.B
136 * \315 norexx (disassembler only) invalid with REX.X
137 * \316 norexr (disassembler only) invalid with REX.R
138 * \317 norexw (disassembler only) invalid with REX.W
139 * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
140 * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
141 * \322 odf indicates that this instruction is only valid when the
142 * operand size is the default (instruction to disassembler,
143 * generates no code in the assembler)
144 * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
145 * \324 o64 indicates 64-bit operand size requiring REX prefix.
146 * \325 nohi instruction which always uses spl/bpl/sil/dil
147 * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
148 disassembler only; for SSE instructions.
149 * \330 a literal byte follows in the code stream, to be added
150 * to the condition code value of the instruction.
151 * \331 norep instruction not valid with REP prefix. Hint for
152 * disassembler only; for SSE instructions.
153 * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
154 * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
155 * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
156 * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
157 * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
158 * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
159 * \336-\337 are still listed as prefixes in the disassembler.
160 * \340 resb reserve <operand 0> bytes of uninitialized storage.
161 * Operand 0 had better be a segmentless constant.
162 * \341 wait this instruction needs a WAIT "prefix"
163 * \360 np no SSE prefix (== \364\331)
164 * \361 66 SSE prefix (== \366\331)
165 * \364 !osp operand-size prefix (0x66) not permitted
166 * \365 !asp address-size prefix (0x67) not permitted
167 * \366 operand-size prefix (0x66) used as opcode extension
168 * \367 address-size prefix (0x67) used as opcode extension
169 * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
170 * jmp8 370 is used for Jcc, 371 is used for JMP.
171 * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
172 * used for conditional jump over longer jump
173 * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
174 * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
175 * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
178 #include "compiler.h"
183 #include <inttypes.h>
187 #include "assemble.h"
195 * Matching errors. These should be sorted so that more specific
196 * errors come later in the sequence.
209 * Matching success; the conditional ones first
211 MOK_JUMP
, /* Matching OK but needs jmp_match() */
212 MOK_GOOD
/* Matching unconditionally OK */
216 enum ea_type type
; /* what kind of EA is this? */
217 int sib_present
; /* is a SIB byte necessary? */
218 int bytes
; /* # of bytes of offset needed */
219 int size
; /* lazy - this is sib+bytes+1 */
220 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
221 int8_t disp8
; /* compressed displacement for EVEX */
224 #define GEN_SIB(scale, index, base) \
225 (((scale) << 6) | ((index) << 3) | ((base)))
227 #define GEN_MODRM(mod, reg, rm) \
228 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
230 static iflag_t cpu
; /* cpu level received from nasm.c */
232 static int64_t calcsize(int32_t, int64_t, int, insn
*,
233 const struct itemplate
*);
234 static void gencode(int32_t segment
, int64_t offset
, int bits
,
235 insn
* ins
, const struct itemplate
*temp
,
237 static enum match_result
find_match(const struct itemplate
**tempp
,
239 int32_t segment
, int64_t offset
, int bits
);
240 static enum match_result
matches(const struct itemplate
*, insn
*, int bits
);
241 static opflags_t
regflag(const operand
*);
242 static int32_t regval(const operand
*);
243 static int rexflags(int, opflags_t
, int);
244 static int op_rexflags(const operand
*, int);
245 static int op_evexflags(const operand
*, int, uint8_t);
246 static void add_asp(insn
*, int);
248 static enum ea_type
process_ea(operand
*, ea
*, int, int, opflags_t
, insn
*);
250 static int has_prefix(insn
* ins
, enum prefix_pos pos
, int prefix
)
252 return ins
->prefixes
[pos
] == prefix
;
255 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
257 if (ins
->prefixes
[pos
])
258 nasm_error(ERR_NONFATAL
, "invalid %s prefix",
259 prefix_name(ins
->prefixes
[pos
]));
262 static const char *size_name(int size
)
286 static void warn_overflow(int pass
, int size
)
288 nasm_error(ERR_WARNING
| pass
| ERR_WARN_NOV
,
289 "%s data exceeds bounds", size_name(size
));
292 static void warn_overflow_const(int64_t data
, int size
)
294 if (overflow_general(data
, size
))
295 warn_overflow(ERR_PASS1
, size
);
298 static void warn_overflow_opd(const struct operand
*o
, int size
)
300 if (o
->wrt
== NO_SEG
&& o
->segment
== NO_SEG
) {
301 if (overflow_general(o
->offset
, size
))
302 warn_overflow(ERR_PASS2
, size
);
307 * Size of an address relocation, or zero if not an address
309 static int addrsize(enum out_type type
, uint64_t size
)
313 return abs((int)size
);
328 * This routine wrappers the real output format's output routine,
329 * in order to pass a copy of the data off to the listing file
330 * generator at the same time, flatten unnecessary relocations,
331 * and verify backend compatibility.
333 static void out(int64_t offset
, int32_t segto
, const void *data
,
334 enum out_type type
, uint64_t size
,
335 int32_t segment
, int32_t wrt
)
337 static int32_t lineno
= 0; /* static!!! */
338 static const char *lnfname
= NULL
;
340 int asize
= addrsize(type
, size
); /* Address size in bytes */
341 const int amax
= ofmt
->maxbits
>> 3; /* Maximum address size in bytes */
343 if (type
== OUT_ADDRESS
&& segment
== NO_SEG
&& wrt
== NO_SEG
) {
345 * This is a non-relocated address, and we're going to
346 * convert it into RAWDATA format.
351 nasm_panic(0, "OUT_ADDRESS with size > 8");
355 WRITEADDR(q
, *(int64_t *)data
, asize
);
359 asize
= 0; /* No longer an address */
362 lfmt
->output(offset
, data
, type
, size
);
365 * this call to src_get determines when we call the
366 * debug-format-specific "linenum" function
367 * it updates lineno and lnfname to the current values
368 * returning 0 if "same as last time", -2 if lnfname
369 * changed, and the amount by which lineno changed,
370 * if it did. thus, these variables must be static
373 if (src_get(&lineno
, &lnfname
))
374 dfmt
->linenum(lnfname
, lineno
, segto
);
376 if (asize
&& asize
> amax
) {
377 if (type
!= OUT_ADDRESS
|| (int)size
< 0) {
378 nasm_error(ERR_NONFATAL
,
379 "%d-bit signed relocation unsupported by output format %s\n",
380 asize
<< 3, ofmt
->shortname
);
383 nasm_error(ERR_WARNING
| ERR_WARN_ZEXTRELOC
,
384 "%d-bit unsigned relocation zero-extended from %d bits\n",
385 asize
<< 3, ofmt
->maxbits
);
386 ofmt
->output(segto
, data
, type
, amax
, segment
, wrt
);
391 segment
= wrt
= NO_SEG
;
394 ofmt
->output(segto
, data
, type
, size
, segment
, wrt
);
397 static void out_imm8(int64_t offset
, int32_t segment
,
398 struct operand
*opx
, int asize
)
400 if (opx
->segment
!= NO_SEG
) {
401 uint64_t data
= opx
->offset
;
402 out(offset
, segment
, &data
, OUT_ADDRESS
, asize
, opx
->segment
, opx
->wrt
);
404 uint8_t byte
= opx
->offset
;
405 out(offset
, segment
, &byte
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
409 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
410 insn
* ins
, const struct itemplate
*temp
)
413 const uint8_t *code
= temp
->code
;
417 if (((c
& ~1) != 0370) || (ins
->oprs
[0].type
& STRICT
))
421 if (optimizing
< 0 && c
== 0371)
424 isize
= calcsize(segment
, offset
, bits
, ins
, temp
);
426 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
427 /* Be optimistic in pass 1 */
430 if (ins
->oprs
[0].segment
!= segment
)
433 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
434 is_byte
= (isize
>= -128 && isize
<= 127); /* is it byte size? */
436 if (is_byte
&& c
== 0371 && ins
->prefixes
[PPS_REP
] == P_BND
) {
437 /* jmp short (opcode eb) cannot be used with bnd prefix. */
438 ins
->prefixes
[PPS_REP
] = P_none
;
439 nasm_error(ERR_WARNING
| ERR_WARN_BND
| ERR_PASS2
,
440 "jmp short does not init bnd regs - bnd prefix dropped.");
446 int64_t assemble(int32_t segment
, int64_t offset
, int bits
, iflag_t cp
,
449 const struct itemplate
*temp
;
454 int64_t start
= offset
;
455 int64_t wsize
; /* size for DB etc. */
459 wsize
= idata_bytes(instruction
->opcode
);
465 int32_t t
= instruction
->times
;
467 nasm_panic(0, "instruction->times < 0 (%"PRId32
") in assemble()", t
);
469 while (t
--) { /* repeat TIMES times */
470 list_for_each(e
, instruction
->eops
) {
471 if (e
->type
== EOT_DB_NUMBER
) {
473 nasm_error(ERR_NONFATAL
,
474 "integer supplied to a DT, DO or DY"
477 out(offset
, segment
, &e
->offset
,
478 OUT_ADDRESS
, wsize
, e
->segment
, e
->wrt
);
481 } else if (e
->type
== EOT_DB_STRING
||
482 e
->type
== EOT_DB_STRING_FREE
) {
485 out(offset
, segment
, e
->stringval
,
486 OUT_RAWDATA
, e
->stringlen
, NO_SEG
, NO_SEG
);
487 align
= e
->stringlen
% wsize
;
490 align
= wsize
- align
;
491 out(offset
, segment
, zero_buffer
,
492 OUT_RAWDATA
, align
, NO_SEG
, NO_SEG
);
494 offset
+= e
->stringlen
+ align
;
497 if (t
> 0 && t
== instruction
->times
- 1) {
499 * Dummy call to lfmt->output to give the offset to the
502 lfmt
->output(offset
, NULL
, OUT_RAWDATA
, 0);
503 lfmt
->uplevel(LIST_TIMES
);
506 if (instruction
->times
> 1)
507 lfmt
->downlevel(LIST_TIMES
);
508 return offset
- start
;
511 if (instruction
->opcode
== I_INCBIN
) {
512 const char *fname
= instruction
->eops
->stringval
;
515 fp
= fopen(fname
, "rb");
517 nasm_error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
519 } else if (fseek(fp
, 0L, SEEK_END
) < 0) {
520 nasm_error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
524 static char buf
[4096];
525 size_t t
= instruction
->times
;
530 if (instruction
->eops
->next
) {
531 base
= instruction
->eops
->next
->offset
;
533 if (instruction
->eops
->next
->next
&&
534 len
> (size_t)instruction
->eops
->next
->next
->offset
)
535 len
= (size_t)instruction
->eops
->next
->next
->offset
;
538 * Dummy call to lfmt->output to give the offset to the
541 lfmt
->output(offset
, NULL
, OUT_RAWDATA
, 0);
542 lfmt
->uplevel(LIST_INCBIN
);
546 fseek(fp
, base
, SEEK_SET
);
550 m
= fread(buf
, 1, l
> sizeof(buf
) ? sizeof(buf
) : l
, fp
);
553 * This shouldn't happen unless the file
554 * actually changes while we are reading
557 nasm_error(ERR_NONFATAL
,
558 "`incbin': unexpected EOF while"
559 " reading file `%s'", fname
);
560 t
= 0; /* Try to exit cleanly */
563 out(offset
, segment
, buf
, OUT_RAWDATA
, m
,
568 lfmt
->downlevel(LIST_INCBIN
);
569 if (instruction
->times
> 1) {
571 * Dummy call to lfmt->output to give the offset to the
574 lfmt
->output(offset
, NULL
, OUT_RAWDATA
, 0);
575 lfmt
->uplevel(LIST_TIMES
);
576 lfmt
->downlevel(LIST_TIMES
);
579 return instruction
->times
* len
;
581 return 0; /* if we're here, there's an error */
584 /* Check to see if we need an address-size prefix */
585 add_asp(instruction
, bits
);
587 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
591 int64_t insn_size
= calcsize(segment
, offset
, bits
, instruction
, temp
);
592 itimes
= instruction
->times
;
593 if (insn_size
< 0) /* shouldn't be, on pass two */
594 nasm_panic(0, "errors made it through from pass one");
597 for (j
= 0; j
< MAXPREFIX
; j
++) {
599 switch (instruction
->prefixes
[j
]) {
620 nasm_error(ERR_WARNING
| ERR_PASS2
,
621 "cs segment base generated, but will be ignored in 64-bit mode");
627 nasm_error(ERR_WARNING
| ERR_PASS2
,
628 "ds segment base generated, but will be ignored in 64-bit mode");
634 nasm_error(ERR_WARNING
| ERR_PASS2
,
635 "es segment base generated, but will be ignored in 64-bit mode");
647 nasm_error(ERR_WARNING
| ERR_PASS2
,
648 "ss segment base generated, but will be ignored in 64-bit mode");
654 nasm_error(ERR_NONFATAL
,
655 "segr6 and segr7 cannot be used as prefixes");
659 nasm_error(ERR_NONFATAL
,
660 "16-bit addressing is not supported "
662 } else if (bits
!= 16)
671 nasm_error(ERR_NONFATAL
,
672 "64-bit addressing is only supported "
700 nasm_panic(0, "invalid instruction prefix");
703 out(offset
, segment
, &c
, OUT_RAWDATA
, 1,
708 insn_end
= offset
+ insn_size
;
709 gencode(segment
, offset
, bits
, instruction
,
712 if (itimes
> 0 && itimes
== instruction
->times
- 1) {
714 * Dummy call to lfmt->output to give the offset to the
717 lfmt
->output(offset
, NULL
, OUT_RAWDATA
, 0);
718 lfmt
->uplevel(LIST_TIMES
);
721 if (instruction
->times
> 1)
722 lfmt
->downlevel(LIST_TIMES
);
723 return offset
- start
;
727 case MERR_OPSIZEMISSING
:
728 nasm_error(ERR_NONFATAL
, "operation size not specified");
730 case MERR_OPSIZEMISMATCH
:
731 nasm_error(ERR_NONFATAL
, "mismatch in operand sizes");
733 case MERR_BRNUMMISMATCH
:
734 nasm_error(ERR_NONFATAL
,
735 "mismatch in the number of broadcasting elements");
738 nasm_error(ERR_NONFATAL
, "no instruction for this cpu level");
741 nasm_error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
744 case MERR_ENCMISMATCH
:
745 nasm_error(ERR_NONFATAL
, "specific encoding scheme not available");
748 nasm_error(ERR_NONFATAL
, "bnd prefix is not allowed");
751 nasm_error(ERR_NONFATAL
, "%s prefix is not allowed",
752 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ?
756 nasm_error(ERR_NONFATAL
,
757 "invalid combination of opcode and operands");
764 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, iflag_t cp
,
767 const struct itemplate
*temp
;
772 if (instruction
->opcode
== I_none
)
775 if (instruction
->opcode
== I_DB
|| instruction
->opcode
== I_DW
||
776 instruction
->opcode
== I_DD
|| instruction
->opcode
== I_DQ
||
777 instruction
->opcode
== I_DT
|| instruction
->opcode
== I_DO
||
778 instruction
->opcode
== I_DY
) {
780 int32_t isize
, osize
, wsize
;
783 wsize
= idata_bytes(instruction
->opcode
);
785 list_for_each(e
, instruction
->eops
) {
789 if (e
->type
== EOT_DB_NUMBER
) {
791 warn_overflow_const(e
->offset
, wsize
);
792 } else if (e
->type
== EOT_DB_STRING
||
793 e
->type
== EOT_DB_STRING_FREE
)
794 osize
= e
->stringlen
;
796 align
= (-osize
) % wsize
;
799 isize
+= osize
+ align
;
801 return isize
* instruction
->times
;
804 if (instruction
->opcode
== I_INCBIN
) {
805 const char *fname
= instruction
->eops
->stringval
;
810 fp
= fopen(fname
, "rb");
812 nasm_error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
814 else if (fseek(fp
, 0L, SEEK_END
) < 0)
815 nasm_error(ERR_NONFATAL
, "`incbin': unable to seek on file `%s'",
819 if (instruction
->eops
->next
) {
820 len
-= instruction
->eops
->next
->offset
;
821 if (instruction
->eops
->next
->next
&&
822 len
> (size_t)instruction
->eops
->next
->next
->offset
) {
823 len
= (size_t)instruction
->eops
->next
->next
->offset
;
826 val
= instruction
->times
* len
;
833 /* Check to see if we need an address-size prefix */
834 add_asp(instruction
, bits
);
836 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
838 /* we've matched an instruction. */
842 isize
= calcsize(segment
, offset
, bits
, instruction
, temp
);
845 for (j
= 0; j
< MAXPREFIX
; j
++) {
846 switch (instruction
->prefixes
[j
]) {
876 return isize
* instruction
->times
;
878 return -1; /* didn't match any instruction */
882 static void bad_hle_warn(const insn
* ins
, uint8_t hleok
)
884 enum prefixes rep_pfx
= ins
->prefixes
[PPS_REP
];
885 enum whatwarn
{ w_none
, w_lock
, w_inval
} ww
;
886 static const enum whatwarn warn
[2][4] =
888 { w_inval
, w_inval
, w_none
, w_lock
}, /* XACQUIRE */
889 { w_inval
, w_none
, w_none
, w_lock
}, /* XRELEASE */
893 n
= (unsigned int)rep_pfx
- P_XACQUIRE
;
895 return; /* Not XACQUIRE/XRELEASE */
898 if (!is_class(MEMORY
, ins
->oprs
[0].type
))
899 ww
= w_inval
; /* HLE requires operand 0 to be memory */
906 if (ins
->prefixes
[PPS_LOCK
] != P_LOCK
) {
907 nasm_error(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
908 "%s with this instruction requires lock",
909 prefix_name(rep_pfx
));
914 nasm_error(ERR_WARNING
| ERR_WARN_HLE
| ERR_PASS2
,
915 "%s invalid with this instruction",
916 prefix_name(rep_pfx
));
921 /* Common construct */
922 #define case3(x) case (x): case (x)+1: case (x)+2
923 #define case4(x) case3(x): case (x)+3
925 static int64_t calcsize(int32_t segment
, int64_t offset
, int bits
,
926 insn
* ins
, const struct itemplate
*temp
)
928 const uint8_t *codes
= temp
->code
;
937 bool lockcheck
= true;
938 enum reg_enum mib_index
= R_none
; /* For a separate index MIB reg form */
940 ins
->rex
= 0; /* Ensure REX is reset */
941 eat
= EA_SCALAR
; /* Expect a scalar EA */
942 memset(ins
->evex_p
, 0, 3); /* Ensure EVEX is reset */
944 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
947 (void)segment
; /* Don't warn that this parameter is unused */
948 (void)offset
; /* Don't warn that this parameter is unused */
952 op1
= (c
& 3) + ((opex
& 1) << 2);
953 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
954 opx
= &ins
->oprs
[op1
];
955 opex
= 0; /* For the next iteration */
959 codes
+= c
, length
+= c
;
968 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
973 /* this is an index reg of MIB operand */
974 mib_index
= opx
->basereg
;
987 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
988 length
+= (opx
->type
& BITS16
) ? 2 : 4;
990 length
+= (bits
== 16) ? 2 : 4;
998 length
+= ins
->addr_size
>> 3;
1006 length
+= 8; /* MOV reg64/imm */
1014 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1015 length
+= (opx
->type
& BITS16
) ? 2 : 4;
1017 length
+= (bits
== 16) ? 2 : 4;
1040 ins
->vexreg
= regval(opx
);
1041 ins
->evex_p
[2] |= op_evexflags(opx
, EVEX_P2VP
, 2); /* High-16 NDS */
1042 ins
->vex_cm
= *codes
++;
1043 ins
->vex_wlp
= *codes
++;
1044 ins
->evex_tuple
= (*codes
++ - 0300);
1050 ins
->vex_cm
= *codes
++;
1051 ins
->vex_wlp
= *codes
++;
1052 ins
->evex_tuple
= (*codes
++ - 0300);
1061 ins
->vexreg
= regval(opx
);
1062 ins
->vex_cm
= *codes
++;
1063 ins
->vex_wlp
= *codes
++;
1069 ins
->vex_cm
= *codes
++;
1070 ins
->vex_wlp
= *codes
++;
1087 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
1091 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1098 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1099 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1108 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1112 nasm_error(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1114 ins
->prefixes
[PPS_OSIZE
] = P_O16
;
1120 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1124 nasm_error(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1126 ins
->prefixes
[PPS_OSIZE
] = P_O32
;
1168 if (!ins
->prefixes
[PPS_REP
])
1169 ins
->prefixes
[PPS_REP
] = P_REP
;
1173 if (!ins
->prefixes
[PPS_REP
])
1174 ins
->prefixes
[PPS_REP
] = P_REPNE
;
1178 if (ins
->oprs
[0].segment
!= NO_SEG
)
1179 nasm_error(ERR_NONFATAL
, "attempt to reserve non-constant"
1180 " quantity of BSS space");
1182 length
+= ins
->oprs
[0].offset
;
1186 if (!ins
->prefixes
[PPS_WAIT
])
1187 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1242 struct operand
*opy
= &ins
->oprs
[op2
];
1243 struct operand
*op_er_sae
;
1245 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1248 /* pick rfield from operand b (opx) */
1249 rflags
= regflag(opx
);
1250 rfield
= nasm_regvals
[opx
->basereg
];
1256 /* EVEX.b1 : evex_brerop contains the operand position */
1257 op_er_sae
= (ins
->evex_brerop
>= 0 ?
1258 &ins
->oprs
[ins
->evex_brerop
] : NULL
);
1260 if (op_er_sae
&& (op_er_sae
->decoflags
& (ER
| SAE
))) {
1262 ins
->evex_p
[2] |= EVEX_P2B
;
1263 if (op_er_sae
->decoflags
& ER
) {
1264 /* set EVEX.RC (rounding control) */
1265 ins
->evex_p
[2] |= ((ins
->evex_rm
- BRC_RN
) << 5)
1269 /* set EVEX.L'L (vector length) */
1270 ins
->evex_p
[2] |= ((ins
->vex_wlp
<< (5 - 2)) & EVEX_P2LL
);
1271 ins
->evex_p
[1] |= ((ins
->vex_wlp
<< (7 - 4)) & EVEX_P1W
);
1272 if (opy
->decoflags
& BRDCAST_MASK
) {
1274 ins
->evex_p
[2] |= EVEX_P2B
;
1278 if (itemp_has(temp
, IF_MIB
)) {
1279 opy
->eaflags
|= EAF_MIB
;
1281 * if a separate form of MIB (ICC style) is used,
1282 * the index reg info is merged into mem operand
1284 if (mib_index
!= R_none
) {
1285 opy
->indexreg
= mib_index
;
1287 opy
->hintbase
= mib_index
;
1288 opy
->hinttype
= EAH_NOTBASE
;
1292 if (process_ea(opy
, &ea_data
, bits
,
1293 rfield
, rflags
, ins
) != eat
) {
1294 nasm_error(ERR_NONFATAL
, "invalid effective address");
1297 ins
->rex
|= ea_data
.rex
;
1298 length
+= ea_data
.size
;
1304 nasm_panic(0, "internal instruction table corrupt"
1305 ": instruction code \\%o (0x%02X) given", c
, c
);
1310 ins
->rex
&= rex_mask
;
1312 if (ins
->rex
& REX_NH
) {
1313 if (ins
->rex
& REX_H
) {
1314 nasm_error(ERR_NONFATAL
, "instruction cannot use high registers");
1317 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1320 switch (ins
->prefixes
[PPS_VEX
]) {
1322 if (!(ins
->rex
& REX_EV
))
1327 if (!(ins
->rex
& REX_V
))
1334 if (ins
->rex
& (REX_V
| REX_EV
)) {
1335 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1337 if (ins
->rex
& REX_H
) {
1338 nasm_error(ERR_NONFATAL
, "cannot use high register in AVX instruction");
1341 switch (ins
->vex_wlp
& 060) {
1355 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->vexreg
> 7)) {
1356 nasm_error(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1358 } else if (!(ins
->rex
& REX_EV
) &&
1359 ((ins
->vexreg
> 15) || (ins
->evex_p
[0] & 0xf0))) {
1360 nasm_error(ERR_NONFATAL
, "invalid high-16 register in non-AVX-512");
1363 if (ins
->rex
& REX_EV
)
1365 else if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1366 ins
->prefixes
[PPS_VEX
] == P_VEX3
)
1370 } else if (ins
->rex
& REX_MASK
) {
1371 if (ins
->rex
& REX_H
) {
1372 nasm_error(ERR_NONFATAL
, "cannot use high register in rex instruction");
1374 } else if (bits
== 64) {
1376 } else if ((ins
->rex
& REX_L
) &&
1377 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1378 iflag_ffs(&cpu
) >= IF_X86_64
) {
1380 assert_no_prefix(ins
, PPS_LOCK
);
1381 lockcheck
= false; /* Already errored, no need for warning */
1384 nasm_error(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1389 if (has_prefix(ins
, PPS_LOCK
, P_LOCK
) && lockcheck
&&
1390 (!itemp_has(temp
,IF_LOCK
) || !is_class(MEMORY
, ins
->oprs
[0].type
))) {
1391 nasm_error(ERR_WARNING
| ERR_WARN_LOCK
| ERR_PASS2
,
1392 "instruction is not lockable");
1395 bad_hle_warn(ins
, hleok
);
1398 * when BND prefix is set by DEFAULT directive,
1399 * BND prefix is added to every appropriate instruction line
1400 * unless it is overridden by NOBND prefix.
1403 (itemp_has(temp
, IF_BND
) && !has_prefix(ins
, PPS_REP
, P_NOBND
)))
1404 ins
->prefixes
[PPS_REP
] = P_BND
;
1409 static inline unsigned int emit_rex(insn
*ins
, int32_t segment
, int64_t offset
, int bits
)
1412 if ((ins
->rex
& REX_MASK
) &&
1413 !(ins
->rex
& (REX_V
| REX_EV
)) &&
1415 int rex
= (ins
->rex
& REX_MASK
) | REX_P
;
1416 out(offset
, segment
, &rex
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1417 ins
->rex_done
= true;
1425 static void gencode(int32_t segment
, int64_t offset
, int bits
,
1426 insn
* ins
, const struct itemplate
*temp
,
1434 struct operand
*opx
;
1435 const uint8_t *codes
= temp
->code
;
1437 enum ea_type eat
= EA_SCALAR
;
1439 ins
->rex_done
= false;
1443 op1
= (c
& 3) + ((opex
& 1) << 2);
1444 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1445 opx
= &ins
->oprs
[op1
];
1446 opex
= 0; /* For the next iteration */
1453 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1454 out(offset
, segment
, codes
, OUT_RAWDATA
, c
, NO_SEG
, NO_SEG
);
1466 offset
+= emit_rex(ins
, segment
, offset
, bits
);
1467 bytes
[0] = *codes
++ + (regval(opx
) & 7);
1468 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1476 if (opx
->offset
< -256 || opx
->offset
> 255) {
1477 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1478 "byte value exceeds bounds");
1480 out_imm8(offset
, segment
, opx
, -1);
1485 if (opx
->offset
< 0 || opx
->offset
> 255)
1486 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1487 "unsigned byte value exceeds bounds");
1488 out_imm8(offset
, segment
, opx
, 1);
1493 warn_overflow_opd(opx
, 2);
1495 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1496 opx
->segment
, opx
->wrt
);
1501 if (opx
->type
& (BITS16
| BITS32
))
1502 size
= (opx
->type
& BITS16
) ? 2 : 4;
1504 size
= (bits
== 16) ? 2 : 4;
1505 warn_overflow_opd(opx
, size
);
1507 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1508 opx
->segment
, opx
->wrt
);
1513 warn_overflow_opd(opx
, 4);
1515 out(offset
, segment
, &data
, OUT_ADDRESS
, 4,
1516 opx
->segment
, opx
->wrt
);
1522 size
= ins
->addr_size
>> 3;
1523 warn_overflow_opd(opx
, size
);
1524 out(offset
, segment
, &data
, OUT_ADDRESS
, size
,
1525 opx
->segment
, opx
->wrt
);
1530 if (opx
->segment
!= segment
) {
1532 out(offset
, segment
, &data
,
1533 OUT_REL1ADR
, insn_end
- offset
,
1534 opx
->segment
, opx
->wrt
);
1536 data
= opx
->offset
- insn_end
;
1537 if (data
> 127 || data
< -128)
1538 nasm_error(ERR_NONFATAL
, "short jump is out of range");
1539 out(offset
, segment
, &data
,
1540 OUT_ADDRESS
, 1, NO_SEG
, NO_SEG
);
1546 data
= (int64_t)opx
->offset
;
1547 out(offset
, segment
, &data
, OUT_ADDRESS
, 8,
1548 opx
->segment
, opx
->wrt
);
1553 if (opx
->segment
!= segment
) {
1555 out(offset
, segment
, &data
,
1556 OUT_REL2ADR
, insn_end
- offset
,
1557 opx
->segment
, opx
->wrt
);
1559 data
= opx
->offset
- insn_end
;
1560 out(offset
, segment
, &data
,
1561 OUT_ADDRESS
, 2, NO_SEG
, NO_SEG
);
1567 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1568 size
= (opx
->type
& BITS16
) ? 2 : 4;
1570 size
= (bits
== 16) ? 2 : 4;
1571 if (opx
->segment
!= segment
) {
1573 out(offset
, segment
, &data
,
1574 size
== 2 ? OUT_REL2ADR
: OUT_REL4ADR
,
1575 insn_end
- offset
, opx
->segment
, opx
->wrt
);
1577 data
= opx
->offset
- insn_end
;
1578 out(offset
, segment
, &data
,
1579 OUT_ADDRESS
, size
, NO_SEG
, NO_SEG
);
1585 if (opx
->segment
!= segment
) {
1587 out(offset
, segment
, &data
,
1588 OUT_REL4ADR
, insn_end
- offset
,
1589 opx
->segment
, opx
->wrt
);
1591 data
= opx
->offset
- insn_end
;
1592 out(offset
, segment
, &data
,
1593 OUT_ADDRESS
, 4, NO_SEG
, NO_SEG
);
1599 if (opx
->segment
== NO_SEG
)
1600 nasm_error(ERR_NONFATAL
, "value referenced by FAR is not"
1603 out(offset
, segment
, &data
, OUT_ADDRESS
, 2,
1604 ofmt
->segbase(1 + opx
->segment
),
1611 opx
= &ins
->oprs
[c
>> 3];
1612 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1613 opx
= &ins
->oprs
[c
& 7];
1614 if (opx
->segment
!= NO_SEG
|| opx
->wrt
!= NO_SEG
) {
1615 nasm_error(ERR_NONFATAL
,
1616 "non-absolute expression not permitted as argument %d",
1619 if (opx
->offset
& ~15) {
1620 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1621 "four-bit argument exceeds bounds");
1623 bytes
[0] |= opx
->offset
& 15;
1625 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1631 opx
= &ins
->oprs
[c
>> 4];
1632 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1634 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1639 bytes
[0] = nasm_regvals
[opx
->basereg
] << 4;
1640 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1646 if (opx
->wrt
== NO_SEG
&& opx
->segment
== NO_SEG
&&
1647 (int32_t)data
!= (int64_t)data
) {
1648 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1649 "signed dword immediate exceeds bounds");
1651 out(offset
, segment
, &data
, OUT_ADDRESS
, -4,
1652 opx
->segment
, opx
->wrt
);
1659 ins
->evex_p
[2] |= op_evexflags(&ins
->oprs
[0],
1660 EVEX_P2Z
| EVEX_P2AAA
, 2);
1661 ins
->evex_p
[2] ^= EVEX_P2VP
; /* 1's complement */
1663 /* EVEX.X can be set by either REX or EVEX for different reasons */
1664 bytes
[1] = ((((ins
->rex
& 7) << 5) |
1665 (ins
->evex_p
[0] & (EVEX_P0X
| EVEX_P0RP
))) ^ 0xf0) |
1667 bytes
[2] = ((ins
->rex
& REX_W
) << (7 - 3)) |
1668 ((~ins
->vexreg
& 15) << 3) |
1669 (1 << 2) | (ins
->vex_wlp
& 3);
1670 bytes
[3] = ins
->evex_p
[2];
1671 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 4, NO_SEG
, NO_SEG
);
1678 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1679 ins
->prefixes
[PPS_VEX
] == P_VEX3
) {
1680 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1681 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1682 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1683 ((~ins
->vexreg
& 15)<< 3) | (ins
->vex_wlp
& 07);
1684 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 3, NO_SEG
, NO_SEG
);
1688 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1689 ((~ins
->vexreg
& 15) << 3) | (ins
->vex_wlp
& 07);
1690 out(offset
, segment
, &bytes
, OUT_RAWDATA
, 2, NO_SEG
, NO_SEG
);
1705 if (ins
->rex
& REX_W
)
1707 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1709 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1714 um
= (uint64_t)2 << (s
-1);
1717 if (uv
> 127 && uv
< (uint64_t)-128 &&
1718 (uv
< um
-128 || uv
> um
-1)) {
1719 /* If this wasn't explicitly byte-sized, warn as though we
1720 * had fallen through to the imm16/32/64 case.
1722 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1723 "%s value exceeds bounds",
1724 (opx
->type
& BITS8
) ? "signed byte" :
1729 if (opx
->segment
!= NO_SEG
) {
1731 out(offset
, segment
, &data
, OUT_ADDRESS
, 1,
1732 opx
->segment
, opx
->wrt
);
1735 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
,
1746 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
)) {
1748 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1755 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
)) {
1757 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1792 *bytes
= *codes
++ ^ get_cond_opcode(ins
->condition
);
1793 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1802 *bytes
= c
- 0332 + 0xF2;
1803 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1808 if (ins
->rex
& REX_R
) {
1810 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1813 ins
->rex
&= ~(REX_L
|REX_R
);
1824 if (ins
->oprs
[0].segment
!= NO_SEG
)
1825 nasm_panic(0, "non-constant BSS size in pass two");
1827 int64_t size
= ins
->oprs
[0].offset
;
1829 out(offset
, segment
, NULL
,
1830 OUT_RESERVE
, size
, NO_SEG
, NO_SEG
);
1843 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1853 *bytes
= c
- 0366 + 0x66;
1854 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1862 *bytes
= bits
== 16 ? 3 : 5;
1863 out(offset
, segment
, bytes
, OUT_RAWDATA
, 1, NO_SEG
, NO_SEG
);
1897 struct operand
*opy
= &ins
->oprs
[op2
];
1900 /* pick rfield from operand b (opx) */
1901 rflags
= regflag(opx
);
1902 rfield
= nasm_regvals
[opx
->basereg
];
1904 /* rfield is constant */
1909 if (process_ea(opy
, &ea_data
, bits
,
1910 rfield
, rflags
, ins
) != eat
)
1911 nasm_error(ERR_NONFATAL
, "invalid effective address");
1914 *p
++ = ea_data
.modrm
;
1915 if (ea_data
.sib_present
)
1919 out(offset
, segment
, bytes
, OUT_RAWDATA
, s
, NO_SEG
, NO_SEG
);
1922 * Make sure the address gets the right offset in case
1923 * the line breaks in the .lst file (BR 1197827)
1928 if (ea_data
.bytes
) {
1929 /* use compressed displacement, if available */
1930 data
= ea_data
.disp8
? ea_data
.disp8
: opy
->offset
;
1933 if (opy
->segment
== segment
) {
1935 if (overflow_signed(data
, ea_data
.bytes
))
1936 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1937 out(offset
, segment
, &data
, OUT_ADDRESS
,
1938 ea_data
.bytes
, NO_SEG
, NO_SEG
);
1940 /* overflow check in output/linker? */
1941 out(offset
, segment
, &data
, OUT_REL4ADR
,
1942 insn_end
- offset
, opy
->segment
, opy
->wrt
);
1945 int asize
= ins
->addr_size
>> 3;
1946 int atype
= ea_data
.bytes
;
1948 if (overflow_general(data
, asize
) ||
1949 signed_bits(data
, ins
->addr_size
) !=
1950 signed_bits(data
, ea_data
.bytes
<< 3))
1951 warn_overflow(ERR_PASS2
, ea_data
.bytes
);
1953 if (asize
> ea_data
.bytes
) {
1955 * If the address isn't the full width of
1956 * the address size, treat is as signed...
1961 out(offset
, segment
, &data
, OUT_ADDRESS
,
1962 atype
, opy
->segment
, opy
->wrt
);
1970 nasm_panic(0, "internal instruction table corrupt"
1971 ": instruction code \\%o (0x%02X) given", c
, c
);
1977 static opflags_t
regflag(const operand
* o
)
1979 if (!is_register(o
->basereg
))
1980 nasm_panic(0, "invalid operand passed to regflag()");
1981 return nasm_reg_flags
[o
->basereg
];
1984 static int32_t regval(const operand
* o
)
1986 if (!is_register(o
->basereg
))
1987 nasm_panic(0, "invalid operand passed to regval()");
1988 return nasm_regvals
[o
->basereg
];
1991 static int op_rexflags(const operand
* o
, int mask
)
1996 if (!is_register(o
->basereg
))
1997 nasm_panic(0, "invalid operand passed to op_rexflags()");
1999 flags
= nasm_reg_flags
[o
->basereg
];
2000 val
= nasm_regvals
[o
->basereg
];
2002 return rexflags(val
, flags
, mask
);
2005 static int rexflags(int val
, opflags_t flags
, int mask
)
2009 if (val
>= 0 && (val
& 8))
2010 rex
|= REX_B
|REX_X
|REX_R
;
2013 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
2015 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
2021 static int evexflags(int val
, decoflags_t deco
,
2022 int mask
, uint8_t byte
)
2028 if (val
>= 0 && (val
& 16))
2029 evex
|= (EVEX_P0RP
| EVEX_P0X
);
2032 if (val
>= 0 && (val
& 16))
2036 if (deco
& OPMASK_MASK
)
2037 evex
|= deco
& EVEX_P2AAA
;
2043 static int op_evexflags(const operand
* o
, int mask
, uint8_t byte
)
2047 val
= nasm_regvals
[o
->basereg
];
2049 return evexflags(val
, o
->decoflags
, mask
, byte
);
2052 static enum match_result
find_match(const struct itemplate
**tempp
,
2054 int32_t segment
, int64_t offset
, int bits
)
2056 const struct itemplate
*temp
;
2057 enum match_result m
, merr
;
2058 opflags_t xsizeflags
[MAX_OPERANDS
];
2059 bool opsizemissing
= false;
2060 int8_t broadcast
= instruction
->evex_brerop
;
2063 /* broadcasting uses a different data element size */
2064 for (i
= 0; i
< instruction
->operands
; i
++)
2066 xsizeflags
[i
] = instruction
->oprs
[i
].decoflags
& BRSIZE_MASK
;
2068 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
2070 merr
= MERR_INVALOP
;
2072 for (temp
= nasm_instructions
[instruction
->opcode
];
2073 temp
->opcode
!= I_none
; temp
++) {
2074 m
= matches(temp
, instruction
, bits
);
2075 if (m
== MOK_JUMP
) {
2076 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2080 } else if (m
== MERR_OPSIZEMISSING
&& !itemp_has(temp
, IF_SX
)) {
2082 * Missing operand size and a candidate for fuzzy matching...
2084 for (i
= 0; i
< temp
->operands
; i
++)
2086 xsizeflags
[i
] |= temp
->deco
[i
] & BRSIZE_MASK
;
2088 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
2089 opsizemissing
= true;
2093 if (merr
== MOK_GOOD
)
2097 /* No match, but see if we can get a fuzzy operand size match... */
2101 for (i
= 0; i
< instruction
->operands
; i
++) {
2103 * We ignore extrinsic operand sizes on registers, so we should
2104 * never try to fuzzy-match on them. This also resolves the case
2105 * when we have e.g. "xmmrm128" in two different positions.
2107 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
2110 /* This tests if xsizeflags[i] has more than one bit set */
2111 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
2112 goto done
; /* No luck */
2114 if (i
== broadcast
) {
2115 instruction
->oprs
[i
].decoflags
|= xsizeflags
[i
];
2116 instruction
->oprs
[i
].type
|= (xsizeflags
[i
] == BR_BITS32
?
2119 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
2123 /* Try matching again... */
2124 for (temp
= nasm_instructions
[instruction
->opcode
];
2125 temp
->opcode
!= I_none
; temp
++) {
2126 m
= matches(temp
, instruction
, bits
);
2127 if (m
== MOK_JUMP
) {
2128 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2135 if (merr
== MOK_GOOD
)
2144 static uint8_t get_broadcast_num(opflags_t opflags
, opflags_t brsize
)
2146 opflags_t opsize
= opflags
& SIZE_MASK
;
2150 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2151 * this cannot be a simple arithmetic calculation.
2153 if (brsize
> BITS64
)
2154 nasm_error(ERR_FATAL
,
2155 "size of broadcasting element is greater than 64 bits");
2159 brcast_num
= BITS64
/ brsize
;
2162 brcast_num
= (opsize
/ BITS128
) * (BITS64
/ brsize
) * 2;
2169 static enum match_result
matches(const struct itemplate
*itemp
,
2170 insn
*instruction
, int bits
)
2172 opflags_t size
[MAX_OPERANDS
], asize
;
2173 bool opsizemissing
= false;
2179 if (itemp
->opcode
!= instruction
->opcode
)
2180 return MERR_INVALOP
;
2183 * Count the operands
2185 if (itemp
->operands
!= instruction
->operands
)
2186 return MERR_INVALOP
;
2191 if (!(optimizing
> 0) && itemp_has(itemp
, IF_OPT
))
2192 return MERR_INVALOP
;
2197 switch (instruction
->prefixes
[PPS_VEX
]) {
2199 if (!itemp_has(itemp
, IF_EVEX
))
2200 return MERR_ENCMISMATCH
;
2204 if (!itemp_has(itemp
, IF_VEX
))
2205 return MERR_ENCMISMATCH
;
2212 * Check that no spurious colons or TOs are present
2214 for (i
= 0; i
< itemp
->operands
; i
++)
2215 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2216 return MERR_INVALOP
;
2219 * Process size flags
2221 switch (itemp_smask(itemp
)) {
2222 case IF_GENBIT(IF_SB
):
2225 case IF_GENBIT(IF_SW
):
2228 case IF_GENBIT(IF_SD
):
2231 case IF_GENBIT(IF_SQ
):
2234 case IF_GENBIT(IF_SO
):
2237 case IF_GENBIT(IF_SY
):
2240 case IF_GENBIT(IF_SZ
):
2243 case IF_GENBIT(IF_SIZE
):
2264 if (itemp_armask(itemp
)) {
2265 /* S- flags only apply to a specific operand */
2266 i
= itemp_arg(itemp
);
2267 memset(size
, 0, sizeof size
);
2270 /* S- flags apply to all operands */
2271 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2276 * Check that the operand flags all match up,
2277 * it's a bit tricky so lets be verbose:
2279 * 1) Find out the size of operand. If instruction
2280 * doesn't have one specified -- we're trying to
2281 * guess it either from template (IF_S* flag) or
2284 * 2) If template operand do not match the instruction OR
2285 * template has an operand size specified AND this size differ
2286 * from which instruction has (perhaps we got it from code bits)
2288 * a) Check that only size of instruction and operand is differ
2289 * other characteristics do match
2290 * b) Perhaps it's a register specified in instruction so
2291 * for such a case we just mark that operand as "size
2292 * missing" and this will turn on fuzzy operand size
2293 * logic facility (handled by a caller)
2295 for (i
= 0; i
< itemp
->operands
; i
++) {
2296 opflags_t type
= instruction
->oprs
[i
].type
;
2297 decoflags_t deco
= instruction
->oprs
[i
].decoflags
;
2298 bool is_broadcast
= deco
& BRDCAST_MASK
;
2299 uint8_t brcast_num
= 0;
2300 opflags_t template_opsize
, insn_opsize
;
2302 if (!(type
& SIZE_MASK
))
2305 insn_opsize
= type
& SIZE_MASK
;
2306 if (!is_broadcast
) {
2307 template_opsize
= itemp
->opd
[i
] & SIZE_MASK
;
2309 decoflags_t deco_brsize
= itemp
->deco
[i
] & BRSIZE_MASK
;
2311 * when broadcasting, the element size depends on
2312 * the instruction type. decorator flag should match.
2316 template_opsize
= (deco_brsize
== BR_BITS32
? BITS32
: BITS64
);
2317 /* calculate the proper number : {1to<brcast_num>} */
2318 brcast_num
= get_broadcast_num(itemp
->opd
[i
], template_opsize
);
2320 template_opsize
= 0;
2324 if ((itemp
->opd
[i
] & ~type
& ~SIZE_MASK
) ||
2325 (deco
& ~itemp
->deco
[i
] & ~BRNUM_MASK
)) {
2326 return MERR_INVALOP
;
2327 } else if (template_opsize
) {
2328 if (template_opsize
!= insn_opsize
) {
2330 return MERR_INVALOP
;
2331 } else if (!is_class(REGISTER
, type
)) {
2333 * Note: we don't honor extrinsic operand sizes for registers,
2334 * so "missing operand size" for a register should be
2335 * considered a wildcard match rather than an error.
2337 opsizemissing
= true;
2339 } else if (is_broadcast
&&
2341 (2U << ((deco
& BRNUM_MASK
) >> BRNUM_SHIFT
)))) {
2343 * broadcasting opsize matches but the number of repeated memory
2344 * element does not match.
2345 * if 64b double precision float is broadcasted to ymm (256b),
2346 * broadcasting decorator must be {1to4}.
2348 return MERR_BRNUMMISMATCH
;
2354 return MERR_OPSIZEMISSING
;
2357 * Check operand sizes
2359 if (itemp_has(itemp
, IF_SM
) || itemp_has(itemp
, IF_SM2
)) {
2360 oprs
= (itemp_has(itemp
, IF_SM2
) ? 2 : itemp
->operands
);
2361 for (i
= 0; i
< oprs
; i
++) {
2362 asize
= itemp
->opd
[i
] & SIZE_MASK
;
2364 for (i
= 0; i
< oprs
; i
++)
2370 oprs
= itemp
->operands
;
2373 for (i
= 0; i
< itemp
->operands
; i
++) {
2374 if (!(itemp
->opd
[i
] & SIZE_MASK
) &&
2375 (instruction
->oprs
[i
].type
& SIZE_MASK
& ~size
[i
]))
2376 return MERR_OPSIZEMISMATCH
;
2380 * Check template is okay at the set cpu level
2382 if (iflag_cmp_cpu_level(&insns_flags
[itemp
->iflag_idx
], &cpu
) > 0)
2386 * Verify the appropriate long mode flag.
2388 if (itemp_has(itemp
, (bits
== 64 ? IF_NOLONG
: IF_LONG
)))
2389 return MERR_BADMODE
;
2392 * If we have a HLE prefix, look for the NOHLE flag
2394 if (itemp_has(itemp
, IF_NOHLE
) &&
2395 (has_prefix(instruction
, PPS_REP
, P_XACQUIRE
) ||
2396 has_prefix(instruction
, PPS_REP
, P_XRELEASE
)))
2400 * Check if special handling needed for Jumps
2402 if ((itemp
->code
[0] & ~1) == 0370)
2406 * Check if BND prefix is allowed.
2407 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
2409 if (!itemp_has(itemp
, IF_BND
) &&
2410 (has_prefix(instruction
, PPS_REP
, P_BND
) ||
2411 has_prefix(instruction
, PPS_REP
, P_NOBND
)))
2413 else if (itemp_has(itemp
, IF_BND
) &&
2414 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ||
2415 has_prefix(instruction
, PPS_REP
, P_REPNZ
)))
2416 return MERR_BADREPNE
;
2422 * Check if ModR/M.mod should/can be 01.
2423 * - EAF_BYTEOFFS is set
2424 * - offset can fit in a byte when EVEX is not used
2425 * - offset can be compressed when EVEX is used
2427 #define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2428 (o >= -128 && o <= 127 && \
2429 seg == NO_SEG && !forw_ref && \
2430 !(input->eaflags & EAF_WORDOFFS) && \
2431 !(ins->rex & REX_EV)) || \
2432 (ins->rex & REX_EV && \
2433 is_disp8n(input, ins, &output->disp8)))
2435 static enum ea_type
process_ea(operand
*input
, ea
*output
, int bits
,
2436 int rfield
, opflags_t rflags
, insn
*ins
)
2438 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2439 int addrbits
= ins
->addr_size
;
2440 int eaflags
= input
->eaflags
;
2442 output
->type
= EA_SCALAR
;
2443 output
->rip
= false;
2446 /* REX flags for the rfield operand */
2447 output
->rex
|= rexflags(rfield
, rflags
, REX_R
| REX_P
| REX_W
| REX_H
);
2448 /* EVEX.R' flag for the REG operand */
2449 ins
->evex_p
[0] |= evexflags(rfield
, 0, EVEX_P0RP
, 0);
2451 if (is_class(REGISTER
, input
->type
)) {
2453 * It's a direct register.
2455 if (!is_register(input
->basereg
))
2458 if (!is_reg_class(REG_EA
, input
->basereg
))
2461 /* broadcasting is not available with a direct register operand. */
2462 if (input
->decoflags
& BRDCAST_MASK
) {
2463 nasm_error(ERR_NONFATAL
, "Broadcasting not allowed from a register");
2467 output
->rex
|= op_rexflags(input
, REX_B
| REX_P
| REX_W
| REX_H
);
2468 ins
->evex_p
[0] |= op_evexflags(input
, EVEX_P0X
, 0);
2469 output
->sib_present
= false; /* no SIB necessary */
2470 output
->bytes
= 0; /* no offset necessary either */
2471 output
->modrm
= GEN_MODRM(3, rfield
, nasm_regvals
[input
->basereg
]);
2474 * It's a memory reference.
2477 /* Embedded rounding or SAE is not available with a mem ref operand. */
2478 if (input
->decoflags
& (ER
| SAE
)) {
2479 nasm_error(ERR_NONFATAL
,
2480 "Embedded rounding is available only with reg-reg op.");
2484 if (input
->basereg
== -1 &&
2485 (input
->indexreg
== -1 || input
->scale
== 0)) {
2487 * It's a pure offset.
2489 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
) &&
2490 input
->segment
== NO_SEG
) {
2491 nasm_error(ERR_WARNING
| ERR_PASS1
, "absolute address can not be RIP-relative");
2492 input
->type
&= ~IP_REL
;
2493 input
->type
|= MEMORY
;
2497 !(IP_REL
& ~input
->type
) && (eaflags
& EAF_MIB
)) {
2498 nasm_error(ERR_NONFATAL
, "RIP-relative addressing is prohibited for mib.");
2502 if (eaflags
& EAF_BYTEOFFS
||
2503 (eaflags
& EAF_WORDOFFS
&&
2504 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16))) {
2505 nasm_error(ERR_WARNING
| ERR_PASS1
, "displacement size ignored on absolute address");
2508 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2509 output
->sib_present
= true;
2510 output
->sib
= GEN_SIB(0, 4, 5);
2512 output
->modrm
= GEN_MODRM(0, rfield
, 4);
2513 output
->rip
= false;
2515 output
->sib_present
= false;
2516 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2517 output
->modrm
= GEN_MODRM(0, rfield
, (addrbits
!= 16 ? 5 : 6));
2518 output
->rip
= bits
== 64;
2522 * It's an indirection.
2524 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2525 int32_t seg
= input
->segment
;
2526 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2527 int t
, it
, bt
; /* register numbers */
2528 opflags_t x
, ix
, bx
; /* register flags */
2531 i
= -1; /* make this easy, at least */
2533 if (is_register(i
)) {
2534 it
= nasm_regvals
[i
];
2535 ix
= nasm_reg_flags
[i
];
2541 if (is_register(b
)) {
2542 bt
= nasm_regvals
[b
];
2543 bx
= nasm_reg_flags
[b
];
2549 /* if either one are a vector register... */
2550 if ((ix
|bx
) & (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
) {
2551 opflags_t sok
= BITS32
| BITS64
;
2552 int32_t o
= input
->offset
;
2553 int mod
, scale
, index
, base
;
2556 * For a vector SIB, one has to be a vector and the other,
2557 * if present, a GPR. The vector must be the index operand.
2559 if (it
== -1 || (bx
& (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
)) {
2565 t
= bt
, bt
= it
, it
= t
;
2566 x
= bx
, bx
= ix
, ix
= x
;
2572 if (!(REG64
& ~bx
) || !(REG32
& ~bx
))
2579 * While we're here, ensure the user didn't specify
2582 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2585 if (addrbits
== 16 ||
2586 (addrbits
== 32 && !(sok
& BITS32
)) ||
2587 (addrbits
== 64 && !(sok
& BITS64
)))
2590 output
->type
= ((ix
& ZMMREG
& ~REG_EA
) ? EA_ZMMVSIB
2591 : ((ix
& YMMREG
& ~REG_EA
)
2592 ? EA_YMMVSIB
: EA_XMMVSIB
));
2594 output
->rex
|= rexflags(it
, ix
, REX_X
);
2595 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2596 ins
->evex_p
[2] |= evexflags(it
, 0, EVEX_P2VP
, 2);
2598 index
= it
& 7; /* it is known to be != -1 */
2613 default: /* then what the smeg is it? */
2614 goto err
; /* panic */
2622 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2623 seg
== NO_SEG
&& !forw_ref
&&
2624 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2626 else if (IS_MOD_01())
2632 output
->sib_present
= true;
2633 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2634 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2635 output
->sib
= GEN_SIB(scale
, index
, base
);
2636 } else if ((ix
|bx
) & (BITS32
|BITS64
)) {
2638 * it must be a 32/64-bit memory reference. Firstly we have
2639 * to check that all registers involved are type E/Rxx.
2641 opflags_t sok
= BITS32
| BITS64
;
2642 int32_t o
= input
->offset
;
2645 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2653 goto err
; /* Invalid register */
2654 if (~sok
& bx
& SIZE_MASK
)
2655 goto err
; /* Invalid size */
2660 * While we're here, ensure the user didn't specify
2663 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2666 if (addrbits
== 16 ||
2667 (addrbits
== 32 && !(sok
& BITS32
)) ||
2668 (addrbits
== 64 && !(sok
& BITS64
)))
2671 /* now reorganize base/index */
2672 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2673 ((hb
== b
&& ht
== EAH_NOTBASE
) ||
2674 (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2675 /* swap if hints say so */
2676 t
= bt
, bt
= it
, it
= t
;
2677 x
= bx
, bx
= ix
, ix
= x
;
2680 if (bt
== -1 && s
== 1 && !(hb
== i
&& ht
== EAH_NOTBASE
)) {
2681 /* make single reg base, unless hint */
2682 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2684 if (eaflags
& EAF_MIB
) {
2685 /* only for mib operands */
2686 if (it
== -1 && (hb
== b
&& ht
== EAH_NOTBASE
)) {
2688 * make a single reg index [reg*1].
2689 * gas uses this form for an explicit index register.
2691 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2693 if ((ht
== EAH_SUMMED
) && bt
== -1) {
2694 /* separate once summed index into [base, index] */
2695 bt
= it
, bx
= ix
, s
--;
2698 if (((s
== 2 && it
!= REG_NUM_ESP
&&
2699 (!(eaflags
& EAF_TIMESTWO
) || (ht
== EAH_SUMMED
))) ||
2700 s
== 3 || s
== 5 || s
== 9) && bt
== -1) {
2701 /* convert 3*EAX to EAX+2*EAX */
2702 bt
= it
, bx
= ix
, s
--;
2704 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
&&
2705 (eaflags
& EAF_TIMESTWO
) &&
2706 (hb
== b
&& ht
== EAH_NOTBASE
)) {
2708 * convert [NOSPLIT EAX*1]
2709 * to sib format with 0x0 displacement - [EAX*1+0].
2711 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2714 if (s
== 1 && it
== REG_NUM_ESP
) {
2715 /* swap ESP into base if scale is 1 */
2716 t
= it
, it
= bt
, bt
= t
;
2717 x
= ix
, ix
= bx
, bx
= x
;
2719 if (it
== REG_NUM_ESP
||
2720 (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2721 goto err
; /* wrong, for various reasons */
2723 output
->rex
|= rexflags(it
, ix
, REX_X
);
2724 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2726 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2735 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2736 seg
== NO_SEG
&& !forw_ref
&&
2737 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2739 else if (IS_MOD_01())
2745 output
->sib_present
= false;
2746 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2747 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2750 int mod
, scale
, index
, base
;
2770 default: /* then what the smeg is it? */
2771 goto err
; /* panic */
2779 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2780 seg
== NO_SEG
&& !forw_ref
&&
2781 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2783 else if (IS_MOD_01())
2789 output
->sib_present
= true;
2790 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2791 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2792 output
->sib
= GEN_SIB(scale
, index
, base
);
2794 } else { /* it's 16-bit */
2796 int16_t o
= input
->offset
;
2798 /* check for 64-bit long mode */
2802 /* check all registers are BX, BP, SI or DI */
2803 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
&& b
!= R_DI
) ||
2804 (i
!= -1 && i
!= R_BP
&& i
!= R_BX
&& i
!= R_SI
&& i
!= R_DI
))
2807 /* ensure the user didn't specify DWORD/QWORD */
2808 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2811 if (s
!= 1 && i
!= -1)
2812 goto err
; /* no can do, in 16-bit EA */
2813 if (b
== -1 && i
!= -1) {
2818 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2823 /* have BX/BP as base, SI/DI index */
2825 goto err
; /* shouldn't ever happen, in theory */
2826 if (i
!= -1 && b
!= -1 &&
2827 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2828 goto err
; /* invalid combinations */
2829 if (b
== -1) /* pure offset: handled above */
2830 goto err
; /* so if it gets to here, panic! */
2834 switch (i
* 256 + b
) {
2835 case R_SI
* 256 + R_BX
:
2838 case R_DI
* 256 + R_BX
:
2841 case R_SI
* 256 + R_BP
:
2844 case R_DI
* 256 + R_BP
:
2862 if (rm
== -1) /* can't happen, in theory */
2863 goto err
; /* so panic if it does */
2865 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2866 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2868 else if (IS_MOD_01())
2873 output
->sib_present
= false; /* no SIB - it's 16-bit */
2874 output
->bytes
= mod
; /* bytes of offset needed */
2875 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2880 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2881 return output
->type
;
2884 return output
->type
= EA_INVALID
;
2887 static void add_asp(insn
*ins
, int addrbits
)
2892 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2894 switch (ins
->prefixes
[PPS_ASIZE
]) {
2905 valid
&= (addrbits
== 32) ? 16 : 32;
2911 for (j
= 0; j
< ins
->operands
; j
++) {
2912 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
2915 /* Verify as Register */
2916 if (!is_register(ins
->oprs
[j
].indexreg
))
2919 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2921 /* Verify as Register */
2922 if (!is_register(ins
->oprs
[j
].basereg
))
2925 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2927 if (ins
->oprs
[j
].scale
== 0)
2931 int ds
= ins
->oprs
[j
].disp_size
;
2932 if ((addrbits
!= 64 && ds
> 8) ||
2933 (addrbits
== 64 && ds
== 16))
2953 if (valid
& addrbits
) {
2954 ins
->addr_size
= addrbits
;
2955 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2956 /* Add an address size prefix */
2957 ins
->prefixes
[PPS_ASIZE
] = (addrbits
== 32) ? P_A16
: P_A32
;;
2958 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2961 nasm_error(ERR_NONFATAL
, "impossible combination of address sizes");
2962 ins
->addr_size
= addrbits
; /* Error recovery */
2965 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2967 for (j
= 0; j
< ins
->operands
; j
++) {
2968 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2969 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
) != ins
->addr_size
) {
2971 * mem_offs sizes must match the address size; if not,
2972 * strip the MEM_OFFS bit and match only EA instructions
2974 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);