1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 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 mmmm is the M field (EVEX.P0[3:0])
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"
187 #include "assemble.h"
195 * Matching errors. These should be sorted so that more specific
196 * errors come later in the sequence.
212 * Matching success; the conditional ones first
214 MOK_JUMP
, /* Matching OK but needs jmp_match() */
215 MOK_GOOD
/* Matching unconditionally OK */
219 enum ea_type type
; /* what kind of EA is this? */
220 int sib_present
; /* is a SIB byte necessary? */
221 int bytes
; /* # of bytes of offset needed */
222 int size
; /* lazy - this is sib+bytes+1 */
223 uint8_t modrm
, sib
, rex
, rip
; /* the bytes themselves */
224 int8_t disp8
; /* compressed displacement for EVEX */
227 #define GEN_SIB(scale, index, base) \
228 (((scale) << 6) | ((index) << 3) | ((base)))
230 #define GEN_MODRM(mod, reg, rm) \
231 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
233 static int64_t calcsize(int32_t, int64_t, int, insn
*,
234 const struct itemplate
*);
235 static int emit_prefix(struct out_data
*data
, const int bits
, insn
*ins
);
236 static void gencode(struct out_data
*data
, insn
*ins
);
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,
249 opflags_t
, insn
*, const char **);
251 static inline bool absolute_op(const struct operand
*o
)
253 return o
->segment
== NO_SEG
&& o
->wrt
== NO_SEG
&&
254 !(o
->opflags
& OPFLAG_RELATIVE
);
257 static int has_prefix(insn
* ins
, enum prefix_pos pos
, int prefix
)
259 return ins
->prefixes
[pos
] == prefix
;
262 static void assert_no_prefix(insn
* ins
, enum prefix_pos pos
)
264 if (ins
->prefixes
[pos
])
265 nasm_error(ERR_NONFATAL
, "invalid %s prefix",
266 prefix_name(ins
->prefixes
[pos
]));
269 static const char *size_name(int size
)
293 static void warn_overflow(int size
)
295 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
296 "%s data exceeds bounds", size_name(size
));
299 static void warn_overflow_const(int64_t data
, int size
)
301 if (overflow_general(data
, size
))
305 static void warn_overflow_out(int64_t data
, int size
, enum out_sign sign
)
311 err
= overflow_general(data
, size
);
314 err
= overflow_signed(data
, size
);
317 err
= overflow_unsigned(data
, size
);
329 * This routine wrappers the real output format's output routine,
330 * in order to pass a copy of the data off to the listing file
331 * generator at the same time, flatten unnecessary relocations,
332 * and verify backend compatibility.
334 static void out(struct out_data
*data
)
336 static int32_t lineno
= 0; /* static!!! */
337 static const char *lnfname
= NULL
;
343 uint64_t zeropad
= 0;
345 int32_t fixseg
; /* Segment for which to produce fixed data */
348 return; /* Nothing to do */
351 * Convert addresses to RAWDATA if possible
352 * XXX: not all backends want this for global symbols!!!!
354 switch (data
->type
) {
356 addrval
= data
->toffset
;
357 fixseg
= NO_SEG
; /* Absolute address is fixed data */
361 addrval
= data
->toffset
- data
->relbase
;
362 fixseg
= data
->segment
; /* Our own segment is fixed data */
366 nasm_assert(data
->size
<= 8);
368 amax
= ofmt
->maxbits
>> 3; /* Maximum address size in bytes */
369 if (data
->tsegment
== fixseg
&& data
->twrt
== NO_SEG
) {
370 warn_overflow_out(addrval
, asize
, data
->sign
);
371 xdata
.q
= cpu_to_le64(addrval
);
372 data
->data
= xdata
.b
;
373 data
->type
= OUT_RAWDATA
;
374 asize
= amax
= 0; /* No longer an address */
379 nasm_assert(data
->size
<= 8);
385 asize
= amax
= 0; /* Not an address */
390 * this call to src_get determines when we call the
391 * debug-format-specific "linenum" function
392 * it updates lineno and lnfname to the current values
393 * returning 0 if "same as last time", -2 if lnfname
394 * changed, and the amount by which lineno changed,
395 * if it did. thus, these variables must be static
398 if (src_get(&lineno
, &lnfname
))
399 dfmt
->linenum(lnfname
, lineno
, data
->segment
);
402 if (data
->type
== OUT_RELADDR
|| data
->sign
== OUT_SIGNED
) {
403 nasm_error(ERR_NONFATAL
,
404 "%u-bit signed relocation unsupported by output format %s",
405 (unsigned int)(asize
<< 3), ofmt
->shortname
);
407 nasm_error(ERR_WARNING
| ERR_WARN_ZEXTRELOC
,
408 "%u-bit %s relocation zero-extended from %u bits",
409 (unsigned int)(asize
<< 3),
410 data
->type
== OUT_SEGMENT
? "segment" : "unsigned",
411 (unsigned int)(amax
<< 3));
413 zeropad
= data
->size
- amax
;
418 data
->offset
+= data
->size
;
419 data
->insoffs
+= data
->size
;
422 data
->type
= OUT_ZERODATA
;
423 data
->size
= zeropad
;
426 data
->offset
+= zeropad
;
427 data
->insoffs
+= zeropad
;
428 data
->size
+= zeropad
; /* Restore original size value */
432 static inline void out_rawdata(struct out_data
*data
, const void *rawdata
,
435 data
->type
= OUT_RAWDATA
;
436 data
->data
= rawdata
;
441 static void out_rawbyte(struct out_data
*data
, uint8_t byte
)
443 data
->type
= OUT_RAWDATA
;
449 static inline void out_reserve(struct out_data
*data
, uint64_t size
)
451 data
->type
= OUT_RESERVE
;
456 static void out_segment(struct out_data
*data
, const struct operand
*opx
)
458 if (opx
->opflags
& OPFLAG_RELATIVE
)
459 nasm_error(ERR_NONFATAL
, "segment references cannot be relative");
461 data
->type
= OUT_SEGMENT
;
462 data
->sign
= OUT_UNSIGNED
;
464 data
->toffset
= opx
->offset
;
465 data
->tsegment
= ofmt
->segbase(opx
->segment
| 1);
466 data
->twrt
= opx
->wrt
;
470 static void out_imm(struct out_data
*data
, const struct operand
*opx
,
471 int size
, enum out_sign sign
)
473 if (opx
->segment
!= NO_SEG
&& (opx
->segment
& 1)) {
475 * This is actually a segment reference, but eval() has
476 * already called ofmt->segbase() for us. Sigh.
479 nasm_error(ERR_NONFATAL
, "segment reference must be 16 bits");
481 data
->type
= OUT_SEGMENT
;
483 data
->type
= (opx
->opflags
& OPFLAG_RELATIVE
)
484 ? OUT_RELADDR
: OUT_ADDRESS
;
487 data
->toffset
= opx
->offset
;
488 data
->tsegment
= opx
->segment
;
489 data
->twrt
= opx
->wrt
;
491 * XXX: improve this if at some point in the future we can
492 * distinguish the subtrahend in expressions like [foo - bar]
493 * where bar is a symbol in the current segment. However, at the
494 * current point, if OPFLAG_RELATIVE is set that subtraction has
502 static void out_reladdr(struct out_data
*data
, const struct operand
*opx
,
505 if (opx
->opflags
& OPFLAG_RELATIVE
)
506 nasm_error(ERR_NONFATAL
, "invalid use of self-relative expression");
508 data
->type
= OUT_RELADDR
;
509 data
->sign
= OUT_SIGNED
;
511 data
->toffset
= opx
->offset
;
512 data
->tsegment
= opx
->segment
;
513 data
->twrt
= opx
->wrt
;
514 data
->relbase
= data
->offset
+ (data
->inslen
- data
->insoffs
);
518 static bool jmp_match(int32_t segment
, int64_t offset
, int bits
,
519 insn
* ins
, const struct itemplate
*temp
)
522 const uint8_t *code
= temp
->code
;
526 if (((c
& ~1) != 0370) || (ins
->oprs
[0].type
& STRICT
))
530 if (optimizing
< 0 && c
== 0371)
533 isize
= calcsize(segment
, offset
, bits
, ins
, temp
);
535 if (ins
->oprs
[0].opflags
& OPFLAG_UNKNOWN
)
536 /* Be optimistic in pass 1 */
539 if (ins
->oprs
[0].segment
!= segment
)
542 isize
= ins
->oprs
[0].offset
- offset
- isize
; /* isize is delta */
543 is_byte
= (isize
>= -128 && isize
<= 127); /* is it byte size? */
545 if (is_byte
&& c
== 0371 && ins
->prefixes
[PPS_REP
] == P_BND
) {
546 /* jmp short (opcode eb) cannot be used with bnd prefix. */
547 ins
->prefixes
[PPS_REP
] = P_none
;
548 nasm_error(ERR_WARNING
| ERR_WARN_BND
| ERR_PASS2
,
549 "jmp short does not init bnd regs - bnd prefix dropped.");
555 /* This is totally just a wild guess what is reasonable... */
556 #define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
558 int64_t assemble(int32_t segment
, int64_t start
, int bits
, insn
*instruction
)
560 struct out_data data
;
561 const struct itemplate
*temp
;
563 int64_t wsize
; /* size for DB etc. */
567 data
.segment
= segment
;
571 wsize
= db_bytes(instruction
->opcode
);
578 list_for_each(e
, instruction
->eops
) {
579 if (e
->type
== EOT_DB_NUMBER
) {
581 nasm_error(ERR_NONFATAL
,
582 "integer supplied to a DT, DO, DY or DZ"
586 data
.inslen
= data
.size
= wsize
;
587 data
.toffset
= e
->offset
;
590 if (e
->segment
!= NO_SEG
&& (e
->segment
& 1)) {
591 data
.tsegment
= e
->segment
;
592 data
.type
= OUT_SEGMENT
;
593 data
.sign
= OUT_UNSIGNED
;
595 data
.tsegment
= e
->segment
;
596 data
.type
= e
->relative
? OUT_RELADDR
: OUT_ADDRESS
;
597 data
.sign
= OUT_WRAP
;
601 } else if (e
->type
== EOT_DB_STRING
||
602 e
->type
== EOT_DB_STRING_FREE
) {
603 int align
= e
->stringlen
% wsize
;
605 align
= wsize
- align
;
608 data
.inslen
= e
->stringlen
+ align
;
610 out_rawdata(&data
, e
->stringval
, e
->stringlen
);
611 out_rawdata(&data
, zero_buffer
, align
);
614 } else if (instruction
->opcode
== I_INCBIN
) {
615 const char *fname
= instruction
->eops
->stringval
;
617 size_t t
= instruction
->times
; /* INCBIN handles TIMES by itself */
620 const void *map
= NULL
;
622 size_t blk
= 0; /* Buffered I/O block size */
623 size_t m
= 0; /* Bytes last read */
628 fp
= nasm_open_read(fname
, NF_BINARY
|NF_FORMAP
);
630 nasm_error(ERR_NONFATAL
, "`incbin': unable to open file `%s'",
635 len
= nasm_file_size(fp
);
637 if (len
== (off_t
)-1) {
638 nasm_error(ERR_NONFATAL
, "`incbin': unable to get length of file `%s'",
643 if (instruction
->eops
->next
) {
644 base
= instruction
->eops
->next
->offset
;
649 if (instruction
->eops
->next
->next
&&
650 len
> (off_t
)instruction
->eops
->next
->next
->offset
)
651 len
= (off_t
)instruction
->eops
->next
->next
->offset
;
655 lfmt
->set_offset(data
.offset
);
656 lfmt
->uplevel(LIST_INCBIN
);
661 /* Try to map file data */
662 map
= nasm_map_file(fp
, base
, len
);
664 blk
= len
< (off_t
)INCBIN_MAX_BUF
? (size_t)len
: INCBIN_MAX_BUF
;
665 buf
= nasm_malloc(blk
);
670 * Consider these irrelevant for INCBIN, since it is fully
671 * possible that these might be (way) bigger than an int
672 * can hold; there is, however, no reason to widen these
673 * types just for INCBIN. data.inslen == 0 signals to the
674 * backend that these fields are meaningless, if at all
681 out_rawdata(&data
, map
, len
);
682 } else if ((off_t
)m
== len
) {
683 out_rawdata(&data
, buf
, len
);
687 if (fseeko(fp
, base
, SEEK_SET
) < 0 || ferror(fp
)) {
688 nasm_error(ERR_NONFATAL
,
689 "`incbin': unable to seek on file `%s'",
694 m
= fread(buf
, 1, l
< (off_t
)blk
? (size_t)l
: blk
, fp
);
695 if (!m
|| feof(fp
)) {
697 * This shouldn't happen unless the file
698 * actually changes while we are reading
701 nasm_error(ERR_NONFATAL
,
702 "`incbin': unexpected EOF while"
703 " reading file `%s'", fname
);
706 out_rawdata(&data
, buf
, m
);
712 lfmt
->downlevel(LIST_INCBIN
);
713 if (instruction
->times
> 1) {
714 lfmt
->uplevel(LIST_TIMES
);
715 lfmt
->downlevel(LIST_TIMES
);
718 nasm_error(ERR_NONFATAL
,
719 "`incbin': error while"
720 " reading file `%s'", fname
);
726 nasm_unmap_file(map
, len
);
729 instruction
->times
= 1; /* Tell the upper layer not to iterate */
732 /* "Real" instruction */
734 /* Check to see if we need an address-size prefix */
735 add_asp(instruction
, bits
);
737 m
= find_match(&temp
, instruction
, data
.segment
, data
.offset
, bits
);
741 int64_t insn_size
= calcsize(data
.segment
, data
.offset
,
742 bits
, instruction
, temp
);
743 nasm_assert(insn_size
>= 0);
748 data
.inslen
= insn_size
;
750 gencode(&data
, instruction
);
751 nasm_assert(data
.insoffs
== insn_size
);
755 case MERR_OPSIZEMISSING
:
756 nasm_error(ERR_NONFATAL
, "operation size not specified");
758 case MERR_OPSIZEMISMATCH
:
759 nasm_error(ERR_NONFATAL
, "mismatch in operand sizes");
762 nasm_error(ERR_NONFATAL
,
763 "broadcast not permitted on this operand");
765 case MERR_BRNUMMISMATCH
:
766 nasm_error(ERR_NONFATAL
,
767 "mismatch in the number of broadcasting elements");
769 case MERR_MASKNOTHERE
:
770 nasm_error(ERR_NONFATAL
,
771 "mask not permitted on this operand");
773 case MERR_DECONOTHERE
:
774 nasm_error(ERR_NONFATAL
, "unsupported mode decorator for instruction");
777 nasm_error(ERR_NONFATAL
, "no instruction for this cpu level");
780 nasm_error(ERR_NONFATAL
, "instruction not supported in %d-bit mode",
783 case MERR_ENCMISMATCH
:
784 nasm_error(ERR_NONFATAL
, "specific encoding scheme not available");
787 nasm_error(ERR_NONFATAL
, "bnd prefix is not allowed");
790 nasm_error(ERR_NONFATAL
, "%s prefix is not allowed",
791 (has_prefix(instruction
, PPS_REP
, P_REPNE
) ?
795 nasm_error(ERR_NONFATAL
,
796 "invalid combination of opcode and operands");
800 instruction
->times
= 1; /* Avoid repeated error messages */
803 return data
.offset
- start
;
806 int64_t insn_size(int32_t segment
, int64_t offset
, int bits
, insn
*instruction
)
808 const struct itemplate
*temp
;
811 if (instruction
->opcode
== I_none
)
814 if (opcode_is_db(instruction
->opcode
)) {
816 int32_t isize
, osize
, wsize
;
819 wsize
= db_bytes(instruction
->opcode
);
820 nasm_assert(wsize
> 0);
822 list_for_each(e
, instruction
->eops
) {
826 if (e
->type
== EOT_DB_NUMBER
) {
828 warn_overflow_const(e
->offset
, wsize
);
829 } else if (e
->type
== EOT_DB_STRING
||
830 e
->type
== EOT_DB_STRING_FREE
)
831 osize
= e
->stringlen
;
833 align
= (-osize
) % wsize
;
836 isize
+= osize
+ align
;
841 if (instruction
->opcode
== I_INCBIN
) {
842 const char *fname
= instruction
->eops
->stringval
;
845 len
= nasm_file_size_by_path(fname
);
846 if (len
== (off_t
)-1) {
847 nasm_error(ERR_NONFATAL
, "`incbin': unable to get length of file `%s'",
852 if (instruction
->eops
->next
) {
853 if (len
<= (off_t
)instruction
->eops
->next
->offset
) {
856 len
-= instruction
->eops
->next
->offset
;
857 if (instruction
->eops
->next
->next
&&
858 len
> (off_t
)instruction
->eops
->next
->next
->offset
) {
859 len
= (off_t
)instruction
->eops
->next
->next
->offset
;
864 len
*= instruction
->times
;
865 instruction
->times
= 1; /* Tell the upper layer to not iterate */
870 /* Check to see if we need an address-size prefix */
871 add_asp(instruction
, bits
);
873 m
= find_match(&temp
, instruction
, segment
, offset
, bits
);
875 /* we've matched an instruction. */
876 return calcsize(segment
, offset
, bits
, instruction
, temp
);
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 */
941 ins
->rex
= 0; /* Ensure REX is reset */
942 eat
= EA_SCALAR
; /* Expect a scalar EA */
943 memset(ins
->evex_p
, 0, 3); /* Ensure EVEX is reset */
945 if (ins
->prefixes
[PPS_OSIZE
] == P_O64
)
948 (void)segment
; /* Don't warn that this parameter is unused */
949 (void)offset
; /* Don't warn that this parameter is unused */
953 op1
= (c
& 3) + ((opex
& 1) << 2);
954 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
955 opx
= &ins
->oprs
[op1
];
956 opex
= 0; /* For the next iteration */
960 codes
+= c
, length
+= c
;
969 op_rexflags(opx
, REX_B
|REX_H
|REX_P
|REX_W
);
974 /* this is an index reg of MIB operand */
975 mib_index
= opx
->basereg
;
988 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
989 length
+= (opx
->type
& BITS16
) ? 2 : 4;
991 length
+= (bits
== 16) ? 2 : 4;
999 length
+= ins
->addr_size
>> 3;
1007 length
+= 8; /* MOV reg64/imm */
1015 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1016 length
+= (opx
->type
& BITS16
) ? 2 : 4;
1018 length
+= (bits
== 16) ? 2 : 4;
1041 ins
->vexreg
= regval(opx
);
1042 ins
->evex_p
[2] |= op_evexflags(opx
, EVEX_P2VP
, 2); /* High-16 NDS */
1043 ins
->vex_cm
= *codes
++;
1044 ins
->vex_wlp
= *codes
++;
1045 ins
->evex_tuple
= (*codes
++ - 0300);
1051 ins
->vex_cm
= *codes
++;
1052 ins
->vex_wlp
= *codes
++;
1053 ins
->evex_tuple
= (*codes
++ - 0300);
1062 ins
->vexreg
= regval(opx
);
1063 ins
->vex_cm
= *codes
++;
1064 ins
->vex_wlp
= *codes
++;
1070 ins
->vex_cm
= *codes
++;
1071 ins
->vex_wlp
= *codes
++;
1088 length
+= (bits
!= 16) && !has_prefix(ins
, PPS_ASIZE
, P_A16
);
1092 length
+= (bits
!= 32) && !has_prefix(ins
, PPS_ASIZE
, P_A32
);
1099 if (bits
!= 64 || has_prefix(ins
, PPS_ASIZE
, P_A16
) ||
1100 has_prefix(ins
, PPS_ASIZE
, P_A32
))
1109 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1113 nasm_error(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1115 ins
->prefixes
[PPS_OSIZE
] = P_O16
;
1121 enum prefixes pfx
= ins
->prefixes
[PPS_OSIZE
];
1125 nasm_error(ERR_WARNING
| ERR_PASS2
, "invalid operand size prefix");
1127 ins
->prefixes
[PPS_OSIZE
] = P_O32
;
1169 if (!ins
->prefixes
[PPS_REP
])
1170 ins
->prefixes
[PPS_REP
] = P_REP
;
1174 if (!ins
->prefixes
[PPS_REP
])
1175 ins
->prefixes
[PPS_REP
] = P_REPNE
;
1179 if (!absolute_op(&ins
->oprs
[0]))
1180 nasm_error(ERR_NONFATAL
, "attempt to reserve non-constant"
1181 " quantity of BSS space");
1182 else if (ins
->oprs
[0].opflags
& OPFLAG_FORWARD
)
1183 nasm_error(ERR_WARNING
| ERR_PASS1
,
1184 "forward reference in RESx can have unpredictable results");
1186 length
+= ins
->oprs
[0].offset
;
1190 if (!ins
->prefixes
[PPS_WAIT
])
1191 ins
->prefixes
[PPS_WAIT
] = P_WAIT
;
1246 struct operand
*opy
= &ins
->oprs
[op2
];
1247 struct operand
*op_er_sae
;
1249 ea_data
.rex
= 0; /* Ensure ea.REX is initially 0 */
1252 /* pick rfield from operand b (opx) */
1253 rflags
= regflag(opx
);
1254 rfield
= nasm_regvals
[opx
->basereg
];
1260 /* EVEX.b1 : evex_brerop contains the operand position */
1261 op_er_sae
= (ins
->evex_brerop
>= 0 ?
1262 &ins
->oprs
[ins
->evex_brerop
] : NULL
);
1264 if (op_er_sae
&& (op_er_sae
->decoflags
& (ER
| SAE
))) {
1266 ins
->evex_p
[2] |= EVEX_P2B
;
1267 if (op_er_sae
->decoflags
& ER
) {
1268 /* set EVEX.RC (rounding control) */
1269 ins
->evex_p
[2] |= ((ins
->evex_rm
- BRC_RN
) << 5)
1273 /* set EVEX.L'L (vector length) */
1274 ins
->evex_p
[2] |= ((ins
->vex_wlp
<< (5 - 2)) & EVEX_P2LL
);
1275 ins
->evex_p
[1] |= ((ins
->vex_wlp
<< (7 - 4)) & EVEX_P1W
);
1276 if (opy
->decoflags
& BRDCAST_MASK
) {
1278 ins
->evex_p
[2] |= EVEX_P2B
;
1282 if (itemp_has(temp
, IF_MIB
)) {
1283 opy
->eaflags
|= EAF_MIB
;
1285 * if a separate form of MIB (ICC style) is used,
1286 * the index reg info is merged into mem operand
1288 if (mib_index
!= R_none
) {
1289 opy
->indexreg
= mib_index
;
1291 opy
->hintbase
= mib_index
;
1292 opy
->hinttype
= EAH_NOTBASE
;
1296 if (process_ea(opy
, &ea_data
, bits
,
1297 rfield
, rflags
, ins
, &errmsg
) != eat
) {
1298 nasm_error(ERR_NONFATAL
, "%s", errmsg
);
1301 ins
->rex
|= ea_data
.rex
;
1302 length
+= ea_data
.size
;
1308 nasm_panic(0, "internal instruction table corrupt"
1309 ": instruction code \\%o (0x%02X) given", c
, c
);
1314 ins
->rex
&= rex_mask
;
1316 if (ins
->rex
& REX_NH
) {
1317 if (ins
->rex
& REX_H
) {
1318 nasm_error(ERR_NONFATAL
, "instruction cannot use high registers");
1321 ins
->rex
&= ~REX_P
; /* Don't force REX prefix due to high reg */
1324 switch (ins
->prefixes
[PPS_VEX
]) {
1326 if (!(ins
->rex
& REX_EV
))
1331 if (!(ins
->rex
& REX_V
))
1338 if (ins
->rex
& (REX_V
| REX_EV
)) {
1339 int bad32
= REX_R
|REX_W
|REX_X
|REX_B
;
1341 if (ins
->rex
& REX_H
) {
1342 nasm_error(ERR_NONFATAL
, "cannot use high register in AVX instruction");
1345 switch (ins
->vex_wlp
& 060) {
1359 if (bits
!= 64 && ((ins
->rex
& bad32
) || ins
->vexreg
> 7)) {
1360 nasm_error(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1362 } else if (!(ins
->rex
& REX_EV
) &&
1363 ((ins
->vexreg
> 15) || (ins
->evex_p
[0] & 0xf0))) {
1364 nasm_error(ERR_NONFATAL
, "invalid high-16 register in non-AVX-512");
1367 if (ins
->rex
& REX_EV
)
1369 else if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1370 ins
->prefixes
[PPS_VEX
] == P_VEX3
)
1374 } else if (ins
->rex
& REX_MASK
) {
1375 if (ins
->rex
& REX_H
) {
1376 nasm_error(ERR_NONFATAL
, "cannot use high register in rex instruction");
1378 } else if (bits
== 64) {
1380 } else if ((ins
->rex
& REX_L
) &&
1381 !(ins
->rex
& (REX_P
|REX_W
|REX_X
|REX_B
)) &&
1382 iflag_ffs(&cpu
) >= IF_X86_64
) {
1384 assert_no_prefix(ins
, PPS_LOCK
);
1385 lockcheck
= false; /* Already errored, no need for warning */
1388 nasm_error(ERR_NONFATAL
, "invalid operands in non-64-bit mode");
1393 if (has_prefix(ins
, PPS_LOCK
, P_LOCK
) && lockcheck
&&
1394 (!itemp_has(temp
,IF_LOCK
) || !is_class(MEMORY
, ins
->oprs
[0].type
))) {
1395 nasm_error(ERR_WARNING
| ERR_WARN_LOCK
| ERR_PASS2
,
1396 "instruction is not lockable");
1399 bad_hle_warn(ins
, hleok
);
1402 * when BND prefix is set by DEFAULT directive,
1403 * BND prefix is added to every appropriate instruction line
1404 * unless it is overridden by NOBND prefix.
1407 (itemp_has(temp
, IF_BND
) && !has_prefix(ins
, PPS_REP
, P_NOBND
)))
1408 ins
->prefixes
[PPS_REP
] = P_BND
;
1411 * Add length of legacy prefixes
1413 length
+= emit_prefix(NULL
, bits
, ins
);
1418 static inline void emit_rex(struct out_data
*data
, insn
*ins
)
1420 if (data
->bits
== 64) {
1421 if ((ins
->rex
& REX_MASK
) &&
1422 !(ins
->rex
& (REX_V
| REX_EV
)) &&
1424 uint8_t rex
= (ins
->rex
& REX_MASK
) | REX_P
;
1425 out_rawbyte(data
, rex
);
1426 ins
->rex_done
= true;
1431 static int emit_prefix(struct out_data
*data
, const int bits
, insn
*ins
)
1436 for (j
= 0; j
< MAXPREFIX
; j
++) {
1438 switch (ins
->prefixes
[j
]) {
1459 nasm_error(ERR_WARNING
| ERR_PASS2
,
1460 "cs segment base generated, but will be ignored in 64-bit mode");
1466 nasm_error(ERR_WARNING
| ERR_PASS2
,
1467 "ds segment base generated, but will be ignored in 64-bit mode");
1473 nasm_error(ERR_WARNING
| ERR_PASS2
,
1474 "es segment base generated, but will be ignored in 64-bit mode");
1486 nasm_error(ERR_WARNING
| ERR_PASS2
,
1487 "ss segment base generated, but will be ignored in 64-bit mode");
1493 nasm_error(ERR_NONFATAL
,
1494 "segr6 and segr7 cannot be used as prefixes");
1498 nasm_error(ERR_NONFATAL
,
1499 "16-bit addressing is not supported "
1501 } else if (bits
!= 16)
1510 nasm_error(ERR_NONFATAL
,
1511 "64-bit addressing is only supported "
1539 nasm_panic(0, "invalid instruction prefix");
1543 out_rawbyte(data
, c
);
1550 static void gencode(struct out_data
*data
, insn
*ins
)
1556 struct operand
*opx
;
1557 const uint8_t *codes
= data
->itemp
->code
;
1559 enum ea_type eat
= EA_SCALAR
;
1561 const int bits
= data
->bits
;
1564 ins
->rex_done
= false;
1566 emit_prefix(data
, bits
, ins
);
1570 op1
= (c
& 3) + ((opex
& 1) << 2);
1571 op2
= ((c
>> 3) & 3) + ((opex
& 2) << 1);
1572 opx
= &ins
->oprs
[op1
];
1573 opex
= 0; /* For the next iteration */
1581 emit_rex(data
, ins
);
1582 out_rawdata(data
, codes
, c
);
1593 emit_rex(data
, ins
);
1594 out_rawbyte(data
, *codes
++ + (regval(opx
) & 7));
1601 out_imm(data
, opx
, 1, OUT_WRAP
);
1605 out_imm(data
, opx
, 1, OUT_UNSIGNED
);
1609 out_imm(data
, opx
, 2, OUT_WRAP
);
1613 if (opx
->type
& (BITS16
| BITS32
))
1614 size
= (opx
->type
& BITS16
) ? 2 : 4;
1616 size
= (bits
== 16) ? 2 : 4;
1617 out_imm(data
, opx
, size
, OUT_WRAP
);
1621 out_imm(data
, opx
, 4, OUT_WRAP
);
1625 size
= ins
->addr_size
>> 3;
1626 out_imm(data
, opx
, size
, OUT_WRAP
);
1630 if (opx
->segment
== data
->segment
) {
1631 int64_t delta
= opx
->offset
- data
->offset
1632 - (data
->inslen
- data
->insoffs
);
1633 if (delta
> 127 || delta
< -128)
1634 nasm_error(ERR_NONFATAL
, "short jump is out of range");
1636 out_reladdr(data
, opx
, 1);
1640 out_imm(data
, opx
, 8, OUT_WRAP
);
1644 out_reladdr(data
, opx
, 2);
1648 if (opx
->type
& (BITS16
| BITS32
| BITS64
))
1649 size
= (opx
->type
& BITS16
) ? 2 : 4;
1651 size
= (bits
== 16) ? 2 : 4;
1653 out_reladdr(data
, opx
, size
);
1657 out_reladdr(data
, opx
, 4);
1661 if (opx
->segment
== NO_SEG
)
1662 nasm_error(ERR_NONFATAL
, "value referenced by FAR is not"
1664 out_segment(data
, opx
);
1669 int mask
= ins
->prefixes
[PPS_VEX
] == P_EVEX
? 7 : 15;
1670 const struct operand
*opy
;
1673 opx
= &ins
->oprs
[c
>> 3];
1674 opy
= &ins
->oprs
[c
& 7];
1675 if (!absolute_op(opy
)) {
1676 nasm_error(ERR_NONFATAL
,
1677 "non-absolute expression not permitted as argument %d",
1679 } else if (opy
->offset
& ~mask
) {
1680 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1681 "is4 argument exceeds bounds");
1683 c
= opy
->offset
& mask
;
1689 opx
= &ins
->oprs
[c
>> 4];
1696 r
= nasm_regvals
[opx
->basereg
];
1697 out_rawbyte(data
, (r
<< 4) | ((r
& 0x10) >> 1) | c
);
1701 if (absolute_op(opx
) &&
1702 (int32_t)opx
->offset
!= (int64_t)opx
->offset
) {
1703 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1704 "signed dword immediate exceeds bounds");
1706 out_imm(data
, opx
, 4, OUT_SIGNED
);
1712 ins
->evex_p
[2] |= op_evexflags(&ins
->oprs
[0],
1713 EVEX_P2Z
| EVEX_P2AAA
, 2);
1714 ins
->evex_p
[2] ^= EVEX_P2VP
; /* 1's complement */
1716 /* EVEX.X can be set by either REX or EVEX for different reasons */
1717 bytes
[1] = ((((ins
->rex
& 7) << 5) |
1718 (ins
->evex_p
[0] & (EVEX_P0X
| EVEX_P0RP
))) ^ 0xf0) |
1719 (ins
->vex_cm
& EVEX_P0MM
);
1720 bytes
[2] = ((ins
->rex
& REX_W
) << (7 - 3)) |
1721 ((~ins
->vexreg
& 15) << 3) |
1722 (1 << 2) | (ins
->vex_wlp
& 3);
1723 bytes
[3] = ins
->evex_p
[2];
1724 out_rawdata(data
, bytes
, 4);
1730 if (ins
->vex_cm
!= 1 || (ins
->rex
& (REX_W
|REX_X
|REX_B
)) ||
1731 ins
->prefixes
[PPS_VEX
] == P_VEX3
) {
1732 bytes
[0] = (ins
->vex_cm
>> 6) ? 0x8f : 0xc4;
1733 bytes
[1] = (ins
->vex_cm
& 31) | ((~ins
->rex
& 7) << 5);
1734 bytes
[2] = ((ins
->rex
& REX_W
) << (7-3)) |
1735 ((~ins
->vexreg
& 15)<< 3) | (ins
->vex_wlp
& 07);
1736 out_rawdata(data
, bytes
, 3);
1739 bytes
[1] = ((~ins
->rex
& REX_R
) << (7-2)) |
1740 ((~ins
->vexreg
& 15) << 3) | (ins
->vex_wlp
& 07);
1741 out_rawdata(data
, bytes
, 2);
1755 if (absolute_op(opx
)) {
1756 if (ins
->rex
& REX_W
)
1758 else if (ins
->prefixes
[PPS_OSIZE
] == P_O16
)
1760 else if (ins
->prefixes
[PPS_OSIZE
] == P_O32
)
1765 um
= (uint64_t)2 << (s
-1);
1768 if (uv
> 127 && uv
< (uint64_t)-128 &&
1769 (uv
< um
-128 || uv
> um
-1)) {
1770 /* If this wasn't explicitly byte-sized, warn as though we
1771 * had fallen through to the imm16/32/64 case.
1773 nasm_error(ERR_WARNING
| ERR_PASS2
| ERR_WARN_NOV
,
1774 "%s value exceeds bounds",
1775 (opx
->type
& BITS8
) ? "signed byte" :
1781 /* Output as a raw byte to avoid byte overflow check */
1782 out_rawbyte(data
, (uint8_t)uv
);
1784 out_imm(data
, opx
, 1, OUT_WRAP
); /* XXX: OUT_SIGNED? */
1793 if (bits
== 32 && !has_prefix(ins
, PPS_ASIZE
, P_A16
))
1794 out_rawbyte(data
, 0x67);
1798 if (bits
!= 32 && !has_prefix(ins
, PPS_ASIZE
, P_A32
))
1799 out_rawbyte(data
, 0x67);
1831 out_rawbyte(data
, *codes
++ ^ get_cond_opcode(ins
->condition
));
1839 out_rawbyte(data
, c
- 0332 + 0xF2);
1843 if (ins
->rex
& REX_R
)
1844 out_rawbyte(data
, 0xF0);
1845 ins
->rex
&= ~(REX_L
|REX_R
);
1856 if (ins
->oprs
[0].segment
!= NO_SEG
)
1857 nasm_panic(0, "non-constant BSS size in pass two");
1859 out_reserve(data
, ins
->oprs
[0].offset
);
1869 out_rawbyte(data
, 0x66);
1878 out_rawbyte(data
, c
- 0366 + 0x66);
1885 out_rawbyte(data
, bits
== 16 ? 3 : 5);
1917 struct operand
*opy
= &ins
->oprs
[op2
];
1920 /* pick rfield from operand b (opx) */
1921 rflags
= regflag(opx
);
1922 rfield
= nasm_regvals
[opx
->basereg
];
1924 /* rfield is constant */
1929 if (process_ea(opy
, &ea_data
, bits
,
1930 rfield
, rflags
, ins
, &errmsg
) != eat
)
1931 nasm_error(ERR_NONFATAL
, "%s", errmsg
);
1934 *p
++ = ea_data
.modrm
;
1935 if (ea_data
.sib_present
)
1937 out_rawdata(data
, bytes
, p
- bytes
);
1940 * Make sure the address gets the right offset in case
1941 * the line breaks in the .lst file (BR 1197827)
1944 if (ea_data
.bytes
) {
1945 /* use compressed displacement, if available */
1946 if (ea_data
.disp8
) {
1947 out_rawbyte(data
, ea_data
.disp8
);
1948 } else if (ea_data
.rip
) {
1949 out_reladdr(data
, opy
, ea_data
.bytes
);
1951 int asize
= ins
->addr_size
>> 3;
1953 if (overflow_general(opy
->offset
, asize
) ||
1954 signed_bits(opy
->offset
, ins
->addr_size
) !=
1955 signed_bits(opy
->offset
, ea_data
.bytes
<< 3))
1956 warn_overflow(ea_data
.bytes
);
1958 out_imm(data
, opy
, ea_data
.bytes
,
1959 (asize
> ea_data
.bytes
)
1960 ? OUT_SIGNED
: OUT_WRAP
);
1967 nasm_panic(0, "internal instruction table corrupt"
1968 ": instruction code \\%o (0x%02X) given", c
, c
);
1974 static opflags_t
regflag(const operand
* o
)
1976 if (!is_register(o
->basereg
))
1977 nasm_panic(0, "invalid operand passed to regflag()");
1978 return nasm_reg_flags
[o
->basereg
];
1981 static int32_t regval(const operand
* o
)
1983 if (!is_register(o
->basereg
))
1984 nasm_panic(0, "invalid operand passed to regval()");
1985 return nasm_regvals
[o
->basereg
];
1988 static int op_rexflags(const operand
* o
, int mask
)
1993 if (!is_register(o
->basereg
))
1994 nasm_panic(0, "invalid operand passed to op_rexflags()");
1996 flags
= nasm_reg_flags
[o
->basereg
];
1997 val
= nasm_regvals
[o
->basereg
];
1999 return rexflags(val
, flags
, mask
);
2002 static int rexflags(int val
, opflags_t flags
, int mask
)
2006 if (val
>= 0 && (val
& 8))
2007 rex
|= REX_B
|REX_X
|REX_R
;
2010 if (!(REG_HIGH
& ~flags
)) /* AH, CH, DH, BH */
2012 else if (!(REG8
& ~flags
) && val
>= 4) /* SPL, BPL, SIL, DIL */
2018 static int evexflags(int val
, decoflags_t deco
,
2019 int mask
, uint8_t byte
)
2025 if (val
>= 0 && (val
& 16))
2026 evex
|= (EVEX_P0RP
| EVEX_P0X
);
2029 if (val
>= 0 && (val
& 16))
2033 if (deco
& OPMASK_MASK
)
2034 evex
|= deco
& EVEX_P2AAA
;
2040 static int op_evexflags(const operand
* o
, int mask
, uint8_t byte
)
2044 val
= nasm_regvals
[o
->basereg
];
2046 return evexflags(val
, o
->decoflags
, mask
, byte
);
2049 static enum match_result
find_match(const struct itemplate
**tempp
,
2051 int32_t segment
, int64_t offset
, int bits
)
2053 const struct itemplate
*temp
;
2054 enum match_result m
, merr
;
2055 opflags_t xsizeflags
[MAX_OPERANDS
];
2056 bool opsizemissing
= false;
2057 int8_t broadcast
= instruction
->evex_brerop
;
2060 /* broadcasting uses a different data element size */
2061 for (i
= 0; i
< instruction
->operands
; i
++)
2063 xsizeflags
[i
] = instruction
->oprs
[i
].decoflags
& BRSIZE_MASK
;
2065 xsizeflags
[i
] = instruction
->oprs
[i
].type
& SIZE_MASK
;
2067 merr
= MERR_INVALOP
;
2069 for (temp
= nasm_instructions
[instruction
->opcode
];
2070 temp
->opcode
!= I_none
; temp
++) {
2071 m
= matches(temp
, instruction
, bits
);
2072 if (m
== MOK_JUMP
) {
2073 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2077 } else if (m
== MERR_OPSIZEMISSING
&& !itemp_has(temp
, IF_SX
)) {
2079 * Missing operand size and a candidate for fuzzy matching...
2081 for (i
= 0; i
< temp
->operands
; i
++)
2083 xsizeflags
[i
] |= temp
->deco
[i
] & BRSIZE_MASK
;
2085 xsizeflags
[i
] |= temp
->opd
[i
] & SIZE_MASK
;
2086 opsizemissing
= true;
2090 if (merr
== MOK_GOOD
)
2094 /* No match, but see if we can get a fuzzy operand size match... */
2098 for (i
= 0; i
< instruction
->operands
; i
++) {
2100 * We ignore extrinsic operand sizes on registers, so we should
2101 * never try to fuzzy-match on them. This also resolves the case
2102 * when we have e.g. "xmmrm128" in two different positions.
2104 if (is_class(REGISTER
, instruction
->oprs
[i
].type
))
2107 /* This tests if xsizeflags[i] has more than one bit set */
2108 if ((xsizeflags
[i
] & (xsizeflags
[i
]-1)))
2109 goto done
; /* No luck */
2111 if (i
== broadcast
) {
2112 instruction
->oprs
[i
].decoflags
|= xsizeflags
[i
];
2113 instruction
->oprs
[i
].type
|= (xsizeflags
[i
] == BR_BITS32
?
2116 instruction
->oprs
[i
].type
|= xsizeflags
[i
]; /* Set the size */
2120 /* Try matching again... */
2121 for (temp
= nasm_instructions
[instruction
->opcode
];
2122 temp
->opcode
!= I_none
; temp
++) {
2123 m
= matches(temp
, instruction
, bits
);
2124 if (m
== MOK_JUMP
) {
2125 if (jmp_match(segment
, offset
, bits
, instruction
, temp
))
2132 if (merr
== MOK_GOOD
)
2141 static uint8_t get_broadcast_num(opflags_t opflags
, opflags_t brsize
)
2143 unsigned int opsize
= (opflags
& SIZE_MASK
) >> SIZE_SHIFT
;
2146 if (brsize
> BITS64
)
2147 nasm_error(ERR_FATAL
,
2148 "size of broadcasting element is greater than 64 bits");
2151 * The shift term is to take care of the extra BITS80 inserted
2152 * between BITS64 and BITS128.
2154 brcast_num
= ((opsize
/ (BITS64
>> SIZE_SHIFT
)) * (BITS64
/ brsize
))
2155 >> (opsize
> (BITS64
>> SIZE_SHIFT
));
2160 static enum match_result
matches(const struct itemplate
*itemp
,
2161 insn
*instruction
, int bits
)
2163 opflags_t size
[MAX_OPERANDS
], asize
;
2164 bool opsizemissing
= false;
2170 if (itemp
->opcode
!= instruction
->opcode
)
2171 return MERR_INVALOP
;
2174 * Count the operands
2176 if (itemp
->operands
!= instruction
->operands
)
2177 return MERR_INVALOP
;
2182 if (!(optimizing
> 0) && itemp_has(itemp
, IF_OPT
))
2183 return MERR_INVALOP
;
2188 switch (instruction
->prefixes
[PPS_VEX
]) {
2190 if (!itemp_has(itemp
, IF_EVEX
))
2191 return MERR_ENCMISMATCH
;
2195 if (!itemp_has(itemp
, IF_VEX
))
2196 return MERR_ENCMISMATCH
;
2203 * Check that no spurious colons or TOs are present
2205 for (i
= 0; i
< itemp
->operands
; i
++)
2206 if (instruction
->oprs
[i
].type
& ~itemp
->opd
[i
] & (COLON
| TO
))
2207 return MERR_INVALOP
;
2210 * Process size flags
2212 switch (itemp_smask(itemp
)) {
2213 case IF_GENBIT(IF_SB
):
2216 case IF_GENBIT(IF_SW
):
2219 case IF_GENBIT(IF_SD
):
2222 case IF_GENBIT(IF_SQ
):
2225 case IF_GENBIT(IF_SO
):
2228 case IF_GENBIT(IF_SY
):
2231 case IF_GENBIT(IF_SZ
):
2234 case IF_GENBIT(IF_SIZE
):
2255 if (itemp_armask(itemp
)) {
2256 /* S- flags only apply to a specific operand */
2257 i
= itemp_arg(itemp
);
2258 memset(size
, 0, sizeof size
);
2261 /* S- flags apply to all operands */
2262 for (i
= 0; i
< MAX_OPERANDS
; i
++)
2267 * Check that the operand flags all match up,
2268 * it's a bit tricky so lets be verbose:
2270 * 1) Find out the size of operand. If instruction
2271 * doesn't have one specified -- we're trying to
2272 * guess it either from template (IF_S* flag) or
2275 * 2) If template operand do not match the instruction OR
2276 * template has an operand size specified AND this size differ
2277 * from which instruction has (perhaps we got it from code bits)
2279 * a) Check that only size of instruction and operand is differ
2280 * other characteristics do match
2281 * b) Perhaps it's a register specified in instruction so
2282 * for such a case we just mark that operand as "size
2283 * missing" and this will turn on fuzzy operand size
2284 * logic facility (handled by a caller)
2286 for (i
= 0; i
< itemp
->operands
; i
++) {
2287 opflags_t type
= instruction
->oprs
[i
].type
;
2288 decoflags_t deco
= instruction
->oprs
[i
].decoflags
;
2289 decoflags_t ideco
= itemp
->deco
[i
];
2290 bool is_broadcast
= deco
& BRDCAST_MASK
;
2291 uint8_t brcast_num
= 0;
2292 opflags_t template_opsize
, insn_opsize
;
2294 if (!(type
& SIZE_MASK
))
2297 insn_opsize
= type
& SIZE_MASK
;
2298 if (!is_broadcast
) {
2299 template_opsize
= itemp
->opd
[i
] & SIZE_MASK
;
2301 decoflags_t deco_brsize
= ideco
& BRSIZE_MASK
;
2303 if (~ideco
& BRDCAST_MASK
)
2304 return MERR_BRNOTHERE
;
2307 * when broadcasting, the element size depends on
2308 * the instruction type. decorator flag should match.
2311 template_opsize
= (deco_brsize
== BR_BITS32
? BITS32
: BITS64
);
2312 /* calculate the proper number : {1to<brcast_num>} */
2313 brcast_num
= get_broadcast_num(itemp
->opd
[i
], template_opsize
);
2315 template_opsize
= 0;
2319 if (~ideco
& deco
& OPMASK_MASK
)
2320 return MERR_MASKNOTHERE
;
2322 if (~ideco
& deco
& (Z_MASK
|STATICRND_MASK
|SAE_MASK
))
2323 return MERR_DECONOTHERE
;
2325 if (itemp
->opd
[i
] & ~type
& ~SIZE_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_WORDOFFS) && \
2428 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2429 is_disp8n(input, ins, &output->disp8) : \
2430 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2431 o <= 127 && seg == NO_SEG && !forw_ref)))
2433 static enum ea_type
process_ea(operand
*input
, ea
*output
, int bits
,
2434 int rfield
, opflags_t rflags
, insn
*ins
,
2435 const char **errmsg
)
2437 bool forw_ref
= !!(input
->opflags
& OPFLAG_UNKNOWN
);
2438 int addrbits
= ins
->addr_size
;
2439 int eaflags
= input
->eaflags
;
2441 *errmsg
= "invalid effective address"; /* Default error message */
2443 output
->type
= EA_SCALAR
;
2444 output
->rip
= false;
2447 /* REX flags for the rfield operand */
2448 output
->rex
|= rexflags(rfield
, rflags
, REX_R
| REX_P
| REX_W
| REX_H
);
2449 /* EVEX.R' flag for the REG operand */
2450 ins
->evex_p
[0] |= evexflags(rfield
, 0, EVEX_P0RP
, 0);
2452 if (is_class(REGISTER
, input
->type
)) {
2454 * It's a direct register.
2456 if (!is_register(input
->basereg
))
2459 if (!is_reg_class(REG_EA
, input
->basereg
))
2462 /* broadcasting is not available with a direct register operand. */
2463 if (input
->decoflags
& BRDCAST_MASK
) {
2464 *errmsg
= "broadcast not allowed with register operand";
2468 output
->rex
|= op_rexflags(input
, REX_B
| REX_P
| REX_W
| REX_H
);
2469 ins
->evex_p
[0] |= op_evexflags(input
, EVEX_P0X
, 0);
2470 output
->sib_present
= false; /* no SIB necessary */
2471 output
->bytes
= 0; /* no offset necessary either */
2472 output
->modrm
= GEN_MODRM(3, rfield
, nasm_regvals
[input
->basereg
]);
2475 * It's a memory reference.
2478 /* Embedded rounding or SAE is not available with a mem ref operand. */
2479 if (input
->decoflags
& (ER
| SAE
)) {
2480 *errmsg
= "embedded rounding is available only with "
2481 "register-register operations";
2485 if (input
->basereg
== -1 &&
2486 (input
->indexreg
== -1 || input
->scale
== 0)) {
2488 * It's a pure offset.
2490 if (bits
== 64 && ((input
->type
& IP_REL
) == IP_REL
)) {
2491 if (input
->segment
== NO_SEG
||
2492 (input
->opflags
& OPFLAG_RELATIVE
)) {
2493 nasm_error(ERR_WARNING
| ERR_PASS2
,
2494 "absolute address can not be RIP-relative");
2495 input
->type
&= ~IP_REL
;
2496 input
->type
|= MEMORY
;
2501 !(IP_REL
& ~input
->type
) && (eaflags
& EAF_MIB
)) {
2502 *errmsg
= "RIP-relative addressing is prohibited for MIB";
2506 if (eaflags
& EAF_BYTEOFFS
||
2507 (eaflags
& EAF_WORDOFFS
&&
2508 input
->disp_size
!= (addrbits
!= 16 ? 32 : 16))) {
2509 nasm_error(ERR_WARNING
| ERR_PASS1
,
2510 "displacement size ignored on absolute address");
2513 if (bits
== 64 && (~input
->type
& IP_REL
)) {
2514 output
->sib_present
= true;
2515 output
->sib
= GEN_SIB(0, 4, 5);
2517 output
->modrm
= GEN_MODRM(0, rfield
, 4);
2518 output
->rip
= false;
2520 output
->sib_present
= false;
2521 output
->bytes
= (addrbits
!= 16 ? 4 : 2);
2522 output
->modrm
= GEN_MODRM(0, rfield
,
2523 (addrbits
!= 16 ? 5 : 6));
2524 output
->rip
= bits
== 64;
2528 * It's an indirection.
2530 int i
= input
->indexreg
, b
= input
->basereg
, s
= input
->scale
;
2531 int32_t seg
= input
->segment
;
2532 int hb
= input
->hintbase
, ht
= input
->hinttype
;
2533 int t
, it
, bt
; /* register numbers */
2534 opflags_t x
, ix
, bx
; /* register flags */
2537 i
= -1; /* make this easy, at least */
2539 if (is_register(i
)) {
2540 it
= nasm_regvals
[i
];
2541 ix
= nasm_reg_flags
[i
];
2547 if (is_register(b
)) {
2548 bt
= nasm_regvals
[b
];
2549 bx
= nasm_reg_flags
[b
];
2555 /* if either one are a vector register... */
2556 if ((ix
|bx
) & (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
) {
2557 opflags_t sok
= BITS32
| BITS64
;
2558 int32_t o
= input
->offset
;
2559 int mod
, scale
, index
, base
;
2562 * For a vector SIB, one has to be a vector and the other,
2563 * if present, a GPR. The vector must be the index operand.
2565 if (it
== -1 || (bx
& (XMMREG
|YMMREG
|ZMMREG
) & ~REG_EA
)) {
2571 t
= bt
, bt
= it
, it
= t
;
2572 x
= bx
, bx
= ix
, ix
= x
;
2578 if (!(REG64
& ~bx
) || !(REG32
& ~bx
))
2585 * While we're here, ensure the user didn't specify
2588 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2591 if (addrbits
== 16 ||
2592 (addrbits
== 32 && !(sok
& BITS32
)) ||
2593 (addrbits
== 64 && !(sok
& BITS64
)))
2596 output
->type
= ((ix
& ZMMREG
& ~REG_EA
) ? EA_ZMMVSIB
2597 : ((ix
& YMMREG
& ~REG_EA
)
2598 ? EA_YMMVSIB
: EA_XMMVSIB
));
2600 output
->rex
|= rexflags(it
, ix
, REX_X
);
2601 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2602 ins
->evex_p
[2] |= evexflags(it
, 0, EVEX_P2VP
, 2);
2604 index
= it
& 7; /* it is known to be != -1 */
2619 default: /* then what the smeg is it? */
2620 goto err
; /* panic */
2628 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2629 seg
== NO_SEG
&& !forw_ref
&&
2630 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2632 else if (IS_MOD_01())
2638 output
->sib_present
= true;
2639 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2640 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2641 output
->sib
= GEN_SIB(scale
, index
, base
);
2642 } else if ((ix
|bx
) & (BITS32
|BITS64
)) {
2644 * it must be a 32/64-bit memory reference. Firstly we have
2645 * to check that all registers involved are type E/Rxx.
2647 opflags_t sok
= BITS32
| BITS64
;
2648 int32_t o
= input
->offset
;
2651 if (!(REG64
& ~ix
) || !(REG32
& ~ix
))
2659 goto err
; /* Invalid register */
2660 if (~sok
& bx
& SIZE_MASK
)
2661 goto err
; /* Invalid size */
2666 * While we're here, ensure the user didn't specify
2669 if (input
->disp_size
== 16 || input
->disp_size
== 64)
2672 if (addrbits
== 16 ||
2673 (addrbits
== 32 && !(sok
& BITS32
)) ||
2674 (addrbits
== 64 && !(sok
& BITS64
)))
2677 /* now reorganize base/index */
2678 if (s
== 1 && bt
!= it
&& bt
!= -1 && it
!= -1 &&
2679 ((hb
== b
&& ht
== EAH_NOTBASE
) ||
2680 (hb
== i
&& ht
== EAH_MAKEBASE
))) {
2681 /* swap if hints say so */
2682 t
= bt
, bt
= it
, it
= t
;
2683 x
= bx
, bx
= ix
, ix
= x
;
2686 if (bt
== -1 && s
== 1 && !(hb
== i
&& ht
== EAH_NOTBASE
)) {
2687 /* make single reg base, unless hint */
2688 bt
= it
, bx
= ix
, it
= -1, ix
= 0;
2690 if (eaflags
& EAF_MIB
) {
2691 /* only for mib operands */
2692 if (it
== -1 && (hb
== b
&& ht
== EAH_NOTBASE
)) {
2694 * make a single reg index [reg*1].
2695 * gas uses this form for an explicit index register.
2697 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2699 if ((ht
== EAH_SUMMED
) && bt
== -1) {
2700 /* separate once summed index into [base, index] */
2701 bt
= it
, bx
= ix
, s
--;
2704 if (((s
== 2 && it
!= REG_NUM_ESP
&&
2705 (!(eaflags
& EAF_TIMESTWO
) || (ht
== EAH_SUMMED
))) ||
2706 s
== 3 || s
== 5 || s
== 9) && bt
== -1) {
2707 /* convert 3*EAX to EAX+2*EAX */
2708 bt
= it
, bx
= ix
, s
--;
2710 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
&&
2711 (eaflags
& EAF_TIMESTWO
) &&
2712 (hb
== b
&& ht
== EAH_NOTBASE
)) {
2714 * convert [NOSPLIT EAX*1]
2715 * to sib format with 0x0 displacement - [EAX*1+0].
2717 it
= bt
, ix
= bx
, bt
= -1, bx
= 0, s
= 1;
2720 if (s
== 1 && it
== REG_NUM_ESP
) {
2721 /* swap ESP into base if scale is 1 */
2722 t
= it
, it
= bt
, bt
= t
;
2723 x
= ix
, ix
= bx
, bx
= x
;
2725 if (it
== REG_NUM_ESP
||
2726 (s
!= 1 && s
!= 2 && s
!= 4 && s
!= 8 && it
!= -1))
2727 goto err
; /* wrong, for various reasons */
2729 output
->rex
|= rexflags(it
, ix
, REX_X
);
2730 output
->rex
|= rexflags(bt
, bx
, REX_B
);
2732 if (it
== -1 && (bt
& 7) != REG_NUM_ESP
) {
2741 if (rm
!= REG_NUM_EBP
&& o
== 0 &&
2742 seg
== NO_SEG
&& !forw_ref
&&
2743 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2745 else if (IS_MOD_01())
2751 output
->sib_present
= false;
2752 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2753 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2756 int mod
, scale
, index
, base
;
2776 default: /* then what the smeg is it? */
2777 goto err
; /* panic */
2785 if (base
!= REG_NUM_EBP
&& o
== 0 &&
2786 seg
== NO_SEG
&& !forw_ref
&&
2787 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2789 else if (IS_MOD_01())
2795 output
->sib_present
= true;
2796 output
->bytes
= (bt
== -1 || mod
== 2 ? 4 : mod
);
2797 output
->modrm
= GEN_MODRM(mod
, rfield
, 4);
2798 output
->sib
= GEN_SIB(scale
, index
, base
);
2800 } else { /* it's 16-bit */
2802 int16_t o
= input
->offset
;
2804 /* check for 64-bit long mode */
2808 /* check all registers are BX, BP, SI or DI */
2809 if ((b
!= -1 && b
!= R_BP
&& b
!= R_BX
&& b
!= R_SI
&& b
!= R_DI
) ||
2810 (i
!= -1 && i
!= R_BP
&& i
!= R_BX
&& i
!= R_SI
&& i
!= R_DI
))
2813 /* ensure the user didn't specify DWORD/QWORD */
2814 if (input
->disp_size
== 32 || input
->disp_size
== 64)
2817 if (s
!= 1 && i
!= -1)
2818 goto err
; /* no can do, in 16-bit EA */
2819 if (b
== -1 && i
!= -1) {
2824 if ((b
== R_SI
|| b
== R_DI
) && i
!= -1) {
2829 /* have BX/BP as base, SI/DI index */
2831 goto err
; /* shouldn't ever happen, in theory */
2832 if (i
!= -1 && b
!= -1 &&
2833 (i
== R_BP
|| i
== R_BX
|| b
== R_SI
|| b
== R_DI
))
2834 goto err
; /* invalid combinations */
2835 if (b
== -1) /* pure offset: handled above */
2836 goto err
; /* so if it gets to here, panic! */
2840 switch (i
* 256 + b
) {
2841 case R_SI
* 256 + R_BX
:
2844 case R_DI
* 256 + R_BX
:
2847 case R_SI
* 256 + R_BP
:
2850 case R_DI
* 256 + R_BP
:
2868 if (rm
== -1) /* can't happen, in theory */
2869 goto err
; /* so panic if it does */
2871 if (o
== 0 && seg
== NO_SEG
&& !forw_ref
&& rm
!= 6 &&
2872 !(eaflags
& (EAF_BYTEOFFS
| EAF_WORDOFFS
)))
2874 else if (IS_MOD_01())
2879 output
->sib_present
= false; /* no SIB - it's 16-bit */
2880 output
->bytes
= mod
; /* bytes of offset needed */
2881 output
->modrm
= GEN_MODRM(mod
, rfield
, rm
);
2886 output
->size
= 1 + output
->sib_present
+ output
->bytes
;
2887 return output
->type
;
2890 return output
->type
= EA_INVALID
;
2893 static void add_asp(insn
*ins
, int addrbits
)
2898 valid
= (addrbits
== 64) ? 64|32 : 32|16;
2900 switch (ins
->prefixes
[PPS_ASIZE
]) {
2911 valid
&= (addrbits
== 32) ? 16 : 32;
2917 for (j
= 0; j
< ins
->operands
; j
++) {
2918 if (is_class(MEMORY
, ins
->oprs
[j
].type
)) {
2921 /* Verify as Register */
2922 if (!is_register(ins
->oprs
[j
].indexreg
))
2925 i
= nasm_reg_flags
[ins
->oprs
[j
].indexreg
];
2927 /* Verify as Register */
2928 if (!is_register(ins
->oprs
[j
].basereg
))
2931 b
= nasm_reg_flags
[ins
->oprs
[j
].basereg
];
2933 if (ins
->oprs
[j
].scale
== 0)
2937 int ds
= ins
->oprs
[j
].disp_size
;
2938 if ((addrbits
!= 64 && ds
> 8) ||
2939 (addrbits
== 64 && ds
== 16))
2959 if (valid
& addrbits
) {
2960 ins
->addr_size
= addrbits
;
2961 } else if (valid
& ((addrbits
== 32) ? 16 : 32)) {
2962 /* Add an address size prefix */
2963 ins
->prefixes
[PPS_ASIZE
] = (addrbits
== 32) ? P_A16
: P_A32
;;
2964 ins
->addr_size
= (addrbits
== 32) ? 16 : 32;
2967 nasm_error(ERR_NONFATAL
, "impossible combination of address sizes");
2968 ins
->addr_size
= addrbits
; /* Error recovery */
2971 defdisp
= ins
->addr_size
== 16 ? 16 : 32;
2973 for (j
= 0; j
< ins
->operands
; j
++) {
2974 if (!(MEM_OFFS
& ~ins
->oprs
[j
].type
) &&
2975 (ins
->oprs
[j
].disp_size
? ins
->oprs
[j
].disp_size
: defdisp
) != ins
->addr_size
) {
2977 * mem_offs sizes must match the address size; if not,
2978 * strip the MEM_OFFS bit and match only EA instructions
2980 ins
->oprs
[j
].type
&= ~(MEM_OFFS
& ~MEMORY
);