blob: 724779fc468299f6e9eca2bac16d6882c35dc988 [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 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400213 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 Anvin04445362016-09-21 15:56:19 -0700586/* This is totally just a wild guess what is reasonable... */
587#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
588
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800589int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000590{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700591 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000592 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700593 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300594 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000595
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700596 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700597 data.offset = start;
598 data.segment = segment;
599 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700600 data.bits = bits;
601
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700602 wsize = db_bytes(instruction->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300603 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000605
H. Peter Anvineba20a72002-04-30 20:53:55 +0000606 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700608
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700609 list_for_each(e, instruction->eops) {
610 if (e->type == EOT_DB_NUMBER) {
611 if (wsize > 8) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300612 nasm_nonfatal("integer supplied to a DT,DO,DY or DZ");
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700613 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700614 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700615 data.inslen = data.size = wsize;
616 data.toffset = e->offset;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700617 data.twrt = e->wrt;
618 data.relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700619 if (e->segment != NO_SEG && (e->segment & 1)) {
620 data.tsegment = e->segment;
621 data.type = OUT_SEGMENT;
622 data.sign = OUT_UNSIGNED;
623 } else {
624 data.tsegment = e->segment;
625 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
626 data.sign = OUT_WRAP;
627 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700628 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000629 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700630 } else if (e->type == EOT_DB_STRING ||
631 e->type == EOT_DB_STRING_FREE) {
632 int align = e->stringlen % wsize;
633 if (align)
634 align = wsize - align;
635
636 data.insoffs = 0;
637 data.inslen = e->stringlen + align;
638
639 out_rawdata(&data, e->stringval, e->stringlen);
640 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700641 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700643 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700644 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700646 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700647 off_t base = 0;
648 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700649 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700650 char *buf = NULL;
651 size_t blk = 0; /* Buffered I/O block size */
652 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000653
H. Peter Anvin94ead272017-09-27 15:22:23 -0700654 if (!t)
655 goto done;
656
H. Peter Anvind81a2352016-09-21 14:03:18 -0700657 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400658 if (!fp) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300659 nasm_nonfatal("`incbin': unable to open file `%s'",
660 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700661 goto done;
662 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000663
H. Peter Anvind81a2352016-09-21 14:03:18 -0700664 len = nasm_file_size(fp);
665
666 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300667 nasm_nonfatal("`incbin': unable to get length of file `%s'",
668 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700669 goto close_done;
670 }
671
H. Peter Anvina77692b2016-09-20 14:04:33 -0700672 if (instruction->eops->next) {
673 base = instruction->eops->next->offset;
674 if (base >= len) {
675 len = 0;
676 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000677 len -= base;
678 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700679 len > (off_t)instruction->eops->next->next->offset)
680 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700683
H. Peter Anvina77692b2016-09-20 14:04:33 -0700684 lfmt->set_offset(data.offset);
685 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000686
H. Peter Anvind81a2352016-09-21 14:03:18 -0700687 if (!len)
688 goto end_incbin;
689
690 /* Try to map file data */
691 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700692 if (!map) {
693 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
694 buf = nasm_malloc(blk);
695 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700696
697 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700698 /*
699 * Consider these irrelevant for INCBIN, since it is fully
700 * possible that these might be (way) bigger than an int
701 * can hold; there is, however, no reason to widen these
702 * types just for INCBIN. data.inslen == 0 signals to the
703 * backend that these fields are meaningless, if at all
704 * needed.
705 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700706 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700707 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700708
H. Peter Anvind81a2352016-09-21 14:03:18 -0700709 if (map) {
710 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700711 } else if ((off_t)m == len) {
712 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700713 } else {
714 off_t l = len;
715
716 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300717 nasm_nonfatal("`incbin': unable to seek on file `%s'",
718 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700719 goto end_incbin;
720 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700721 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700722 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700723 if (!m || feof(fp)) {
724 /*
725 * This shouldn't happen unless the file
726 * actually changes while we are reading
727 * it.
728 */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300729 nasm_nonfatal("`incbin': unexpected EOF while"
730 " reading file `%s'", fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700731 goto end_incbin;
732 }
733 out_rawdata(&data, buf, m);
734 l -= m;
735 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700736 }
737 }
738 end_incbin:
739 lfmt->downlevel(LIST_INCBIN);
740 if (instruction->times > 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700741 lfmt->uplevel(LIST_TIMES);
742 lfmt->downlevel(LIST_TIMES);
743 }
744 if (ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300745 nasm_nonfatal("`incbin': error while"
746 " reading file `%s'", fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700747 }
748 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700749 if (buf)
750 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700751 if (map)
752 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700753 fclose(fp);
754 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700755 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700756 ;
757 } else {
758 /* "Real" instruction */
759
760 /* Check to see if we need an address-size prefix */
761 add_asp(instruction, bits);
762
763 m = find_match(&temp, instruction, data.segment, data.offset, bits);
764
765 if (m == MOK_GOOD) {
766 /* Matches! */
767 int64_t insn_size = calcsize(data.segment, data.offset,
768 bits, instruction, temp);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700769 nasm_assert(insn_size >= 0);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700770
771 data.itemp = temp;
772 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700773 data.insoffs = 0;
774 data.inslen = insn_size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700775
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700776 gencode(&data, instruction);
777 nasm_assert(data.insoffs == insn_size);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700778 } else {
779 /* No match */
780 switch (m) {
781 case MERR_OPSIZEMISSING:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300782 nasm_nonfatal("operation size not specified");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700783 break;
784 case MERR_OPSIZEMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300785 nasm_nonfatal("mismatch in operand sizes");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700786 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700787 case MERR_BRNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300788 nasm_nonfatal("broadcast not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700789 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700790 case MERR_BRNUMMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300791 nasm_nonfatal("mismatch in the number of broadcasting elements");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700792 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700793 case MERR_MASKNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300794 nasm_nonfatal("mask not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700795 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700796 case MERR_DECONOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300797 nasm_nonfatal("unsupported mode decorator for instruction");
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700798 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700799 case MERR_BADCPU:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300800 nasm_nonfatal("no instruction for this cpu level");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700801 break;
802 case MERR_BADMODE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300803 nasm_nonfatal("instruction not supported in %d-bit mode", bits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700804 break;
805 case MERR_ENCMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300806 nasm_nonfatal("specific encoding scheme not available");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700807 break;
808 case MERR_BADBND:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300809 nasm_nonfatal("bnd prefix is not allowed");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700810 break;
811 case MERR_BADREPNE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300812 nasm_nonfatal("%s prefix is not allowed",
813 (has_prefix(instruction, PPS_REP, P_REPNE) ?
814 "repne" : "repnz"));
H. Peter Anvina77692b2016-09-20 14:04:33 -0700815 break;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700816 case MERR_REGSETSIZE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300817 nasm_nonfatal("invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700818 break;
819 case MERR_REGSET:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300820 nasm_nonfatal("register set not valid for operand");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700821 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700822 default:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300823 nasm_nonfatal("invalid combination of opcode and operands");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700824 break;
825 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700826
827 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400828 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000829 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700830 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000831}
832
H. Peter Anvin29651542018-12-18 19:14:40 -0800833static void debug_set_db_type(insn *instruction)
834{
835 /* Is this really correct? .operands doesn't mean much for Dx */
836 int32_t typeinfo = TYS_ELEMENTS(instruction->operands);
837
838 switch (instruction->opcode) {
839 case I_DB:
840 typeinfo |= TY_BYTE;
841 break;
842 case I_DW:
843 typeinfo |= TY_WORD;
844 break;
845 case I_DD:
846 if (instruction->eops_float)
847 typeinfo |= TY_FLOAT;
848 else
849 typeinfo |= TY_DWORD;
850 break;
851 case I_DQ:
852 /* What about double? */
853 typeinfo |= TY_QWORD;
854 break;
855 case I_DT:
856 /* What about long double? */
857 typeinfo |= TY_TBYTE;
858 break;
859 case I_DO:
860 typeinfo |= TY_OWORD;
861 break;
862 case I_DY:
863 typeinfo |= TY_YWORD;
864 break;
865 case I_DZ:
866 typeinfo |= TY_ZWORD;
867 break;
868 default:
869 panic();
870 }
871
872 dfmt->debug_typevalue(typeinfo);
873}
874
875static void debug_set_type(insn *instruction)
876{
877 int32_t typeinfo;
878
879 if (opcode_is_resb(instruction->opcode)) {
880 typeinfo = TYS_ELEMENTS(instruction->oprs[0].offset);
881
882 switch (instruction->opcode) {
883 case I_RESB:
884 typeinfo |= TY_BYTE;
885 break;
886 case I_RESW:
887 typeinfo |= TY_WORD;
888 break;
889 case I_RESD:
890 typeinfo |= TY_DWORD;
891 break;
892 case I_RESQ:
893 typeinfo |= TY_QWORD;
894 break;
895 case I_REST:
896 typeinfo |= TY_TBYTE;
897 break;
898 case I_RESO:
899 typeinfo |= TY_OWORD;
900 break;
901 case I_RESY:
902 typeinfo |= TY_YWORD;
903 break;
904 case I_RESZ:
905 typeinfo |= TY_ZWORD;
906 break;
907 default:
908 panic();
909 }
910 } else {
911 typeinfo = TY_LABEL;
912 }
913
914 dfmt->debug_typevalue(typeinfo);
915}
916
917
918/* Proecess an EQU directive */
919static void define_equ(insn * instruction)
920{
921 if (!instruction->label) {
922 nasm_nonfatal("EQU not preceded by label");
923 } else if (instruction->operands == 1 &&
924 (instruction->oprs[0].type & IMMEDIATE) &&
925 instruction->oprs[0].wrt == NO_SEG) {
926 define_label(instruction->label,
927 instruction->oprs[0].segment,
928 instruction->oprs[0].offset, false);
929 } else if (instruction->operands == 2
930 && (instruction->oprs[0].type & IMMEDIATE)
931 && (instruction->oprs[0].type & COLON)
932 && instruction->oprs[0].segment == NO_SEG
933 && instruction->oprs[0].wrt == NO_SEG
934 && (instruction->oprs[1].type & IMMEDIATE)
935 && instruction->oprs[1].segment == NO_SEG
936 && instruction->oprs[1].wrt == NO_SEG) {
937 define_label(instruction->label,
938 instruction->oprs[0].offset | SEG_ABS,
939 instruction->oprs[1].offset, false);
940 } else {
941 nasm_nonfatal("bad syntax for EQU");
942 }
943}
944
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800945int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000946{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000947 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700948 enum match_result m;
H. Peter Anvin29651542018-12-18 19:14:40 -0800949 int64_t isize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000950
H. Peter Anvin29651542018-12-18 19:14:40 -0800951 if (instruction->opcode == I_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000952 return 0;
H. Peter Anvin29651542018-12-18 19:14:40 -0800953 } else if (instruction->opcode == I_EQU) {
954 define_equ(instruction);
955 return 0;
956 } else if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 extop *e;
H. Peter Anvin29651542018-12-18 19:14:40 -0800958 int32_t osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000959
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700960 wsize = db_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700961 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000962
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400963 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000964 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000965
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400967 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400969 warn_overflow_const(e->offset, wsize);
970 } else if (e->type == EOT_DB_STRING ||
971 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000973
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 align = (-osize) % wsize;
975 if (align < 0)
976 align += wsize;
977 isize += osize + align;
978 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000979
H. Peter Anvin29651542018-12-18 19:14:40 -0800980 debug_set_db_type(instruction);
981 return isize;
982 } else if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400983 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700984 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000985
H. Peter Anvind81a2352016-09-21 14:03:18 -0700986 len = nasm_file_size_by_path(fname);
987 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300988 nasm_nonfatal("`incbin': unable to get length of file `%s'",
989 fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700990 return 0;
991 }
992
993 if (instruction->eops->next) {
994 if (len <= (off_t)instruction->eops->next->offset) {
995 len = 0;
996 } else {
997 len -= instruction->eops->next->offset;
998 if (instruction->eops->next->next &&
999 len > (off_t)instruction->eops->next->next->offset) {
1000 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 }
1002 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001004
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001005 len *= instruction->times;
1006 instruction->times = 1; /* Tell the upper layer to not iterate */
1007
H. Peter Anvind81a2352016-09-21 14:03:18 -07001008 return len;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001009 } else {
H. Peter Anvin29651542018-12-18 19:14:40 -08001010 /* Normal instruction, or RESx */
1011
1012 /* Check to see if we need an address-size prefix */
1013 add_asp(instruction, bits);
1014
1015 m = find_match(&temp, instruction, segment, offset, bits);
1016 if (m != MOK_GOOD)
1017 return -1; /* No match */
1018
1019 isize = calcsize(segment, offset, bits, instruction, temp);
1020 debug_set_type(instruction);
1021
1022 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001023 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001024}
1025
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001026static void bad_hle_warn(const insn * ins, uint8_t hleok)
1027{
1028 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001029 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001030 static const enum whatwarn warn[2][4] =
1031 {
1032 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
1033 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
1034 };
1035 unsigned int n;
1036
1037 n = (unsigned int)rep_pfx - P_XACQUIRE;
1038 if (n > 1)
1039 return; /* Not XACQUIRE/XRELEASE */
1040
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001041 ww = warn[n][hleok];
1042 if (!is_class(MEMORY, ins->oprs[0].type))
1043 ww = w_inval; /* HLE requires operand 0 to be memory */
1044
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001045 /*!
1046 *!hle [on] invalid HLE prefixes
1047 *! warns about invalid use of the HLE \c{XACQUIRE} or \c{XRELEASE}
1048 *! prefixes.
1049 */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001050 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001051 case w_none:
1052 break;
1053
1054 case w_lock:
1055 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001056 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001057 "%s with this instruction requires lock",
1058 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001059 }
1060 break;
1061
1062 case w_inval:
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001063 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001064 "%s invalid with this instruction",
1065 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001066 break;
1067 }
1068}
1069
H. Peter Anvin507ae032008-10-09 15:37:10 -07001070/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +04001071#define case3(x) case (x): case (x)+1: case (x)+2
1072#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -07001073
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001074static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001075 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001076{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001077 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001078 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001079 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001080 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001081 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001082 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001083 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001084 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001085 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001086 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -07001087 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -07001088 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001089
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001090 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001091 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001092 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001093
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001094 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001095 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001096
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 (void)segment; /* Don't warn that this parameter is unused */
1098 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001099
H. Peter Anvin839eca22007-10-29 23:12:47 -07001100 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001101 c = *codes++;
1102 op1 = (c & 3) + ((opex & 1) << 2);
1103 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1104 opx = &ins->oprs[op1];
1105 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001106
H. Peter Anvin839eca22007-10-29 23:12:47 -07001107 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001108 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 codes += c, length += c;
1110 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001111
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001112 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001113 opex = c;
1114 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001115
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001116 case4(010):
1117 ins->rex |=
1118 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 codes++, length++;
1120 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001121
Jin Kyu Song164d6072013-10-15 19:10:13 -07001122 case4(014):
1123 /* this is an index reg of MIB operand */
1124 mib_index = opx->basereg;
1125 break;
1126
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001127 case4(020):
1128 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 length++;
1130 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001131
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001132 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 length += 2;
1134 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001135
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001136 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001137 if (opx->type & (BITS16 | BITS32 | BITS64))
1138 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 else
1140 length += (bits == 16) ? 2 : 4;
1141 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001142
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001143 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 length += 4;
1145 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001146
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001147 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001148 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001150
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001151 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152 length++;
1153 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001154
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001155 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001156 length += 8; /* MOV reg64/imm */
1157 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001158
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001159 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 length += 2;
1161 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001162
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001163 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001164 if (opx->type & (BITS16 | BITS32 | BITS64))
1165 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 else
1167 length += (bits == 16) ? 2 : 4;
1168 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001169
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001170 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 length += 4;
1172 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001173
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001174 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001175 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001177
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001178 case 0172:
1179 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001180 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001181 length++;
1182 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001183
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001184 case4(0174):
1185 length++;
1186 break;
1187
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001188 case4(0240):
1189 ins->rex |= REX_EV;
1190 ins->vexreg = regval(opx);
1191 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1192 ins->vex_cm = *codes++;
1193 ins->vex_wlp = *codes++;
1194 ins->evex_tuple = (*codes++ - 0300);
1195 break;
1196
1197 case 0250:
1198 ins->rex |= REX_EV;
1199 ins->vexreg = 0;
1200 ins->vex_cm = *codes++;
1201 ins->vex_wlp = *codes++;
1202 ins->evex_tuple = (*codes++ - 0300);
1203 break;
1204
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001205 case4(0254):
1206 length += 4;
1207 break;
1208
1209 case4(0260):
1210 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001211 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001212 ins->vex_cm = *codes++;
1213 ins->vex_wlp = *codes++;
1214 break;
1215
1216 case 0270:
1217 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001218 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001219 ins->vex_cm = *codes++;
1220 ins->vex_wlp = *codes++;
1221 break;
1222
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001223 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001224 hleok = c & 3;
1225 break;
1226
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001227 case4(0274):
1228 length++;
1229 break;
1230
1231 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001233
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001235 if (bits == 64)
1236 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001237 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001239
H. Peter Anvine2c80182005-01-15 22:15:51 +00001240 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001241 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001242 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001243
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001245 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001246
Keith Kaniosb7a89542007-04-12 02:40:54 +00001247 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001248 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1249 has_prefix(ins, PPS_ASIZE, P_A32))
1250 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001252
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001253 case4(0314):
1254 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001255
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001257 {
1258 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1259 if (pfx == P_O16)
1260 break;
1261 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001262 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001263 else
1264 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001265 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001266 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001267
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001269 {
1270 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1271 if (pfx == P_O32)
1272 break;
1273 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001274 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001275 else
1276 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001278 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001279
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 case 0322:
1281 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001282
Keith Kaniosb7a89542007-04-12 02:40:54 +00001283 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001284 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001285 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001286
Keith Kaniosb7a89542007-04-12 02:40:54 +00001287 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001288 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001289 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001290
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001291 case 0325:
1292 ins->rex |= REX_NH;
1293 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001294
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001295 case 0326:
1296 break;
1297
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 case 0330:
1299 codes++, length++;
1300 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001301
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001304
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001305 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001306 case 0333:
1307 length++;
1308 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001309
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001310 case 0334:
1311 ins->rex |= REX_L;
1312 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001313
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001314 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001315 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001316
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001317 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001318 if (!ins->prefixes[PPS_REP])
1319 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001320 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001321
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001322 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001323 if (!ins->prefixes[PPS_REP])
1324 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001325 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001326
H. Peter Anvine2c80182005-01-15 22:15:51 +00001327 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001328 if (!absolute_op(&ins->oprs[0]))
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001329 nasm_nonfatal("attempt to reserve non-constant"
1330 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001331 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001332 nasm_warn(WARN_OTHER, "forward reference in RESx "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001333 "can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001334 else
H. Peter Anvin5358b982018-12-18 18:06:26 -08001335 length += ins->oprs[0].offset * resb_bytes(ins->opcode);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001336 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001337
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001338 case 0341:
1339 if (!ins->prefixes[PPS_WAIT])
1340 ins->prefixes[PPS_WAIT] = P_WAIT;
1341 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001342
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001343 case 0360:
1344 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001345
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001346 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001347 length++;
1348 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001349
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001350 case 0364:
1351 case 0365:
1352 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001353
Keith Kanios48af1772007-08-17 07:37:52 +00001354 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001355 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001356 length++;
1357 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001358
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001359 case 0370:
1360 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001361 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001362
H. Peter Anvine2c80182005-01-15 22:15:51 +00001363 case 0373:
1364 length++;
1365 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001366
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001367 case 0374:
1368 eat = EA_XMMVSIB;
1369 break;
1370
1371 case 0375:
1372 eat = EA_YMMVSIB;
1373 break;
1374
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001375 case 0376:
1376 eat = EA_ZMMVSIB;
1377 break;
1378
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001379 case4(0100):
1380 case4(0110):
1381 case4(0120):
1382 case4(0130):
1383 case4(0200):
1384 case4(0204):
1385 case4(0210):
1386 case4(0214):
1387 case4(0220):
1388 case4(0224):
1389 case4(0230):
1390 case4(0234):
1391 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001392 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001393 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001394 opflags_t rflags;
1395 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001396 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001397
Keith Kaniosb7a89542007-04-12 02:40:54 +00001398 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001399
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001400 if (c <= 0177) {
1401 /* pick rfield from operand b (opx) */
1402 rflags = regflag(opx);
1403 rfield = nasm_regvals[opx->basereg];
1404 } else {
1405 rflags = 0;
1406 rfield = c & 7;
1407 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001408
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001409 /* EVEX.b1 : evex_brerop contains the operand position */
1410 op_er_sae = (ins->evex_brerop >= 0 ?
1411 &ins->oprs[ins->evex_brerop] : NULL);
1412
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001413 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1414 /* set EVEX.b */
1415 ins->evex_p[2] |= EVEX_P2B;
1416 if (op_er_sae->decoflags & ER) {
1417 /* set EVEX.RC (rounding control) */
1418 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1419 & EVEX_P2RC;
1420 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001421 } else {
1422 /* set EVEX.L'L (vector length) */
1423 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001424 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001425 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001426 /* set EVEX.b */
1427 ins->evex_p[2] |= EVEX_P2B;
1428 }
1429 }
1430
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001431 if (itemp_has(temp, IF_MIB)) {
1432 opy->eaflags |= EAF_MIB;
1433 /*
1434 * if a separate form of MIB (ICC style) is used,
1435 * the index reg info is merged into mem operand
1436 */
1437 if (mib_index != R_none) {
1438 opy->indexreg = mib_index;
1439 opy->scale = 1;
1440 opy->hintbase = mib_index;
1441 opy->hinttype = EAH_NOTBASE;
1442 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001443 }
1444
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001445 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001446 rfield, rflags, ins, &errmsg) != eat) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001447 nasm_nonfatal("%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001449 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001450 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001451 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001452 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001453 }
1454 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001455
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001456 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001457 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001458 ": instruction code \\%o (0x%02X) given", c, c);
1459 break;
1460 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001461 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001462
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001463 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001464
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001465 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001466 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001467 nasm_nonfatal("instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001468 return -1;
1469 }
1470 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001471 }
1472
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001473 switch (ins->prefixes[PPS_VEX]) {
1474 case P_EVEX:
1475 if (!(ins->rex & REX_EV))
1476 return -1;
1477 break;
1478 case P_VEX3:
1479 case P_VEX2:
1480 if (!(ins->rex & REX_V))
1481 return -1;
1482 break;
1483 default:
1484 break;
1485 }
1486
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001487 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001488 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001489
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001490 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001491 nasm_nonfatal("cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001492 return -1;
1493 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001494 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001495 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001496 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001497 ins->rex &= ~REX_W;
1498 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001499 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001500 ins->rex |= REX_W;
1501 bad32 &= ~REX_W;
1502 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001503 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001504 /* Follow REX_W */
1505 break;
1506 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001507
H. Peter Anvinfc561202011-07-07 16:58:22 -07001508 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001509 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001510 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001511 } else if (!(ins->rex & REX_EV) &&
1512 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001513 nasm_nonfatal("invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001514 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001515 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001516 if (ins->rex & REX_EV)
1517 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001518 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1519 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001520 length += 3;
1521 else
1522 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001523 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001524 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001525 nasm_nonfatal("cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001526 return -1;
1527 } else if (bits == 64) {
1528 length++;
1529 } else if ((ins->rex & REX_L) &&
1530 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001531 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001532 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001533 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001534 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001535 length++;
1536 } else {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001537 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001538 return -1;
1539 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001540 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001541
1542 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001543 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001544 /*!
H. Peter Anvin (Intel)be99ebd2018-12-13 22:12:37 -08001545 *!lock [on] LOCK prefix on unlockable instructions
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001546 *! warns about \c{LOCK} prefixes on unlockable instructions.
1547 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001548 nasm_warn(WARN_LOCK | ERR_PASS2 , "instruction is not lockable");
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001549 }
1550
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001551 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001552
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001553 /*
1554 * when BND prefix is set by DEFAULT directive,
1555 * BND prefix is added to every appropriate instruction line
1556 * unless it is overridden by NOBND prefix.
1557 */
1558 if (globalbnd &&
1559 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1560 ins->prefixes[PPS_REP] = P_BND;
1561
H. Peter Anvina77692b2016-09-20 14:04:33 -07001562 /*
1563 * Add length of legacy prefixes
1564 */
1565 length += emit_prefix(NULL, bits, ins);
1566
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001567 return length;
1568}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001569
H. Peter Anvina77692b2016-09-20 14:04:33 -07001570static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001571{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001572 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001573 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001574 !(ins->rex & (REX_V | REX_EV)) &&
1575 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001576 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1577 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001578 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001579 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001580 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001581}
1582
H. Peter Anvina77692b2016-09-20 14:04:33 -07001583static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1584{
1585 int bytes = 0;
1586 int j;
1587
1588 for (j = 0; j < MAXPREFIX; j++) {
1589 uint8_t c = 0;
1590 switch (ins->prefixes[j]) {
1591 case P_WAIT:
1592 c = 0x9B;
1593 break;
1594 case P_LOCK:
1595 c = 0xF0;
1596 break;
1597 case P_REPNE:
1598 case P_REPNZ:
1599 case P_XACQUIRE:
1600 case P_BND:
1601 c = 0xF2;
1602 break;
1603 case P_REPE:
1604 case P_REPZ:
1605 case P_REP:
1606 case P_XRELEASE:
1607 c = 0xF3;
1608 break;
1609 case R_CS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001610 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001611 nasm_warn(WARN_OTHER|ERR_PASS2, "cs segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001612 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001613 c = 0x2E;
1614 break;
1615 case R_DS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001616 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001617 nasm_warn(WARN_OTHER|ERR_PASS2, "ds segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001618 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001619 c = 0x3E;
1620 break;
1621 case R_ES:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001622 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001623 nasm_warn(WARN_OTHER|ERR_PASS2, "es segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001624 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001625 c = 0x26;
1626 break;
1627 case R_FS:
1628 c = 0x64;
1629 break;
1630 case R_GS:
1631 c = 0x65;
1632 break;
1633 case R_SS:
1634 if (bits == 64) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001635 nasm_warn(WARN_OTHER|ERR_PASS2, "ss segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001636 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001637 }
1638 c = 0x36;
1639 break;
1640 case R_SEGR6:
1641 case R_SEGR7:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001642 nasm_nonfatal("segr6 and segr7 cannot be used as prefixes");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001643 break;
1644 case P_A16:
1645 if (bits == 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001646 nasm_nonfatal("16-bit addressing is not supported "
1647 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001648 } else if (bits != 16)
1649 c = 0x67;
1650 break;
1651 case P_A32:
1652 if (bits != 32)
1653 c = 0x67;
1654 break;
1655 case P_A64:
1656 if (bits != 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001657 nasm_nonfatal("64-bit addressing is only supported "
1658 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001659 }
1660 break;
1661 case P_ASP:
1662 c = 0x67;
1663 break;
1664 case P_O16:
1665 if (bits != 16)
1666 c = 0x66;
1667 break;
1668 case P_O32:
1669 if (bits == 16)
1670 c = 0x66;
1671 break;
1672 case P_O64:
1673 /* REX.W */
1674 break;
1675 case P_OSP:
1676 c = 0x66;
1677 break;
1678 case P_EVEX:
1679 case P_VEX3:
1680 case P_VEX2:
1681 case P_NOBND:
1682 case P_none:
1683 break;
1684 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001685 nasm_panic("invalid instruction prefix");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001686 }
1687 if (c) {
1688 if (data)
1689 out_rawbyte(data, c);
1690 bytes++;
1691 }
1692 }
1693 return bytes;
1694}
1695
1696static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001697{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001698 uint8_t c;
1699 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001700 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001701 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001702 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001703 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001704 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001705 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001706 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001707 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001708 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001709
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001710 ins->rex_done = false;
1711
H. Peter Anvina77692b2016-09-20 14:04:33 -07001712 emit_prefix(data, bits, ins);
1713
H. Peter Anvin839eca22007-10-29 23:12:47 -07001714 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001715 c = *codes++;
1716 op1 = (c & 3) + ((opex & 1) << 2);
1717 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1718 opx = &ins->oprs[op1];
1719 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001720
H. Peter Anvina77692b2016-09-20 14:04:33 -07001721
H. Peter Anvin839eca22007-10-29 23:12:47 -07001722 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001723 case 01:
1724 case 02:
1725 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001726 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001727 emit_rex(data, ins);
1728 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001729 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001731
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001732 case 05:
1733 case 06:
1734 case 07:
1735 opex = c;
1736 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001737
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001738 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001739 emit_rex(data, ins);
1740 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001742
Jin Kyu Song164d6072013-10-15 19:10:13 -07001743 case4(014):
1744 break;
1745
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001746 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001747 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001749
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001750 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001751 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001753
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001754 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001755 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001757
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001758 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001759 if (opx->type & (BITS16 | BITS32))
1760 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 else
1762 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001763 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001765
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001766 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001767 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001768 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001769
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001770 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001771 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001772 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001774
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001775 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001776 if (opx->segment == data->segment) {
1777 int64_t delta = opx->offset - data->offset
1778 - (data->inslen - data->insoffs);
1779 if (delta > 127 || delta < -128)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001780 nasm_nonfatal("short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001781 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001782 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001784
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001785 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001786 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001787 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001788
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001789 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001790 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001792
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001793 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001794 if (opx->type & (BITS16 | BITS32 | BITS64))
1795 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 else
1797 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001798
1799 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001801
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001802 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001803 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001805
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001806 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001807 if (opx->segment == NO_SEG)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001808 nasm_nonfatal("value referenced by FAR is not relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001809 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001811
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001812 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001813 {
1814 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1815 const struct operand *opy;
1816
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001817 c = *codes++;
1818 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001819 opy = &ins->oprs[c & 7];
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001820 if (!absolute_op(opy))
1821 nasm_nonfatal("non-absolute expression not permitted "
1822 "as argument %d", c & 7);
1823 else if (opy->offset & ~mask)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001824 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001825 "is4 argument exceeds bounds");
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001826 c = opy->offset & mask;
1827 goto emit_is4;
1828 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001829
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001830 case 0173:
1831 c = *codes++;
1832 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001833 c &= 15;
1834 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001835
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001836 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001837 c = 0;
1838 emit_is4:
1839 r = nasm_regvals[opx->basereg];
1840 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001841 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001842
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001843 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001844 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001845 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001846 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001847 "signed dword immediate exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001848 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001849 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001850 break;
1851
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001852 case4(0240):
1853 case 0250:
1854 codes += 3;
1855 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1856 EVEX_P2Z | EVEX_P2AAA, 2);
1857 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1858 bytes[0] = 0x62;
1859 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001860 bytes[1] = ((((ins->rex & 7) << 5) |
1861 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001862 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001863 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1864 ((~ins->vexreg & 15) << 3) |
1865 (1 << 2) | (ins->vex_wlp & 3);
1866 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001867 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001868 break;
1869
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001870 case4(0260):
1871 case 0270:
1872 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001873 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1874 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001875 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1876 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1877 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001878 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001879 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001880 } else {
1881 bytes[0] = 0xc5;
1882 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001883 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001884 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001885 }
1886 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001887
H. Peter Anvine014f352012-02-25 22:35:19 -08001888 case 0271:
1889 case 0272:
1890 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001891 break;
1892
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001893 case4(0274):
1894 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001895 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001896 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001897
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001898 if (absolute_op(opx)) {
1899 if (ins->rex & REX_W)
1900 s = 64;
1901 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1902 s = 16;
1903 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1904 s = 32;
1905 else
1906 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001907
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001908 um = (uint64_t)2 << (s-1);
1909 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001910
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001911 if (uv > 127 && uv < (uint64_t)-128 &&
1912 (uv < um-128 || uv > um-1)) {
1913 /* If this wasn't explicitly byte-sized, warn as though we
1914 * had fallen through to the imm16/32/64 case.
1915 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001916 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001917 "%s value exceeds bounds",
1918 (opx->type & BITS8) ? "signed byte" :
1919 s == 16 ? "word" :
1920 s == 32 ? "dword" :
1921 "signed dword");
1922 }
1923
1924 /* Output as a raw byte to avoid byte overflow check */
1925 out_rawbyte(data, (uint8_t)uv);
1926 } else {
1927 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001928 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001929 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001930 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001931
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001932 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001934
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001936 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1937 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001938 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001939
H. Peter Anvine2c80182005-01-15 22:15:51 +00001940 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001941 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1942 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001944
H. Peter Anvine2c80182005-01-15 22:15:51 +00001945 case 0312:
1946 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001947
Keith Kaniosb7a89542007-04-12 02:40:54 +00001948 case 0313:
1949 ins->rex = 0;
1950 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001951
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001952 case4(0314):
1953 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001954
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001956 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001957 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001958
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001960 case 0323:
1961 break;
1962
Keith Kaniosb7a89542007-04-12 02:40:54 +00001963 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001964 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001966
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001967 case 0325:
1968 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001969
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001970 case 0326:
1971 break;
1972
H. Peter Anvine2c80182005-01-15 22:15:51 +00001973 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001974 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001975 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001976
H. Peter Anvine2c80182005-01-15 22:15:51 +00001977 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001978 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001979
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001980 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001982 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001984
Keith Kanios48af1772007-08-17 07:37:52 +00001985 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001986 if (ins->rex & REX_R)
1987 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001988 ins->rex &= ~(REX_L|REX_R);
1989 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001990
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001991 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001992 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001993
H. Peter Anvin962e3052008-08-28 17:47:16 -07001994 case 0336:
1995 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001996 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001997
H. Peter Anvine2c80182005-01-15 22:15:51 +00001998 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002000 nasm_panic("non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07002001
H. Peter Anvin5358b982018-12-18 18:06:26 -08002002 out_reserve(data, ins->oprs[0].offset * resb_bytes(ins->opcode));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002004
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002005 case 0341:
2006 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08002007
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002008 case 0360:
2009 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002010
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002011 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002012 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002013 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002014
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002015 case 0364:
2016 case 0365:
2017 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002018
Keith Kanios48af1772007-08-17 07:37:52 +00002019 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002020 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002021 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00002022 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002023
Jin Kyu Song03041092013-10-15 19:38:51 -07002024 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002026
H. Peter Anvine2c80182005-01-15 22:15:51 +00002027 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002028 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002030
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002031 case 0374:
2032 eat = EA_XMMVSIB;
2033 break;
2034
2035 case 0375:
2036 eat = EA_YMMVSIB;
2037 break;
2038
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002039 case 0376:
2040 eat = EA_ZMMVSIB;
2041 break;
2042
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002043 case4(0100):
2044 case4(0110):
2045 case4(0120):
2046 case4(0130):
2047 case4(0200):
2048 case4(0204):
2049 case4(0210):
2050 case4(0214):
2051 case4(0220):
2052 case4(0224):
2053 case4(0230):
2054 case4(0234):
2055 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002056 ea ea_data;
2057 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002058 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002059 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002060 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07002061
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002062 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002063 /* pick rfield from operand b (opx) */
2064 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07002065 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002066 } else {
2067 /* rfield is constant */
2068 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002069 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002070 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002071
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002072 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002073 rfield, rflags, ins, &errmsg) != eat)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002074 nasm_nonfatal("%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07002075
H. Peter Anvine2c80182005-01-15 22:15:51 +00002076 p = bytes;
2077 *p++ = ea_data.modrm;
2078 if (ea_data.sib_present)
2079 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07002080 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002081
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002082 /*
2083 * Make sure the address gets the right offset in case
2084 * the line breaks in the .lst file (BR 1197827)
2085 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002086
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002087 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002088 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07002089 if (ea_data.disp8) {
2090 out_rawbyte(data, ea_data.disp8);
2091 } else if (ea_data.rip) {
2092 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002093 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002094 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002095
H. Peter Anvina77692b2016-09-20 14:04:33 -07002096 if (overflow_general(opy->offset, asize) ||
2097 signed_bits(opy->offset, ins->addr_size) !=
2098 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08002099 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002100
H. Peter Anvina77692b2016-09-20 14:04:33 -07002101 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07002102 (asize > ea_data.bytes)
2103 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002104 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002105 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002106 }
2107 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07002108
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002109 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07002110 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002111 ": instruction code \\%o (0x%02X) given", c, c);
2112 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002113 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07002114 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002115}
2116
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002117static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002118{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002119 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002120 nasm_panic("invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002121 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002122}
2123
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00002124static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002125{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002126 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002127 nasm_panic("invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002128 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002129}
2130
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002131static int op_rexflags(const operand * o, int mask)
2132{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002133 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002134 int val;
2135
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002136 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002137 nasm_panic("invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002138
H. Peter Anvina4835d42008-05-20 14:21:29 -07002139 flags = nasm_reg_flags[o->basereg];
2140 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002141
2142 return rexflags(val, flags, mask);
2143}
2144
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002145static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002146{
2147 int rex = 0;
2148
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002149 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002150 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002151 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002152 rex |= REX_W;
2153 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2154 rex |= REX_H;
2155 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2156 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002157
2158 return rex & mask;
2159}
2160
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002161static int evexflags(int val, decoflags_t deco,
2162 int mask, uint8_t byte)
2163{
2164 int evex = 0;
2165
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002166 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002167 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002168 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002169 evex |= (EVEX_P0RP | EVEX_P0X);
2170 break;
2171 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002172 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002173 evex |= EVEX_P2VP;
2174 if (deco & Z)
2175 evex |= EVEX_P2Z;
2176 if (deco & OPMASK_MASK)
2177 evex |= deco & EVEX_P2AAA;
2178 break;
2179 }
2180 return evex & mask;
2181}
2182
2183static int op_evexflags(const operand * o, int mask, uint8_t byte)
2184{
2185 int val;
2186
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002187 val = nasm_regvals[o->basereg];
2188
2189 return evexflags(val, o->decoflags, mask, byte);
2190}
2191
H. Peter Anvin23595f52009-07-25 17:44:25 -07002192static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002193 insn *instruction,
2194 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002195{
2196 const struct itemplate *temp;
2197 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002198 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002199 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002200 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002201 int i;
2202
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002203 /* broadcasting uses a different data element size */
2204 for (i = 0; i < instruction->operands; i++)
2205 if (i == broadcast)
2206 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2207 else
2208 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002209
2210 merr = MERR_INVALOP;
2211
2212 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002213 temp->opcode != I_none; temp++) {
2214 m = matches(temp, instruction, bits);
2215 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002216 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002217 m = MOK_GOOD;
2218 else
2219 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002220 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002221 /*
2222 * Missing operand size and a candidate for fuzzy matching...
2223 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002224 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002225 if (i == broadcast)
2226 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2227 else
2228 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002229 opsizemissing = true;
2230 }
2231 if (m > merr)
2232 merr = m;
2233 if (merr == MOK_GOOD)
2234 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002235 }
2236
2237 /* No match, but see if we can get a fuzzy operand size match... */
2238 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002239 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002240
2241 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002242 /*
2243 * We ignore extrinsic operand sizes on registers, so we should
2244 * never try to fuzzy-match on them. This also resolves the case
2245 * when we have e.g. "xmmrm128" in two different positions.
2246 */
2247 if (is_class(REGISTER, instruction->oprs[i].type))
2248 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002249
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002250 /* This tests if xsizeflags[i] has more than one bit set */
2251 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2252 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002253
Jin Kyu Song7903c072013-10-30 03:00:12 -07002254 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002255 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002256 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2257 BITS32 : BITS64);
2258 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002259 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002260 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002261 }
2262
2263 /* Try matching again... */
2264 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002265 temp->opcode != I_none; temp++) {
2266 m = matches(temp, instruction, bits);
2267 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002268 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002269 m = MOK_GOOD;
2270 else
2271 m = MERR_INVALOP;
2272 }
2273 if (m > merr)
2274 merr = m;
2275 if (merr == MOK_GOOD)
2276 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002277 }
2278
H. Peter Anvina81655b2009-07-25 18:15:28 -07002279done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002280 *tempp = temp;
2281 return merr;
2282}
2283
Mark Charneydcaef4b2014-10-09 13:45:17 -04002284static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2285{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002286 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002287 uint8_t brcast_num;
2288
Mark Charneydcaef4b2014-10-09 13:45:17 -04002289 if (brsize > BITS64)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002290 nasm_fatal("size of broadcasting element is greater than 64 bits");
Mark Charneydcaef4b2014-10-09 13:45:17 -04002291
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002292 /*
2293 * The shift term is to take care of the extra BITS80 inserted
2294 * between BITS64 and BITS128.
2295 */
2296 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2297 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002298
2299 return brcast_num;
2300}
2301
H. Peter Anvin65289e82009-07-25 17:25:11 -07002302static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002303 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002304{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002305 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002306 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002307 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002308
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002309 /*
2310 * Check the opcode
2311 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002312 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002313 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002314
2315 /*
2316 * Count the operands
2317 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002318 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002319 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002320
2321 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002322 * Is it legal?
2323 */
Chang S. Baea5786342018-08-15 23:22:21 +03002324 if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002325 return MERR_INVALOP;
2326
2327 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002328 * {evex} available?
2329 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002330 switch (instruction->prefixes[PPS_VEX]) {
2331 case P_EVEX:
2332 if (!itemp_has(itemp, IF_EVEX))
2333 return MERR_ENCMISMATCH;
2334 break;
2335 case P_VEX3:
2336 case P_VEX2:
2337 if (!itemp_has(itemp, IF_VEX))
2338 return MERR_ENCMISMATCH;
2339 break;
2340 default:
2341 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002342 }
2343
2344 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002345 * Check that no spurious colons or TOs are present
2346 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002347 for (i = 0; i < itemp->operands; i++)
2348 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002349 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002350
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002351 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002352 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002353 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002354 switch (itemp_smask(itemp)) {
2355 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002356 asize = BITS8;
2357 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002358 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002359 asize = BITS16;
2360 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002361 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002362 asize = BITS32;
2363 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002364 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002365 asize = BITS64;
2366 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002367 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002368 asize = BITS128;
2369 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002370 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002371 asize = BITS256;
2372 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002373 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002374 asize = BITS512;
2375 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002376 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002377 switch (bits) {
2378 case 16:
2379 asize = BITS16;
2380 break;
2381 case 32:
2382 asize = BITS32;
2383 break;
2384 case 64:
2385 asize = BITS64;
2386 break;
2387 default:
2388 asize = 0;
2389 break;
2390 }
2391 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002392 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002393 asize = 0;
2394 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002395 }
2396
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002397 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002398 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002399 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002400 memset(size, 0, sizeof size);
2401 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002402 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002403 /* S- flags apply to all operands */
2404 for (i = 0; i < MAX_OPERANDS; i++)
2405 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002406 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002407
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002408 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002409 * Check that the operand flags all match up,
2410 * it's a bit tricky so lets be verbose:
2411 *
2412 * 1) Find out the size of operand. If instruction
2413 * doesn't have one specified -- we're trying to
2414 * guess it either from template (IF_S* flag) or
2415 * from code bits.
2416 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002417 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002418 * template has an operand size specified AND this size differ
2419 * from which instruction has (perhaps we got it from code bits)
2420 * we are:
2421 * a) Check that only size of instruction and operand is differ
2422 * other characteristics do match
2423 * b) Perhaps it's a register specified in instruction so
2424 * for such a case we just mark that operand as "size
2425 * missing" and this will turn on fuzzy operand size
2426 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002427 */
2428 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002429 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002430 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002431 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002432 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002433 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002434 opflags_t template_opsize, insn_opsize;
2435
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002436 if (!(type & SIZE_MASK))
2437 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002438
Jin Kyu Song7903c072013-10-30 03:00:12 -07002439 insn_opsize = type & SIZE_MASK;
2440 if (!is_broadcast) {
2441 template_opsize = itemp->opd[i] & SIZE_MASK;
2442 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002443 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2444
2445 if (~ideco & BRDCAST_MASK)
2446 return MERR_BRNOTHERE;
2447
Jin Kyu Song7903c072013-10-30 03:00:12 -07002448 /*
2449 * when broadcasting, the element size depends on
2450 * the instruction type. decorator flag should match.
2451 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002452 if (deco_brsize) {
2453 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002454 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002455 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002456 } else {
2457 template_opsize = 0;
2458 }
2459 }
2460
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002461 if (~ideco & deco & OPMASK_MASK)
2462 return MERR_MASKNOTHERE;
2463
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002464 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2465 return MERR_DECONOTHERE;
2466
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002467 if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002468 return MERR_INVALOP;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002469
2470 if (~itemp->opd[i] & type & REGSET_MASK)
2471 return (itemp->opd[i] & REGSET_MASK)
2472 ? MERR_REGSETSIZE : MERR_REGSET;
2473
2474 if (template_opsize) {
Jin Kyu Song7903c072013-10-30 03:00:12 -07002475 if (template_opsize != insn_opsize) {
2476 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002477 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002478 } else if (!is_class(REGISTER, type)) {
2479 /*
2480 * Note: we don't honor extrinsic operand sizes for registers,
2481 * so "missing operand size" for a register should be
2482 * considered a wildcard match rather than an error.
2483 */
2484 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002485 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002486 } else if (is_broadcast &&
2487 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002488 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002489 /*
2490 * broadcasting opsize matches but the number of repeated memory
2491 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002492 * if 64b double precision float is broadcasted to ymm (256b),
2493 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002494 */
2495 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002496 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002497 }
2498 }
2499
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002500 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002501 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002502
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002503 /*
2504 * Check operand sizes
2505 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002506 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2507 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002509 asize = itemp->opd[i] & SIZE_MASK;
2510 if (asize) {
2511 for (i = 0; i < oprs; i++)
2512 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002513 break;
2514 }
2515 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002516 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002517 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002518 }
2519
Keith Kaniosb7a89542007-04-12 02:40:54 +00002520 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 if (!(itemp->opd[i] & SIZE_MASK) &&
2522 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002523 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002524 }
2525
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002526 /*
2527 * Check template is okay at the set cpu level
2528 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002529 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002530 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002531
Keith Kaniosb7a89542007-04-12 02:40:54 +00002532 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002533 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002534 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002535 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002536 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002538 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002539 * If we have a HLE prefix, look for the NOHLE flag
2540 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002541 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002542 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2543 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2544 return MERR_BADHLE;
2545
2546 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002547 * Check if special handling needed for Jumps
2548 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002549 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002550 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002551
Jin Kyu Song03041092013-10-15 19:38:51 -07002552 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002553 * Check if BND prefix is allowed.
2554 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002555 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002556 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002557 (has_prefix(instruction, PPS_REP, P_BND) ||
2558 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002559 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002560 else if (itemp_has(itemp, IF_BND) &&
2561 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2562 has_prefix(instruction, PPS_REP, P_REPNZ)))
2563 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002564
H. Peter Anvin60926242009-07-26 16:25:38 -07002565 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002566}
2567
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002568/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002569 * Check if ModR/M.mod should/can be 01.
2570 * - EAF_BYTEOFFS is set
2571 * - offset can fit in a byte when EVEX is not used
2572 * - offset can be compressed when EVEX is used
2573 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002574#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2575 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2576 is_disp8n(input, ins, &output->disp8) : \
2577 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2578 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002579
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002580static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002581 int rfield, opflags_t rflags, insn *ins,
2582 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002583{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002584 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002585 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002586 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002587
H. Peter Anvin8f622462017-04-02 19:02:29 -07002588 *errmsg = "invalid effective address"; /* Default error message */
2589
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002590 output->type = EA_SCALAR;
2591 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002592 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002593
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002594 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002595 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002596 /* EVEX.R' flag for the REG operand */
2597 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002598
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002599 if (is_class(REGISTER, input->type)) {
2600 /*
2601 * It's a direct register.
2602 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002603 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002604 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002605
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002606 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002607 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002608
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002609 /* broadcasting is not available with a direct register operand. */
2610 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002611 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002612 goto err;
2613 }
2614
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002615 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002616 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002617 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002618 output->bytes = 0; /* no offset necessary either */
2619 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2620 } else {
2621 /*
2622 * It's a memory reference.
2623 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002624
2625 /* Embedded rounding or SAE is not available with a mem ref operand. */
2626 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002627 *errmsg = "embedded rounding is available only with "
2628 "register-register operations";
2629 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002630 }
2631
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002632 if (input->basereg == -1 &&
2633 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002634 /*
2635 * It's a pure offset.
2636 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002637 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002638 if (input->segment == NO_SEG ||
2639 (input->opflags & OPFLAG_RELATIVE)) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08002640 nasm_warn(WARN_OTHER|ERR_PASS2, "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002641 input->type &= ~IP_REL;
2642 input->type |= MEMORY;
2643 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002644 }
2645
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002646 if (bits == 64 &&
2647 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002648 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002649 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002650 }
2651
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002652 if (eaflags & EAF_BYTEOFFS ||
2653 (eaflags & EAF_WORDOFFS &&
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002654 input->disp_size != (addrbits != 16 ? 32 : 16)))
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002655 nasm_warn(WARN_OTHER, "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002656
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002657 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002658 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002659 output->sib = GEN_SIB(0, 4, 5);
2660 output->bytes = 4;
2661 output->modrm = GEN_MODRM(0, rfield, 4);
2662 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002663 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002664 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002665 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002666 output->modrm = GEN_MODRM(0, rfield,
2667 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002668 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002669 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002670 } else {
2671 /*
2672 * It's an indirection.
2673 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002674 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002675 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002677 int t, it, bt; /* register numbers */
2678 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002679
H. Peter Anvine2c80182005-01-15 22:15:51 +00002680 if (s == 0)
2681 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002682
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002683 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002684 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002685 ix = nasm_reg_flags[i];
2686 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002687 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002688 ix = 0;
2689 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002690
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002691 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002692 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002693 bx = nasm_reg_flags[b];
2694 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002695 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002696 bx = 0;
2697 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002698
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002699 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002700 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002701 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002702 int32_t o = input->offset;
2703 int mod, scale, index, base;
2704
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002705 /*
2706 * For a vector SIB, one has to be a vector and the other,
2707 * if present, a GPR. The vector must be the index operand.
2708 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002709 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002710 if (s == 0)
2711 s = 1;
2712 else if (s != 1)
2713 goto err;
2714
2715 t = bt, bt = it, it = t;
2716 x = bx, bx = ix, ix = x;
2717 }
2718
2719 if (bt != -1) {
2720 if (REG_GPR & ~bx)
2721 goto err;
2722 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2723 sok &= bx;
2724 else
2725 goto err;
2726 }
2727
2728 /*
2729 * While we're here, ensure the user didn't specify
2730 * WORD or QWORD
2731 */
2732 if (input->disp_size == 16 || input->disp_size == 64)
2733 goto err;
2734
2735 if (addrbits == 16 ||
2736 (addrbits == 32 && !(sok & BITS32)) ||
2737 (addrbits == 64 && !(sok & BITS64)))
2738 goto err;
2739
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002740 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2741 : ((ix & YMMREG & ~REG_EA)
2742 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002743
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002744 output->rex |= rexflags(it, ix, REX_X);
2745 output->rex |= rexflags(bt, bx, REX_B);
2746 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002747
2748 index = it & 7; /* it is known to be != -1 */
2749
2750 switch (s) {
2751 case 1:
2752 scale = 0;
2753 break;
2754 case 2:
2755 scale = 1;
2756 break;
2757 case 4:
2758 scale = 2;
2759 break;
2760 case 8:
2761 scale = 3;
2762 break;
2763 default: /* then what the smeg is it? */
2764 goto err; /* panic */
2765 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002766
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002767 if (bt == -1) {
2768 base = 5;
2769 mod = 0;
2770 } else {
2771 base = (bt & 7);
2772 if (base != REG_NUM_EBP && o == 0 &&
2773 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002774 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002775 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002776 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002777 mod = 1;
2778 else
2779 mod = 2;
2780 }
2781
2782 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002783 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2784 output->modrm = GEN_MODRM(mod, rfield, 4);
2785 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002786 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002787 /*
2788 * it must be a 32/64-bit memory reference. Firstly we have
2789 * to check that all registers involved are type E/Rxx.
2790 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002791 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002792 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002793
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002794 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002795 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2796 sok &= ix;
2797 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002798 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002799 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002800
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002801 if (bt != -1) {
2802 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002803 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002804 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002805 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002806 sok &= bx;
2807 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002808
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002809 /*
2810 * While we're here, ensure the user didn't specify
2811 * WORD or QWORD
2812 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002813 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002814 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002815
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002816 if (addrbits == 16 ||
2817 (addrbits == 32 && !(sok & BITS32)) ||
2818 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002819 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002820
Keith Kaniosb7a89542007-04-12 02:40:54 +00002821 /* now reorganize base/index */
2822 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002823 ((hb == b && ht == EAH_NOTBASE) ||
2824 (hb == i && ht == EAH_MAKEBASE))) {
2825 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002826 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002827 x = bx, bx = ix, ix = x;
2828 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002829
Jin Kyu Song164d6072013-10-15 19:10:13 -07002830 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002831 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002832 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002833 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002834 if (eaflags & EAF_MIB) {
2835 /* only for mib operands */
2836 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2837 /*
2838 * make a single reg index [reg*1].
2839 * gas uses this form for an explicit index register.
2840 */
2841 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2842 }
2843 if ((ht == EAH_SUMMED) && bt == -1) {
2844 /* separate once summed index into [base, index] */
2845 bt = it, bx = ix, s--;
2846 }
2847 } else {
2848 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002849 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002850 s == 3 || s == 5 || s == 9) && bt == -1) {
2851 /* convert 3*EAX to EAX+2*EAX */
2852 bt = it, bx = ix, s--;
2853 }
2854 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002855 (eaflags & EAF_TIMESTWO) &&
2856 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002857 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002858 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002859 * to sib format with 0x0 displacement - [EAX*1+0].
2860 */
2861 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2862 }
2863 }
Keith Kanios48af1772007-08-17 07:37:52 +00002864 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002865 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002866 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002867 x = ix, ix = bx, bx = x;
2868 }
2869 if (it == REG_NUM_ESP ||
2870 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002871 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002872
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002873 output->rex |= rexflags(it, ix, REX_X);
2874 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002875
Keith Kanios48af1772007-08-17 07:37:52 +00002876 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002877 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002878 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002879
Keith Kaniosb7a89542007-04-12 02:40:54 +00002880 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002881 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002882 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002883 } else {
2884 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002885 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002886 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002887 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002888 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002889 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002890 mod = 1;
2891 else
2892 mod = 2;
2893 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002894
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002895 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002896 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2897 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002898 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002899 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002901
Keith Kaniosb7a89542007-04-12 02:40:54 +00002902 if (it == -1)
2903 index = 4, s = 1;
2904 else
2905 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002906
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 switch (s) {
2908 case 1:
2909 scale = 0;
2910 break;
2911 case 2:
2912 scale = 1;
2913 break;
2914 case 4:
2915 scale = 2;
2916 break;
2917 case 8:
2918 scale = 3;
2919 break;
2920 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002921 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002922 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002923
Keith Kaniosb7a89542007-04-12 02:40:54 +00002924 if (bt == -1) {
2925 base = 5;
2926 mod = 0;
2927 } else {
2928 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002929 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002930 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002931 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002932 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002933 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002934 mod = 1;
2935 else
2936 mod = 2;
2937 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002938
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002939 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002940 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2941 output->modrm = GEN_MODRM(mod, rfield, 4);
2942 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002943 }
2944 } else { /* it's 16-bit */
2945 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002946 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002947
Keith Kaniosb7a89542007-04-12 02:40:54 +00002948 /* check for 64-bit long mode */
2949 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002950 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002951
H. Peter Anvine2c80182005-01-15 22:15:51 +00002952 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002953 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2954 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002955 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002956
Keith Kaniosb7a89542007-04-12 02:40:54 +00002957 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002958 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002959 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002960
H. Peter Anvine2c80182005-01-15 22:15:51 +00002961 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002962 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002963 if (b == -1 && i != -1) {
2964 int tmp = b;
2965 b = i;
2966 i = tmp;
2967 } /* swap */
2968 if ((b == R_SI || b == R_DI) && i != -1) {
2969 int tmp = b;
2970 b = i;
2971 i = tmp;
2972 }
2973 /* have BX/BP as base, SI/DI index */
2974 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002975 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002976 if (i != -1 && b != -1 &&
2977 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002978 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002979 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002980 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002981
H. Peter Anvine2c80182005-01-15 22:15:51 +00002982 rm = -1;
2983 if (i != -1)
2984 switch (i * 256 + b) {
2985 case R_SI * 256 + R_BX:
2986 rm = 0;
2987 break;
2988 case R_DI * 256 + R_BX:
2989 rm = 1;
2990 break;
2991 case R_SI * 256 + R_BP:
2992 rm = 2;
2993 break;
2994 case R_DI * 256 + R_BP:
2995 rm = 3;
2996 break;
2997 } else
2998 switch (b) {
2999 case R_SI:
3000 rm = 4;
3001 break;
3002 case R_DI:
3003 rm = 5;
3004 break;
3005 case R_BP:
3006 rm = 6;
3007 break;
3008 case R_BX:
3009 rm = 7;
3010 break;
3011 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003012 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003013 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003014
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003015 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003016 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003018 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00003019 mod = 1;
3020 else
3021 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003022
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003023 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003024 output->bytes = mod; /* bytes of offset needed */
3025 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003026 }
3027 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003028 }
H. Peter Anvin70653092007-10-19 14:42:29 -07003029
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003030 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003031 return output->type;
3032
3033err:
3034 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003035}
3036
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003037static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003038{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003039 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003040 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003041
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003042 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003043
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003044 switch (ins->prefixes[PPS_ASIZE]) {
3045 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003046 valid &= 16;
3047 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003048 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003049 valid &= 32;
3050 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003051 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003052 valid &= 64;
3053 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003054 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003055 valid &= (addrbits == 32) ? 16 : 32;
3056 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003057 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003058 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003059 }
3060
3061 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003062 if (is_class(MEMORY, ins->oprs[j].type)) {
3063 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07003064
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003065 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003066 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003067 i = 0;
3068 else
3069 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003070
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003071 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003072 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003073 b = 0;
3074 else
3075 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003076
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003077 if (ins->oprs[j].scale == 0)
3078 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003079
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003080 if (!i && !b) {
3081 int ds = ins->oprs[j].disp_size;
3082 if ((addrbits != 64 && ds > 8) ||
3083 (addrbits == 64 && ds == 16))
3084 valid &= ds;
3085 } else {
3086 if (!(REG16 & ~b))
3087 valid &= 16;
3088 if (!(REG32 & ~b))
3089 valid &= 32;
3090 if (!(REG64 & ~b))
3091 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07003092
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003093 if (!(REG16 & ~i))
3094 valid &= 16;
3095 if (!(REG32 & ~i))
3096 valid &= 32;
3097 if (!(REG64 & ~i))
3098 valid &= 64;
3099 }
3100 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003101 }
3102
3103 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003104 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003105 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003106 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04003107 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003108 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003109 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003110 /* Impossible... */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03003111 nasm_nonfatal("impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003112 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003113 }
3114
3115 defdisp = ins->addr_size == 16 ? 16 : 32;
3116
3117 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003118 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
3119 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
3120 /*
3121 * mem_offs sizes must match the address size; if not,
3122 * strip the MEM_OFFS bit and match only EA instructions
3123 */
3124 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
3125 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003126 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003127}