blob: 82d6c6cf0b5aea2ab74f3f4c1758668077078e06 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvin (Intel)38ddb192019-01-11 12:27:02 -08003 * Copyright 1996-2019 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
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.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * 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.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040037 * Bytecode specification
38 * ----------------------
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070039 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040040 *
41 * Codes Mnemonic Explanation
42 *
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.
76 *
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.
81 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070082 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -070084 * cc 00m mmm
85 * c = 2 for EVEX and mmmm is the M field (EVEX.P0[3:0])
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070086 * and wlp is:
87 * 00 wwl lpp
88 * [l0] ll = 0 (.128, .lz)
89 * [l1] ll = 1 (.256)
90 * [l2] ll = 2 (.512)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
92 *
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
97 *
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
100 * [f3] pp = 2
101 * [f2] pp = 3
102 *
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
105 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400106 * \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.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700111 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700114 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700115 * [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)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700118 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700119 * [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
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700123 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700125 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400126 * \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"
Cyrill Gorcunov8a5d3e62014-08-25 20:04:30 +0400163 * \360 np no SSE prefix (== \364\331)
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400164 * \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
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 */
177
H. Peter Anvinfe501952007-10-02 21:53:51 -0700178#include "compiler.h"
179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180
181#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000182#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800183#include "error.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000184#include "assemble.h"
185#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700186#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800187#include "disp8.h"
H. Peter Anvin172b8402016-02-18 01:16:18 -0800188#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000189
H. Peter Anvin65289e82009-07-25 17:25:11 -0700190enum match_result {
191 /*
192 * Matching errors. These should be sorted so that more specific
193 * errors come later in the sequence.
194 */
195 MERR_INVALOP,
196 MERR_OPSIZEMISSING,
197 MERR_OPSIZEMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700198 MERR_BRNOTHERE,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700199 MERR_BRNUMMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700200 MERR_MASKNOTHERE,
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700201 MERR_DECONOTHERE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700202 MERR_BADCPU,
203 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800204 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700205 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700206 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800207 MERR_BADREPNE,
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700208 MERR_REGSETSIZE,
209 MERR_REGSET,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700210 /*
211 * Matching success; the conditional ones first
212 */
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700213 MOK_JUMP, /* Matching OK but needs jmp_match() */
214 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700215};
216
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000217typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700218 enum ea_type type; /* what kind of EA is this? */
219 int sib_present; /* is a SIB byte necessary? */
220 int bytes; /* # of bytes of offset needed */
221 int size; /* lazy - this is sib+bytes+1 */
222 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700223 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224} ea;
225
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400226#define GEN_SIB(scale, index, base) \
227 (((scale) << 6) | ((index) << 3) | ((base)))
228
229#define GEN_MODRM(mod, reg, rm) \
230 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
231
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800232static int64_t calcsize(int32_t, int64_t, int, insn *,
233 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700234static int emit_prefix(struct out_data *data, const int bits, insn *ins);
235static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700236static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400237 insn *instruction,
238 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700239static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700240static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000241static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700242static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000243static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700244static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700245static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000246
H. Peter Anvin8f622462017-04-02 19:02:29 -0700247static enum ea_type process_ea(operand *, ea *, int, int,
248 opflags_t, insn *, const char **);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700249
H. Peter Anvin164d2462017-02-20 02:39:56 -0800250static inline bool absolute_op(const struct operand *o)
251{
252 return o->segment == NO_SEG && o->wrt == NO_SEG &&
253 !(o->opflags & OPFLAG_RELATIVE);
254}
255
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400256static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000257{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700258 return ins->prefixes[pos] == prefix;
259}
260
261static void assert_no_prefix(insn * ins, enum prefix_pos pos)
262{
263 if (ins->prefixes[pos])
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300264 nasm_nonfatal("invalid %s prefix", prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265}
266
267static const char *size_name(int size)
268{
269 switch (size) {
270 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400271 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700272 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400273 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700274 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700276 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400277 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700278 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400279 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700280 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700282 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400283 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700284 case 64:
285 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700286 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400287 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000288 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700289}
290
H. Peter Anvin285222f2017-03-01 13:27:33 -0800291static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400292{
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800293 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW, "%s data exceeds bounds",
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300294 size_name(size));
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400295}
296
297static void warn_overflow_const(int64_t data, int size)
298{
299 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800300 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400301}
302
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800303static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
304{
305 bool err;
306
307 switch (sign) {
308 case OUT_WRAP:
309 err = overflow_general(data, size);
310 break;
311 case OUT_SIGNED:
312 err = overflow_signed(data, size);
313 break;
314 case OUT_UNSIGNED:
315 err = overflow_unsigned(data, size);
316 break;
317 default:
318 panic();
319 break;
320 }
321
322 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800323 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800324}
325
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000326/*
327 * This routine wrappers the real output format's output routine,
328 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800329 * generator at the same time, flatten unnecessary relocations,
330 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000331 */
H. Peter Anvin (Intel)38ddb192019-01-11 12:27:02 -0800332/*
333 * This warning is currently issued by backends, but in the future
334 * this code should be centralized.
335 *
336 *!zeroing [on] RESx in initialized section becomes zero
337 *! a \c{RESx} directive was used in a section which contains
338 *! initialized data, and the output format does not support
339 *! this. Instead, this will be replaced with explicit zero
340 *! content, which may produce a large output file.
341 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700342static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000343{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000344 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700345 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700346 union {
347 uint8_t b[8];
348 uint64_t q;
349 } xdata;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700350 size_t asize, amax;
351 uint64_t zeropad = 0;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800352 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800353 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000354
H. Peter Anvina77692b2016-09-20 14:04:33 -0700355 if (!data->size)
356 return; /* Nothing to do */
357
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700358 /*
359 * Convert addresses to RAWDATA if possible
360 * XXX: not all backends want this for global symbols!!!!
361 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700362 switch (data->type) {
363 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800364 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800365 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800366 goto address;
367
368 case OUT_RELADDR:
369 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800370 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800371 goto address;
372
373 address:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700374 nasm_assert(data->size <= 8);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700375 asize = data->size;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700376 amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
Chang S. Bae427d8e32018-05-02 08:07:52 -0700377 if ((ofmt->flags & OFMT_KEEP_ADDR) == 0 && data->tsegment == fixseg &&
378 data->twrt == NO_SEG) {
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800379 warn_overflow_out(addrval, asize, data->sign);
Martin Storsjö869087d2017-05-22 13:54:20 +0300380 xdata.q = cpu_to_le64(addrval);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700381 data->data = xdata.b;
382 data->type = OUT_RAWDATA;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700383 asize = amax = 0; /* No longer an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700384 }
385 break;
386
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700387 case OUT_SEGMENT:
388 nasm_assert(data->size <= 8);
389 asize = data->size;
390 amax = 2;
391 break;
392
H. Peter Anvina77692b2016-09-20 14:04:33 -0700393 default:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700394 asize = amax = 0; /* Not an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700395 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000396 }
397
Frank Kotlerabebb082003-09-06 04:45:37 +0000398 /*
399 * this call to src_get determines when we call the
400 * debug-format-specific "linenum" function
401 * it updates lineno and lnfname to the current values
402 * returning 0 if "same as last time", -2 if lnfname
403 * changed, and the amount by which lineno changed,
404 * if it did. thus, these variables must be static
405 */
406
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400407 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700408 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000409
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700410 if (asize > amax) {
411 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300412 nasm_nonfatal("%u-bit signed relocation unsupported by output format %s",
413 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800414 } else {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800415 /*!
416 *!zext-reloc [on] relocation zero-extended to match output format
417 *! warns that a relocation has been zero-extended due
418 *! to limitations in the output format.
419 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800420 nasm_warn(WARN_ZEXT_RELOC,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700421 "%u-bit %s relocation zero-extended from %u bits",
422 (unsigned int)(asize << 3),
423 data->type == OUT_SEGMENT ? "segment" : "unsigned",
424 (unsigned int)(amax << 3));
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800425 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700426 zeropad = data->size - amax;
427 data->size = amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800428 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700429 lfmt->output(data);
H. Peter Anvined859f72018-06-15 00:03:53 -0700430
431 if (likely(data->segment != NO_SEG)) {
432 ofmt->output(data);
433 } else {
434 /* Outputting to ABSOLUTE section - only reserve is permitted */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300435 if (data->type != OUT_RESERVE)
436 nasm_nonfatal("attempt to assemble code in [ABSOLUTE] space");
H. Peter Anvined859f72018-06-15 00:03:53 -0700437 /* No need to push to the backend */
438 }
439
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700440 data->offset += data->size;
441 data->insoffs += data->size;
442
443 if (zeropad) {
444 data->type = OUT_ZERODATA;
445 data->size = zeropad;
446 lfmt->output(data);
447 ofmt->output(data);
448 data->offset += zeropad;
449 data->insoffs += zeropad;
450 data->size += zeropad; /* Restore original size value */
451 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000452}
453
H. Peter Anvina77692b2016-09-20 14:04:33 -0700454static inline void out_rawdata(struct out_data *data, const void *rawdata,
455 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400456{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700457 data->type = OUT_RAWDATA;
458 data->data = rawdata;
459 data->size = size;
460 out(data);
461}
462
463static void out_rawbyte(struct out_data *data, uint8_t byte)
464{
465 data->type = OUT_RAWDATA;
466 data->data = &byte;
467 data->size = 1;
468 out(data);
469}
470
471static inline void out_reserve(struct out_data *data, uint64_t size)
472{
473 data->type = OUT_RESERVE;
474 data->size = size;
475 out(data);
476}
477
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700478static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700479{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700480 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300481 nasm_nonfatal("segment references cannot be relative");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700482
483 data->type = OUT_SEGMENT;
484 data->sign = OUT_UNSIGNED;
485 data->size = 2;
486 data->toffset = opx->offset;
487 data->tsegment = ofmt->segbase(opx->segment | 1);
488 data->twrt = opx->wrt;
489 out(data);
490}
491
492static void out_imm(struct out_data *data, const struct operand *opx,
493 int size, enum out_sign sign)
494{
495 if (opx->segment != NO_SEG && (opx->segment & 1)) {
496 /*
497 * This is actually a segment reference, but eval() has
498 * already called ofmt->segbase() for us. Sigh.
499 */
500 if (size < 2)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300501 nasm_nonfatal("segment reference must be 16 bits");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700502
503 data->type = OUT_SEGMENT;
504 } else {
505 data->type = (opx->opflags & OPFLAG_RELATIVE)
506 ? OUT_RELADDR : OUT_ADDRESS;
507 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700508 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700509 data->toffset = opx->offset;
510 data->tsegment = opx->segment;
511 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800512 /*
513 * XXX: improve this if at some point in the future we can
514 * distinguish the subtrahend in expressions like [foo - bar]
515 * where bar is a symbol in the current segment. However, at the
516 * current point, if OPFLAG_RELATIVE is set that subtraction has
517 * already occurred.
518 */
519 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700520 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700521 out(data);
522}
523
H. Peter Anvin164d2462017-02-20 02:39:56 -0800524static void out_reladdr(struct out_data *data, const struct operand *opx,
525 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700526{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800527 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300528 nasm_nonfatal("invalid use of self-relative expression");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800529
H. Peter Anvina77692b2016-09-20 14:04:33 -0700530 data->type = OUT_RELADDR;
531 data->sign = OUT_SIGNED;
532 data->size = size;
533 data->toffset = opx->offset;
534 data->tsegment = opx->segment;
535 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800536 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700537 out(data);
538}
539
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700540static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800541 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800543 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800544 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000545 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800546 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000547
H. Peter Anvin755f5212012-02-25 11:41:34 -0800548 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700549 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300550 if (!optimizing.level || (optimizing.flag & OPTIM_DISABLE_JMP_MATCH))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400551 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300552 if (optimizing.level < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400553 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700554
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800555 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100556
Victor van den Elzen154e5922009-02-25 17:32:00 +0100557 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100558 /* Be optimistic in pass 1 */
559 return true;
560
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700562 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000563
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700564 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800565 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
566
567 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
568 /* jmp short (opcode eb) cannot be used with bnd prefix. */
569 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800570 /*!
571 *!bnd [on] invalid BND prefixes
572 *! warns about ineffective use of the \c{BND} prefix when the
573 *! \c{JMP} instruction is converted to the \c{SHORT} form.
574 *! This should be extremely rare since the short \c{JMP} only
575 *! is applicable to jumps inside the same module, but if
576 *! it is legitimate, it may be necessary to use
H. Peter Anvin959702b2019-06-06 20:56:50 -0700577 *! \c{bnd jmp dword}.
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800578 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800579 nasm_warn(WARN_BND | ERR_PASS2 ,
H. Peter Anvin959702b2019-06-06 20:56:50 -0700580 "jmp short does not init bnd regs - bnd prefix dropped");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800581 }
582
583 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000584}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000585
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700586static inline int64_t merge_resb(insn *ins, int64_t isize)
587{
588 int nbytes = resb_bytes(ins->opcode);
589
590 if (likely(!nbytes))
591 return isize;
592
593 if (isize != nbytes * ins->oprs[0].offset)
594 return isize; /* Has prefixes of some sort */
595
596 ins->oprs[0].offset *= ins->times;
597 isize *= ins->times;
598 ins->times = 1;
599 return isize;
600}
601
H. Peter Anvin04445362016-09-21 15:56:19 -0700602/* This is totally just a wild guess what is reasonable... */
603#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
604
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800605int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000606{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700607 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000608 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700609 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300610 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000611
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700612 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700613 data.offset = start;
614 data.segment = segment;
615 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700616 data.bits = bits;
617
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700618 wsize = db_bytes(instruction->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300619 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000621
H. Peter Anvineba20a72002-04-30 20:53:55 +0000622 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700624
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700625 list_for_each(e, instruction->eops) {
626 if (e->type == EOT_DB_NUMBER) {
627 if (wsize > 8) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300628 nasm_nonfatal("integer supplied to a DT,DO,DY or DZ");
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700629 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700630 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700631 data.inslen = data.size = wsize;
632 data.toffset = e->offset;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700633 data.twrt = e->wrt;
634 data.relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700635 if (e->segment != NO_SEG && (e->segment & 1)) {
636 data.tsegment = e->segment;
637 data.type = OUT_SEGMENT;
638 data.sign = OUT_UNSIGNED;
639 } else {
640 data.tsegment = e->segment;
641 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
642 data.sign = OUT_WRAP;
643 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700644 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700646 } else if (e->type == EOT_DB_STRING ||
647 e->type == EOT_DB_STRING_FREE) {
648 int align = e->stringlen % wsize;
649 if (align)
650 align = wsize - align;
651
652 data.insoffs = 0;
653 data.inslen = e->stringlen + align;
654
655 out_rawdata(&data, e->stringval, e->stringlen);
656 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700657 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700659 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700660 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700662 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700663 off_t base = 0;
664 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700665 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700666 char *buf = NULL;
667 size_t blk = 0; /* Buffered I/O block size */
668 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000669
H. Peter Anvin94ead272017-09-27 15:22:23 -0700670 if (!t)
671 goto done;
672
H. Peter Anvind81a2352016-09-21 14:03:18 -0700673 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400674 if (!fp) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300675 nasm_nonfatal("`incbin': unable to open file `%s'",
676 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700677 goto done;
678 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000679
H. Peter Anvind81a2352016-09-21 14:03:18 -0700680 len = nasm_file_size(fp);
681
682 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300683 nasm_nonfatal("`incbin': unable to get length of file `%s'",
684 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700685 goto close_done;
686 }
687
H. Peter Anvina77692b2016-09-20 14:04:33 -0700688 if (instruction->eops->next) {
689 base = instruction->eops->next->offset;
690 if (base >= len) {
691 len = 0;
692 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 len -= base;
694 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700695 len > (off_t)instruction->eops->next->next->offset)
696 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700699
H. Peter Anvina77692b2016-09-20 14:04:33 -0700700 lfmt->set_offset(data.offset);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700701 lfmt->uplevel(LIST_INCBIN, len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000702
H. Peter Anvind81a2352016-09-21 14:03:18 -0700703 if (!len)
704 goto end_incbin;
705
706 /* Try to map file data */
707 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700708 if (!map) {
709 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
710 buf = nasm_malloc(blk);
711 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700712
713 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700714 /*
715 * Consider these irrelevant for INCBIN, since it is fully
716 * possible that these might be (way) bigger than an int
717 * can hold; there is, however, no reason to widen these
718 * types just for INCBIN. data.inslen == 0 signals to the
719 * backend that these fields are meaningless, if at all
720 * needed.
721 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700722 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700723 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700724
H. Peter Anvind81a2352016-09-21 14:03:18 -0700725 if (map) {
726 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700727 } else if ((off_t)m == len) {
728 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700729 } else {
730 off_t l = len;
731
732 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300733 nasm_nonfatal("`incbin': unable to seek on file `%s'",
734 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700735 goto end_incbin;
736 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700737 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700738 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700739 if (!m || feof(fp)) {
740 /*
741 * This shouldn't happen unless the file
742 * actually changes while we are reading
743 * it.
744 */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300745 nasm_nonfatal("`incbin': unexpected EOF while"
746 " reading file `%s'", fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700747 goto end_incbin;
748 }
749 out_rawdata(&data, buf, m);
750 l -= m;
751 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700752 }
753 }
754 end_incbin:
755 lfmt->downlevel(LIST_INCBIN);
756 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700757 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700758 lfmt->downlevel(LIST_TIMES);
759 }
760 if (ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300761 nasm_nonfatal("`incbin': error while"
762 " reading file `%s'", fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700763 }
764 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700765 if (buf)
766 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700767 if (map)
768 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700769 fclose(fp);
770 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700771 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700772 ;
773 } else {
774 /* "Real" instruction */
775
776 /* Check to see if we need an address-size prefix */
777 add_asp(instruction, bits);
778
779 m = find_match(&temp, instruction, data.segment, data.offset, bits);
780
781 if (m == MOK_GOOD) {
782 /* Matches! */
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700783 if (unlikely(itemp_has(temp, IF_OBSOLETE))) {
784 /*
785 * If IF_OBSOLETE is set, warn unless we have *exactly*
786 * the correct CPU level set.
787 *
788 *!obsolete [on] instruction obsolete for the target CPU
789 *! warns if an instruction which has been removed
790 *! from the architecture, and is no longer included
791 *! in the CPU definition given in the \c{[CPU]}
792 *! directive, for example \c{POP CS}, the opcode for
793 *! which, \c{0Fh}, instead is an opcode prefix on
794 *! CPUs newer than the first generation 8086.
795 */
796
797 if (iflag_cmp_cpu_level(&insns_flags[temp->iflag_idx], &cpu)) {
798 nasm_warn(WARN_OBSOLETE,
799 "obsolete instruction invalid on the target CPU");
800 }
801 }
802
H. Peter Anvina77692b2016-09-20 14:04:33 -0700803 data.itemp = temp;
804 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700805 data.insoffs = 0;
H. Peter Anvin (Intel)77335212019-08-06 23:22:48 -0700806
807 data.inslen = calcsize(data.segment, data.offset,
808 bits, instruction, temp);
809 nasm_assert(data.inslen >= 0);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700810 data.inslen = merge_resb(instruction, data.inslen);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700811
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700812 gencode(&data, instruction);
H. Peter Anvin (Intel)77335212019-08-06 23:22:48 -0700813 nasm_assert(data.insoffs == data.inslen);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700814 } else {
815 /* No match */
816 switch (m) {
817 case MERR_OPSIZEMISSING:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300818 nasm_nonfatal("operation size not specified");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700819 break;
820 case MERR_OPSIZEMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300821 nasm_nonfatal("mismatch in operand sizes");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700822 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700823 case MERR_BRNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300824 nasm_nonfatal("broadcast not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700825 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700826 case MERR_BRNUMMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300827 nasm_nonfatal("mismatch in the number of broadcasting elements");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700828 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700829 case MERR_MASKNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300830 nasm_nonfatal("mask not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700831 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700832 case MERR_DECONOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300833 nasm_nonfatal("unsupported mode decorator for instruction");
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700834 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700835 case MERR_BADCPU:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300836 nasm_nonfatal("no instruction for this cpu level");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700837 break;
838 case MERR_BADMODE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300839 nasm_nonfatal("instruction not supported in %d-bit mode", bits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700840 break;
841 case MERR_ENCMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300842 nasm_nonfatal("specific encoding scheme not available");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700843 break;
844 case MERR_BADBND:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300845 nasm_nonfatal("bnd prefix is not allowed");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700846 break;
847 case MERR_BADREPNE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300848 nasm_nonfatal("%s prefix is not allowed",
849 (has_prefix(instruction, PPS_REP, P_REPNE) ?
850 "repne" : "repnz"));
H. Peter Anvina77692b2016-09-20 14:04:33 -0700851 break;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700852 case MERR_REGSETSIZE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300853 nasm_nonfatal("invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700854 break;
855 case MERR_REGSET:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300856 nasm_nonfatal("register set not valid for operand");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700857 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700858 default:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300859 nasm_nonfatal("invalid combination of opcode and operands");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700860 break;
861 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700862
863 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400864 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000865 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700866 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000867}
868
H. Peter Anvin29651542018-12-18 19:14:40 -0800869static void debug_set_db_type(insn *instruction)
870{
871 /* Is this really correct? .operands doesn't mean much for Dx */
872 int32_t typeinfo = TYS_ELEMENTS(instruction->operands);
873
874 switch (instruction->opcode) {
875 case I_DB:
876 typeinfo |= TY_BYTE;
877 break;
878 case I_DW:
879 typeinfo |= TY_WORD;
880 break;
881 case I_DD:
882 if (instruction->eops_float)
883 typeinfo |= TY_FLOAT;
884 else
885 typeinfo |= TY_DWORD;
886 break;
887 case I_DQ:
888 /* What about double? */
889 typeinfo |= TY_QWORD;
890 break;
891 case I_DT:
892 /* What about long double? */
893 typeinfo |= TY_TBYTE;
894 break;
895 case I_DO:
896 typeinfo |= TY_OWORD;
897 break;
898 case I_DY:
899 typeinfo |= TY_YWORD;
900 break;
901 case I_DZ:
902 typeinfo |= TY_ZWORD;
903 break;
904 default:
905 panic();
906 }
907
908 dfmt->debug_typevalue(typeinfo);
909}
910
911static void debug_set_type(insn *instruction)
912{
913 int32_t typeinfo;
914
915 if (opcode_is_resb(instruction->opcode)) {
916 typeinfo = TYS_ELEMENTS(instruction->oprs[0].offset);
917
918 switch (instruction->opcode) {
919 case I_RESB:
920 typeinfo |= TY_BYTE;
921 break;
922 case I_RESW:
923 typeinfo |= TY_WORD;
924 break;
925 case I_RESD:
926 typeinfo |= TY_DWORD;
927 break;
928 case I_RESQ:
929 typeinfo |= TY_QWORD;
930 break;
931 case I_REST:
932 typeinfo |= TY_TBYTE;
933 break;
934 case I_RESO:
935 typeinfo |= TY_OWORD;
936 break;
937 case I_RESY:
938 typeinfo |= TY_YWORD;
939 break;
940 case I_RESZ:
941 typeinfo |= TY_ZWORD;
942 break;
943 default:
944 panic();
945 }
946 } else {
947 typeinfo = TY_LABEL;
948 }
949
950 dfmt->debug_typevalue(typeinfo);
951}
952
953
954/* Proecess an EQU directive */
955static void define_equ(insn * instruction)
956{
957 if (!instruction->label) {
958 nasm_nonfatal("EQU not preceded by label");
959 } else if (instruction->operands == 1 &&
960 (instruction->oprs[0].type & IMMEDIATE) &&
961 instruction->oprs[0].wrt == NO_SEG) {
962 define_label(instruction->label,
963 instruction->oprs[0].segment,
964 instruction->oprs[0].offset, false);
965 } else if (instruction->operands == 2
966 && (instruction->oprs[0].type & IMMEDIATE)
967 && (instruction->oprs[0].type & COLON)
968 && instruction->oprs[0].segment == NO_SEG
969 && instruction->oprs[0].wrt == NO_SEG
970 && (instruction->oprs[1].type & IMMEDIATE)
971 && instruction->oprs[1].segment == NO_SEG
972 && instruction->oprs[1].wrt == NO_SEG) {
973 define_label(instruction->label,
974 instruction->oprs[0].offset | SEG_ABS,
975 instruction->oprs[1].offset, false);
976 } else {
977 nasm_nonfatal("bad syntax for EQU");
978 }
979}
980
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700981
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800982int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000983{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000984 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700985 enum match_result m;
H. Peter Anvin29651542018-12-18 19:14:40 -0800986 int64_t isize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000987
H. Peter Anvin29651542018-12-18 19:14:40 -0800988 if (instruction->opcode == I_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 return 0;
H. Peter Anvin29651542018-12-18 19:14:40 -0800990 } else if (instruction->opcode == I_EQU) {
991 define_equ(instruction);
992 return 0;
993 } else if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 extop *e;
H. Peter Anvin29651542018-12-18 19:14:40 -0800995 int32_t osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000996
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700997 wsize = db_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700998 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000999
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +04001000 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001001 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001002
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001004 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001006 warn_overflow_const(e->offset, wsize);
1007 } else if (e->type == EOT_DB_STRING ||
1008 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001010
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 align = (-osize) % wsize;
1012 if (align < 0)
1013 align += wsize;
1014 isize += osize + align;
1015 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001016
H. Peter Anvin29651542018-12-18 19:14:40 -08001017 debug_set_db_type(instruction);
1018 return isize;
1019 } else if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001020 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001021 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001022
H. Peter Anvind81a2352016-09-21 14:03:18 -07001023 len = nasm_file_size_by_path(fname);
1024 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001025 nasm_nonfatal("`incbin': unable to get length of file `%s'",
1026 fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001027 return 0;
1028 }
1029
1030 if (instruction->eops->next) {
1031 if (len <= (off_t)instruction->eops->next->offset) {
1032 len = 0;
1033 } else {
1034 len -= instruction->eops->next->offset;
1035 if (instruction->eops->next->next &&
1036 len > (off_t)instruction->eops->next->next->offset) {
1037 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 }
1039 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001041
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001042 len *= instruction->times;
1043 instruction->times = 1; /* Tell the upper layer to not iterate */
1044
H. Peter Anvind81a2352016-09-21 14:03:18 -07001045 return len;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001046 } else {
H. Peter Anvin29651542018-12-18 19:14:40 -08001047 /* Normal instruction, or RESx */
1048
1049 /* Check to see if we need an address-size prefix */
1050 add_asp(instruction, bits);
1051
1052 m = find_match(&temp, instruction, segment, offset, bits);
1053 if (m != MOK_GOOD)
1054 return -1; /* No match */
1055
1056 isize = calcsize(segment, offset, bits, instruction, temp);
1057 debug_set_type(instruction);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001058 isize = merge_resb(instruction, isize);
H. Peter Anvin29651542018-12-18 19:14:40 -08001059
1060 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001061 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001062}
1063
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001064static void bad_hle_warn(const insn * ins, uint8_t hleok)
1065{
1066 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001067 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001068 static const enum whatwarn warn[2][4] =
1069 {
1070 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
1071 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
1072 };
1073 unsigned int n;
1074
1075 n = (unsigned int)rep_pfx - P_XACQUIRE;
1076 if (n > 1)
1077 return; /* Not XACQUIRE/XRELEASE */
1078
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001079 ww = warn[n][hleok];
1080 if (!is_class(MEMORY, ins->oprs[0].type))
1081 ww = w_inval; /* HLE requires operand 0 to be memory */
1082
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001083 /*!
1084 *!hle [on] invalid HLE prefixes
1085 *! warns about invalid use of the HLE \c{XACQUIRE} or \c{XRELEASE}
1086 *! prefixes.
1087 */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001088 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001089 case w_none:
1090 break;
1091
1092 case w_lock:
1093 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001094 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001095 "%s with this instruction requires lock",
1096 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001097 }
1098 break;
1099
1100 case w_inval:
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001101 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001102 "%s invalid with this instruction",
1103 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001104 break;
1105 }
1106}
1107
H. Peter Anvin507ae032008-10-09 15:37:10 -07001108/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +04001109#define case3(x) case (x): case (x)+1: case (x)+2
1110#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -07001111
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001112static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001113 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001114{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001115 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001116 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001117 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001118 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001119 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001120 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001121 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001122 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001123 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001124 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -07001125 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -07001126 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001127
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001128 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001129 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001130 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001131
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001132 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001133 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001134
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 (void)segment; /* Don't warn that this parameter is unused */
1136 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001137
H. Peter Anvin839eca22007-10-29 23:12:47 -07001138 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001139 c = *codes++;
1140 op1 = (c & 3) + ((opex & 1) << 2);
1141 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1142 opx = &ins->oprs[op1];
1143 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001144
H. Peter Anvin839eca22007-10-29 23:12:47 -07001145 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001146 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 codes += c, length += c;
1148 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001149
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001150 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001151 opex = c;
1152 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001153
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001154 case4(010):
1155 ins->rex |=
1156 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 codes++, length++;
1158 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001159
Jin Kyu Song164d6072013-10-15 19:10:13 -07001160 case4(014):
1161 /* this is an index reg of MIB operand */
1162 mib_index = opx->basereg;
1163 break;
1164
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001165 case4(020):
1166 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 length++;
1168 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001169
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001170 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 length += 2;
1172 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001173
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001174 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001175 if (opx->type & (BITS16 | BITS32 | BITS64))
1176 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 else
1178 length += (bits == 16) ? 2 : 4;
1179 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001180
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001181 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 length += 4;
1183 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001184
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001185 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001186 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001188
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001189 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190 length++;
1191 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001192
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001193 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001194 length += 8; /* MOV reg64/imm */
1195 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001196
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001197 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 length += 2;
1199 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001200
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001201 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001202 if (opx->type & (BITS16 | BITS32 | BITS64))
1203 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 else
1205 length += (bits == 16) ? 2 : 4;
1206 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001207
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001208 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 length += 4;
1210 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001211
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001212 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001213 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001215
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001216 case 0172:
1217 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001218 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001219 length++;
1220 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001221
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001222 case4(0174):
1223 length++;
1224 break;
1225
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001226 case4(0240):
1227 ins->rex |= REX_EV;
1228 ins->vexreg = regval(opx);
1229 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1230 ins->vex_cm = *codes++;
1231 ins->vex_wlp = *codes++;
1232 ins->evex_tuple = (*codes++ - 0300);
1233 break;
1234
1235 case 0250:
1236 ins->rex |= REX_EV;
1237 ins->vexreg = 0;
1238 ins->vex_cm = *codes++;
1239 ins->vex_wlp = *codes++;
1240 ins->evex_tuple = (*codes++ - 0300);
1241 break;
1242
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001243 case4(0254):
1244 length += 4;
1245 break;
1246
1247 case4(0260):
1248 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001249 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001250 ins->vex_cm = *codes++;
1251 ins->vex_wlp = *codes++;
1252 break;
1253
1254 case 0270:
1255 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001256 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001257 ins->vex_cm = *codes++;
1258 ins->vex_wlp = *codes++;
1259 break;
1260
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001261 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001262 hleok = c & 3;
1263 break;
1264
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001265 case4(0274):
1266 length++;
1267 break;
1268
1269 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001271
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001273 if (bits == 64)
1274 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001275 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001277
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001279 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001281
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001283 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001284
Keith Kaniosb7a89542007-04-12 02:40:54 +00001285 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001286 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1287 has_prefix(ins, PPS_ASIZE, P_A32))
1288 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001290
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001291 case4(0314):
1292 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001293
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001295 {
1296 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1297 if (pfx == P_O16)
1298 break;
1299 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001300 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001301 else
1302 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001304 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001305
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001307 {
1308 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1309 if (pfx == P_O32)
1310 break;
1311 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001312 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001313 else
1314 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001316 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001317
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 case 0322:
1319 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001320
Keith Kaniosb7a89542007-04-12 02:40:54 +00001321 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001322 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001323 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001324
Keith Kaniosb7a89542007-04-12 02:40:54 +00001325 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001326 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001327 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001328
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001329 case 0325:
1330 ins->rex |= REX_NH;
1331 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001332
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001333 case 0326:
1334 break;
1335
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 case 0330:
1337 codes++, length++;
1338 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001339
H. Peter Anvine2c80182005-01-15 22:15:51 +00001340 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001341 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001342
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001343 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 case 0333:
1345 length++;
1346 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001347
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001348 case 0334:
1349 ins->rex |= REX_L;
1350 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001351
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001352 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001353 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001354
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001355 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001356 if (!ins->prefixes[PPS_REP])
1357 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001358 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001359
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001360 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001361 if (!ins->prefixes[PPS_REP])
1362 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001363 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001364
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001366 if (!absolute_op(&ins->oprs[0]))
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001367 nasm_nonfatal("attempt to reserve non-constant"
1368 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001369 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001370 nasm_warn(WARN_OTHER, "forward reference in RESx "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001371 "can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001372 else
H. Peter Anvin5358b982018-12-18 18:06:26 -08001373 length += ins->oprs[0].offset * resb_bytes(ins->opcode);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001375
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001376 case 0341:
1377 if (!ins->prefixes[PPS_WAIT])
1378 ins->prefixes[PPS_WAIT] = P_WAIT;
1379 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001380
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001381 case 0360:
1382 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001383
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001384 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001385 length++;
1386 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001387
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001388 case 0364:
1389 case 0365:
1390 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001391
Keith Kanios48af1772007-08-17 07:37:52 +00001392 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001393 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001394 length++;
1395 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001396
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001397 case 0370:
1398 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001399 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001400
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 case 0373:
1402 length++;
1403 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001404
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001405 case 0374:
1406 eat = EA_XMMVSIB;
1407 break;
1408
1409 case 0375:
1410 eat = EA_YMMVSIB;
1411 break;
1412
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001413 case 0376:
1414 eat = EA_ZMMVSIB;
1415 break;
1416
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001417 case4(0100):
1418 case4(0110):
1419 case4(0120):
1420 case4(0130):
1421 case4(0200):
1422 case4(0204):
1423 case4(0210):
1424 case4(0214):
1425 case4(0220):
1426 case4(0224):
1427 case4(0230):
1428 case4(0234):
1429 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001431 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001432 opflags_t rflags;
1433 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001434 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001435
Keith Kaniosb7a89542007-04-12 02:40:54 +00001436 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001437
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001438 if (c <= 0177) {
1439 /* pick rfield from operand b (opx) */
1440 rflags = regflag(opx);
1441 rfield = nasm_regvals[opx->basereg];
1442 } else {
1443 rflags = 0;
1444 rfield = c & 7;
1445 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001446
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001447 /* EVEX.b1 : evex_brerop contains the operand position */
1448 op_er_sae = (ins->evex_brerop >= 0 ?
1449 &ins->oprs[ins->evex_brerop] : NULL);
1450
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001451 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1452 /* set EVEX.b */
1453 ins->evex_p[2] |= EVEX_P2B;
1454 if (op_er_sae->decoflags & ER) {
1455 /* set EVEX.RC (rounding control) */
1456 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1457 & EVEX_P2RC;
1458 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001459 } else {
1460 /* set EVEX.L'L (vector length) */
1461 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001462 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001463 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001464 /* set EVEX.b */
1465 ins->evex_p[2] |= EVEX_P2B;
1466 }
1467 }
1468
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001469 if (itemp_has(temp, IF_MIB)) {
1470 opy->eaflags |= EAF_MIB;
1471 /*
1472 * if a separate form of MIB (ICC style) is used,
1473 * the index reg info is merged into mem operand
1474 */
1475 if (mib_index != R_none) {
1476 opy->indexreg = mib_index;
1477 opy->scale = 1;
1478 opy->hintbase = mib_index;
1479 opy->hinttype = EAH_NOTBASE;
1480 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001481 }
1482
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001483 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001484 rfield, rflags, ins, &errmsg) != eat) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001485 nasm_nonfatal("%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001486 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001487 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001488 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001489 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001490 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001491 }
1492 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001493
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001494 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001495 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001496 ": instruction code \\%o (0x%02X) given", c, c);
1497 break;
1498 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001499 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001500
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001501 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001502
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001503 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001504 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001505 nasm_nonfatal("instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001506 return -1;
1507 }
1508 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001509 }
1510
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001511 switch (ins->prefixes[PPS_VEX]) {
1512 case P_EVEX:
1513 if (!(ins->rex & REX_EV))
1514 return -1;
1515 break;
1516 case P_VEX3:
1517 case P_VEX2:
1518 if (!(ins->rex & REX_V))
1519 return -1;
1520 break;
1521 default:
1522 break;
1523 }
1524
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001525 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001526 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001527
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001528 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001529 nasm_nonfatal("cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001530 return -1;
1531 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001532 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001533 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001534 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001535 ins->rex &= ~REX_W;
1536 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001537 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001538 ins->rex |= REX_W;
1539 bad32 &= ~REX_W;
1540 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001541 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001542 /* Follow REX_W */
1543 break;
1544 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001545
H. Peter Anvinfc561202011-07-07 16:58:22 -07001546 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001547 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001548 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001549 } else if (!(ins->rex & REX_EV) &&
1550 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001551 nasm_nonfatal("invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001552 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001553 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001554 if (ins->rex & REX_EV)
1555 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001556 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1557 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001558 length += 3;
1559 else
1560 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001561 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001562 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001563 nasm_nonfatal("cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001564 return -1;
1565 } else if (bits == 64) {
1566 length++;
1567 } else if ((ins->rex & REX_L) &&
1568 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001569 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001570 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001571 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001572 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001573 length++;
1574 } else {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001575 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001576 return -1;
1577 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001578 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001579
1580 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001581 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001582 /*!
H. Peter Anvin (Intel)be99ebd2018-12-13 22:12:37 -08001583 *!lock [on] LOCK prefix on unlockable instructions
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001584 *! warns about \c{LOCK} prefixes on unlockable instructions.
1585 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001586 nasm_warn(WARN_LOCK | ERR_PASS2 , "instruction is not lockable");
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001587 }
1588
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001589 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001590
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001591 /*
1592 * when BND prefix is set by DEFAULT directive,
1593 * BND prefix is added to every appropriate instruction line
1594 * unless it is overridden by NOBND prefix.
1595 */
1596 if (globalbnd &&
1597 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1598 ins->prefixes[PPS_REP] = P_BND;
1599
H. Peter Anvina77692b2016-09-20 14:04:33 -07001600 /*
1601 * Add length of legacy prefixes
1602 */
1603 length += emit_prefix(NULL, bits, ins);
1604
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001605 return length;
1606}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001607
H. Peter Anvina77692b2016-09-20 14:04:33 -07001608static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001609{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001610 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001611 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001612 !(ins->rex & (REX_V | REX_EV)) &&
1613 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001614 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1615 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001616 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001617 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001618 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001619}
1620
H. Peter Anvina77692b2016-09-20 14:04:33 -07001621static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1622{
1623 int bytes = 0;
1624 int j;
1625
1626 for (j = 0; j < MAXPREFIX; j++) {
1627 uint8_t c = 0;
1628 switch (ins->prefixes[j]) {
1629 case P_WAIT:
1630 c = 0x9B;
1631 break;
1632 case P_LOCK:
1633 c = 0xF0;
1634 break;
1635 case P_REPNE:
1636 case P_REPNZ:
1637 case P_XACQUIRE:
1638 case P_BND:
1639 c = 0xF2;
1640 break;
1641 case P_REPE:
1642 case P_REPZ:
1643 case P_REP:
1644 case P_XRELEASE:
1645 c = 0xF3;
1646 break;
1647 case R_CS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001648 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001649 nasm_warn(WARN_OTHER|ERR_PASS2, "cs segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001650 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001651 c = 0x2E;
1652 break;
1653 case R_DS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001654 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001655 nasm_warn(WARN_OTHER|ERR_PASS2, "ds segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001656 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001657 c = 0x3E;
1658 break;
1659 case R_ES:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001660 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001661 nasm_warn(WARN_OTHER|ERR_PASS2, "es segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001662 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001663 c = 0x26;
1664 break;
1665 case R_FS:
1666 c = 0x64;
1667 break;
1668 case R_GS:
1669 c = 0x65;
1670 break;
1671 case R_SS:
1672 if (bits == 64) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001673 nasm_warn(WARN_OTHER|ERR_PASS2, "ss segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001674 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001675 }
1676 c = 0x36;
1677 break;
1678 case R_SEGR6:
1679 case R_SEGR7:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001680 nasm_nonfatal("segr6 and segr7 cannot be used as prefixes");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001681 break;
1682 case P_A16:
1683 if (bits == 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001684 nasm_nonfatal("16-bit addressing is not supported "
1685 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001686 } else if (bits != 16)
1687 c = 0x67;
1688 break;
1689 case P_A32:
1690 if (bits != 32)
1691 c = 0x67;
1692 break;
1693 case P_A64:
1694 if (bits != 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001695 nasm_nonfatal("64-bit addressing is only supported "
1696 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001697 }
1698 break;
1699 case P_ASP:
1700 c = 0x67;
1701 break;
1702 case P_O16:
1703 if (bits != 16)
1704 c = 0x66;
1705 break;
1706 case P_O32:
1707 if (bits == 16)
1708 c = 0x66;
1709 break;
1710 case P_O64:
1711 /* REX.W */
1712 break;
1713 case P_OSP:
1714 c = 0x66;
1715 break;
1716 case P_EVEX:
1717 case P_VEX3:
1718 case P_VEX2:
1719 case P_NOBND:
1720 case P_none:
1721 break;
1722 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001723 nasm_panic("invalid instruction prefix");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001724 }
1725 if (c) {
1726 if (data)
1727 out_rawbyte(data, c);
1728 bytes++;
1729 }
1730 }
1731 return bytes;
1732}
1733
1734static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001735{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001736 uint8_t c;
1737 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001738 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001739 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001740 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001741 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001742 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001743 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001744 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001745 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001746 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001747
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001748 ins->rex_done = false;
1749
H. Peter Anvina77692b2016-09-20 14:04:33 -07001750 emit_prefix(data, bits, ins);
1751
H. Peter Anvin839eca22007-10-29 23:12:47 -07001752 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001753 c = *codes++;
1754 op1 = (c & 3) + ((opex & 1) << 2);
1755 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1756 opx = &ins->oprs[op1];
1757 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001758
H. Peter Anvina77692b2016-09-20 14:04:33 -07001759
H. Peter Anvin839eca22007-10-29 23:12:47 -07001760 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 case 01:
1762 case 02:
1763 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001764 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001765 emit_rex(data, ins);
1766 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001769
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001770 case 05:
1771 case 06:
1772 case 07:
1773 opex = c;
1774 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001775
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001776 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001777 emit_rex(data, ins);
1778 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001780
Jin Kyu Song164d6072013-10-15 19:10:13 -07001781 case4(014):
1782 break;
1783
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001784 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001785 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001787
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001788 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001789 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001791
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001792 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001793 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001795
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001796 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001797 if (opx->type & (BITS16 | BITS32))
1798 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 else
1800 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001801 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001803
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001804 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001805 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001807
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001808 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001809 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001810 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001812
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001813 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001814 if (opx->segment == data->segment) {
1815 int64_t delta = opx->offset - data->offset
1816 - (data->inslen - data->insoffs);
1817 if (delta > 127 || delta < -128)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001818 nasm_nonfatal("short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001819 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001820 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001822
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001823 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001824 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001825 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001826
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001827 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001828 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001830
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001831 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001832 if (opx->type & (BITS16 | BITS32 | BITS64))
1833 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 else
1835 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001836
1837 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001839
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001840 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001841 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001843
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001844 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001845 if (opx->segment == NO_SEG)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001846 nasm_nonfatal("value referenced by FAR is not relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001847 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001849
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001850 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001851 {
1852 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1853 const struct operand *opy;
1854
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001855 c = *codes++;
1856 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001857 opy = &ins->oprs[c & 7];
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001858 if (!absolute_op(opy))
1859 nasm_nonfatal("non-absolute expression not permitted "
1860 "as argument %d", c & 7);
1861 else if (opy->offset & ~mask)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001862 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001863 "is4 argument exceeds bounds");
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001864 c = opy->offset & mask;
1865 goto emit_is4;
1866 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001867
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001868 case 0173:
1869 c = *codes++;
1870 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001871 c &= 15;
1872 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001873
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001874 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001875 c = 0;
1876 emit_is4:
1877 r = nasm_regvals[opx->basereg];
1878 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001879 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001880
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001881 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001882 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001883 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001884 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001885 "signed dword immediate exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001886 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001887 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001888 break;
1889
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001890 case4(0240):
1891 case 0250:
1892 codes += 3;
1893 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1894 EVEX_P2Z | EVEX_P2AAA, 2);
1895 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1896 bytes[0] = 0x62;
1897 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001898 bytes[1] = ((((ins->rex & 7) << 5) |
1899 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001900 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001901 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1902 ((~ins->vexreg & 15) << 3) |
1903 (1 << 2) | (ins->vex_wlp & 3);
1904 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001905 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001906 break;
1907
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001908 case4(0260):
1909 case 0270:
1910 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001911 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1912 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001913 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1914 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1915 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001916 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001917 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001918 } else {
1919 bytes[0] = 0xc5;
1920 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001921 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001922 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001923 }
1924 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001925
H. Peter Anvine014f352012-02-25 22:35:19 -08001926 case 0271:
1927 case 0272:
1928 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001929 break;
1930
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001931 case4(0274):
1932 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001933 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001934 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001935
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001936 if (absolute_op(opx)) {
1937 if (ins->rex & REX_W)
1938 s = 64;
1939 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1940 s = 16;
1941 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1942 s = 32;
1943 else
1944 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001945
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001946 um = (uint64_t)2 << (s-1);
1947 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001948
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001949 if (uv > 127 && uv < (uint64_t)-128 &&
1950 (uv < um-128 || uv > um-1)) {
1951 /* If this wasn't explicitly byte-sized, warn as though we
1952 * had fallen through to the imm16/32/64 case.
1953 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001954 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001955 "%s value exceeds bounds",
1956 (opx->type & BITS8) ? "signed byte" :
1957 s == 16 ? "word" :
1958 s == 32 ? "dword" :
1959 "signed dword");
1960 }
1961
1962 /* Output as a raw byte to avoid byte overflow check */
1963 out_rawbyte(data, (uint8_t)uv);
1964 } else {
1965 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001966 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001967 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001968 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001969
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001970 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001972
H. Peter Anvine2c80182005-01-15 22:15:51 +00001973 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001974 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1975 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001977
H. Peter Anvine2c80182005-01-15 22:15:51 +00001978 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001979 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1980 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001982
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 case 0312:
1984 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001985
Keith Kaniosb7a89542007-04-12 02:40:54 +00001986 case 0313:
1987 ins->rex = 0;
1988 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001989
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001990 case4(0314):
1991 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001992
H. Peter Anvine2c80182005-01-15 22:15:51 +00001993 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001994 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001995 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001996
H. Peter Anvine2c80182005-01-15 22:15:51 +00001997 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001998 case 0323:
1999 break;
2000
Keith Kaniosb7a89542007-04-12 02:40:54 +00002001 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002002 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07002004
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002005 case 0325:
2006 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07002007
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04002008 case 0326:
2009 break;
2010
H. Peter Anvine2c80182005-01-15 22:15:51 +00002011 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002012 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002013 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002014
H. Peter Anvine2c80182005-01-15 22:15:51 +00002015 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002016 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002017
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002018 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002020 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002021 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002022
Keith Kanios48af1772007-08-17 07:37:52 +00002023 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002024 if (ins->rex & REX_R)
2025 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00002026 ins->rex &= ~(REX_L|REX_R);
2027 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00002028
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002029 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002030 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002031
H. Peter Anvin962e3052008-08-28 17:47:16 -07002032 case 0336:
2033 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002034 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07002035
H. Peter Anvine2c80182005-01-15 22:15:51 +00002036 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002037 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002038 nasm_panic("non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07002039
H. Peter Anvin5358b982018-12-18 18:06:26 -08002040 out_reserve(data, ins->oprs[0].offset * resb_bytes(ins->opcode));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002041 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002042
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002043 case 0341:
2044 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08002045
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002046 case 0360:
2047 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002048
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002049 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002050 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002051 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002052
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002053 case 0364:
2054 case 0365:
2055 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002056
Keith Kanios48af1772007-08-17 07:37:52 +00002057 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002058 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002059 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00002060 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002061
Jin Kyu Song03041092013-10-15 19:38:51 -07002062 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002063 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002064
H. Peter Anvine2c80182005-01-15 22:15:51 +00002065 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002066 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002067 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002068
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002069 case 0374:
2070 eat = EA_XMMVSIB;
2071 break;
2072
2073 case 0375:
2074 eat = EA_YMMVSIB;
2075 break;
2076
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002077 case 0376:
2078 eat = EA_ZMMVSIB;
2079 break;
2080
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002081 case4(0100):
2082 case4(0110):
2083 case4(0120):
2084 case4(0130):
2085 case4(0200):
2086 case4(0204):
2087 case4(0210):
2088 case4(0214):
2089 case4(0220):
2090 case4(0224):
2091 case4(0230):
2092 case4(0234):
2093 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002094 ea ea_data;
2095 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002096 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002097 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002098 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07002099
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002100 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002101 /* pick rfield from operand b (opx) */
2102 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07002103 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002104 } else {
2105 /* rfield is constant */
2106 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002107 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002108 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002109
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002110 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002111 rfield, rflags, ins, &errmsg) != eat)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002112 nasm_nonfatal("%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07002113
H. Peter Anvine2c80182005-01-15 22:15:51 +00002114 p = bytes;
2115 *p++ = ea_data.modrm;
2116 if (ea_data.sib_present)
2117 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07002118 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002119
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002120 /*
2121 * Make sure the address gets the right offset in case
2122 * the line breaks in the .lst file (BR 1197827)
2123 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002124
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002125 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002126 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07002127 if (ea_data.disp8) {
2128 out_rawbyte(data, ea_data.disp8);
2129 } else if (ea_data.rip) {
2130 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002131 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002132 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002133
H. Peter Anvina77692b2016-09-20 14:04:33 -07002134 if (overflow_general(opy->offset, asize) ||
2135 signed_bits(opy->offset, ins->addr_size) !=
2136 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08002137 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002138
H. Peter Anvina77692b2016-09-20 14:04:33 -07002139 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07002140 (asize > ea_data.bytes)
2141 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002142 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002144 }
2145 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07002146
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002147 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07002148 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002149 ": instruction code \\%o (0x%02X) given", c, c);
2150 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002151 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07002152 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002153}
2154
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002155static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002156{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002157 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002158 nasm_panic("invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002159 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002160}
2161
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00002162static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002163{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002164 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002165 nasm_panic("invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002166 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002167}
2168
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002169static int op_rexflags(const operand * o, int mask)
2170{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002171 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002172 int val;
2173
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002174 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002175 nasm_panic("invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002176
H. Peter Anvina4835d42008-05-20 14:21:29 -07002177 flags = nasm_reg_flags[o->basereg];
2178 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002179
2180 return rexflags(val, flags, mask);
2181}
2182
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002183static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002184{
2185 int rex = 0;
2186
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002187 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002188 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002189 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002190 rex |= REX_W;
2191 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2192 rex |= REX_H;
2193 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2194 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002195
2196 return rex & mask;
2197}
2198
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002199static int evexflags(int val, decoflags_t deco,
2200 int mask, uint8_t byte)
2201{
2202 int evex = 0;
2203
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002204 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002205 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002206 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002207 evex |= (EVEX_P0RP | EVEX_P0X);
2208 break;
2209 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002210 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002211 evex |= EVEX_P2VP;
2212 if (deco & Z)
2213 evex |= EVEX_P2Z;
2214 if (deco & OPMASK_MASK)
2215 evex |= deco & EVEX_P2AAA;
2216 break;
2217 }
2218 return evex & mask;
2219}
2220
2221static int op_evexflags(const operand * o, int mask, uint8_t byte)
2222{
2223 int val;
2224
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002225 val = nasm_regvals[o->basereg];
2226
2227 return evexflags(val, o->decoflags, mask, byte);
2228}
2229
H. Peter Anvin23595f52009-07-25 17:44:25 -07002230static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002231 insn *instruction,
2232 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002233{
2234 const struct itemplate *temp;
2235 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002236 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002237 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002238 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002239 int i;
2240
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002241 /* broadcasting uses a different data element size */
2242 for (i = 0; i < instruction->operands; i++)
2243 if (i == broadcast)
2244 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2245 else
2246 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002247
2248 merr = MERR_INVALOP;
2249
2250 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002251 temp->opcode != I_none; temp++) {
2252 m = matches(temp, instruction, bits);
2253 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002254 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002255 m = MOK_GOOD;
2256 else
2257 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002258 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002259 /*
2260 * Missing operand size and a candidate for fuzzy matching...
2261 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002262 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002263 if (i == broadcast)
2264 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2265 else
2266 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002267 opsizemissing = true;
2268 }
2269 if (m > merr)
2270 merr = m;
2271 if (merr == MOK_GOOD)
2272 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002273 }
2274
2275 /* No match, but see if we can get a fuzzy operand size match... */
2276 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002277 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002278
2279 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002280 /*
2281 * We ignore extrinsic operand sizes on registers, so we should
2282 * never try to fuzzy-match on them. This also resolves the case
2283 * when we have e.g. "xmmrm128" in two different positions.
2284 */
2285 if (is_class(REGISTER, instruction->oprs[i].type))
2286 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002287
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002288 /* This tests if xsizeflags[i] has more than one bit set */
2289 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2290 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002291
Jin Kyu Song7903c072013-10-30 03:00:12 -07002292 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002293 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002294 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2295 BITS32 : BITS64);
2296 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002297 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002298 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002299 }
2300
2301 /* Try matching again... */
2302 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002303 temp->opcode != I_none; temp++) {
2304 m = matches(temp, instruction, bits);
2305 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002306 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002307 m = MOK_GOOD;
2308 else
2309 m = MERR_INVALOP;
2310 }
2311 if (m > merr)
2312 merr = m;
2313 if (merr == MOK_GOOD)
2314 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002315 }
2316
H. Peter Anvina81655b2009-07-25 18:15:28 -07002317done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002318 *tempp = temp;
2319 return merr;
2320}
2321
Mark Charneydcaef4b2014-10-09 13:45:17 -04002322static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2323{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002324 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002325 uint8_t brcast_num;
2326
Mark Charneydcaef4b2014-10-09 13:45:17 -04002327 if (brsize > BITS64)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002328 nasm_fatal("size of broadcasting element is greater than 64 bits");
Mark Charneydcaef4b2014-10-09 13:45:17 -04002329
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002330 /*
2331 * The shift term is to take care of the extra BITS80 inserted
2332 * between BITS64 and BITS128.
2333 */
2334 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2335 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002336
2337 return brcast_num;
2338}
2339
H. Peter Anvin65289e82009-07-25 17:25:11 -07002340static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002341 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002342{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002343 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002344 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002345 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002346
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002347 /*
2348 * Check the opcode
2349 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002350 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002351 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002352
2353 /*
2354 * Count the operands
2355 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002356 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002357 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002358
2359 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002360 * Is it legal?
2361 */
Chang S. Baea5786342018-08-15 23:22:21 +03002362 if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002363 return MERR_INVALOP;
2364
2365 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002366 * {evex} available?
2367 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002368 switch (instruction->prefixes[PPS_VEX]) {
2369 case P_EVEX:
2370 if (!itemp_has(itemp, IF_EVEX))
2371 return MERR_ENCMISMATCH;
2372 break;
2373 case P_VEX3:
2374 case P_VEX2:
2375 if (!itemp_has(itemp, IF_VEX))
2376 return MERR_ENCMISMATCH;
2377 break;
2378 default:
2379 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002380 }
2381
2382 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002383 * Check that no spurious colons or TOs are present
2384 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 for (i = 0; i < itemp->operands; i++)
2386 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002387 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002388
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002389 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002390 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002391 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002392 switch (itemp_smask(itemp)) {
2393 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002394 asize = BITS8;
2395 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002396 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002397 asize = BITS16;
2398 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002399 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002400 asize = BITS32;
2401 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002402 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002403 asize = BITS64;
2404 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002405 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002406 asize = BITS128;
2407 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002408 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002409 asize = BITS256;
2410 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002411 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002412 asize = BITS512;
2413 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002414 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002415 switch (bits) {
2416 case 16:
2417 asize = BITS16;
2418 break;
2419 case 32:
2420 asize = BITS32;
2421 break;
2422 case 64:
2423 asize = BITS64;
2424 break;
2425 default:
2426 asize = 0;
2427 break;
2428 }
2429 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002430 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002431 asize = 0;
2432 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002433 }
2434
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002435 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002436 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002437 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002438 memset(size, 0, sizeof size);
2439 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002440 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002441 /* S- flags apply to all operands */
2442 for (i = 0; i < MAX_OPERANDS; i++)
2443 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002444 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002445
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002446 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002447 * Check that the operand flags all match up,
2448 * it's a bit tricky so lets be verbose:
2449 *
2450 * 1) Find out the size of operand. If instruction
2451 * doesn't have one specified -- we're trying to
2452 * guess it either from template (IF_S* flag) or
2453 * from code bits.
2454 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002455 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002456 * template has an operand size specified AND this size differ
2457 * from which instruction has (perhaps we got it from code bits)
2458 * we are:
2459 * a) Check that only size of instruction and operand is differ
2460 * other characteristics do match
2461 * b) Perhaps it's a register specified in instruction so
2462 * for such a case we just mark that operand as "size
2463 * missing" and this will turn on fuzzy operand size
2464 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002465 */
2466 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002467 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002468 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002469 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002470 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002471 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002472 opflags_t template_opsize, insn_opsize;
2473
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002474 if (!(type & SIZE_MASK))
2475 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002476
Jin Kyu Song7903c072013-10-30 03:00:12 -07002477 insn_opsize = type & SIZE_MASK;
2478 if (!is_broadcast) {
2479 template_opsize = itemp->opd[i] & SIZE_MASK;
2480 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002481 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2482
2483 if (~ideco & BRDCAST_MASK)
2484 return MERR_BRNOTHERE;
2485
Jin Kyu Song7903c072013-10-30 03:00:12 -07002486 /*
2487 * when broadcasting, the element size depends on
2488 * the instruction type. decorator flag should match.
2489 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002490 if (deco_brsize) {
2491 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002492 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002493 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002494 } else {
2495 template_opsize = 0;
2496 }
2497 }
2498
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002499 if (~ideco & deco & OPMASK_MASK)
2500 return MERR_MASKNOTHERE;
2501
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002502 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2503 return MERR_DECONOTHERE;
2504
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002505 if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002506 return MERR_INVALOP;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002507
2508 if (~itemp->opd[i] & type & REGSET_MASK)
2509 return (itemp->opd[i] & REGSET_MASK)
2510 ? MERR_REGSETSIZE : MERR_REGSET;
2511
2512 if (template_opsize) {
Jin Kyu Song7903c072013-10-30 03:00:12 -07002513 if (template_opsize != insn_opsize) {
2514 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002515 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002516 } else if (!is_class(REGISTER, type)) {
2517 /*
2518 * Note: we don't honor extrinsic operand sizes for registers,
2519 * so "missing operand size" for a register should be
2520 * considered a wildcard match rather than an error.
2521 */
2522 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002523 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002524 } else if (is_broadcast &&
2525 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002526 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002527 /*
2528 * broadcasting opsize matches but the number of repeated memory
2529 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002530 * if 64b double precision float is broadcasted to ymm (256b),
2531 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002532 */
2533 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002534 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002535 }
2536 }
2537
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002538 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002539 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002540
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002541 /*
2542 * Check operand sizes
2543 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002544 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2545 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002547 asize = itemp->opd[i] & SIZE_MASK;
2548 if (asize) {
2549 for (i = 0; i < oprs; i++)
2550 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551 break;
2552 }
2553 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002554 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002556 }
2557
Keith Kaniosb7a89542007-04-12 02:40:54 +00002558 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559 if (!(itemp->opd[i] & SIZE_MASK) &&
2560 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002561 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002562 }
2563
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002564 /*
2565 * Check template is okay at the set cpu level
2566 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002567 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002568 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002569
Keith Kaniosb7a89542007-04-12 02:40:54 +00002570 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002571 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002572 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002573 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002574 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002576 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002577 * If we have a HLE prefix, look for the NOHLE flag
2578 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002579 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002580 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2581 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2582 return MERR_BADHLE;
2583
2584 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002585 * Check if special handling needed for Jumps
2586 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002587 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002588 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002589
Jin Kyu Song03041092013-10-15 19:38:51 -07002590 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002591 * Check if BND prefix is allowed.
2592 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002593 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002594 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002595 (has_prefix(instruction, PPS_REP, P_BND) ||
2596 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002597 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002598 else if (itemp_has(itemp, IF_BND) &&
2599 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2600 has_prefix(instruction, PPS_REP, P_REPNZ)))
2601 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002602
H. Peter Anvin60926242009-07-26 16:25:38 -07002603 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002604}
2605
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002606/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002607 * Check if ModR/M.mod should/can be 01.
2608 * - EAF_BYTEOFFS is set
2609 * - offset can fit in a byte when EVEX is not used
2610 * - offset can be compressed when EVEX is used
2611 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002612#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2613 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2614 is_disp8n(input, ins, &output->disp8) : \
2615 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2616 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002617
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002618static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002619 int rfield, opflags_t rflags, insn *ins,
2620 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002621{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002622 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002623 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002624 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002625
H. Peter Anvin8f622462017-04-02 19:02:29 -07002626 *errmsg = "invalid effective address"; /* Default error message */
2627
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002628 output->type = EA_SCALAR;
2629 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002630 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002631
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002632 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002633 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002634 /* EVEX.R' flag for the REG operand */
2635 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002636
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002637 if (is_class(REGISTER, input->type)) {
2638 /*
2639 * It's a direct register.
2640 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002641 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002642 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002643
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002644 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002645 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002646
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002647 /* broadcasting is not available with a direct register operand. */
2648 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002649 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002650 goto err;
2651 }
2652
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002653 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002654 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002655 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002656 output->bytes = 0; /* no offset necessary either */
2657 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2658 } else {
2659 /*
2660 * It's a memory reference.
2661 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002662
2663 /* Embedded rounding or SAE is not available with a mem ref operand. */
2664 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002665 *errmsg = "embedded rounding is available only with "
2666 "register-register operations";
2667 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002668 }
2669
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002670 if (input->basereg == -1 &&
2671 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002672 /*
2673 * It's a pure offset.
2674 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002675 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002676 if (input->segment == NO_SEG ||
2677 (input->opflags & OPFLAG_RELATIVE)) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08002678 nasm_warn(WARN_OTHER|ERR_PASS2, "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002679 input->type &= ~IP_REL;
2680 input->type |= MEMORY;
2681 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002682 }
2683
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002684 if (bits == 64 &&
2685 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002686 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002687 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002688 }
2689
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002690 if (eaflags & EAF_BYTEOFFS ||
2691 (eaflags & EAF_WORDOFFS &&
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002692 input->disp_size != (addrbits != 16 ? 32 : 16)))
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002693 nasm_warn(WARN_OTHER, "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002694
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002695 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002696 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002697 output->sib = GEN_SIB(0, 4, 5);
2698 output->bytes = 4;
2699 output->modrm = GEN_MODRM(0, rfield, 4);
2700 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002701 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002702 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002703 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002704 output->modrm = GEN_MODRM(0, rfield,
2705 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002706 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002707 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002708 } else {
2709 /*
2710 * It's an indirection.
2711 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002713 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002714 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002715 int t, it, bt; /* register numbers */
2716 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002717
H. Peter Anvine2c80182005-01-15 22:15:51 +00002718 if (s == 0)
2719 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002720
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002721 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002722 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002723 ix = nasm_reg_flags[i];
2724 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002725 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002726 ix = 0;
2727 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002728
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002729 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002730 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002731 bx = nasm_reg_flags[b];
2732 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002733 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002734 bx = 0;
2735 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002736
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002737 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002738 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002739 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002740 int32_t o = input->offset;
2741 int mod, scale, index, base;
2742
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002743 /*
2744 * For a vector SIB, one has to be a vector and the other,
2745 * if present, a GPR. The vector must be the index operand.
2746 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002747 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002748 if (s == 0)
2749 s = 1;
2750 else if (s != 1)
2751 goto err;
2752
2753 t = bt, bt = it, it = t;
2754 x = bx, bx = ix, ix = x;
2755 }
2756
2757 if (bt != -1) {
2758 if (REG_GPR & ~bx)
2759 goto err;
2760 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2761 sok &= bx;
2762 else
2763 goto err;
2764 }
2765
2766 /*
2767 * While we're here, ensure the user didn't specify
2768 * WORD or QWORD
2769 */
2770 if (input->disp_size == 16 || input->disp_size == 64)
2771 goto err;
2772
2773 if (addrbits == 16 ||
2774 (addrbits == 32 && !(sok & BITS32)) ||
2775 (addrbits == 64 && !(sok & BITS64)))
2776 goto err;
2777
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002778 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2779 : ((ix & YMMREG & ~REG_EA)
2780 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002781
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002782 output->rex |= rexflags(it, ix, REX_X);
2783 output->rex |= rexflags(bt, bx, REX_B);
2784 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002785
2786 index = it & 7; /* it is known to be != -1 */
2787
2788 switch (s) {
2789 case 1:
2790 scale = 0;
2791 break;
2792 case 2:
2793 scale = 1;
2794 break;
2795 case 4:
2796 scale = 2;
2797 break;
2798 case 8:
2799 scale = 3;
2800 break;
2801 default: /* then what the smeg is it? */
2802 goto err; /* panic */
2803 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002804
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002805 if (bt == -1) {
2806 base = 5;
2807 mod = 0;
2808 } else {
2809 base = (bt & 7);
2810 if (base != REG_NUM_EBP && o == 0 &&
2811 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002812 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002813 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002814 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002815 mod = 1;
2816 else
2817 mod = 2;
2818 }
2819
2820 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002821 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2822 output->modrm = GEN_MODRM(mod, rfield, 4);
2823 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002824 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002825 /*
2826 * it must be a 32/64-bit memory reference. Firstly we have
2827 * to check that all registers involved are type E/Rxx.
2828 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002829 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002830 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002831
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002832 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002833 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2834 sok &= ix;
2835 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002836 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002837 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002838
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002839 if (bt != -1) {
2840 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002841 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002842 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002843 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002844 sok &= bx;
2845 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002846
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002847 /*
2848 * While we're here, ensure the user didn't specify
2849 * WORD or QWORD
2850 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002851 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002852 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002853
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002854 if (addrbits == 16 ||
2855 (addrbits == 32 && !(sok & BITS32)) ||
2856 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002857 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002858
Keith Kaniosb7a89542007-04-12 02:40:54 +00002859 /* now reorganize base/index */
2860 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002861 ((hb == b && ht == EAH_NOTBASE) ||
2862 (hb == i && ht == EAH_MAKEBASE))) {
2863 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002864 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002865 x = bx, bx = ix, ix = x;
2866 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002867
Jin Kyu Song164d6072013-10-15 19:10:13 -07002868 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002869 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002870 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002871 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002872 if (eaflags & EAF_MIB) {
2873 /* only for mib operands */
2874 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2875 /*
2876 * make a single reg index [reg*1].
2877 * gas uses this form for an explicit index register.
2878 */
2879 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2880 }
2881 if ((ht == EAH_SUMMED) && bt == -1) {
2882 /* separate once summed index into [base, index] */
2883 bt = it, bx = ix, s--;
2884 }
2885 } else {
2886 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002887 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002888 s == 3 || s == 5 || s == 9) && bt == -1) {
2889 /* convert 3*EAX to EAX+2*EAX */
2890 bt = it, bx = ix, s--;
2891 }
2892 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002893 (eaflags & EAF_TIMESTWO) &&
2894 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002895 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002896 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002897 * to sib format with 0x0 displacement - [EAX*1+0].
2898 */
2899 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2900 }
2901 }
Keith Kanios48af1772007-08-17 07:37:52 +00002902 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002903 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002904 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 x = ix, ix = bx, bx = x;
2906 }
2907 if (it == REG_NUM_ESP ||
2908 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002909 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002910
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 output->rex |= rexflags(it, ix, REX_X);
2912 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002913
Keith Kanios48af1772007-08-17 07:37:52 +00002914 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002915 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002916 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002917
Keith Kaniosb7a89542007-04-12 02:40:54 +00002918 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002919 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002920 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002921 } else {
2922 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002923 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002924 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002925 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002926 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002927 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002928 mod = 1;
2929 else
2930 mod = 2;
2931 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002932
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002933 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002934 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2935 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002936 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002937 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002938 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002939
Keith Kaniosb7a89542007-04-12 02:40:54 +00002940 if (it == -1)
2941 index = 4, s = 1;
2942 else
2943 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002944
H. Peter Anvine2c80182005-01-15 22:15:51 +00002945 switch (s) {
2946 case 1:
2947 scale = 0;
2948 break;
2949 case 2:
2950 scale = 1;
2951 break;
2952 case 4:
2953 scale = 2;
2954 break;
2955 case 8:
2956 scale = 3;
2957 break;
2958 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002959 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002960 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002961
Keith Kaniosb7a89542007-04-12 02:40:54 +00002962 if (bt == -1) {
2963 base = 5;
2964 mod = 0;
2965 } else {
2966 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002967 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002968 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002969 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002970 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002971 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002972 mod = 1;
2973 else
2974 mod = 2;
2975 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002976
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002977 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002978 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2979 output->modrm = GEN_MODRM(mod, rfield, 4);
2980 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002981 }
2982 } else { /* it's 16-bit */
2983 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002984 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002985
Keith Kaniosb7a89542007-04-12 02:40:54 +00002986 /* check for 64-bit long mode */
2987 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002988 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002989
H. Peter Anvine2c80182005-01-15 22:15:51 +00002990 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002991 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2992 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002993 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002994
Keith Kaniosb7a89542007-04-12 02:40:54 +00002995 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002996 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002997 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002998
H. Peter Anvine2c80182005-01-15 22:15:51 +00002999 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003000 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003001 if (b == -1 && i != -1) {
3002 int tmp = b;
3003 b = i;
3004 i = tmp;
3005 } /* swap */
3006 if ((b == R_SI || b == R_DI) && i != -1) {
3007 int tmp = b;
3008 b = i;
3009 i = tmp;
3010 }
3011 /* have BX/BP as base, SI/DI index */
3012 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003013 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003014 if (i != -1 && b != -1 &&
3015 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003016 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003017 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003018 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003019
H. Peter Anvine2c80182005-01-15 22:15:51 +00003020 rm = -1;
3021 if (i != -1)
3022 switch (i * 256 + b) {
3023 case R_SI * 256 + R_BX:
3024 rm = 0;
3025 break;
3026 case R_DI * 256 + R_BX:
3027 rm = 1;
3028 break;
3029 case R_SI * 256 + R_BP:
3030 rm = 2;
3031 break;
3032 case R_DI * 256 + R_BP:
3033 rm = 3;
3034 break;
3035 } else
3036 switch (b) {
3037 case R_SI:
3038 rm = 4;
3039 break;
3040 case R_DI:
3041 rm = 5;
3042 break;
3043 case R_BP:
3044 rm = 6;
3045 break;
3046 case R_BX:
3047 rm = 7;
3048 break;
3049 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003050 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003051 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003052
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003053 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003054 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003055 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003056 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00003057 mod = 1;
3058 else
3059 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003060
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003061 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003062 output->bytes = mod; /* bytes of offset needed */
3063 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003064 }
3065 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003066 }
H. Peter Anvin70653092007-10-19 14:42:29 -07003067
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003068 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003069 return output->type;
3070
3071err:
3072 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003073}
3074
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003075static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003076{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003077 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003078 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003079
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003080 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003081
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003082 switch (ins->prefixes[PPS_ASIZE]) {
3083 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003084 valid &= 16;
3085 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003086 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003087 valid &= 32;
3088 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003089 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003090 valid &= 64;
3091 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003092 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003093 valid &= (addrbits == 32) ? 16 : 32;
3094 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003095 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003096 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003097 }
3098
3099 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003100 if (is_class(MEMORY, ins->oprs[j].type)) {
3101 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07003102
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003103 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003104 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003105 i = 0;
3106 else
3107 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003108
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003109 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003110 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003111 b = 0;
3112 else
3113 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003114
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003115 if (ins->oprs[j].scale == 0)
3116 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003117
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003118 if (!i && !b) {
3119 int ds = ins->oprs[j].disp_size;
3120 if ((addrbits != 64 && ds > 8) ||
3121 (addrbits == 64 && ds == 16))
3122 valid &= ds;
3123 } else {
3124 if (!(REG16 & ~b))
3125 valid &= 16;
3126 if (!(REG32 & ~b))
3127 valid &= 32;
3128 if (!(REG64 & ~b))
3129 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07003130
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003131 if (!(REG16 & ~i))
3132 valid &= 16;
3133 if (!(REG32 & ~i))
3134 valid &= 32;
3135 if (!(REG64 & ~i))
3136 valid &= 64;
3137 }
3138 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003139 }
3140
3141 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003142 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003143 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003144 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04003145 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003146 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003147 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003148 /* Impossible... */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03003149 nasm_nonfatal("impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003150 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003151 }
3152
3153 defdisp = ins->addr_size == 16 ? 16 : 32;
3154
3155 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003156 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
3157 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
3158 /*
3159 * mem_offs sizes must match the address size; if not,
3160 * strip the MEM_OFFS bit and match only EA instructions
3161 */
3162 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
3163 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003164 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003165}