blob: de1abc26a8c8385c7b174c12844ccfe9a81a80b6 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvin (Intel)38ddb192019-01-11 12:27:02 -08003 * Copyright 1996-2019 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040037 * Bytecode specification
38 * ----------------------
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070039 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040040 *
41 * Codes Mnemonic Explanation
42 *
43 * \0 terminates the code. (Unless it's a literal of course.)
44 * \1..\4 that many literal bytes follow in the code stream
45 * \5 add 4 to the primary operand number (b, low octdigit)
46 * \6 add 4 to the secondary operand number (a, middle octdigit)
47 * \7 add 4 to both the primary and the secondary operand number
48 * \10..\13 a literal byte follows in the code stream, to be added
49 * to the register value of operand 0..3
50 * \14..\17 the position of index register operand in MIB (BND insns)
51 * \20..\23 ib a byte immediate operand, from operand 0..3
52 * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
53 * \30..\33 iw a word immediate operand, from operand 0..3
54 * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
55 * assembly mode or the operand-size override on the operand
56 * \40..\43 id a long immediate operand, from operand 0..3
57 * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
58 * depending on the address size of the instruction.
59 * \50..\53 rel8 a byte relative operand, from operand 0..3
60 * \54..\57 iq a qword immediate operand, from operand 0..3
61 * \60..\63 rel16 a word relative operand, from operand 0..3
62 * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
63 * assembly mode or the operand-size override on the operand
64 * \70..\73 rel32 a long relative operand, from operand 0..3
65 * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
66 * \1ab a ModRM, calculated on EA in operand a, with the spare
67 * field the register value of operand b.
68 * \172\ab the register number from operand a in bits 7..4, with
69 * the 4-bit immediate from operand b in bits 3..0.
70 * \173\xab the register number from operand a in bits 7..4, with
71 * the value b in bits 3..0.
72 * \174..\177 the register number from operand 0..3 in bits 7..4, and
73 * an arbitrary value in bits 3..0 (assembled as zero.)
74 * \2ab a ModRM, calculated on EA in operand a, with the spare
75 * field equal to digit b.
76 *
77 * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
78 * V field taken from operand 0..3.
79 * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
80 * V field set to 1111b.
81 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070082 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -070084 * cc 00m mmm
85 * c = 2 for EVEX and mmmm is the M field (EVEX.P0[3:0])
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070086 * and wlp is:
87 * 00 wwl lpp
88 * [l0] ll = 0 (.128, .lz)
89 * [l1] ll = 1 (.256)
90 * [l2] ll = 2 (.512)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
92 *
93 * [w0] ww = 0 for W = 0
94 * [w1] ww = 1 for W = 1
95 * [wig] ww = 2 for W don't care (always assembled as 0)
96 * [ww] ww = 3 for W used as REX.W
97 *
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
100 * [f3] pp = 2
101 * [f2] pp = 3
102 *
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
105 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400106 * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
107 * \260..\263 this instruction uses VEX/XOP rather than REX, with the
108 * V field taken from operand 0..3.
109 * \270 this instruction uses VEX/XOP rather than REX, with the
110 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700111 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700114 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700115 * [l0] ll = 0 for L = 0 (.128, .lz)
116 * [l1] ll = 1 for L = 1 (.256)
117 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700118 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700119 * [w0] ww = 0 for W = 0
120 * [w1 ] ww = 1 for W = 1
121 * [wig] ww = 2 for W don't care (always assembled as 0)
122 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700123 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700125 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400126 * \271 hlexr instruction takes XRELEASE (F3) with or without lock
127 * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
128 * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
129 * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
130 * to the operand size (if o16/o32/o64 present) or the bit size
131 * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
132 * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
133 * \312 adf (disassembler only) invalid with non-default address size.
134 * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
135 * \314 norexb (disassembler only) invalid with REX.B
136 * \315 norexx (disassembler only) invalid with REX.X
137 * \316 norexr (disassembler only) invalid with REX.R
138 * \317 norexw (disassembler only) invalid with REX.W
139 * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
140 * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
141 * \322 odf indicates that this instruction is only valid when the
142 * operand size is the default (instruction to disassembler,
143 * generates no code in the assembler)
144 * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
145 * \324 o64 indicates 64-bit operand size requiring REX prefix.
146 * \325 nohi instruction which always uses spl/bpl/sil/dil
147 * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
148 disassembler only; for SSE instructions.
149 * \330 a literal byte follows in the code stream, to be added
150 * to the condition code value of the instruction.
151 * \331 norep instruction not valid with REP prefix. Hint for
152 * disassembler only; for SSE instructions.
153 * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
154 * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
155 * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
156 * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
157 * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
158 * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
159 * \336-\337 are still listed as prefixes in the disassembler.
160 * \340 resb reserve <operand 0> bytes of uninitialized storage.
161 * Operand 0 had better be a segmentless constant.
162 * \341 wait this instruction needs a WAIT "prefix"
Cyrill Gorcunov8a5d3e62014-08-25 20:04:30 +0400163 * \360 np no SSE prefix (== \364\331)
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400164 * \361 66 SSE prefix (== \366\331)
165 * \364 !osp operand-size prefix (0x66) not permitted
166 * \365 !asp address-size prefix (0x67) not permitted
167 * \366 operand-size prefix (0x66) used as opcode extension
168 * \367 address-size prefix (0x67) used as opcode extension
169 * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
170 * jmp8 370 is used for Jcc, 371 is used for JMP.
171 * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
172 * used for conditional jump over longer jump
173 * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
174 * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
175 * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 */
177
H. Peter Anvinfe501952007-10-02 21:53:51 -0700178#include "compiler.h"
179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180
181#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000182#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800183#include "error.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000184#include "assemble.h"
185#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700186#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800187#include "disp8.h"
H. Peter Anvin172b8402016-02-18 01:16:18 -0800188#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000189
H. Peter Anvin65289e82009-07-25 17:25:11 -0700190enum match_result {
191 /*
192 * Matching errors. These should be sorted so that more specific
193 * errors come later in the sequence.
194 */
195 MERR_INVALOP,
196 MERR_OPSIZEMISSING,
197 MERR_OPSIZEMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700198 MERR_BRNOTHERE,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700199 MERR_BRNUMMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700200 MERR_MASKNOTHERE,
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700201 MERR_DECONOTHERE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700202 MERR_BADCPU,
203 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800204 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700205 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700206 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800207 MERR_BADREPNE,
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700208 MERR_REGSETSIZE,
209 MERR_REGSET,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700210 /*
211 * Matching success; the conditional ones first
212 */
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700213 MOK_JUMP, /* Matching OK but needs jmp_match() */
214 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700215};
216
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000217typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700218 enum ea_type type; /* what kind of EA is this? */
219 int sib_present; /* is a SIB byte necessary? */
220 int bytes; /* # of bytes of offset needed */
221 int size; /* lazy - this is sib+bytes+1 */
222 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700223 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224} ea;
225
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400226#define GEN_SIB(scale, index, base) \
227 (((scale) << 6) | ((index) << 3) | ((base)))
228
229#define GEN_MODRM(mod, reg, rm) \
230 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
231
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800232static int64_t calcsize(int32_t, int64_t, int, insn *,
233 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700234static int emit_prefix(struct out_data *data, const int bits, insn *ins);
235static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700236static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400237 insn *instruction,
238 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700239static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700240static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000241static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700242static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000243static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700244static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700245static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000246
H. Peter Anvin8f622462017-04-02 19:02:29 -0700247static enum ea_type process_ea(operand *, ea *, int, int,
248 opflags_t, insn *, const char **);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700249
H. Peter Anvin164d2462017-02-20 02:39:56 -0800250static inline bool absolute_op(const struct operand *o)
251{
252 return o->segment == NO_SEG && o->wrt == NO_SEG &&
253 !(o->opflags & OPFLAG_RELATIVE);
254}
255
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400256static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000257{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700258 return ins->prefixes[pos] == prefix;
259}
260
261static void assert_no_prefix(insn * ins, enum prefix_pos pos)
262{
263 if (ins->prefixes[pos])
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300264 nasm_nonfatal("invalid %s prefix", prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265}
266
267static const char *size_name(int size)
268{
269 switch (size) {
270 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400271 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700272 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400273 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700274 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700276 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400277 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700278 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400279 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700280 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700282 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400283 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700284 case 64:
285 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700286 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400287 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000288 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700289}
290
H. Peter Anvin285222f2017-03-01 13:27:33 -0800291static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400292{
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800293 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW, "%s data exceeds bounds",
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300294 size_name(size));
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400295}
296
297static void warn_overflow_const(int64_t data, int size)
298{
299 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800300 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400301}
302
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800303static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
304{
305 bool err;
306
307 switch (sign) {
308 case OUT_WRAP:
309 err = overflow_general(data, size);
310 break;
311 case OUT_SIGNED:
312 err = overflow_signed(data, size);
313 break;
314 case OUT_UNSIGNED:
315 err = overflow_unsigned(data, size);
316 break;
317 default:
318 panic();
319 break;
320 }
321
322 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800323 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800324}
325
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000326/*
327 * This routine wrappers the real output format's output routine,
328 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800329 * generator at the same time, flatten unnecessary relocations,
330 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000331 */
H. Peter Anvin (Intel)38ddb192019-01-11 12:27:02 -0800332/*
333 * This warning is currently issued by backends, but in the future
334 * this code should be centralized.
335 *
336 *!zeroing [on] RESx in initialized section becomes zero
337 *! a \c{RESx} directive was used in a section which contains
338 *! initialized data, and the output format does not support
339 *! this. Instead, this will be replaced with explicit zero
340 *! content, which may produce a large output file.
341 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700342static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000343{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000344 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700345 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700346 union {
347 uint8_t b[8];
348 uint64_t q;
349 } xdata;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700350 size_t asize, amax;
351 uint64_t zeropad = 0;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800352 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800353 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000354
H. Peter Anvina77692b2016-09-20 14:04:33 -0700355 if (!data->size)
356 return; /* Nothing to do */
357
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700358 /*
359 * Convert addresses to RAWDATA if possible
360 * XXX: not all backends want this for global symbols!!!!
361 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700362 switch (data->type) {
363 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800364 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800365 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800366 goto address;
367
368 case OUT_RELADDR:
369 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800370 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800371 goto address;
372
373 address:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700374 nasm_assert(data->size <= 8);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700375 asize = data->size;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700376 amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
Chang S. Bae427d8e32018-05-02 08:07:52 -0700377 if ((ofmt->flags & OFMT_KEEP_ADDR) == 0 && data->tsegment == fixseg &&
378 data->twrt == NO_SEG) {
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800379 warn_overflow_out(addrval, asize, data->sign);
Martin Storsjö869087d2017-05-22 13:54:20 +0300380 xdata.q = cpu_to_le64(addrval);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700381 data->data = xdata.b;
382 data->type = OUT_RAWDATA;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700383 asize = amax = 0; /* No longer an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700384 }
385 break;
386
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700387 case OUT_SEGMENT:
388 nasm_assert(data->size <= 8);
389 asize = data->size;
390 amax = 2;
391 break;
392
H. Peter Anvina77692b2016-09-20 14:04:33 -0700393 default:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700394 asize = amax = 0; /* Not an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700395 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000396 }
397
Frank Kotlerabebb082003-09-06 04:45:37 +0000398 /*
399 * this call to src_get determines when we call the
400 * debug-format-specific "linenum" function
401 * it updates lineno and lnfname to the current values
402 * returning 0 if "same as last time", -2 if lnfname
403 * changed, and the amount by which lineno changed,
404 * if it did. thus, these variables must be static
405 */
406
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400407 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700408 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000409
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700410 if (asize > amax) {
411 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300412 nasm_nonfatal("%u-bit signed relocation unsupported by output format %s",
413 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800414 } else {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800415 /*!
416 *!zext-reloc [on] relocation zero-extended to match output format
417 *! warns that a relocation has been zero-extended due
418 *! to limitations in the output format.
419 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800420 nasm_warn(WARN_ZEXT_RELOC,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700421 "%u-bit %s relocation zero-extended from %u bits",
422 (unsigned int)(asize << 3),
423 data->type == OUT_SEGMENT ? "segment" : "unsigned",
424 (unsigned int)(amax << 3));
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800425 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700426 zeropad = data->size - amax;
427 data->size = amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800428 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700429 lfmt->output(data);
H. Peter Anvined859f72018-06-15 00:03:53 -0700430
431 if (likely(data->segment != NO_SEG)) {
432 ofmt->output(data);
433 } else {
434 /* Outputting to ABSOLUTE section - only reserve is permitted */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300435 if (data->type != OUT_RESERVE)
436 nasm_nonfatal("attempt to assemble code in [ABSOLUTE] space");
H. Peter Anvined859f72018-06-15 00:03:53 -0700437 /* No need to push to the backend */
438 }
439
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700440 data->offset += data->size;
441 data->insoffs += data->size;
442
443 if (zeropad) {
444 data->type = OUT_ZERODATA;
445 data->size = zeropad;
446 lfmt->output(data);
447 ofmt->output(data);
448 data->offset += zeropad;
449 data->insoffs += zeropad;
450 data->size += zeropad; /* Restore original size value */
451 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000452}
453
H. Peter Anvina77692b2016-09-20 14:04:33 -0700454static inline void out_rawdata(struct out_data *data, const void *rawdata,
455 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400456{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700457 data->type = OUT_RAWDATA;
458 data->data = rawdata;
459 data->size = size;
460 out(data);
461}
462
463static void out_rawbyte(struct out_data *data, uint8_t byte)
464{
465 data->type = OUT_RAWDATA;
466 data->data = &byte;
467 data->size = 1;
468 out(data);
469}
470
471static inline void out_reserve(struct out_data *data, uint64_t size)
472{
473 data->type = OUT_RESERVE;
474 data->size = size;
475 out(data);
476}
477
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700478static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700479{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700480 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300481 nasm_nonfatal("segment references cannot be relative");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700482
483 data->type = OUT_SEGMENT;
484 data->sign = OUT_UNSIGNED;
485 data->size = 2;
486 data->toffset = opx->offset;
487 data->tsegment = ofmt->segbase(opx->segment | 1);
488 data->twrt = opx->wrt;
489 out(data);
490}
491
492static void out_imm(struct out_data *data, const struct operand *opx,
493 int size, enum out_sign sign)
494{
495 if (opx->segment != NO_SEG && (opx->segment & 1)) {
496 /*
497 * This is actually a segment reference, but eval() has
498 * already called ofmt->segbase() for us. Sigh.
499 */
500 if (size < 2)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300501 nasm_nonfatal("segment reference must be 16 bits");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700502
503 data->type = OUT_SEGMENT;
504 } else {
505 data->type = (opx->opflags & OPFLAG_RELATIVE)
506 ? OUT_RELADDR : OUT_ADDRESS;
507 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700508 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700509 data->toffset = opx->offset;
510 data->tsegment = opx->segment;
511 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800512 /*
513 * XXX: improve this if at some point in the future we can
514 * distinguish the subtrahend in expressions like [foo - bar]
515 * where bar is a symbol in the current segment. However, at the
516 * current point, if OPFLAG_RELATIVE is set that subtraction has
517 * already occurred.
518 */
519 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700520 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700521 out(data);
522}
523
H. Peter Anvin164d2462017-02-20 02:39:56 -0800524static void out_reladdr(struct out_data *data, const struct operand *opx,
525 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700526{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800527 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300528 nasm_nonfatal("invalid use of self-relative expression");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800529
H. Peter Anvina77692b2016-09-20 14:04:33 -0700530 data->type = OUT_RELADDR;
531 data->sign = OUT_SIGNED;
532 data->size = size;
533 data->toffset = opx->offset;
534 data->tsegment = opx->segment;
535 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800536 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700537 out(data);
538}
539
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700540static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800541 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800543 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800544 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000545 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800546 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000547
H. Peter Anvin755f5212012-02-25 11:41:34 -0800548 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700549 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300550 if (!optimizing.level || (optimizing.flag & OPTIM_DISABLE_JMP_MATCH))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400551 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300552 if (optimizing.level < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400553 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700554
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800555 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100556
Victor van den Elzen154e5922009-02-25 17:32:00 +0100557 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100558 /* Be optimistic in pass 1 */
559 return true;
560
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700562 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000563
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700564 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800565 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
566
567 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
568 /* jmp short (opcode eb) cannot be used with bnd prefix. */
569 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800570 /*!
571 *!bnd [on] invalid BND prefixes
572 *! warns about ineffective use of the \c{BND} prefix when the
573 *! \c{JMP} instruction is converted to the \c{SHORT} form.
574 *! This should be extremely rare since the short \c{JMP} only
575 *! is applicable to jumps inside the same module, but if
576 *! it is legitimate, it may be necessary to use
H. Peter Anvin959702b2019-06-06 20:56:50 -0700577 *! \c{bnd jmp dword}.
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800578 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800579 nasm_warn(WARN_BND | ERR_PASS2 ,
H. Peter Anvin959702b2019-06-06 20:56:50 -0700580 "jmp short does not init bnd regs - bnd prefix dropped");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800581 }
582
583 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000584}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000585
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700586static inline int64_t merge_resb(insn *ins, int64_t isize)
587{
588 int nbytes = resb_bytes(ins->opcode);
589
590 if (likely(!nbytes))
591 return isize;
592
593 if (isize != nbytes * ins->oprs[0].offset)
594 return isize; /* Has prefixes of some sort */
595
596 ins->oprs[0].offset *= ins->times;
597 isize *= ins->times;
598 ins->times = 1;
599 return isize;
600}
601
H. Peter Anvin04445362016-09-21 15:56:19 -0700602/* This is totally just a wild guess what is reasonable... */
603#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
604
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800605int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000606{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700607 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000608 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700609 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300610 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000611
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700612 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700613 data.offset = start;
614 data.segment = segment;
615 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700616 data.bits = bits;
617
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700618 wsize = db_bytes(instruction->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300619 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000621
H. Peter Anvineba20a72002-04-30 20:53:55 +0000622 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700624
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700625 list_for_each(e, instruction->eops) {
626 if (e->type == EOT_DB_NUMBER) {
627 if (wsize > 8) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300628 nasm_nonfatal("integer supplied to a DT,DO,DY or DZ");
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700629 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700630 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700631 data.inslen = data.size = wsize;
632 data.toffset = e->offset;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700633 data.twrt = e->wrt;
634 data.relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700635 if (e->segment != NO_SEG && (e->segment & 1)) {
636 data.tsegment = e->segment;
637 data.type = OUT_SEGMENT;
638 data.sign = OUT_UNSIGNED;
639 } else {
640 data.tsegment = e->segment;
641 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
642 data.sign = OUT_WRAP;
643 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700644 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700646 } else if (e->type == EOT_DB_STRING ||
647 e->type == EOT_DB_STRING_FREE) {
648 int align = e->stringlen % wsize;
649 if (align)
650 align = wsize - align;
651
652 data.insoffs = 0;
653 data.inslen = e->stringlen + align;
654
655 out_rawdata(&data, e->stringval, e->stringlen);
656 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700657 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700659 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700660 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700662 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700663 off_t base = 0;
664 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700665 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700666 char *buf = NULL;
667 size_t blk = 0; /* Buffered I/O block size */
668 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000669
H. Peter Anvin94ead272017-09-27 15:22:23 -0700670 if (!t)
671 goto done;
672
H. Peter Anvind81a2352016-09-21 14:03:18 -0700673 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400674 if (!fp) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300675 nasm_nonfatal("`incbin': unable to open file `%s'",
676 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700677 goto done;
678 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000679
H. Peter Anvind81a2352016-09-21 14:03:18 -0700680 len = nasm_file_size(fp);
681
682 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300683 nasm_nonfatal("`incbin': unable to get length of file `%s'",
684 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700685 goto close_done;
686 }
687
H. Peter Anvina77692b2016-09-20 14:04:33 -0700688 if (instruction->eops->next) {
689 base = instruction->eops->next->offset;
690 if (base >= len) {
691 len = 0;
692 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 len -= base;
694 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700695 len > (off_t)instruction->eops->next->next->offset)
696 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700699
H. Peter Anvina77692b2016-09-20 14:04:33 -0700700 lfmt->set_offset(data.offset);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700701 lfmt->uplevel(LIST_INCBIN, len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000702
H. Peter Anvind81a2352016-09-21 14:03:18 -0700703 if (!len)
704 goto end_incbin;
705
706 /* Try to map file data */
707 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700708 if (!map) {
709 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
710 buf = nasm_malloc(blk);
711 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700712
713 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700714 /*
715 * Consider these irrelevant for INCBIN, since it is fully
716 * possible that these might be (way) bigger than an int
717 * can hold; there is, however, no reason to widen these
718 * types just for INCBIN. data.inslen == 0 signals to the
719 * backend that these fields are meaningless, if at all
720 * needed.
721 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700722 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700723 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700724
H. Peter Anvind81a2352016-09-21 14:03:18 -0700725 if (map) {
726 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700727 } else if ((off_t)m == len) {
728 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700729 } else {
730 off_t l = len;
731
732 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300733 nasm_nonfatal("`incbin': unable to seek on file `%s'",
734 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700735 goto end_incbin;
736 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700737 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700738 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700739 if (!m || feof(fp)) {
740 /*
741 * This shouldn't happen unless the file
742 * actually changes while we are reading
743 * it.
744 */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300745 nasm_nonfatal("`incbin': unexpected EOF while"
746 " reading file `%s'", fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700747 goto end_incbin;
748 }
749 out_rawdata(&data, buf, m);
750 l -= m;
751 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700752 }
753 }
754 end_incbin:
755 lfmt->downlevel(LIST_INCBIN);
756 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700757 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700758 lfmt->downlevel(LIST_TIMES);
759 }
760 if (ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300761 nasm_nonfatal("`incbin': error while"
762 " reading file `%s'", fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700763 }
764 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700765 if (buf)
766 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700767 if (map)
768 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700769 fclose(fp);
770 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700771 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700772 ;
773 } else {
774 /* "Real" instruction */
775
776 /* Check to see if we need an address-size prefix */
777 add_asp(instruction, bits);
778
779 m = find_match(&temp, instruction, data.segment, data.offset, bits);
780
781 if (m == MOK_GOOD) {
782 /* Matches! */
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700783 if (unlikely(itemp_has(temp, IF_OBSOLETE))) {
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700784 errflags warning;
785 const char *whathappened;
786 const char *validity;
787 bool never = itemp_has(temp, IF_NEVER);
788
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700789 /*
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700790 * If IF_OBSOLETE is set, warn the user. Different
791 * warning classes for "obsolete but valid for this
792 * specific CPU" and "obsolete and gone."
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700793 *
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700794 *!obsolete-removed [on] instruction obsolete and removed on the target CPU
795 *! warns for an instruction which has been removed
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700796 *! from the architecture, and is no longer included
797 *! in the CPU definition given in the \c{[CPU]}
798 *! directive, for example \c{POP CS}, the opcode for
799 *! which, \c{0Fh}, instead is an opcode prefix on
800 *! CPUs newer than the first generation 8086.
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700801 *
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700802 *!obsolete-nop [on] instruction obsolete and is a noop on the target CPU
803 *! warns for an instruction which has been removed
804 *! from the architecture, but has been architecturally
805 *! defined to be a noop for future CPUs.
806 *
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700807 *!obsolete-valid [on] instruction obsolete but valid on the target CPU
808 *! warns for an instruction which has been removed
809 *! from the architecture, but is still valid on the
810 *! specific CPU given in the \c{CPU} directive. Code
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700811 *! using these instructions is most likely not
812 *! forward compatible.
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700813 */
814
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700815 whathappened = never ? "never implemented" : "obsolete";
816
817 if (!never && !iflag_cmp_cpu_level(&insns_flags[temp->iflag_idx], &cpu)) {
818 warning = WARN_OBSOLETE_VALID;
819 validity = "but valid on";
820 } else if (itemp_has(temp, IF_NOP)) {
821 warning = WARN_OBSOLETE_NOP;
822 validity = "and is a noop on";
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700823 } else {
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700824 warning = WARN_OBSOLETE_REMOVED;
825 validity = "and removed from";
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700826 }
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700827
828 nasm_warn(warning, "instruction %s %s the target CPU",
829 whathappened, validity);
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700830 }
831
H. Peter Anvina77692b2016-09-20 14:04:33 -0700832 data.itemp = temp;
833 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700834 data.insoffs = 0;
H. Peter Anvin (Intel)77335212019-08-06 23:22:48 -0700835
836 data.inslen = calcsize(data.segment, data.offset,
837 bits, instruction, temp);
838 nasm_assert(data.inslen >= 0);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700839 data.inslen = merge_resb(instruction, data.inslen);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700840
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700841 gencode(&data, instruction);
H. Peter Anvin (Intel)77335212019-08-06 23:22:48 -0700842 nasm_assert(data.insoffs == data.inslen);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700843 } else {
844 /* No match */
845 switch (m) {
846 case MERR_OPSIZEMISSING:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300847 nasm_nonfatal("operation size not specified");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700848 break;
849 case MERR_OPSIZEMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300850 nasm_nonfatal("mismatch in operand sizes");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700851 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700852 case MERR_BRNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300853 nasm_nonfatal("broadcast not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700854 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700855 case MERR_BRNUMMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300856 nasm_nonfatal("mismatch in the number of broadcasting elements");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700857 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700858 case MERR_MASKNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300859 nasm_nonfatal("mask not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700860 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700861 case MERR_DECONOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300862 nasm_nonfatal("unsupported mode decorator for instruction");
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700863 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700864 case MERR_BADCPU:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300865 nasm_nonfatal("no instruction for this cpu level");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700866 break;
867 case MERR_BADMODE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300868 nasm_nonfatal("instruction not supported in %d-bit mode", bits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700869 break;
870 case MERR_ENCMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300871 nasm_nonfatal("specific encoding scheme not available");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700872 break;
873 case MERR_BADBND:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300874 nasm_nonfatal("bnd prefix is not allowed");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700875 break;
876 case MERR_BADREPNE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300877 nasm_nonfatal("%s prefix is not allowed",
878 (has_prefix(instruction, PPS_REP, P_REPNE) ?
879 "repne" : "repnz"));
H. Peter Anvina77692b2016-09-20 14:04:33 -0700880 break;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700881 case MERR_REGSETSIZE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300882 nasm_nonfatal("invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700883 break;
884 case MERR_REGSET:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300885 nasm_nonfatal("register set not valid for operand");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700886 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700887 default:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300888 nasm_nonfatal("invalid combination of opcode and operands");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700889 break;
890 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700891
892 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400893 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000894 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700895 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000896}
897
H. Peter Anvin29651542018-12-18 19:14:40 -0800898static void debug_set_db_type(insn *instruction)
899{
900 /* Is this really correct? .operands doesn't mean much for Dx */
901 int32_t typeinfo = TYS_ELEMENTS(instruction->operands);
902
903 switch (instruction->opcode) {
904 case I_DB:
905 typeinfo |= TY_BYTE;
906 break;
907 case I_DW:
908 typeinfo |= TY_WORD;
909 break;
910 case I_DD:
911 if (instruction->eops_float)
912 typeinfo |= TY_FLOAT;
913 else
914 typeinfo |= TY_DWORD;
915 break;
916 case I_DQ:
917 /* What about double? */
918 typeinfo |= TY_QWORD;
919 break;
920 case I_DT:
921 /* What about long double? */
922 typeinfo |= TY_TBYTE;
923 break;
924 case I_DO:
925 typeinfo |= TY_OWORD;
926 break;
927 case I_DY:
928 typeinfo |= TY_YWORD;
929 break;
930 case I_DZ:
931 typeinfo |= TY_ZWORD;
932 break;
933 default:
934 panic();
935 }
936
937 dfmt->debug_typevalue(typeinfo);
938}
939
940static void debug_set_type(insn *instruction)
941{
942 int32_t typeinfo;
943
944 if (opcode_is_resb(instruction->opcode)) {
945 typeinfo = TYS_ELEMENTS(instruction->oprs[0].offset);
946
947 switch (instruction->opcode) {
948 case I_RESB:
949 typeinfo |= TY_BYTE;
950 break;
951 case I_RESW:
952 typeinfo |= TY_WORD;
953 break;
954 case I_RESD:
955 typeinfo |= TY_DWORD;
956 break;
957 case I_RESQ:
958 typeinfo |= TY_QWORD;
959 break;
960 case I_REST:
961 typeinfo |= TY_TBYTE;
962 break;
963 case I_RESO:
964 typeinfo |= TY_OWORD;
965 break;
966 case I_RESY:
967 typeinfo |= TY_YWORD;
968 break;
969 case I_RESZ:
970 typeinfo |= TY_ZWORD;
971 break;
972 default:
973 panic();
974 }
975 } else {
976 typeinfo = TY_LABEL;
977 }
978
979 dfmt->debug_typevalue(typeinfo);
980}
981
982
983/* Proecess an EQU directive */
984static void define_equ(insn * instruction)
985{
986 if (!instruction->label) {
987 nasm_nonfatal("EQU not preceded by label");
988 } else if (instruction->operands == 1 &&
989 (instruction->oprs[0].type & IMMEDIATE) &&
990 instruction->oprs[0].wrt == NO_SEG) {
991 define_label(instruction->label,
992 instruction->oprs[0].segment,
993 instruction->oprs[0].offset, false);
994 } else if (instruction->operands == 2
995 && (instruction->oprs[0].type & IMMEDIATE)
996 && (instruction->oprs[0].type & COLON)
997 && instruction->oprs[0].segment == NO_SEG
998 && instruction->oprs[0].wrt == NO_SEG
999 && (instruction->oprs[1].type & IMMEDIATE)
1000 && instruction->oprs[1].segment == NO_SEG
1001 && instruction->oprs[1].wrt == NO_SEG) {
1002 define_label(instruction->label,
1003 instruction->oprs[0].offset | SEG_ABS,
1004 instruction->oprs[1].offset, false);
1005 } else {
1006 nasm_nonfatal("bad syntax for EQU");
1007 }
1008}
1009
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001010
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001011int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001012{
H. Peter Anvin3360d792007-09-11 04:16:57 +00001013 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001014 enum match_result m;
H. Peter Anvin29651542018-12-18 19:14:40 -08001015 int64_t isize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001016
H. Peter Anvin29651542018-12-18 19:14:40 -08001017 if (instruction->opcode == I_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 return 0;
H. Peter Anvin29651542018-12-18 19:14:40 -08001019 } else if (instruction->opcode == I_EQU) {
1020 define_equ(instruction);
1021 return 0;
1022 } else if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 extop *e;
H. Peter Anvin29651542018-12-18 19:14:40 -08001024 int32_t osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001025
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -07001026 wsize = db_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001027 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001028
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +04001029 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001030 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001031
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001033 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001034 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001035 warn_overflow_const(e->offset, wsize);
1036 } else if (e->type == EOT_DB_STRING ||
1037 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001038 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001039
H. Peter Anvine2c80182005-01-15 22:15:51 +00001040 align = (-osize) % wsize;
1041 if (align < 0)
1042 align += wsize;
1043 isize += osize + align;
1044 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001045
H. Peter Anvin29651542018-12-18 19:14:40 -08001046 debug_set_db_type(instruction);
1047 return isize;
1048 } else if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001049 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001050 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001051
H. Peter Anvind81a2352016-09-21 14:03:18 -07001052 len = nasm_file_size_by_path(fname);
1053 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001054 nasm_nonfatal("`incbin': unable to get length of file `%s'",
1055 fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001056 return 0;
1057 }
1058
1059 if (instruction->eops->next) {
1060 if (len <= (off_t)instruction->eops->next->offset) {
1061 len = 0;
1062 } else {
1063 len -= instruction->eops->next->offset;
1064 if (instruction->eops->next->next &&
1065 len > (off_t)instruction->eops->next->next->offset) {
1066 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 }
1068 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001070
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001071 len *= instruction->times;
1072 instruction->times = 1; /* Tell the upper layer to not iterate */
1073
H. Peter Anvind81a2352016-09-21 14:03:18 -07001074 return len;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001075 } else {
H. Peter Anvin29651542018-12-18 19:14:40 -08001076 /* Normal instruction, or RESx */
1077
1078 /* Check to see if we need an address-size prefix */
1079 add_asp(instruction, bits);
1080
1081 m = find_match(&temp, instruction, segment, offset, bits);
1082 if (m != MOK_GOOD)
1083 return -1; /* No match */
1084
1085 isize = calcsize(segment, offset, bits, instruction, temp);
1086 debug_set_type(instruction);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001087 isize = merge_resb(instruction, isize);
H. Peter Anvin29651542018-12-18 19:14:40 -08001088
1089 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001090 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001091}
1092
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001093static void bad_hle_warn(const insn * ins, uint8_t hleok)
1094{
1095 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001096 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001097 static const enum whatwarn warn[2][4] =
1098 {
1099 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
1100 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
1101 };
1102 unsigned int n;
1103
1104 n = (unsigned int)rep_pfx - P_XACQUIRE;
1105 if (n > 1)
1106 return; /* Not XACQUIRE/XRELEASE */
1107
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001108 ww = warn[n][hleok];
1109 if (!is_class(MEMORY, ins->oprs[0].type))
1110 ww = w_inval; /* HLE requires operand 0 to be memory */
1111
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001112 /*!
1113 *!hle [on] invalid HLE prefixes
1114 *! warns about invalid use of the HLE \c{XACQUIRE} or \c{XRELEASE}
1115 *! prefixes.
1116 */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001117 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001118 case w_none:
1119 break;
1120
1121 case w_lock:
1122 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001123 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001124 "%s with this instruction requires lock",
1125 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001126 }
1127 break;
1128
1129 case w_inval:
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001130 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001131 "%s invalid with this instruction",
1132 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001133 break;
1134 }
1135}
1136
H. Peter Anvin507ae032008-10-09 15:37:10 -07001137/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +04001138#define case3(x) case (x): case (x)+1: case (x)+2
1139#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -07001140
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001141static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001142 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001143{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001144 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001145 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001146 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001147 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001148 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001149 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001150 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001151 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001152 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001153 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -07001154 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -07001155 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001156
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001157 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001158 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001159 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001160
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001161 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001162 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001163
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 (void)segment; /* Don't warn that this parameter is unused */
1165 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001166
H. Peter Anvin839eca22007-10-29 23:12:47 -07001167 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001168 c = *codes++;
1169 op1 = (c & 3) + ((opex & 1) << 2);
1170 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1171 opx = &ins->oprs[op1];
1172 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001173
H. Peter Anvin839eca22007-10-29 23:12:47 -07001174 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001175 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 codes += c, length += c;
1177 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001178
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001179 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001180 opex = c;
1181 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001182
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001183 case4(010):
1184 ins->rex |=
1185 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 codes++, length++;
1187 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001188
Jin Kyu Song164d6072013-10-15 19:10:13 -07001189 case4(014):
1190 /* this is an index reg of MIB operand */
1191 mib_index = opx->basereg;
1192 break;
1193
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001194 case4(020):
1195 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001196 length++;
1197 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001198
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001199 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001200 length += 2;
1201 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001202
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001203 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001204 if (opx->type & (BITS16 | BITS32 | BITS64))
1205 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001206 else
1207 length += (bits == 16) ? 2 : 4;
1208 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001209
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001210 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001211 length += 4;
1212 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001213
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001214 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001215 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001217
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001218 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 length++;
1220 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001221
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001222 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001223 length += 8; /* MOV reg64/imm */
1224 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001225
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001226 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001227 length += 2;
1228 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001229
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001230 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001231 if (opx->type & (BITS16 | BITS32 | BITS64))
1232 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 else
1234 length += (bits == 16) ? 2 : 4;
1235 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001236
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001237 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 length += 4;
1239 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001240
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001241 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001242 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001244
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001245 case 0172:
1246 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001247 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001248 length++;
1249 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001250
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001251 case4(0174):
1252 length++;
1253 break;
1254
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001255 case4(0240):
1256 ins->rex |= REX_EV;
1257 ins->vexreg = regval(opx);
1258 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1259 ins->vex_cm = *codes++;
1260 ins->vex_wlp = *codes++;
1261 ins->evex_tuple = (*codes++ - 0300);
1262 break;
1263
1264 case 0250:
1265 ins->rex |= REX_EV;
1266 ins->vexreg = 0;
1267 ins->vex_cm = *codes++;
1268 ins->vex_wlp = *codes++;
1269 ins->evex_tuple = (*codes++ - 0300);
1270 break;
1271
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001272 case4(0254):
1273 length += 4;
1274 break;
1275
1276 case4(0260):
1277 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001278 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001279 ins->vex_cm = *codes++;
1280 ins->vex_wlp = *codes++;
1281 break;
1282
1283 case 0270:
1284 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001285 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001286 ins->vex_cm = *codes++;
1287 ins->vex_wlp = *codes++;
1288 break;
1289
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001290 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001291 hleok = c & 3;
1292 break;
1293
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001294 case4(0274):
1295 length++;
1296 break;
1297
1298 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001300
H. Peter Anvine2c80182005-01-15 22:15:51 +00001301 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001302 if (bits == 64)
1303 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001304 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001306
H. Peter Anvine2c80182005-01-15 22:15:51 +00001307 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001308 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001309 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001310
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001312 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001313
Keith Kaniosb7a89542007-04-12 02:40:54 +00001314 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001315 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1316 has_prefix(ins, PPS_ASIZE, P_A32))
1317 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001319
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001320 case4(0314):
1321 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001322
H. Peter Anvine2c80182005-01-15 22:15:51 +00001323 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001324 {
1325 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1326 if (pfx == P_O16)
1327 break;
1328 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001329 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001330 else
1331 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001332 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001333 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001334
H. Peter Anvine2c80182005-01-15 22:15:51 +00001335 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001336 {
1337 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1338 if (pfx == P_O32)
1339 break;
1340 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001341 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001342 else
1343 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001344 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001345 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001346
H. Peter Anvine2c80182005-01-15 22:15:51 +00001347 case 0322:
1348 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001349
Keith Kaniosb7a89542007-04-12 02:40:54 +00001350 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001351 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001352 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001353
Keith Kaniosb7a89542007-04-12 02:40:54 +00001354 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001355 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001356 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001357
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001358 case 0325:
1359 ins->rex |= REX_NH;
1360 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001361
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001362 case 0326:
1363 break;
1364
H. Peter Anvine2c80182005-01-15 22:15:51 +00001365 case 0330:
1366 codes++, length++;
1367 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001368
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001370 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001371
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001372 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 case 0333:
1374 length++;
1375 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001376
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001377 case 0334:
1378 ins->rex |= REX_L;
1379 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001380
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001381 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001382 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001383
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001384 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001385 if (!ins->prefixes[PPS_REP])
1386 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001387 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001388
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001389 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001390 if (!ins->prefixes[PPS_REP])
1391 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001392 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001393
H. Peter Anvine2c80182005-01-15 22:15:51 +00001394 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001395 if (!absolute_op(&ins->oprs[0]))
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001396 nasm_nonfatal("attempt to reserve non-constant"
1397 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001398 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001399 nasm_warn(WARN_OTHER, "forward reference in RESx "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001400 "can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001401 else
H. Peter Anvin5358b982018-12-18 18:06:26 -08001402 length += ins->oprs[0].offset * resb_bytes(ins->opcode);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001403 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001404
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001405 case 0341:
1406 if (!ins->prefixes[PPS_WAIT])
1407 ins->prefixes[PPS_WAIT] = P_WAIT;
1408 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001409
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001410 case 0360:
1411 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001412
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001413 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001414 length++;
1415 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001416
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001417 case 0364:
1418 case 0365:
1419 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001420
Keith Kanios48af1772007-08-17 07:37:52 +00001421 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001422 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001423 length++;
1424 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001425
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001426 case 0370:
1427 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001428 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001429
H. Peter Anvine2c80182005-01-15 22:15:51 +00001430 case 0373:
1431 length++;
1432 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001433
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001434 case 0374:
1435 eat = EA_XMMVSIB;
1436 break;
1437
1438 case 0375:
1439 eat = EA_YMMVSIB;
1440 break;
1441
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001442 case 0376:
1443 eat = EA_ZMMVSIB;
1444 break;
1445
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001446 case4(0100):
1447 case4(0110):
1448 case4(0120):
1449 case4(0130):
1450 case4(0200):
1451 case4(0204):
1452 case4(0210):
1453 case4(0214):
1454 case4(0220):
1455 case4(0224):
1456 case4(0230):
1457 case4(0234):
1458 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001459 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001460 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001461 opflags_t rflags;
1462 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001463 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001464
Keith Kaniosb7a89542007-04-12 02:40:54 +00001465 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001466
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001467 if (c <= 0177) {
1468 /* pick rfield from operand b (opx) */
1469 rflags = regflag(opx);
1470 rfield = nasm_regvals[opx->basereg];
1471 } else {
1472 rflags = 0;
1473 rfield = c & 7;
1474 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001475
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001476 /* EVEX.b1 : evex_brerop contains the operand position */
1477 op_er_sae = (ins->evex_brerop >= 0 ?
1478 &ins->oprs[ins->evex_brerop] : NULL);
1479
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001480 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1481 /* set EVEX.b */
1482 ins->evex_p[2] |= EVEX_P2B;
1483 if (op_er_sae->decoflags & ER) {
1484 /* set EVEX.RC (rounding control) */
1485 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1486 & EVEX_P2RC;
1487 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001488 } else {
1489 /* set EVEX.L'L (vector length) */
1490 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001491 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001492 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001493 /* set EVEX.b */
1494 ins->evex_p[2] |= EVEX_P2B;
1495 }
1496 }
1497
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001498 if (itemp_has(temp, IF_MIB)) {
1499 opy->eaflags |= EAF_MIB;
1500 /*
1501 * if a separate form of MIB (ICC style) is used,
1502 * the index reg info is merged into mem operand
1503 */
1504 if (mib_index != R_none) {
1505 opy->indexreg = mib_index;
1506 opy->scale = 1;
1507 opy->hintbase = mib_index;
1508 opy->hinttype = EAH_NOTBASE;
1509 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001510 }
1511
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001512 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001513 rfield, rflags, ins, &errmsg) != eat) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001514 nasm_nonfatal("%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001516 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001517 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001518 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001519 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001520 }
1521 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001522
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001523 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001524 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001525 ": instruction code \\%o (0x%02X) given", c, c);
1526 break;
1527 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001528 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001529
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001530 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001531
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001532 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001533 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001534 nasm_nonfatal("instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001535 return -1;
1536 }
1537 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001538 }
1539
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001540 switch (ins->prefixes[PPS_VEX]) {
1541 case P_EVEX:
1542 if (!(ins->rex & REX_EV))
1543 return -1;
1544 break;
1545 case P_VEX3:
1546 case P_VEX2:
1547 if (!(ins->rex & REX_V))
1548 return -1;
1549 break;
1550 default:
1551 break;
1552 }
1553
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001554 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001555 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001556
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001557 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001558 nasm_nonfatal("cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001559 return -1;
1560 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001561 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001562 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001563 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001564 ins->rex &= ~REX_W;
1565 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001566 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001567 ins->rex |= REX_W;
1568 bad32 &= ~REX_W;
1569 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001570 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001571 /* Follow REX_W */
1572 break;
1573 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001574
H. Peter Anvinfc561202011-07-07 16:58:22 -07001575 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001576 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001577 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001578 } else if (!(ins->rex & REX_EV) &&
1579 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001580 nasm_nonfatal("invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001581 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001582 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001583 if (ins->rex & REX_EV)
1584 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001585 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1586 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001587 length += 3;
1588 else
1589 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001590 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001591 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001592 nasm_nonfatal("cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 return -1;
1594 } else if (bits == 64) {
1595 length++;
1596 } else if ((ins->rex & REX_L) &&
1597 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001598 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001599 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001600 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001601 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001602 length++;
1603 } else {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001604 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001605 return -1;
1606 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001607 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001608
1609 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001610 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001611 /*!
H. Peter Anvin (Intel)be99ebd2018-12-13 22:12:37 -08001612 *!lock [on] LOCK prefix on unlockable instructions
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001613 *! warns about \c{LOCK} prefixes on unlockable instructions.
1614 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001615 nasm_warn(WARN_LOCK | ERR_PASS2 , "instruction is not lockable");
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001616 }
1617
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001618 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001619
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001620 /*
1621 * when BND prefix is set by DEFAULT directive,
1622 * BND prefix is added to every appropriate instruction line
1623 * unless it is overridden by NOBND prefix.
1624 */
1625 if (globalbnd &&
1626 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1627 ins->prefixes[PPS_REP] = P_BND;
1628
H. Peter Anvina77692b2016-09-20 14:04:33 -07001629 /*
1630 * Add length of legacy prefixes
1631 */
1632 length += emit_prefix(NULL, bits, ins);
1633
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001634 return length;
1635}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001636
H. Peter Anvina77692b2016-09-20 14:04:33 -07001637static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001638{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001639 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001640 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001641 !(ins->rex & (REX_V | REX_EV)) &&
1642 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001643 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1644 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001645 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001646 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001647 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001648}
1649
H. Peter Anvina77692b2016-09-20 14:04:33 -07001650static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1651{
1652 int bytes = 0;
1653 int j;
1654
1655 for (j = 0; j < MAXPREFIX; j++) {
1656 uint8_t c = 0;
1657 switch (ins->prefixes[j]) {
1658 case P_WAIT:
1659 c = 0x9B;
1660 break;
1661 case P_LOCK:
1662 c = 0xF0;
1663 break;
1664 case P_REPNE:
1665 case P_REPNZ:
1666 case P_XACQUIRE:
1667 case P_BND:
1668 c = 0xF2;
1669 break;
1670 case P_REPE:
1671 case P_REPZ:
1672 case P_REP:
1673 case P_XRELEASE:
1674 c = 0xF3;
1675 break;
1676 case R_CS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001677 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001678 nasm_warn(WARN_OTHER|ERR_PASS2, "cs segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001679 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001680 c = 0x2E;
1681 break;
1682 case R_DS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001683 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001684 nasm_warn(WARN_OTHER|ERR_PASS2, "ds segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001685 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001686 c = 0x3E;
1687 break;
1688 case R_ES:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001689 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001690 nasm_warn(WARN_OTHER|ERR_PASS2, "es segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001691 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001692 c = 0x26;
1693 break;
1694 case R_FS:
1695 c = 0x64;
1696 break;
1697 case R_GS:
1698 c = 0x65;
1699 break;
1700 case R_SS:
1701 if (bits == 64) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001702 nasm_warn(WARN_OTHER|ERR_PASS2, "ss segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001703 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001704 }
1705 c = 0x36;
1706 break;
1707 case R_SEGR6:
1708 case R_SEGR7:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001709 nasm_nonfatal("segr6 and segr7 cannot be used as prefixes");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001710 break;
1711 case P_A16:
1712 if (bits == 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001713 nasm_nonfatal("16-bit addressing is not supported "
1714 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001715 } else if (bits != 16)
1716 c = 0x67;
1717 break;
1718 case P_A32:
1719 if (bits != 32)
1720 c = 0x67;
1721 break;
1722 case P_A64:
1723 if (bits != 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001724 nasm_nonfatal("64-bit addressing is only supported "
1725 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001726 }
1727 break;
1728 case P_ASP:
1729 c = 0x67;
1730 break;
1731 case P_O16:
1732 if (bits != 16)
1733 c = 0x66;
1734 break;
1735 case P_O32:
1736 if (bits == 16)
1737 c = 0x66;
1738 break;
1739 case P_O64:
1740 /* REX.W */
1741 break;
1742 case P_OSP:
1743 c = 0x66;
1744 break;
1745 case P_EVEX:
1746 case P_VEX3:
1747 case P_VEX2:
1748 case P_NOBND:
1749 case P_none:
1750 break;
1751 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001752 nasm_panic("invalid instruction prefix");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001753 }
1754 if (c) {
1755 if (data)
1756 out_rawbyte(data, c);
1757 bytes++;
1758 }
1759 }
1760 return bytes;
1761}
1762
1763static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001764{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001765 uint8_t c;
1766 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001767 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001768 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001769 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001770 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001771 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001772 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001773 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001774 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001775 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001776
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001777 ins->rex_done = false;
1778
H. Peter Anvina77692b2016-09-20 14:04:33 -07001779 emit_prefix(data, bits, ins);
1780
H. Peter Anvin839eca22007-10-29 23:12:47 -07001781 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001782 c = *codes++;
1783 op1 = (c & 3) + ((opex & 1) << 2);
1784 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1785 opx = &ins->oprs[op1];
1786 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001787
H. Peter Anvina77692b2016-09-20 14:04:33 -07001788
H. Peter Anvin839eca22007-10-29 23:12:47 -07001789 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 case 01:
1791 case 02:
1792 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001793 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001794 emit_rex(data, ins);
1795 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001798
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001799 case 05:
1800 case 06:
1801 case 07:
1802 opex = c;
1803 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001804
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001805 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001806 emit_rex(data, ins);
1807 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001809
Jin Kyu Song164d6072013-10-15 19:10:13 -07001810 case4(014):
1811 break;
1812
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001813 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001814 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001816
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001817 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001818 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001820
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001821 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001822 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001824
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001825 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001826 if (opx->type & (BITS16 | BITS32))
1827 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 else
1829 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001830 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001832
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001833 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001834 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001836
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001837 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001838 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001839 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001841
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001842 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001843 if (opx->segment == data->segment) {
1844 int64_t delta = opx->offset - data->offset
1845 - (data->inslen - data->insoffs);
1846 if (delta > 127 || delta < -128)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001847 nasm_nonfatal("short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001848 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001849 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001851
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001852 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001853 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001854 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001855
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001856 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001857 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001859
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001860 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001861 if (opx->type & (BITS16 | BITS32 | BITS64))
1862 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001863 else
1864 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001865
1866 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001868
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001869 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001870 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001872
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001873 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001874 if (opx->segment == NO_SEG)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001875 nasm_nonfatal("value referenced by FAR is not relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001876 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001878
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001879 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001880 {
1881 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1882 const struct operand *opy;
1883
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001884 c = *codes++;
1885 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001886 opy = &ins->oprs[c & 7];
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001887 if (!absolute_op(opy))
1888 nasm_nonfatal("non-absolute expression not permitted "
1889 "as argument %d", c & 7);
1890 else if (opy->offset & ~mask)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001891 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001892 "is4 argument exceeds bounds");
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001893 c = opy->offset & mask;
1894 goto emit_is4;
1895 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001896
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001897 case 0173:
1898 c = *codes++;
1899 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001900 c &= 15;
1901 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001902
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001903 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001904 c = 0;
1905 emit_is4:
1906 r = nasm_regvals[opx->basereg];
1907 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001908 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001909
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001910 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001911 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001912 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001913 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001914 "signed dword immediate exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001915 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001916 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001917 break;
1918
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001919 case4(0240):
1920 case 0250:
1921 codes += 3;
1922 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1923 EVEX_P2Z | EVEX_P2AAA, 2);
1924 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1925 bytes[0] = 0x62;
1926 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001927 bytes[1] = ((((ins->rex & 7) << 5) |
1928 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001929 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001930 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1931 ((~ins->vexreg & 15) << 3) |
1932 (1 << 2) | (ins->vex_wlp & 3);
1933 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001934 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001935 break;
1936
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001937 case4(0260):
1938 case 0270:
1939 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001940 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1941 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001942 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1943 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1944 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001945 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001946 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001947 } else {
1948 bytes[0] = 0xc5;
1949 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001950 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001951 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001952 }
1953 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001954
H. Peter Anvine014f352012-02-25 22:35:19 -08001955 case 0271:
1956 case 0272:
1957 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001958 break;
1959
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001960 case4(0274):
1961 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001962 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001963 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001964
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001965 if (absolute_op(opx)) {
1966 if (ins->rex & REX_W)
1967 s = 64;
1968 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1969 s = 16;
1970 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1971 s = 32;
1972 else
1973 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001974
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001975 um = (uint64_t)2 << (s-1);
1976 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001977
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001978 if (uv > 127 && uv < (uint64_t)-128 &&
1979 (uv < um-128 || uv > um-1)) {
1980 /* If this wasn't explicitly byte-sized, warn as though we
1981 * had fallen through to the imm16/32/64 case.
1982 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001983 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001984 "%s value exceeds bounds",
1985 (opx->type & BITS8) ? "signed byte" :
1986 s == 16 ? "word" :
1987 s == 32 ? "dword" :
1988 "signed dword");
1989 }
1990
1991 /* Output as a raw byte to avoid byte overflow check */
1992 out_rawbyte(data, (uint8_t)uv);
1993 } else {
1994 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001995 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001996 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001997 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001998
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001999 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002001
H. Peter Anvine2c80182005-01-15 22:15:51 +00002002 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002003 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
2004 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002006
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002008 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
2009 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002010 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002011
H. Peter Anvine2c80182005-01-15 22:15:51 +00002012 case 0312:
2013 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002014
Keith Kaniosb7a89542007-04-12 02:40:54 +00002015 case 0313:
2016 ins->rex = 0;
2017 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002018
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002019 case4(0314):
2020 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08002021
H. Peter Anvine2c80182005-01-15 22:15:51 +00002022 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002023 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002024 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002025
H. Peter Anvine2c80182005-01-15 22:15:51 +00002026 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07002027 case 0323:
2028 break;
2029
Keith Kaniosb7a89542007-04-12 02:40:54 +00002030 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002031 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002032 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07002033
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002034 case 0325:
2035 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07002036
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04002037 case 0326:
2038 break;
2039
H. Peter Anvine2c80182005-01-15 22:15:51 +00002040 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002041 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002042 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002043
H. Peter Anvine2c80182005-01-15 22:15:51 +00002044 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002045 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002046
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002047 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002048 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002049 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002050 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002051
Keith Kanios48af1772007-08-17 07:37:52 +00002052 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002053 if (ins->rex & REX_R)
2054 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00002055 ins->rex &= ~(REX_L|REX_R);
2056 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00002057
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002058 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002059 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002060
H. Peter Anvin962e3052008-08-28 17:47:16 -07002061 case 0336:
2062 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002063 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07002064
H. Peter Anvine2c80182005-01-15 22:15:51 +00002065 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002066 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002067 nasm_panic("non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07002068
H. Peter Anvin5358b982018-12-18 18:06:26 -08002069 out_reserve(data, ins->oprs[0].offset * resb_bytes(ins->opcode));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002070 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002071
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002072 case 0341:
2073 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08002074
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002075 case 0360:
2076 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002077
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002078 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002079 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002080 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002081
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002082 case 0364:
2083 case 0365:
2084 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002085
Keith Kanios48af1772007-08-17 07:37:52 +00002086 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002087 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002088 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00002089 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002090
Jin Kyu Song03041092013-10-15 19:38:51 -07002091 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002092 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002093
H. Peter Anvine2c80182005-01-15 22:15:51 +00002094 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002095 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002096 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002097
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002098 case 0374:
2099 eat = EA_XMMVSIB;
2100 break;
2101
2102 case 0375:
2103 eat = EA_YMMVSIB;
2104 break;
2105
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002106 case 0376:
2107 eat = EA_ZMMVSIB;
2108 break;
2109
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002110 case4(0100):
2111 case4(0110):
2112 case4(0120):
2113 case4(0130):
2114 case4(0200):
2115 case4(0204):
2116 case4(0210):
2117 case4(0214):
2118 case4(0220):
2119 case4(0224):
2120 case4(0230):
2121 case4(0234):
2122 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002123 ea ea_data;
2124 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002125 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002126 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002127 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07002128
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002129 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002130 /* pick rfield from operand b (opx) */
2131 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07002132 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002133 } else {
2134 /* rfield is constant */
2135 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002136 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002137 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002138
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002139 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002140 rfield, rflags, ins, &errmsg) != eat)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002141 nasm_nonfatal("%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07002142
H. Peter Anvine2c80182005-01-15 22:15:51 +00002143 p = bytes;
2144 *p++ = ea_data.modrm;
2145 if (ea_data.sib_present)
2146 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07002147 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002148
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002149 /*
2150 * Make sure the address gets the right offset in case
2151 * the line breaks in the .lst file (BR 1197827)
2152 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002153
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002154 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002155 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07002156 if (ea_data.disp8) {
2157 out_rawbyte(data, ea_data.disp8);
2158 } else if (ea_data.rip) {
2159 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002160 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002161 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002162
H. Peter Anvina77692b2016-09-20 14:04:33 -07002163 if (overflow_general(opy->offset, asize) ||
2164 signed_bits(opy->offset, ins->addr_size) !=
2165 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08002166 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002167
H. Peter Anvina77692b2016-09-20 14:04:33 -07002168 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07002169 (asize > ea_data.bytes)
2170 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002171 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002172 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002173 }
2174 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07002175
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002176 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07002177 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002178 ": instruction code \\%o (0x%02X) given", c, c);
2179 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002180 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07002181 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002182}
2183
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002184static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002185{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002186 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002187 nasm_panic("invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002188 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002189}
2190
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00002191static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002192{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002193 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002194 nasm_panic("invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002195 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002196}
2197
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002198static int op_rexflags(const operand * o, int mask)
2199{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002200 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002201 int val;
2202
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002203 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002204 nasm_panic("invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002205
H. Peter Anvina4835d42008-05-20 14:21:29 -07002206 flags = nasm_reg_flags[o->basereg];
2207 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002208
2209 return rexflags(val, flags, mask);
2210}
2211
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002212static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002213{
2214 int rex = 0;
2215
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002216 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002217 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002218 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002219 rex |= REX_W;
2220 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2221 rex |= REX_H;
2222 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2223 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002224
2225 return rex & mask;
2226}
2227
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002228static int evexflags(int val, decoflags_t deco,
2229 int mask, uint8_t byte)
2230{
2231 int evex = 0;
2232
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002233 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002234 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002235 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002236 evex |= (EVEX_P0RP | EVEX_P0X);
2237 break;
2238 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002239 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002240 evex |= EVEX_P2VP;
2241 if (deco & Z)
2242 evex |= EVEX_P2Z;
2243 if (deco & OPMASK_MASK)
2244 evex |= deco & EVEX_P2AAA;
2245 break;
2246 }
2247 return evex & mask;
2248}
2249
2250static int op_evexflags(const operand * o, int mask, uint8_t byte)
2251{
2252 int val;
2253
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002254 val = nasm_regvals[o->basereg];
2255
2256 return evexflags(val, o->decoflags, mask, byte);
2257}
2258
H. Peter Anvin23595f52009-07-25 17:44:25 -07002259static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002260 insn *instruction,
2261 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002262{
2263 const struct itemplate *temp;
2264 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002265 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002266 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002267 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002268 int i;
2269
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002270 /* broadcasting uses a different data element size */
2271 for (i = 0; i < instruction->operands; i++)
2272 if (i == broadcast)
2273 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2274 else
2275 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002276
2277 merr = MERR_INVALOP;
2278
2279 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002280 temp->opcode != I_none; temp++) {
2281 m = matches(temp, instruction, bits);
2282 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002283 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002284 m = MOK_GOOD;
2285 else
2286 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002287 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002288 /*
2289 * Missing operand size and a candidate for fuzzy matching...
2290 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002291 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002292 if (i == broadcast)
2293 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2294 else
2295 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002296 opsizemissing = true;
2297 }
2298 if (m > merr)
2299 merr = m;
2300 if (merr == MOK_GOOD)
2301 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002302 }
2303
2304 /* No match, but see if we can get a fuzzy operand size match... */
2305 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002306 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002307
2308 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002309 /*
2310 * We ignore extrinsic operand sizes on registers, so we should
2311 * never try to fuzzy-match on them. This also resolves the case
2312 * when we have e.g. "xmmrm128" in two different positions.
2313 */
2314 if (is_class(REGISTER, instruction->oprs[i].type))
2315 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002316
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002317 /* This tests if xsizeflags[i] has more than one bit set */
2318 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2319 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002320
Jin Kyu Song7903c072013-10-30 03:00:12 -07002321 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002322 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002323 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2324 BITS32 : BITS64);
2325 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002326 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002327 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002328 }
2329
2330 /* Try matching again... */
2331 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002332 temp->opcode != I_none; temp++) {
2333 m = matches(temp, instruction, bits);
2334 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002335 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002336 m = MOK_GOOD;
2337 else
2338 m = MERR_INVALOP;
2339 }
2340 if (m > merr)
2341 merr = m;
2342 if (merr == MOK_GOOD)
2343 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002344 }
2345
H. Peter Anvina81655b2009-07-25 18:15:28 -07002346done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002347 *tempp = temp;
2348 return merr;
2349}
2350
Mark Charneydcaef4b2014-10-09 13:45:17 -04002351static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2352{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002353 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002354 uint8_t brcast_num;
2355
Mark Charneydcaef4b2014-10-09 13:45:17 -04002356 if (brsize > BITS64)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002357 nasm_fatal("size of broadcasting element is greater than 64 bits");
Mark Charneydcaef4b2014-10-09 13:45:17 -04002358
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002359 /*
2360 * The shift term is to take care of the extra BITS80 inserted
2361 * between BITS64 and BITS128.
2362 */
2363 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2364 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002365
2366 return brcast_num;
2367}
2368
H. Peter Anvin65289e82009-07-25 17:25:11 -07002369static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002370 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002371{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002372 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002373 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002374 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002375
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002376 /*
2377 * Check the opcode
2378 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002379 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002380 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002381
2382 /*
2383 * Count the operands
2384 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002385 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002386 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002387
2388 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002389 * Is it legal?
2390 */
Chang S. Baea5786342018-08-15 23:22:21 +03002391 if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002392 return MERR_INVALOP;
2393
2394 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002395 * {evex} available?
2396 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002397 switch (instruction->prefixes[PPS_VEX]) {
2398 case P_EVEX:
2399 if (!itemp_has(itemp, IF_EVEX))
2400 return MERR_ENCMISMATCH;
2401 break;
2402 case P_VEX3:
2403 case P_VEX2:
2404 if (!itemp_has(itemp, IF_VEX))
2405 return MERR_ENCMISMATCH;
2406 break;
2407 default:
2408 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002409 }
2410
2411 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002412 * Check that no spurious colons or TOs are present
2413 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002414 for (i = 0; i < itemp->operands; i++)
2415 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002416 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002417
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002418 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002419 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002420 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002421 switch (itemp_smask(itemp)) {
2422 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002423 asize = BITS8;
2424 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002425 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002426 asize = BITS16;
2427 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002428 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002429 asize = BITS32;
2430 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002431 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002432 asize = BITS64;
2433 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002434 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002435 asize = BITS128;
2436 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002437 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002438 asize = BITS256;
2439 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002440 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002441 asize = BITS512;
2442 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002443 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002444 switch (bits) {
2445 case 16:
2446 asize = BITS16;
2447 break;
2448 case 32:
2449 asize = BITS32;
2450 break;
2451 case 64:
2452 asize = BITS64;
2453 break;
2454 default:
2455 asize = 0;
2456 break;
2457 }
2458 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002459 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002460 asize = 0;
2461 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002462 }
2463
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002464 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002465 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002466 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002467 memset(size, 0, sizeof size);
2468 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002469 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002470 /* S- flags apply to all operands */
2471 for (i = 0; i < MAX_OPERANDS; i++)
2472 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002473 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002474
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002475 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002476 * Check that the operand flags all match up,
2477 * it's a bit tricky so lets be verbose:
2478 *
2479 * 1) Find out the size of operand. If instruction
2480 * doesn't have one specified -- we're trying to
2481 * guess it either from template (IF_S* flag) or
2482 * from code bits.
2483 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002484 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002485 * template has an operand size specified AND this size differ
2486 * from which instruction has (perhaps we got it from code bits)
2487 * we are:
2488 * a) Check that only size of instruction and operand is differ
2489 * other characteristics do match
2490 * b) Perhaps it's a register specified in instruction so
2491 * for such a case we just mark that operand as "size
2492 * missing" and this will turn on fuzzy operand size
2493 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002494 */
2495 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002496 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002497 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002498 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002499 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002500 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002501 opflags_t template_opsize, insn_opsize;
2502
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002503 if (!(type & SIZE_MASK))
2504 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002505
Jin Kyu Song7903c072013-10-30 03:00:12 -07002506 insn_opsize = type & SIZE_MASK;
2507 if (!is_broadcast) {
2508 template_opsize = itemp->opd[i] & SIZE_MASK;
2509 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002510 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2511
2512 if (~ideco & BRDCAST_MASK)
2513 return MERR_BRNOTHERE;
2514
Jin Kyu Song7903c072013-10-30 03:00:12 -07002515 /*
2516 * when broadcasting, the element size depends on
2517 * the instruction type. decorator flag should match.
2518 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002519 if (deco_brsize) {
2520 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002521 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002522 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002523 } else {
2524 template_opsize = 0;
2525 }
2526 }
2527
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002528 if (~ideco & deco & OPMASK_MASK)
2529 return MERR_MASKNOTHERE;
2530
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002531 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2532 return MERR_DECONOTHERE;
2533
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002534 if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002535 return MERR_INVALOP;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002536
2537 if (~itemp->opd[i] & type & REGSET_MASK)
2538 return (itemp->opd[i] & REGSET_MASK)
2539 ? MERR_REGSETSIZE : MERR_REGSET;
2540
2541 if (template_opsize) {
Jin Kyu Song7903c072013-10-30 03:00:12 -07002542 if (template_opsize != insn_opsize) {
2543 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002544 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002545 } else if (!is_class(REGISTER, type)) {
2546 /*
2547 * Note: we don't honor extrinsic operand sizes for registers,
2548 * so "missing operand size" for a register should be
2549 * considered a wildcard match rather than an error.
2550 */
2551 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002552 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002553 } else if (is_broadcast &&
2554 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002555 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002556 /*
2557 * broadcasting opsize matches but the number of repeated memory
2558 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002559 * if 64b double precision float is broadcasted to ymm (256b),
2560 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002561 */
2562 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002563 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002564 }
2565 }
2566
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002567 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002568 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002569
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002570 /*
2571 * Check operand sizes
2572 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002573 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2574 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002575 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002576 asize = itemp->opd[i] & SIZE_MASK;
2577 if (asize) {
2578 for (i = 0; i < oprs; i++)
2579 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002580 break;
2581 }
2582 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002583 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002584 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002585 }
2586
Keith Kaniosb7a89542007-04-12 02:40:54 +00002587 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002588 if (!(itemp->opd[i] & SIZE_MASK) &&
2589 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002590 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002591 }
2592
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002593 /*
2594 * Check template is okay at the set cpu level
2595 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002596 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002597 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002598
Keith Kaniosb7a89542007-04-12 02:40:54 +00002599 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002600 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002601 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002602 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002603 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002604
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002605 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002606 * If we have a HLE prefix, look for the NOHLE flag
2607 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002608 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002609 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2610 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2611 return MERR_BADHLE;
2612
2613 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002614 * Check if special handling needed for Jumps
2615 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002616 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002617 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002618
Jin Kyu Song03041092013-10-15 19:38:51 -07002619 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002620 * Check if BND prefix is allowed.
2621 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002622 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002623 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002624 (has_prefix(instruction, PPS_REP, P_BND) ||
2625 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002626 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002627 else if (itemp_has(itemp, IF_BND) &&
2628 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2629 has_prefix(instruction, PPS_REP, P_REPNZ)))
2630 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002631
H. Peter Anvin60926242009-07-26 16:25:38 -07002632 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002633}
2634
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002635/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002636 * Check if ModR/M.mod should/can be 01.
2637 * - EAF_BYTEOFFS is set
2638 * - offset can fit in a byte when EVEX is not used
2639 * - offset can be compressed when EVEX is used
2640 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002641#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2642 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2643 is_disp8n(input, ins, &output->disp8) : \
2644 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2645 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002646
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002647static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002648 int rfield, opflags_t rflags, insn *ins,
2649 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002650{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002651 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002652 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002653 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002654
H. Peter Anvin8f622462017-04-02 19:02:29 -07002655 *errmsg = "invalid effective address"; /* Default error message */
2656
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002657 output->type = EA_SCALAR;
2658 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002659 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002660
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002661 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002662 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002663 /* EVEX.R' flag for the REG operand */
2664 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002665
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002666 if (is_class(REGISTER, input->type)) {
2667 /*
2668 * It's a direct register.
2669 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002670 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002671 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002672
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002673 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002674 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002675
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002676 /* broadcasting is not available with a direct register operand. */
2677 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002678 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002679 goto err;
2680 }
2681
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002682 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002683 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002684 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002685 output->bytes = 0; /* no offset necessary either */
2686 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2687 } else {
2688 /*
2689 * It's a memory reference.
2690 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002691
2692 /* Embedded rounding or SAE is not available with a mem ref operand. */
2693 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002694 *errmsg = "embedded rounding is available only with "
2695 "register-register operations";
2696 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002697 }
2698
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002699 if (input->basereg == -1 &&
2700 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002701 /*
2702 * It's a pure offset.
2703 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002704 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002705 if (input->segment == NO_SEG ||
2706 (input->opflags & OPFLAG_RELATIVE)) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08002707 nasm_warn(WARN_OTHER|ERR_PASS2, "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002708 input->type &= ~IP_REL;
2709 input->type |= MEMORY;
2710 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002711 }
2712
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002713 if (bits == 64 &&
2714 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002715 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002716 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002717 }
2718
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002719 if (eaflags & EAF_BYTEOFFS ||
2720 (eaflags & EAF_WORDOFFS &&
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002721 input->disp_size != (addrbits != 16 ? 32 : 16)))
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002722 nasm_warn(WARN_OTHER, "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002723
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002724 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002725 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002726 output->sib = GEN_SIB(0, 4, 5);
2727 output->bytes = 4;
2728 output->modrm = GEN_MODRM(0, rfield, 4);
2729 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002730 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002731 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002732 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002733 output->modrm = GEN_MODRM(0, rfield,
2734 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002735 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002736 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002737 } else {
2738 /*
2739 * It's an indirection.
2740 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002742 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002743 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002744 int t, it, bt; /* register numbers */
2745 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002746
H. Peter Anvine2c80182005-01-15 22:15:51 +00002747 if (s == 0)
2748 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002749
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002750 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002751 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002752 ix = nasm_reg_flags[i];
2753 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002754 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002755 ix = 0;
2756 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002757
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002758 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002759 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002760 bx = nasm_reg_flags[b];
2761 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002762 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002763 bx = 0;
2764 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002765
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002766 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002767 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002768 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002769 int32_t o = input->offset;
2770 int mod, scale, index, base;
2771
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002772 /*
2773 * For a vector SIB, one has to be a vector and the other,
2774 * if present, a GPR. The vector must be the index operand.
2775 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002776 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002777 if (s == 0)
2778 s = 1;
2779 else if (s != 1)
2780 goto err;
2781
2782 t = bt, bt = it, it = t;
2783 x = bx, bx = ix, ix = x;
2784 }
2785
2786 if (bt != -1) {
2787 if (REG_GPR & ~bx)
2788 goto err;
2789 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2790 sok &= bx;
2791 else
2792 goto err;
2793 }
2794
2795 /*
2796 * While we're here, ensure the user didn't specify
2797 * WORD or QWORD
2798 */
2799 if (input->disp_size == 16 || input->disp_size == 64)
2800 goto err;
2801
2802 if (addrbits == 16 ||
2803 (addrbits == 32 && !(sok & BITS32)) ||
2804 (addrbits == 64 && !(sok & BITS64)))
2805 goto err;
2806
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002807 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2808 : ((ix & YMMREG & ~REG_EA)
2809 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002810
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002811 output->rex |= rexflags(it, ix, REX_X);
2812 output->rex |= rexflags(bt, bx, REX_B);
2813 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002814
2815 index = it & 7; /* it is known to be != -1 */
2816
2817 switch (s) {
2818 case 1:
2819 scale = 0;
2820 break;
2821 case 2:
2822 scale = 1;
2823 break;
2824 case 4:
2825 scale = 2;
2826 break;
2827 case 8:
2828 scale = 3;
2829 break;
2830 default: /* then what the smeg is it? */
2831 goto err; /* panic */
2832 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002833
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002834 if (bt == -1) {
2835 base = 5;
2836 mod = 0;
2837 } else {
2838 base = (bt & 7);
2839 if (base != REG_NUM_EBP && o == 0 &&
2840 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002841 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002842 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002843 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002844 mod = 1;
2845 else
2846 mod = 2;
2847 }
2848
2849 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002850 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2851 output->modrm = GEN_MODRM(mod, rfield, 4);
2852 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002853 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002854 /*
2855 * it must be a 32/64-bit memory reference. Firstly we have
2856 * to check that all registers involved are type E/Rxx.
2857 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002858 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002859 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002860
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002861 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002862 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2863 sok &= ix;
2864 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002865 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002866 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002867
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002868 if (bt != -1) {
2869 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002870 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002871 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002872 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002873 sok &= bx;
2874 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002875
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002876 /*
2877 * While we're here, ensure the user didn't specify
2878 * WORD or QWORD
2879 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002880 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002881 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002882
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002883 if (addrbits == 16 ||
2884 (addrbits == 32 && !(sok & BITS32)) ||
2885 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002886 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002887
Keith Kaniosb7a89542007-04-12 02:40:54 +00002888 /* now reorganize base/index */
2889 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002890 ((hb == b && ht == EAH_NOTBASE) ||
2891 (hb == i && ht == EAH_MAKEBASE))) {
2892 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002893 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002894 x = bx, bx = ix, ix = x;
2895 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002896
Jin Kyu Song164d6072013-10-15 19:10:13 -07002897 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002898 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002899 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002900 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002901 if (eaflags & EAF_MIB) {
2902 /* only for mib operands */
2903 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2904 /*
2905 * make a single reg index [reg*1].
2906 * gas uses this form for an explicit index register.
2907 */
2908 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2909 }
2910 if ((ht == EAH_SUMMED) && bt == -1) {
2911 /* separate once summed index into [base, index] */
2912 bt = it, bx = ix, s--;
2913 }
2914 } else {
2915 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002916 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002917 s == 3 || s == 5 || s == 9) && bt == -1) {
2918 /* convert 3*EAX to EAX+2*EAX */
2919 bt = it, bx = ix, s--;
2920 }
2921 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002922 (eaflags & EAF_TIMESTWO) &&
2923 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002924 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002925 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002926 * to sib format with 0x0 displacement - [EAX*1+0].
2927 */
2928 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2929 }
2930 }
Keith Kanios48af1772007-08-17 07:37:52 +00002931 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002932 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002933 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002934 x = ix, ix = bx, bx = x;
2935 }
2936 if (it == REG_NUM_ESP ||
2937 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002938 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002939
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002940 output->rex |= rexflags(it, ix, REX_X);
2941 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002942
Keith Kanios48af1772007-08-17 07:37:52 +00002943 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002944 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002945 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002946
Keith Kaniosb7a89542007-04-12 02:40:54 +00002947 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002948 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002949 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002950 } else {
2951 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002952 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002953 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002954 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002955 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002956 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002957 mod = 1;
2958 else
2959 mod = 2;
2960 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002961
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002962 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002963 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2964 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002965 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002966 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002967 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002968
Keith Kaniosb7a89542007-04-12 02:40:54 +00002969 if (it == -1)
2970 index = 4, s = 1;
2971 else
2972 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002973
H. Peter Anvine2c80182005-01-15 22:15:51 +00002974 switch (s) {
2975 case 1:
2976 scale = 0;
2977 break;
2978 case 2:
2979 scale = 1;
2980 break;
2981 case 4:
2982 scale = 2;
2983 break;
2984 case 8:
2985 scale = 3;
2986 break;
2987 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002988 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002989 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002990
Keith Kaniosb7a89542007-04-12 02:40:54 +00002991 if (bt == -1) {
2992 base = 5;
2993 mod = 0;
2994 } else {
2995 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002996 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002997 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002998 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002999 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003000 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00003001 mod = 1;
3002 else
3003 mod = 2;
3004 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003005
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003006 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003007 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
3008 output->modrm = GEN_MODRM(mod, rfield, 4);
3009 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003010 }
3011 } else { /* it's 16-bit */
3012 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003013 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07003014
Keith Kaniosb7a89542007-04-12 02:40:54 +00003015 /* check for 64-bit long mode */
3016 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003017 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003018
H. Peter Anvine2c80182005-01-15 22:15:51 +00003019 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003020 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
3021 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003022 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003023
Keith Kaniosb7a89542007-04-12 02:40:54 +00003024 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003025 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003026 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003027
H. Peter Anvine2c80182005-01-15 22:15:51 +00003028 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003029 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 if (b == -1 && i != -1) {
3031 int tmp = b;
3032 b = i;
3033 i = tmp;
3034 } /* swap */
3035 if ((b == R_SI || b == R_DI) && i != -1) {
3036 int tmp = b;
3037 b = i;
3038 i = tmp;
3039 }
3040 /* have BX/BP as base, SI/DI index */
3041 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003042 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003043 if (i != -1 && b != -1 &&
3044 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003045 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003046 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003047 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003048
H. Peter Anvine2c80182005-01-15 22:15:51 +00003049 rm = -1;
3050 if (i != -1)
3051 switch (i * 256 + b) {
3052 case R_SI * 256 + R_BX:
3053 rm = 0;
3054 break;
3055 case R_DI * 256 + R_BX:
3056 rm = 1;
3057 break;
3058 case R_SI * 256 + R_BP:
3059 rm = 2;
3060 break;
3061 case R_DI * 256 + R_BP:
3062 rm = 3;
3063 break;
3064 } else
3065 switch (b) {
3066 case R_SI:
3067 rm = 4;
3068 break;
3069 case R_DI:
3070 rm = 5;
3071 break;
3072 case R_BP:
3073 rm = 6;
3074 break;
3075 case R_BX:
3076 rm = 7;
3077 break;
3078 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003079 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003080 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003081
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003082 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003083 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003084 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003085 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00003086 mod = 1;
3087 else
3088 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003089
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003090 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003091 output->bytes = mod; /* bytes of offset needed */
3092 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003093 }
3094 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003095 }
H. Peter Anvin70653092007-10-19 14:42:29 -07003096
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003097 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003098 return output->type;
3099
3100err:
3101 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003102}
3103
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003104static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003105{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003106 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003107 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003108
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003109 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003110
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003111 switch (ins->prefixes[PPS_ASIZE]) {
3112 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003113 valid &= 16;
3114 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003115 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003116 valid &= 32;
3117 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003118 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003119 valid &= 64;
3120 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003121 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003122 valid &= (addrbits == 32) ? 16 : 32;
3123 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003124 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003125 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003126 }
3127
3128 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003129 if (is_class(MEMORY, ins->oprs[j].type)) {
3130 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07003131
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003132 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003133 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003134 i = 0;
3135 else
3136 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003137
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003138 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003139 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003140 b = 0;
3141 else
3142 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003143
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003144 if (ins->oprs[j].scale == 0)
3145 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003146
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003147 if (!i && !b) {
3148 int ds = ins->oprs[j].disp_size;
3149 if ((addrbits != 64 && ds > 8) ||
3150 (addrbits == 64 && ds == 16))
3151 valid &= ds;
3152 } else {
3153 if (!(REG16 & ~b))
3154 valid &= 16;
3155 if (!(REG32 & ~b))
3156 valid &= 32;
3157 if (!(REG64 & ~b))
3158 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07003159
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003160 if (!(REG16 & ~i))
3161 valid &= 16;
3162 if (!(REG32 & ~i))
3163 valid &= 32;
3164 if (!(REG64 & ~i))
3165 valid &= 64;
3166 }
3167 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003168 }
3169
3170 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003171 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003172 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003173 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04003174 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003175 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003176 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003177 /* Impossible... */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03003178 nasm_nonfatal("impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003179 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003180 }
3181
3182 defdisp = ins->addr_size == 16 ? 16 : 32;
3183
3184 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003185 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
3186 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
3187 /*
3188 * mem_offs sizes must match the address size; if not,
3189 * strip the MEM_OFFS bit and match only EA instructions
3190 */
3191 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
3192 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003193 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003194}