blob: 270262aebbcdf9c8d6283b6ca368120b900fa327 [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{
H. Peter Anvin (Intel)2f171dd2020-07-09 20:03:55 -0700344 static struct last_debug_info {
345 struct src_location where;
346 int32_t segment;
347 } dbg;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700348 union {
349 uint8_t b[8];
350 uint64_t q;
351 } xdata;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700352 size_t asize, amax;
353 uint64_t zeropad = 0;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800354 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800355 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000356
H. Peter Anvina77692b2016-09-20 14:04:33 -0700357 if (!data->size)
358 return; /* Nothing to do */
359
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700360 /*
361 * Convert addresses to RAWDATA if possible
362 * XXX: not all backends want this for global symbols!!!!
363 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700364 switch (data->type) {
365 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800366 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800367 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800368 goto address;
369
370 case OUT_RELADDR:
371 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800372 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800373 goto address;
374
375 address:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700376 nasm_assert(data->size <= 8);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700377 asize = data->size;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700378 amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
Chang S. Bae427d8e32018-05-02 08:07:52 -0700379 if ((ofmt->flags & OFMT_KEEP_ADDR) == 0 && data->tsegment == fixseg &&
380 data->twrt == NO_SEG) {
H. Peter Anvin86b2e932019-09-12 18:20:07 -0700381 if (asize >= (size_t)(data->bits >> 3))
382 data->sign = OUT_WRAP; /* Support address space wrapping for low-bit modes */
383 warn_overflow_out(addrval, asize, data->sign);
Martin Storsjö869087d2017-05-22 13:54:20 +0300384 xdata.q = cpu_to_le64(addrval);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700385 data->data = xdata.b;
386 data->type = OUT_RAWDATA;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700387 asize = amax = 0; /* No longer an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700388 }
389 break;
390
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700391 case OUT_SEGMENT:
392 nasm_assert(data->size <= 8);
393 asize = data->size;
394 amax = 2;
395 break;
396
H. Peter Anvina77692b2016-09-20 14:04:33 -0700397 default:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700398 asize = amax = 0; /* Not an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700399 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000400 }
401
Frank Kotlerabebb082003-09-06 04:45:37 +0000402 /*
H. Peter Anvin (Intel)2f171dd2020-07-09 20:03:55 -0700403 * If the source location or output segment has changed,
404 * let the debug backend know.
Frank Kotlerabebb082003-09-06 04:45:37 +0000405 */
H. Peter Anvin (Intel)2f171dd2020-07-09 20:03:55 -0700406 data->where = src_where();
407 if (!src_location_same(data->where, dbg.where) |
408 (data->segment != dbg.segment)) {
409 dbg.where = data->where;
410 dbg.segment = data->segment;
411 dfmt->linenum(dbg.where.filename, dbg.where.lineno, data->segment);
412 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000413
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700414 if (asize > amax) {
415 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300416 nasm_nonfatal("%u-bit signed relocation unsupported by output format %s",
417 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800418 } else {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800419 /*!
420 *!zext-reloc [on] relocation zero-extended to match output format
421 *! warns that a relocation has been zero-extended due
422 *! to limitations in the output format.
423 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800424 nasm_warn(WARN_ZEXT_RELOC,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700425 "%u-bit %s relocation zero-extended from %u bits",
426 (unsigned int)(asize << 3),
427 data->type == OUT_SEGMENT ? "segment" : "unsigned",
428 (unsigned int)(amax << 3));
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800429 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700430 zeropad = data->size - amax;
431 data->size = amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800432 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700433 lfmt->output(data);
H. Peter Anvined859f72018-06-15 00:03:53 -0700434
435 if (likely(data->segment != NO_SEG)) {
436 ofmt->output(data);
437 } else {
438 /* Outputting to ABSOLUTE section - only reserve is permitted */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300439 if (data->type != OUT_RESERVE)
440 nasm_nonfatal("attempt to assemble code in [ABSOLUTE] space");
H. Peter Anvined859f72018-06-15 00:03:53 -0700441 /* No need to push to the backend */
442 }
443
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700444 data->offset += data->size;
445 data->insoffs += data->size;
446
447 if (zeropad) {
448 data->type = OUT_ZERODATA;
449 data->size = zeropad;
450 lfmt->output(data);
451 ofmt->output(data);
452 data->offset += zeropad;
453 data->insoffs += zeropad;
454 data->size += zeropad; /* Restore original size value */
455 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000456}
457
H. Peter Anvina77692b2016-09-20 14:04:33 -0700458static inline void out_rawdata(struct out_data *data, const void *rawdata,
459 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400460{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700461 data->type = OUT_RAWDATA;
462 data->data = rawdata;
463 data->size = size;
464 out(data);
465}
466
467static void out_rawbyte(struct out_data *data, uint8_t byte)
468{
469 data->type = OUT_RAWDATA;
470 data->data = &byte;
471 data->size = 1;
472 out(data);
473}
474
475static inline void out_reserve(struct out_data *data, uint64_t size)
476{
477 data->type = OUT_RESERVE;
478 data->size = size;
479 out(data);
480}
481
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700482static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700483{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700484 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300485 nasm_nonfatal("segment references cannot be relative");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700486
487 data->type = OUT_SEGMENT;
488 data->sign = OUT_UNSIGNED;
489 data->size = 2;
490 data->toffset = opx->offset;
491 data->tsegment = ofmt->segbase(opx->segment | 1);
492 data->twrt = opx->wrt;
493 out(data);
494}
495
496static void out_imm(struct out_data *data, const struct operand *opx,
497 int size, enum out_sign sign)
498{
499 if (opx->segment != NO_SEG && (opx->segment & 1)) {
500 /*
501 * This is actually a segment reference, but eval() has
502 * already called ofmt->segbase() for us. Sigh.
503 */
504 if (size < 2)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300505 nasm_nonfatal("segment reference must be 16 bits");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700506
507 data->type = OUT_SEGMENT;
508 } else {
509 data->type = (opx->opflags & OPFLAG_RELATIVE)
510 ? OUT_RELADDR : OUT_ADDRESS;
511 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700512 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700513 data->toffset = opx->offset;
514 data->tsegment = opx->segment;
515 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800516 /*
517 * XXX: improve this if at some point in the future we can
518 * distinguish the subtrahend in expressions like [foo - bar]
519 * where bar is a symbol in the current segment. However, at the
520 * current point, if OPFLAG_RELATIVE is set that subtraction has
521 * already occurred.
522 */
523 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700524 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700525 out(data);
526}
527
H. Peter Anvin164d2462017-02-20 02:39:56 -0800528static void out_reladdr(struct out_data *data, const struct operand *opx,
529 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700530{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800531 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300532 nasm_nonfatal("invalid use of self-relative expression");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800533
H. Peter Anvina77692b2016-09-20 14:04:33 -0700534 data->type = OUT_RELADDR;
535 data->sign = OUT_SIGNED;
536 data->size = size;
537 data->toffset = opx->offset;
538 data->tsegment = opx->segment;
539 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800540 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700541 out(data);
542}
543
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700544static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800545 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800547 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800548 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000549 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800550 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000551
H. Peter Anvin755f5212012-02-25 11:41:34 -0800552 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700553 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300554 if (!optimizing.level || (optimizing.flag & OPTIM_DISABLE_JMP_MATCH))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400555 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300556 if (optimizing.level < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400557 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700558
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800559 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100560
Victor van den Elzen154e5922009-02-25 17:32:00 +0100561 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100562 /* Be optimistic in pass 1 */
563 return true;
564
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700566 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000567
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700568 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800569 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
570
571 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
572 /* jmp short (opcode eb) cannot be used with bnd prefix. */
573 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800574 /*!
575 *!bnd [on] invalid BND prefixes
576 *! warns about ineffective use of the \c{BND} prefix when the
577 *! \c{JMP} instruction is converted to the \c{SHORT} form.
578 *! This should be extremely rare since the short \c{JMP} only
579 *! is applicable to jumps inside the same module, but if
580 *! it is legitimate, it may be necessary to use
H. Peter Anvin959702b2019-06-06 20:56:50 -0700581 *! \c{bnd jmp dword}.
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800582 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800583 nasm_warn(WARN_BND | ERR_PASS2 ,
H. Peter Anvin959702b2019-06-06 20:56:50 -0700584 "jmp short does not init bnd regs - bnd prefix dropped");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800585 }
586
587 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000589
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700590static inline int64_t merge_resb(insn *ins, int64_t isize)
591{
592 int nbytes = resb_bytes(ins->opcode);
593
594 if (likely(!nbytes))
595 return isize;
596
597 if (isize != nbytes * ins->oprs[0].offset)
598 return isize; /* Has prefixes of some sort */
599
600 ins->oprs[0].offset *= ins->times;
601 isize *= ins->times;
602 ins->times = 1;
603 return isize;
604}
605
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700606/* This must be handle non-power-of-2 alignment values */
607static inline size_t pad_bytes(size_t len, size_t align)
608{
609 size_t partial = len % align;
610 return partial ? align - partial : 0;
611}
612
613static void out_eops(struct out_data *data, const extop *e)
614{
615 while (e) {
616 size_t dup = e->dup;
617
618 switch (e->type) {
619 case EOT_NOTHING:
620 break;
621
622 case EOT_EXTOP:
623 while (dup--)
624 out_eops(data, e->val.subexpr);
625 break;
626
627 case EOT_DB_NUMBER:
628 if (e->elem > 8) {
629 nasm_nonfatal("integer supplied as %d-bit data",
630 e->elem << 3);
631 } else {
632 while (dup--) {
633 data->insoffs = 0;
634 data->inslen = data->size = e->elem;
635 data->tsegment = e->val.num.segment;
636 data->toffset = e->val.num.offset;
637 data->twrt = e->val.num.wrt;
638 data->relbase = 0;
639 if (e->val.num.segment != NO_SEG &&
640 (e->val.num.segment & 1)) {
641 data->type = OUT_SEGMENT;
642 data->sign = OUT_UNSIGNED;
643 } else {
644 data->type = e->val.num.relative
645 ? OUT_RELADDR : OUT_ADDRESS;
646 data->sign = OUT_WRAP;
647 }
648 out(data);
649 }
650 }
651 break;
652
653 case EOT_DB_FLOAT:
654 case EOT_DB_STRING:
655 case EOT_DB_STRING_FREE:
656 {
657 size_t pad, len;
658
659 pad = pad_bytes(e->val.string.len, e->elem);
660 len = e->val.string.len + pad;
661
662 while (dup--) {
663 data->insoffs = 0;
664 data->inslen = len;
665 out_rawdata(data, e->val.string.data, e->val.string.len);
666 if (pad)
667 out_rawdata(data, zero_buffer, pad);
668 }
669 break;
670 }
671
672 case EOT_DB_RESERVE:
673 data->insoffs = 0;
674 data->inslen = dup * e->elem;
675 out_reserve(data, data->inslen);
676 break;
677 }
678
679 e = e->next;
680 }
681}
682
H. Peter Anvin04445362016-09-21 15:56:19 -0700683/* This is totally just a wild guess what is reasonable... */
684#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
685
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800686int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000687{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700688 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000689 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700690 enum match_result m;
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700691
692 if (instruction->opcode == I_none)
693 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000694
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700695 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700696 data.offset = start;
697 data.segment = segment;
698 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700699 data.bits = bits;
700
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700701 if (opcode_is_db(instruction->opcode)) {
702 out_eops(&data, instruction->eops);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700703 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700704 const char *fname = instruction->eops->val.string.data;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000705 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700706 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700707 off_t base = 0;
708 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700709 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700710 char *buf = NULL;
711 size_t blk = 0; /* Buffered I/O block size */
712 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000713
H. Peter Anvin94ead272017-09-27 15:22:23 -0700714 if (!t)
715 goto done;
716
H. Peter Anvind81a2352016-09-21 14:03:18 -0700717 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400718 if (!fp) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300719 nasm_nonfatal("`incbin': unable to open file `%s'",
720 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700721 goto done;
722 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000723
H. Peter Anvind81a2352016-09-21 14:03:18 -0700724 len = nasm_file_size(fp);
725
726 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300727 nasm_nonfatal("`incbin': unable to get length of file `%s'",
728 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700729 goto close_done;
730 }
731
H. Peter Anvina77692b2016-09-20 14:04:33 -0700732 if (instruction->eops->next) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700733 base = instruction->eops->next->val.num.offset;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700734 if (base >= len) {
735 len = 0;
736 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000737 len -= base;
738 if (instruction->eops->next->next &&
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700739 len > (off_t)instruction->eops->next->next->val.num.offset)
740 len = (off_t)instruction->eops->next->next->val.num.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000741 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000742 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700743
H. Peter Anvina77692b2016-09-20 14:04:33 -0700744 lfmt->set_offset(data.offset);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700745 lfmt->uplevel(LIST_INCBIN, len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000746
H. Peter Anvind81a2352016-09-21 14:03:18 -0700747 if (!len)
748 goto end_incbin;
749
750 /* Try to map file data */
751 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700752 if (!map) {
753 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
754 buf = nasm_malloc(blk);
755 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700756
757 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700758 /*
759 * Consider these irrelevant for INCBIN, since it is fully
760 * possible that these might be (way) bigger than an int
761 * can hold; there is, however, no reason to widen these
762 * types just for INCBIN. data.inslen == 0 signals to the
763 * backend that these fields are meaningless, if at all
764 * needed.
765 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700766 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700767 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700768
H. Peter Anvind81a2352016-09-21 14:03:18 -0700769 if (map) {
770 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700771 } else if ((off_t)m == len) {
772 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700773 } else {
774 off_t l = len;
775
776 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300777 nasm_nonfatal("`incbin': unable to seek on file `%s'",
778 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700779 goto end_incbin;
780 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700781 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700782 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700783 if (!m || feof(fp)) {
784 /*
785 * This shouldn't happen unless the file
786 * actually changes while we are reading
787 * it.
788 */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300789 nasm_nonfatal("`incbin': unexpected EOF while"
790 " reading file `%s'", fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700791 goto end_incbin;
792 }
793 out_rawdata(&data, buf, m);
794 l -= m;
795 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700796 }
797 }
798 end_incbin:
799 lfmt->downlevel(LIST_INCBIN);
800 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700801 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700802 lfmt->downlevel(LIST_TIMES);
803 }
804 if (ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300805 nasm_nonfatal("`incbin': error while"
806 " reading file `%s'", fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700807 }
808 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700809 if (buf)
810 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700811 if (map)
812 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700813 fclose(fp);
814 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700815 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700816 ;
817 } else {
818 /* "Real" instruction */
819
820 /* Check to see if we need an address-size prefix */
821 add_asp(instruction, bits);
822
823 m = find_match(&temp, instruction, data.segment, data.offset, bits);
824
825 if (m == MOK_GOOD) {
826 /* Matches! */
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700827 if (unlikely(itemp_has(temp, IF_OBSOLETE))) {
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700828 errflags warning;
829 const char *whathappened;
830 const char *validity;
831 bool never = itemp_has(temp, IF_NEVER);
832
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700833 /*
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700834 * If IF_OBSOLETE is set, warn the user. Different
835 * warning classes for "obsolete but valid for this
836 * specific CPU" and "obsolete and gone."
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700837 *
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700838 *!obsolete-removed [on] instruction obsolete and removed on the target CPU
839 *! warns for an instruction which has been removed
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700840 *! from the architecture, and is no longer included
841 *! in the CPU definition given in the \c{[CPU]}
842 *! directive, for example \c{POP CS}, the opcode for
843 *! which, \c{0Fh}, instead is an opcode prefix on
844 *! CPUs newer than the first generation 8086.
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700845 *
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700846 *!obsolete-nop [on] instruction obsolete and is a noop on the target CPU
847 *! warns for an instruction which has been removed
848 *! from the architecture, but has been architecturally
849 *! defined to be a noop for future CPUs.
850 *
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700851 *!obsolete-valid [on] instruction obsolete but valid on the target CPU
852 *! warns for an instruction which has been removed
853 *! from the architecture, but is still valid on the
854 *! specific CPU given in the \c{CPU} directive. Code
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700855 *! using these instructions is most likely not
856 *! forward compatible.
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700857 */
858
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700859 whathappened = never ? "never implemented" : "obsolete";
860
861 if (!never && !iflag_cmp_cpu_level(&insns_flags[temp->iflag_idx], &cpu)) {
862 warning = WARN_OBSOLETE_VALID;
863 validity = "but valid on";
864 } else if (itemp_has(temp, IF_NOP)) {
865 warning = WARN_OBSOLETE_NOP;
866 validity = "and is a noop on";
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700867 } else {
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700868 warning = WARN_OBSOLETE_REMOVED;
H. Peter Anvin (Intel)fb118ae2019-08-09 15:01:28 -0700869 validity = never ? "and invalid on" : "and removed from";
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700870 }
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700871
872 nasm_warn(warning, "instruction %s %s the target CPU",
873 whathappened, validity);
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700874 }
875
H. Peter Anvina77692b2016-09-20 14:04:33 -0700876 data.itemp = temp;
877 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700878 data.insoffs = 0;
H. Peter Anvin (Intel)77335212019-08-06 23:22:48 -0700879
880 data.inslen = calcsize(data.segment, data.offset,
881 bits, instruction, temp);
882 nasm_assert(data.inslen >= 0);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700883 data.inslen = merge_resb(instruction, data.inslen);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700884
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700885 gencode(&data, instruction);
H. Peter Anvin (Intel)77335212019-08-06 23:22:48 -0700886 nasm_assert(data.insoffs == data.inslen);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700887 } else {
888 /* No match */
889 switch (m) {
890 case MERR_OPSIZEMISSING:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300891 nasm_nonfatal("operation size not specified");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700892 break;
893 case MERR_OPSIZEMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300894 nasm_nonfatal("mismatch in operand sizes");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700895 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700896 case MERR_BRNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300897 nasm_nonfatal("broadcast not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700898 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700899 case MERR_BRNUMMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300900 nasm_nonfatal("mismatch in the number of broadcasting elements");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700901 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700902 case MERR_MASKNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300903 nasm_nonfatal("mask not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700904 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700905 case MERR_DECONOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300906 nasm_nonfatal("unsupported mode decorator for instruction");
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700907 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700908 case MERR_BADCPU:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300909 nasm_nonfatal("no instruction for this cpu level");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700910 break;
911 case MERR_BADMODE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300912 nasm_nonfatal("instruction not supported in %d-bit mode", bits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700913 break;
914 case MERR_ENCMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300915 nasm_nonfatal("specific encoding scheme not available");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700916 break;
917 case MERR_BADBND:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300918 nasm_nonfatal("bnd prefix is not allowed");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700919 break;
920 case MERR_BADREPNE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300921 nasm_nonfatal("%s prefix is not allowed",
922 (has_prefix(instruction, PPS_REP, P_REPNE) ?
923 "repne" : "repnz"));
H. Peter Anvina77692b2016-09-20 14:04:33 -0700924 break;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700925 case MERR_REGSETSIZE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300926 nasm_nonfatal("invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700927 break;
928 case MERR_REGSET:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300929 nasm_nonfatal("register set not valid for operand");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700930 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700931 default:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300932 nasm_nonfatal("invalid combination of opcode and operands");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700933 break;
934 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700935
936 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400937 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000938 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700939 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000940}
941
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700942static int32_t eops_typeinfo(const extop *e)
H. Peter Anvin29651542018-12-18 19:14:40 -0800943{
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700944 int32_t typeinfo = 0;
H. Peter Anvin29651542018-12-18 19:14:40 -0800945
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700946 while (e) {
947 switch (e->type) {
948 case EOT_NOTHING:
949 break;
950
951 case EOT_EXTOP:
952 typeinfo |= eops_typeinfo(e->val.subexpr);
953 break;
954
955 case EOT_DB_FLOAT:
956 switch (e->elem) {
957 case 1: typeinfo |= TY_BYTE; break;
958 case 2: typeinfo |= TY_WORD; break;
959 case 4: typeinfo |= TY_FLOAT; break;
960 case 8: typeinfo |= TY_QWORD; break; /* double? */
961 case 10: typeinfo |= TY_TBYTE; break; /* long double? */
962 case 16: typeinfo |= TY_YWORD; break;
963 case 32: typeinfo |= TY_ZWORD; break;
964 default: break;
965 }
966 break;
967
968 default:
969 switch (e->elem) {
970 case 1: typeinfo |= TY_BYTE; break;
971 case 2: typeinfo |= TY_WORD; break;
972 case 4: typeinfo |= TY_DWORD; break;
973 case 8: typeinfo |= TY_QWORD; break;
974 case 10: typeinfo |= TY_TBYTE; break;
975 case 16: typeinfo |= TY_YWORD; break;
976 case 32: typeinfo |= TY_ZWORD; break;
977 default: break;
978 }
979 break;
980 }
981 e = e->next;
H. Peter Anvin29651542018-12-18 19:14:40 -0800982 }
983
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700984 return typeinfo;
985}
986
987static inline void debug_set_db_type(insn *instruction)
988{
989
990 int32_t typeinfo = TYS_ELEMENTS(instruction->operands);
991
992 typeinfo |= eops_typeinfo(instruction->eops);
H. Peter Anvin29651542018-12-18 19:14:40 -0800993 dfmt->debug_typevalue(typeinfo);
994}
995
996static void debug_set_type(insn *instruction)
997{
998 int32_t typeinfo;
999
1000 if (opcode_is_resb(instruction->opcode)) {
1001 typeinfo = TYS_ELEMENTS(instruction->oprs[0].offset);
1002
1003 switch (instruction->opcode) {
1004 case I_RESB:
1005 typeinfo |= TY_BYTE;
1006 break;
1007 case I_RESW:
1008 typeinfo |= TY_WORD;
1009 break;
1010 case I_RESD:
1011 typeinfo |= TY_DWORD;
1012 break;
1013 case I_RESQ:
1014 typeinfo |= TY_QWORD;
1015 break;
1016 case I_REST:
1017 typeinfo |= TY_TBYTE;
1018 break;
1019 case I_RESO:
1020 typeinfo |= TY_OWORD;
1021 break;
1022 case I_RESY:
1023 typeinfo |= TY_YWORD;
1024 break;
1025 case I_RESZ:
1026 typeinfo |= TY_ZWORD;
1027 break;
1028 default:
1029 panic();
1030 }
1031 } else {
1032 typeinfo = TY_LABEL;
1033 }
1034
1035 dfmt->debug_typevalue(typeinfo);
1036}
1037
1038
1039/* Proecess an EQU directive */
1040static void define_equ(insn * instruction)
1041{
1042 if (!instruction->label) {
1043 nasm_nonfatal("EQU not preceded by label");
1044 } else if (instruction->operands == 1 &&
1045 (instruction->oprs[0].type & IMMEDIATE) &&
1046 instruction->oprs[0].wrt == NO_SEG) {
1047 define_label(instruction->label,
1048 instruction->oprs[0].segment,
1049 instruction->oprs[0].offset, false);
1050 } else if (instruction->operands == 2
1051 && (instruction->oprs[0].type & IMMEDIATE)
1052 && (instruction->oprs[0].type & COLON)
1053 && instruction->oprs[0].segment == NO_SEG
1054 && instruction->oprs[0].wrt == NO_SEG
1055 && (instruction->oprs[1].type & IMMEDIATE)
1056 && instruction->oprs[1].segment == NO_SEG
1057 && instruction->oprs[1].wrt == NO_SEG) {
1058 define_label(instruction->label,
1059 instruction->oprs[0].offset | SEG_ABS,
1060 instruction->oprs[1].offset, false);
1061 } else {
1062 nasm_nonfatal("bad syntax for EQU");
1063 }
1064}
1065
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001066static int64_t len_extops(const extop *e)
1067{
1068 int64_t isize = 0;
1069 size_t pad;
1070
1071 while (e) {
1072 switch (e->type) {
1073 case EOT_NOTHING:
1074 break;
1075
1076 case EOT_EXTOP:
1077 isize += e->dup * len_extops(e->val.subexpr);
1078 break;
1079
1080 case EOT_DB_STRING:
1081 case EOT_DB_STRING_FREE:
1082 case EOT_DB_FLOAT:
1083 pad = pad_bytes(e->val.string.len, e->elem);
1084 isize += e->dup * (e->val.string.len + pad);
1085 break;
1086
1087 case EOT_DB_NUMBER:
1088 warn_overflow_const(e->val.num.offset, e->elem);
1089 isize += e->dup * e->elem;
1090 break;
1091
1092 case EOT_DB_RESERVE:
1093 isize += e->dup;
1094 break;
1095 }
1096
1097 e = e->next;
1098 }
1099
1100 return isize;
1101}
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001102
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001103int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001104{
H. Peter Anvin3360d792007-09-11 04:16:57 +00001105 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001106 enum match_result m;
H. Peter Anvin29651542018-12-18 19:14:40 -08001107 int64_t isize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001108
H. Peter Anvin29651542018-12-18 19:14:40 -08001109 if (instruction->opcode == I_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 return 0;
H. Peter Anvin29651542018-12-18 19:14:40 -08001111 } else if (instruction->opcode == I_EQU) {
1112 define_equ(instruction);
1113 return 0;
1114 } else if (opcode_is_db(instruction->opcode)) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001115 isize = len_extops(instruction->eops);
H. Peter Anvin29651542018-12-18 19:14:40 -08001116 debug_set_db_type(instruction);
1117 return isize;
1118 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001119 const extop *e = instruction->eops;
1120 const char *fname = e->val.string.data;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001121 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001122
H. Peter Anvind81a2352016-09-21 14:03:18 -07001123 len = nasm_file_size_by_path(fname);
1124 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001125 nasm_nonfatal("`incbin': unable to get length of file `%s'",
1126 fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001127 return 0;
1128 }
1129
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001130 e = e->next;
1131 if (e) {
1132 if (len <= (off_t)e->val.num.offset) {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001133 len = 0;
1134 } else {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001135 len -= e->val.num.offset;
1136 e = e->next;
1137 if (e && len > (off_t)e->val.num.offset) {
1138 len = (off_t)e->val.num.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 }
1140 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001142
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001143 len *= instruction->times;
1144 instruction->times = 1; /* Tell the upper layer to not iterate */
1145
H. Peter Anvind81a2352016-09-21 14:03:18 -07001146 return len;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001147 } else {
H. Peter Anvin29651542018-12-18 19:14:40 -08001148 /* Normal instruction, or RESx */
1149
1150 /* Check to see if we need an address-size prefix */
1151 add_asp(instruction, bits);
1152
1153 m = find_match(&temp, instruction, segment, offset, bits);
1154 if (m != MOK_GOOD)
1155 return -1; /* No match */
1156
1157 isize = calcsize(segment, offset, bits, instruction, temp);
1158 debug_set_type(instruction);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001159 isize = merge_resb(instruction, isize);
H. Peter Anvin29651542018-12-18 19:14:40 -08001160
1161 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001162 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001163}
1164
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001165static void bad_hle_warn(const insn * ins, uint8_t hleok)
1166{
1167 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001168 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001169 static const enum whatwarn warn[2][4] =
1170 {
1171 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
1172 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
1173 };
1174 unsigned int n;
1175
1176 n = (unsigned int)rep_pfx - P_XACQUIRE;
1177 if (n > 1)
1178 return; /* Not XACQUIRE/XRELEASE */
1179
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001180 ww = warn[n][hleok];
1181 if (!is_class(MEMORY, ins->oprs[0].type))
1182 ww = w_inval; /* HLE requires operand 0 to be memory */
1183
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001184 /*!
1185 *!hle [on] invalid HLE prefixes
1186 *! warns about invalid use of the HLE \c{XACQUIRE} or \c{XRELEASE}
1187 *! prefixes.
1188 */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001189 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001190 case w_none:
1191 break;
1192
1193 case w_lock:
1194 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001195 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001196 "%s with this instruction requires lock",
1197 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001198 }
1199 break;
1200
1201 case w_inval:
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001202 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001203 "%s invalid with this instruction",
1204 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001205 break;
1206 }
1207}
1208
H. Peter Anvin507ae032008-10-09 15:37:10 -07001209/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +04001210#define case3(x) case (x): case (x)+1: case (x)+2
1211#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -07001212
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001213static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001214 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001215{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001216 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001217 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001218 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001219 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001220 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001221 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001222 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001223 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001224 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001225 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -07001226 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -07001227 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001228
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001229 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001230 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001231 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001232
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001233 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001234 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001235
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 (void)segment; /* Don't warn that this parameter is unused */
1237 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001238
H. Peter Anvin839eca22007-10-29 23:12:47 -07001239 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001240 c = *codes++;
1241 op1 = (c & 3) + ((opex & 1) << 2);
1242 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1243 opx = &ins->oprs[op1];
1244 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001245
H. Peter Anvin839eca22007-10-29 23:12:47 -07001246 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001247 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001248 codes += c, length += c;
1249 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001250
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001251 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001252 opex = c;
1253 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001254
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001255 case4(010):
1256 ins->rex |=
1257 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 codes++, length++;
1259 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001260
Jin Kyu Song164d6072013-10-15 19:10:13 -07001261 case4(014):
1262 /* this is an index reg of MIB operand */
1263 mib_index = opx->basereg;
1264 break;
1265
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001266 case4(020):
1267 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001268 length++;
1269 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001270
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001271 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001272 length += 2;
1273 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001274
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001275 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001276 if (opx->type & (BITS16 | BITS32 | BITS64))
1277 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 else
1279 length += (bits == 16) ? 2 : 4;
1280 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001281
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001282 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001283 length += 4;
1284 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001285
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001286 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001287 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001289
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001290 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 length++;
1292 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001293
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001294 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001295 length += 8; /* MOV reg64/imm */
1296 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001297
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001298 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 length += 2;
1300 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001301
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001302 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001303 if (opx->type & (BITS16 | BITS32 | BITS64))
1304 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 else
1306 length += (bits == 16) ? 2 : 4;
1307 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001308
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001309 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001310 length += 4;
1311 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001312
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001313 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001314 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001315 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001316
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001317 case 0172:
1318 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001319 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001320 length++;
1321 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001322
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001323 case4(0174):
1324 length++;
1325 break;
1326
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001327 case4(0240):
1328 ins->rex |= REX_EV;
1329 ins->vexreg = regval(opx);
1330 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1331 ins->vex_cm = *codes++;
1332 ins->vex_wlp = *codes++;
1333 ins->evex_tuple = (*codes++ - 0300);
1334 break;
1335
1336 case 0250:
1337 ins->rex |= REX_EV;
1338 ins->vexreg = 0;
1339 ins->vex_cm = *codes++;
1340 ins->vex_wlp = *codes++;
1341 ins->evex_tuple = (*codes++ - 0300);
1342 break;
1343
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001344 case4(0254):
1345 length += 4;
1346 break;
1347
1348 case4(0260):
1349 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001350 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001351 ins->vex_cm = *codes++;
1352 ins->vex_wlp = *codes++;
1353 break;
1354
1355 case 0270:
1356 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001357 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001358 ins->vex_cm = *codes++;
1359 ins->vex_wlp = *codes++;
1360 break;
1361
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001362 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001363 hleok = c & 3;
1364 break;
1365
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001366 case4(0274):
1367 length++;
1368 break;
1369
1370 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001372
H. Peter Anvine2c80182005-01-15 22:15:51 +00001373 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001374 if (bits == 64)
1375 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001376 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001378
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001380 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001382
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001384 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001385
Keith Kaniosb7a89542007-04-12 02:40:54 +00001386 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001387 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1388 has_prefix(ins, PPS_ASIZE, P_A32))
1389 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001390 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001391
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001392 case4(0314):
1393 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001394
H. Peter Anvine2c80182005-01-15 22:15:51 +00001395 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001396 {
1397 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1398 if (pfx == P_O16)
1399 break;
1400 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001401 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001402 else
1403 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001404 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001405 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001406
H. Peter Anvine2c80182005-01-15 22:15:51 +00001407 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001408 {
1409 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1410 if (pfx == P_O32)
1411 break;
1412 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001413 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001414 else
1415 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001416 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001417 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001418
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 case 0322:
1420 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001421
Keith Kaniosb7a89542007-04-12 02:40:54 +00001422 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001423 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001424 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001425
Keith Kaniosb7a89542007-04-12 02:40:54 +00001426 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001427 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001428 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001429
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001430 case 0325:
1431 ins->rex |= REX_NH;
1432 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001433
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001434 case 0326:
1435 break;
1436
H. Peter Anvine2c80182005-01-15 22:15:51 +00001437 case 0330:
1438 codes++, length++;
1439 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001440
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001442 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001443
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001444 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 case 0333:
1446 length++;
1447 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001448
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001449 case 0334:
1450 ins->rex |= REX_L;
1451 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001452
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001453 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001454 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001455
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001456 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001457 if (!ins->prefixes[PPS_REP])
1458 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001459 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001460
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001461 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001462 if (!ins->prefixes[PPS_REP])
1463 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001464 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001465
H. Peter Anvine2c80182005-01-15 22:15:51 +00001466 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001467 if (!absolute_op(&ins->oprs[0]))
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001468 nasm_nonfatal("attempt to reserve non-constant"
1469 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001470 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001471 nasm_warn(WARN_OTHER, "forward reference in RESx "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001472 "can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 else
H. Peter Anvin5358b982018-12-18 18:06:26 -08001474 length += ins->oprs[0].offset * resb_bytes(ins->opcode);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001476
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001477 case 0341:
1478 if (!ins->prefixes[PPS_WAIT])
1479 ins->prefixes[PPS_WAIT] = P_WAIT;
1480 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001481
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001482 case 0360:
1483 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001484
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001485 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001486 length++;
1487 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001488
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001489 case 0364:
1490 case 0365:
1491 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001492
Keith Kanios48af1772007-08-17 07:37:52 +00001493 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001494 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001495 length++;
1496 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001497
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001498 case 0370:
1499 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001501
H. Peter Anvine2c80182005-01-15 22:15:51 +00001502 case 0373:
1503 length++;
1504 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001505
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001506 case 0374:
1507 eat = EA_XMMVSIB;
1508 break;
1509
1510 case 0375:
1511 eat = EA_YMMVSIB;
1512 break;
1513
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001514 case 0376:
1515 eat = EA_ZMMVSIB;
1516 break;
1517
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001518 case4(0100):
1519 case4(0110):
1520 case4(0120):
1521 case4(0130):
1522 case4(0200):
1523 case4(0204):
1524 case4(0210):
1525 case4(0214):
1526 case4(0220):
1527 case4(0224):
1528 case4(0230):
1529 case4(0234):
1530 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001532 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001533 opflags_t rflags;
1534 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001535 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001536
Keith Kaniosb7a89542007-04-12 02:40:54 +00001537 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001538
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001539 if (c <= 0177) {
1540 /* pick rfield from operand b (opx) */
1541 rflags = regflag(opx);
1542 rfield = nasm_regvals[opx->basereg];
1543 } else {
1544 rflags = 0;
1545 rfield = c & 7;
1546 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001547
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001548 /* EVEX.b1 : evex_brerop contains the operand position */
1549 op_er_sae = (ins->evex_brerop >= 0 ?
1550 &ins->oprs[ins->evex_brerop] : NULL);
1551
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001552 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1553 /* set EVEX.b */
1554 ins->evex_p[2] |= EVEX_P2B;
1555 if (op_er_sae->decoflags & ER) {
1556 /* set EVEX.RC (rounding control) */
1557 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1558 & EVEX_P2RC;
1559 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001560 } else {
1561 /* set EVEX.L'L (vector length) */
1562 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001563 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001564 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001565 /* set EVEX.b */
1566 ins->evex_p[2] |= EVEX_P2B;
1567 }
1568 }
1569
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001570 if (itemp_has(temp, IF_MIB)) {
1571 opy->eaflags |= EAF_MIB;
1572 /*
1573 * if a separate form of MIB (ICC style) is used,
1574 * the index reg info is merged into mem operand
1575 */
1576 if (mib_index != R_none) {
1577 opy->indexreg = mib_index;
1578 opy->scale = 1;
1579 opy->hintbase = mib_index;
1580 opy->hinttype = EAH_NOTBASE;
1581 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001582 }
1583
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001584 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001585 rfield, rflags, ins, &errmsg) != eat) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001586 nasm_nonfatal("%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001588 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001589 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001591 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001592 }
1593 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001594
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001595 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001596 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001597 ": instruction code \\%o (0x%02X) given", c, c);
1598 break;
1599 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001600 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001601
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001602 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001603
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001604 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001605 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001606 nasm_nonfatal("instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001607 return -1;
1608 }
1609 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001610 }
1611
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001612 switch (ins->prefixes[PPS_VEX]) {
1613 case P_EVEX:
1614 if (!(ins->rex & REX_EV))
1615 return -1;
1616 break;
1617 case P_VEX3:
1618 case P_VEX2:
1619 if (!(ins->rex & REX_V))
1620 return -1;
1621 break;
1622 default:
1623 break;
1624 }
1625
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001626 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001627 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001628
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001629 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001630 nasm_nonfatal("cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001631 return -1;
1632 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001633 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001634 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001635 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001636 ins->rex &= ~REX_W;
1637 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001638 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001639 ins->rex |= REX_W;
1640 bad32 &= ~REX_W;
1641 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001642 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001643 /* Follow REX_W */
1644 break;
1645 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001646
H. Peter Anvinfc561202011-07-07 16:58:22 -07001647 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001648 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001649 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001650 } else if (!(ins->rex & REX_EV) &&
1651 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001652 nasm_nonfatal("invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001653 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001654 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001655 if (ins->rex & REX_EV)
1656 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001657 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1658 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001659 length += 3;
1660 else
1661 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001662 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001663 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001664 nasm_nonfatal("cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001665 return -1;
1666 } else if (bits == 64) {
1667 length++;
1668 } else if ((ins->rex & REX_L) &&
1669 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001670 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001671 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001672 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001673 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001674 length++;
1675 } else {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001676 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001677 return -1;
1678 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001679 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001680
1681 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001682 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001683 /*!
H. Peter Anvin (Intel)be99ebd2018-12-13 22:12:37 -08001684 *!lock [on] LOCK prefix on unlockable instructions
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001685 *! warns about \c{LOCK} prefixes on unlockable instructions.
1686 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001687 nasm_warn(WARN_LOCK | ERR_PASS2 , "instruction is not lockable");
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001688 }
1689
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001690 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001691
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001692 /*
1693 * when BND prefix is set by DEFAULT directive,
1694 * BND prefix is added to every appropriate instruction line
1695 * unless it is overridden by NOBND prefix.
1696 */
1697 if (globalbnd &&
1698 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1699 ins->prefixes[PPS_REP] = P_BND;
1700
H. Peter Anvina77692b2016-09-20 14:04:33 -07001701 /*
1702 * Add length of legacy prefixes
1703 */
1704 length += emit_prefix(NULL, bits, ins);
1705
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001706 return length;
1707}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001708
H. Peter Anvina77692b2016-09-20 14:04:33 -07001709static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001710{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001711 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001712 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001713 !(ins->rex & (REX_V | REX_EV)) &&
1714 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001715 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1716 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001717 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001718 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001719 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001720}
1721
H. Peter Anvina77692b2016-09-20 14:04:33 -07001722static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1723{
1724 int bytes = 0;
1725 int j;
1726
1727 for (j = 0; j < MAXPREFIX; j++) {
1728 uint8_t c = 0;
1729 switch (ins->prefixes[j]) {
1730 case P_WAIT:
1731 c = 0x9B;
1732 break;
1733 case P_LOCK:
1734 c = 0xF0;
1735 break;
1736 case P_REPNE:
1737 case P_REPNZ:
1738 case P_XACQUIRE:
1739 case P_BND:
1740 c = 0xF2;
1741 break;
1742 case P_REPE:
1743 case P_REPZ:
1744 case P_REP:
1745 case P_XRELEASE:
1746 c = 0xF3;
1747 break;
1748 case R_CS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001749 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001750 nasm_warn(WARN_OTHER|ERR_PASS2, "cs segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001751 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001752 c = 0x2E;
1753 break;
1754 case R_DS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001755 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001756 nasm_warn(WARN_OTHER|ERR_PASS2, "ds segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001757 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001758 c = 0x3E;
1759 break;
1760 case R_ES:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001761 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001762 nasm_warn(WARN_OTHER|ERR_PASS2, "es segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001763 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001764 c = 0x26;
1765 break;
1766 case R_FS:
1767 c = 0x64;
1768 break;
1769 case R_GS:
1770 c = 0x65;
1771 break;
1772 case R_SS:
1773 if (bits == 64) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001774 nasm_warn(WARN_OTHER|ERR_PASS2, "ss segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001775 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001776 }
1777 c = 0x36;
1778 break;
1779 case R_SEGR6:
1780 case R_SEGR7:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001781 nasm_nonfatal("segr6 and segr7 cannot be used as prefixes");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001782 break;
1783 case P_A16:
1784 if (bits == 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001785 nasm_nonfatal("16-bit addressing is not supported "
1786 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001787 } else if (bits != 16)
1788 c = 0x67;
1789 break;
1790 case P_A32:
1791 if (bits != 32)
1792 c = 0x67;
1793 break;
1794 case P_A64:
1795 if (bits != 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001796 nasm_nonfatal("64-bit addressing is only supported "
1797 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001798 }
1799 break;
1800 case P_ASP:
1801 c = 0x67;
1802 break;
1803 case P_O16:
1804 if (bits != 16)
1805 c = 0x66;
1806 break;
1807 case P_O32:
1808 if (bits == 16)
1809 c = 0x66;
1810 break;
1811 case P_O64:
1812 /* REX.W */
1813 break;
1814 case P_OSP:
1815 c = 0x66;
1816 break;
1817 case P_EVEX:
1818 case P_VEX3:
1819 case P_VEX2:
1820 case P_NOBND:
1821 case P_none:
1822 break;
1823 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001824 nasm_panic("invalid instruction prefix");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001825 }
1826 if (c) {
1827 if (data)
1828 out_rawbyte(data, c);
1829 bytes++;
1830 }
1831 }
1832 return bytes;
1833}
1834
1835static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001836{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001837 uint8_t c;
1838 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001839 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001840 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001841 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001842 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001843 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001844 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001845 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001846 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001847 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001848
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001849 ins->rex_done = false;
1850
H. Peter Anvina77692b2016-09-20 14:04:33 -07001851 emit_prefix(data, bits, ins);
1852
H. Peter Anvin839eca22007-10-29 23:12:47 -07001853 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001854 c = *codes++;
1855 op1 = (c & 3) + ((opex & 1) << 2);
1856 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1857 opx = &ins->oprs[op1];
1858 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001859
H. Peter Anvina77692b2016-09-20 14:04:33 -07001860
H. Peter Anvin839eca22007-10-29 23:12:47 -07001861 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 case 01:
1863 case 02:
1864 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001865 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001866 emit_rex(data, ins);
1867 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001868 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001869 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001870
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001871 case 05:
1872 case 06:
1873 case 07:
1874 opex = c;
1875 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001876
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001877 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001878 emit_rex(data, ins);
1879 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001881
Jin Kyu Song164d6072013-10-15 19:10:13 -07001882 case4(014):
1883 break;
1884
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001885 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001886 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001887 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001888
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001889 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001890 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001892
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001893 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001894 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001895 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001896
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001897 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001898 if (opx->type & (BITS16 | BITS32))
1899 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001900 else
1901 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001902 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001903 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001904
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001905 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001906 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001907 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001908
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001909 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001910 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001911 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001913
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001914 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001915 if (opx->segment == data->segment) {
1916 int64_t delta = opx->offset - data->offset
1917 - (data->inslen - data->insoffs);
1918 if (delta > 127 || delta < -128)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001919 nasm_nonfatal("short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001920 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001921 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001923
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001924 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001925 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001926 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001927
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001928 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001929 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001931
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001932 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001933 if (opx->type & (BITS16 | BITS32 | BITS64))
1934 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 else
1936 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001937
1938 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001940
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001941 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001942 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001944
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001945 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001946 if (opx->segment == NO_SEG)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001947 nasm_nonfatal("value referenced by FAR is not relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001948 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001950
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001951 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001952 {
1953 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1954 const struct operand *opy;
1955
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001956 c = *codes++;
1957 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001958 opy = &ins->oprs[c & 7];
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001959 if (!absolute_op(opy))
1960 nasm_nonfatal("non-absolute expression not permitted "
1961 "as argument %d", c & 7);
1962 else if (opy->offset & ~mask)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001963 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001964 "is4 argument exceeds bounds");
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001965 c = opy->offset & mask;
1966 goto emit_is4;
1967 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001968
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001969 case 0173:
1970 c = *codes++;
1971 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001972 c &= 15;
1973 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001974
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001975 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001976 c = 0;
1977 emit_is4:
1978 r = nasm_regvals[opx->basereg];
1979 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001980 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001981
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001982 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001983 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001984 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001985 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001986 "signed dword immediate exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001987 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001988 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001989 break;
1990
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001991 case4(0240):
1992 case 0250:
1993 codes += 3;
1994 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1995 EVEX_P2Z | EVEX_P2AAA, 2);
1996 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1997 bytes[0] = 0x62;
1998 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001999 bytes[1] = ((((ins->rex & 7) << 5) |
2000 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07002001 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002002 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
2003 ((~ins->vexreg & 15) << 3) |
2004 (1 << 2) | (ins->vex_wlp & 3);
2005 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07002006 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002007 break;
2008
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002009 case4(0260):
2010 case 0270:
2011 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002012 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
2013 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002014 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
2015 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
2016 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07002017 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07002018 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002019 } else {
2020 bytes[0] = 0xc5;
2021 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07002022 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07002023 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002024 }
2025 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002026
H. Peter Anvine014f352012-02-25 22:35:19 -08002027 case 0271:
2028 case 0272:
2029 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08002030 break;
2031
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002032 case4(0274):
2033 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08002034 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002035 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07002036
H. Peter Anvin64e87d02017-03-01 13:45:02 -08002037 if (absolute_op(opx)) {
2038 if (ins->rex & REX_W)
2039 s = 64;
2040 else if (ins->prefixes[PPS_OSIZE] == P_O16)
2041 s = 16;
2042 else if (ins->prefixes[PPS_OSIZE] == P_O32)
2043 s = 32;
2044 else
2045 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07002046
H. Peter Anvin64e87d02017-03-01 13:45:02 -08002047 um = (uint64_t)2 << (s-1);
2048 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08002049
H. Peter Anvin64e87d02017-03-01 13:45:02 -08002050 if (uv > 127 && uv < (uint64_t)-128 &&
2051 (uv < um-128 || uv > um-1)) {
2052 /* If this wasn't explicitly byte-sized, warn as though we
2053 * had fallen through to the imm16/32/64 case.
2054 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08002055 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin64e87d02017-03-01 13:45:02 -08002056 "%s value exceeds bounds",
2057 (opx->type & BITS8) ? "signed byte" :
2058 s == 16 ? "word" :
2059 s == 32 ? "dword" :
2060 "signed dword");
2061 }
2062
2063 /* Output as a raw byte to avoid byte overflow check */
2064 out_rawbyte(data, (uint8_t)uv);
2065 } else {
2066 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002067 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07002068 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002069 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07002070
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002071 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002072 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002073
H. Peter Anvine2c80182005-01-15 22:15:51 +00002074 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002075 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
2076 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002078
H. Peter Anvine2c80182005-01-15 22:15:51 +00002079 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002080 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
2081 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002082 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002083
H. Peter Anvine2c80182005-01-15 22:15:51 +00002084 case 0312:
2085 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002086
Keith Kaniosb7a89542007-04-12 02:40:54 +00002087 case 0313:
Keith Kaniosb7a89542007-04-12 02:40:54 +00002088 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002089
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002090 case4(0314):
2091 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08002092
H. Peter Anvine2c80182005-01-15 22:15:51 +00002093 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002094 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002095 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002096
H. Peter Anvine2c80182005-01-15 22:15:51 +00002097 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07002098 case 0323:
2099 break;
2100
Keith Kaniosb7a89542007-04-12 02:40:54 +00002101 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002102 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002103 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07002104
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002105 case 0325:
2106 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07002107
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04002108 case 0326:
2109 break;
2110
H. Peter Anvine2c80182005-01-15 22:15:51 +00002111 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002112 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002113 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002114
H. Peter Anvine2c80182005-01-15 22:15:51 +00002115 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002116 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002117
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002118 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002119 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002120 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002121 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002122
Keith Kanios48af1772007-08-17 07:37:52 +00002123 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002124 if (ins->rex & REX_R)
2125 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00002126 ins->rex &= ~(REX_L|REX_R);
2127 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00002128
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002129 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002130 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002131
H. Peter Anvin962e3052008-08-28 17:47:16 -07002132 case 0336:
2133 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002134 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07002135
H. Peter Anvine2c80182005-01-15 22:15:51 +00002136 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002137 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002138 nasm_panic("non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07002139
H. Peter Anvin5358b982018-12-18 18:06:26 -08002140 out_reserve(data, ins->oprs[0].offset * resb_bytes(ins->opcode));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002141 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002142
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002143 case 0341:
2144 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08002145
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002146 case 0360:
2147 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002148
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002149 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002150 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002151 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002152
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002153 case 0364:
2154 case 0365:
2155 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002156
Keith Kanios48af1772007-08-17 07:37:52 +00002157 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002158 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002159 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00002160 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002161
Jin Kyu Song03041092013-10-15 19:38:51 -07002162 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002163 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002164
H. Peter Anvine2c80182005-01-15 22:15:51 +00002165 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002166 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002167 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002168
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002169 case 0374:
2170 eat = EA_XMMVSIB;
2171 break;
2172
2173 case 0375:
2174 eat = EA_YMMVSIB;
2175 break;
2176
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002177 case 0376:
2178 eat = EA_ZMMVSIB;
2179 break;
2180
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002181 case4(0100):
2182 case4(0110):
2183 case4(0120):
2184 case4(0130):
2185 case4(0200):
2186 case4(0204):
2187 case4(0210):
2188 case4(0214):
2189 case4(0220):
2190 case4(0224):
2191 case4(0230):
2192 case4(0234):
2193 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002194 ea ea_data;
2195 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002196 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002197 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002198 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07002199
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002200 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002201 /* pick rfield from operand b (opx) */
2202 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07002203 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002204 } else {
2205 /* rfield is constant */
2206 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002207 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002208 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002209
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002210 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002211 rfield, rflags, ins, &errmsg) != eat)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002212 nasm_nonfatal("%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07002213
H. Peter Anvine2c80182005-01-15 22:15:51 +00002214 p = bytes;
2215 *p++ = ea_data.modrm;
2216 if (ea_data.sib_present)
2217 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07002218 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002219
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002220 /*
2221 * Make sure the address gets the right offset in case
2222 * the line breaks in the .lst file (BR 1197827)
2223 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002224
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002225 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002226 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07002227 if (ea_data.disp8) {
2228 out_rawbyte(data, ea_data.disp8);
2229 } else if (ea_data.rip) {
2230 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002231 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002232 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002233
H. Peter Anvina77692b2016-09-20 14:04:33 -07002234 if (overflow_general(opy->offset, asize) ||
2235 signed_bits(opy->offset, ins->addr_size) !=
2236 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08002237 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002238
H. Peter Anvina77692b2016-09-20 14:04:33 -07002239 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07002240 (asize > ea_data.bytes)
2241 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002242 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002243 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002244 }
2245 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07002246
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002247 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07002248 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002249 ": instruction code \\%o (0x%02X) given", c, c);
2250 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002251 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07002252 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002253}
2254
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002255static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002256{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002257 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002258 nasm_panic("invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002259 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002260}
2261
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00002262static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002263{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002264 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002265 nasm_panic("invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002266 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002267}
2268
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002269static int op_rexflags(const operand * o, int mask)
2270{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002271 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002272 int val;
2273
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002274 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002275 nasm_panic("invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002276
H. Peter Anvina4835d42008-05-20 14:21:29 -07002277 flags = nasm_reg_flags[o->basereg];
2278 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002279
2280 return rexflags(val, flags, mask);
2281}
2282
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002283static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002284{
2285 int rex = 0;
2286
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002287 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002288 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002289 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002290 rex |= REX_W;
2291 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2292 rex |= REX_H;
2293 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2294 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002295
2296 return rex & mask;
2297}
2298
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002299static int evexflags(int val, decoflags_t deco,
2300 int mask, uint8_t byte)
2301{
2302 int evex = 0;
2303
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002304 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002305 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002306 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002307 evex |= (EVEX_P0RP | EVEX_P0X);
2308 break;
2309 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002310 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002311 evex |= EVEX_P2VP;
2312 if (deco & Z)
2313 evex |= EVEX_P2Z;
2314 if (deco & OPMASK_MASK)
2315 evex |= deco & EVEX_P2AAA;
2316 break;
2317 }
2318 return evex & mask;
2319}
2320
2321static int op_evexflags(const operand * o, int mask, uint8_t byte)
2322{
2323 int val;
2324
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002325 val = nasm_regvals[o->basereg];
2326
2327 return evexflags(val, o->decoflags, mask, byte);
2328}
2329
H. Peter Anvin23595f52009-07-25 17:44:25 -07002330static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002331 insn *instruction,
2332 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002333{
2334 const struct itemplate *temp;
2335 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002336 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002337 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002338 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002339 int i;
2340
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002341 /* broadcasting uses a different data element size */
2342 for (i = 0; i < instruction->operands; i++)
2343 if (i == broadcast)
2344 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2345 else
2346 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002347
2348 merr = MERR_INVALOP;
2349
2350 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002351 temp->opcode != I_none; temp++) {
2352 m = matches(temp, instruction, bits);
2353 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002354 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002355 m = MOK_GOOD;
2356 else
2357 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002358 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002359 /*
2360 * Missing operand size and a candidate for fuzzy matching...
2361 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002362 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002363 if (i == broadcast)
2364 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2365 else
2366 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002367 opsizemissing = true;
2368 }
2369 if (m > merr)
2370 merr = m;
2371 if (merr == MOK_GOOD)
2372 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002373 }
2374
2375 /* No match, but see if we can get a fuzzy operand size match... */
2376 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002377 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002378
2379 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002380 /*
2381 * We ignore extrinsic operand sizes on registers, so we should
2382 * never try to fuzzy-match on them. This also resolves the case
2383 * when we have e.g. "xmmrm128" in two different positions.
2384 */
2385 if (is_class(REGISTER, instruction->oprs[i].type))
2386 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002387
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002388 /* This tests if xsizeflags[i] has more than one bit set */
2389 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2390 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002391
Jin Kyu Song7903c072013-10-30 03:00:12 -07002392 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002393 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002394 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2395 BITS32 : BITS64);
2396 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002397 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002398 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002399 }
2400
2401 /* Try matching again... */
2402 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002403 temp->opcode != I_none; temp++) {
2404 m = matches(temp, instruction, bits);
2405 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002406 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002407 m = MOK_GOOD;
2408 else
2409 m = MERR_INVALOP;
2410 }
2411 if (m > merr)
2412 merr = m;
2413 if (merr == MOK_GOOD)
2414 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002415 }
2416
H. Peter Anvina81655b2009-07-25 18:15:28 -07002417done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002418 *tempp = temp;
2419 return merr;
2420}
2421
Mark Charneydcaef4b2014-10-09 13:45:17 -04002422static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2423{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002424 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002425 uint8_t brcast_num;
2426
Mark Charneydcaef4b2014-10-09 13:45:17 -04002427 if (brsize > BITS64)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002428 nasm_fatal("size of broadcasting element is greater than 64 bits");
Mark Charneydcaef4b2014-10-09 13:45:17 -04002429
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002430 /*
2431 * The shift term is to take care of the extra BITS80 inserted
2432 * between BITS64 and BITS128.
2433 */
2434 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2435 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002436
2437 return brcast_num;
2438}
2439
H. Peter Anvin65289e82009-07-25 17:25:11 -07002440static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002441 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002442{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002443 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002444 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002445 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002446
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002447 /*
2448 * Check the opcode
2449 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002450 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002451 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002452
2453 /*
2454 * Count the operands
2455 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002456 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002457 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002458
2459 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002460 * Is it legal?
2461 */
Chang S. Baea5786342018-08-15 23:22:21 +03002462 if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002463 return MERR_INVALOP;
2464
2465 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002466 * {evex} available?
2467 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002468 switch (instruction->prefixes[PPS_VEX]) {
2469 case P_EVEX:
2470 if (!itemp_has(itemp, IF_EVEX))
2471 return MERR_ENCMISMATCH;
2472 break;
2473 case P_VEX3:
2474 case P_VEX2:
2475 if (!itemp_has(itemp, IF_VEX))
2476 return MERR_ENCMISMATCH;
2477 break;
2478 default:
2479 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002480 }
2481
2482 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002483 * Check that no spurious colons or TOs are present
2484 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002485 for (i = 0; i < itemp->operands; i++)
2486 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002487 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002488
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002489 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002490 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002491 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002492 switch (itemp_smask(itemp)) {
2493 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002494 asize = BITS8;
2495 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002496 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002497 asize = BITS16;
2498 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002499 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002500 asize = BITS32;
2501 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002502 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002503 asize = BITS64;
2504 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002505 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002506 asize = BITS128;
2507 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002508 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002509 asize = BITS256;
2510 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002511 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002512 asize = BITS512;
2513 break;
H. Peter Anvin (Intel)02b60dd2019-08-14 15:23:00 -07002514 case IF_GENBIT(IF_ANYSIZE):
2515 asize = SIZE_MASK;
2516 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002517 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002518 switch (bits) {
2519 case 16:
2520 asize = BITS16;
2521 break;
2522 case 32:
2523 asize = BITS32;
2524 break;
2525 case 64:
2526 asize = BITS64;
2527 break;
2528 default:
2529 asize = 0;
2530 break;
2531 }
2532 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002533 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002534 asize = 0;
2535 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002536 }
2537
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002538 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002539 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002540 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002541 memset(size, 0, sizeof size);
2542 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002543 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002544 /* S- flags apply to all operands */
2545 for (i = 0; i < MAX_OPERANDS; i++)
2546 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002547 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002548
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002549 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002550 * Check that the operand flags all match up,
2551 * it's a bit tricky so lets be verbose:
2552 *
2553 * 1) Find out the size of operand. If instruction
2554 * doesn't have one specified -- we're trying to
2555 * guess it either from template (IF_S* flag) or
2556 * from code bits.
2557 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002558 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002559 * template has an operand size specified AND this size differ
2560 * from which instruction has (perhaps we got it from code bits)
2561 * we are:
2562 * a) Check that only size of instruction and operand is differ
2563 * other characteristics do match
2564 * b) Perhaps it's a register specified in instruction so
2565 * for such a case we just mark that operand as "size
2566 * missing" and this will turn on fuzzy operand size
2567 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002568 */
2569 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002570 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002571 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002572 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002573 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002574 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002575 opflags_t template_opsize, insn_opsize;
2576
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002577 if (!(type & SIZE_MASK))
2578 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002579
Jin Kyu Song7903c072013-10-30 03:00:12 -07002580 insn_opsize = type & SIZE_MASK;
2581 if (!is_broadcast) {
2582 template_opsize = itemp->opd[i] & SIZE_MASK;
2583 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002584 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2585
2586 if (~ideco & BRDCAST_MASK)
2587 return MERR_BRNOTHERE;
2588
Jin Kyu Song7903c072013-10-30 03:00:12 -07002589 /*
2590 * when broadcasting, the element size depends on
2591 * the instruction type. decorator flag should match.
2592 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002593 if (deco_brsize) {
2594 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002595 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002596 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002597 } else {
2598 template_opsize = 0;
2599 }
2600 }
2601
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002602 if (~ideco & deco & OPMASK_MASK)
2603 return MERR_MASKNOTHERE;
2604
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002605 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2606 return MERR_DECONOTHERE;
2607
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002608 if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002609 return MERR_INVALOP;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002610
2611 if (~itemp->opd[i] & type & REGSET_MASK)
2612 return (itemp->opd[i] & REGSET_MASK)
2613 ? MERR_REGSETSIZE : MERR_REGSET;
2614
2615 if (template_opsize) {
Jin Kyu Song7903c072013-10-30 03:00:12 -07002616 if (template_opsize != insn_opsize) {
2617 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002618 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002619 } else if (!is_class(REGISTER, type)) {
2620 /*
2621 * Note: we don't honor extrinsic operand sizes for registers,
2622 * so "missing operand size" for a register should be
2623 * considered a wildcard match rather than an error.
2624 */
2625 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002626 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002627 } else if (is_broadcast &&
2628 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002629 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002630 /*
2631 * broadcasting opsize matches but the number of repeated memory
2632 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002633 * if 64b double precision float is broadcasted to ymm (256b),
2634 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002635 */
2636 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002637 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002638 }
2639 }
2640
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002641 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002642 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002643
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002644 /*
2645 * Check operand sizes
2646 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002647 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2648 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002649 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002650 asize = itemp->opd[i] & SIZE_MASK;
2651 if (asize) {
2652 for (i = 0; i < oprs; i++)
2653 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002654 break;
2655 }
2656 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002657 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002658 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002659 }
2660
Keith Kaniosb7a89542007-04-12 02:40:54 +00002661 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002662 if (!(itemp->opd[i] & SIZE_MASK) &&
2663 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002664 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002665 }
2666
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002667 /*
2668 * Check template is okay at the set cpu level
2669 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002670 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002671 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002672
Keith Kaniosb7a89542007-04-12 02:40:54 +00002673 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002674 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002675 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002676 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002677 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002679 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002680 * If we have a HLE prefix, look for the NOHLE flag
2681 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002682 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002683 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2684 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2685 return MERR_BADHLE;
2686
2687 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002688 * Check if special handling needed for Jumps
2689 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002690 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002691 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002692
Jin Kyu Song03041092013-10-15 19:38:51 -07002693 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002694 * Check if BND prefix is allowed.
2695 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002696 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002697 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002698 (has_prefix(instruction, PPS_REP, P_BND) ||
2699 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002700 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002701 else if (itemp_has(itemp, IF_BND) &&
2702 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2703 has_prefix(instruction, PPS_REP, P_REPNZ)))
2704 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002705
H. Peter Anvin60926242009-07-26 16:25:38 -07002706 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002707}
2708
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002709/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002710 * Check if ModR/M.mod should/can be 01.
2711 * - EAF_BYTEOFFS is set
2712 * - offset can fit in a byte when EVEX is not used
2713 * - offset can be compressed when EVEX is used
2714 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002715#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2716 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2717 is_disp8n(input, ins, &output->disp8) : \
2718 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2719 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002720
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002721static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002722 int rfield, opflags_t rflags, insn *ins,
2723 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002724{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002725 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002726 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002727 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002728
H. Peter Anvin8f622462017-04-02 19:02:29 -07002729 *errmsg = "invalid effective address"; /* Default error message */
2730
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002731 output->type = EA_SCALAR;
2732 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002733 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002734
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002735 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002736 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002737 /* EVEX.R' flag for the REG operand */
2738 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002739
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002740 if (is_class(REGISTER, input->type)) {
2741 /*
2742 * It's a direct register.
2743 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002744 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002745 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002746
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002747 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002748 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002749
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002750 /* broadcasting is not available with a direct register operand. */
2751 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002752 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002753 goto err;
2754 }
2755
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002756 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002757 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002758 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002759 output->bytes = 0; /* no offset necessary either */
2760 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2761 } else {
2762 /*
2763 * It's a memory reference.
2764 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002765
2766 /* Embedded rounding or SAE is not available with a mem ref operand. */
2767 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002768 *errmsg = "embedded rounding is available only with "
2769 "register-register operations";
2770 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002771 }
2772
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002773 if (input->basereg == -1 &&
2774 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002775 /*
2776 * It's a pure offset.
2777 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002778 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002779 if (input->segment == NO_SEG ||
2780 (input->opflags & OPFLAG_RELATIVE)) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08002781 nasm_warn(WARN_OTHER|ERR_PASS2, "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002782 input->type &= ~IP_REL;
2783 input->type |= MEMORY;
2784 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002785 }
2786
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002787 if (bits == 64 &&
2788 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002789 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002790 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002791 }
2792
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002793 if (eaflags & EAF_BYTEOFFS ||
2794 (eaflags & EAF_WORDOFFS &&
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002795 input->disp_size != (addrbits != 16 ? 32 : 16)))
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002796 nasm_warn(WARN_OTHER, "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002797
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002798 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002799 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002800 output->sib = GEN_SIB(0, 4, 5);
2801 output->bytes = 4;
2802 output->modrm = GEN_MODRM(0, rfield, 4);
2803 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002804 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002805 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002806 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002807 output->modrm = GEN_MODRM(0, rfield,
2808 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002809 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002810 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002811 } else {
2812 /*
2813 * It's an indirection.
2814 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002816 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002818 int t, it, bt; /* register numbers */
2819 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002820
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 if (s == 0)
2822 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002823
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002824 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002825 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002826 ix = nasm_reg_flags[i];
2827 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002828 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002829 ix = 0;
2830 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002831
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002832 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002833 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002834 bx = nasm_reg_flags[b];
2835 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002836 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002837 bx = 0;
2838 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002839
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002840 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002841 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002842 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002843 int32_t o = input->offset;
2844 int mod, scale, index, base;
2845
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002846 /*
2847 * For a vector SIB, one has to be a vector and the other,
2848 * if present, a GPR. The vector must be the index operand.
2849 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002850 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002851 if (s == 0)
2852 s = 1;
2853 else if (s != 1)
2854 goto err;
2855
2856 t = bt, bt = it, it = t;
2857 x = bx, bx = ix, ix = x;
2858 }
2859
2860 if (bt != -1) {
2861 if (REG_GPR & ~bx)
2862 goto err;
2863 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2864 sok &= bx;
2865 else
2866 goto err;
2867 }
2868
2869 /*
2870 * While we're here, ensure the user didn't specify
2871 * WORD or QWORD
2872 */
2873 if (input->disp_size == 16 || input->disp_size == 64)
2874 goto err;
2875
2876 if (addrbits == 16 ||
2877 (addrbits == 32 && !(sok & BITS32)) ||
2878 (addrbits == 64 && !(sok & BITS64)))
2879 goto err;
2880
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002881 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2882 : ((ix & YMMREG & ~REG_EA)
2883 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002884
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002885 output->rex |= rexflags(it, ix, REX_X);
2886 output->rex |= rexflags(bt, bx, REX_B);
2887 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002888
2889 index = it & 7; /* it is known to be != -1 */
2890
2891 switch (s) {
2892 case 1:
2893 scale = 0;
2894 break;
2895 case 2:
2896 scale = 1;
2897 break;
2898 case 4:
2899 scale = 2;
2900 break;
2901 case 8:
2902 scale = 3;
2903 break;
2904 default: /* then what the smeg is it? */
2905 goto err; /* panic */
2906 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002907
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002908 if (bt == -1) {
2909 base = 5;
2910 mod = 0;
2911 } else {
2912 base = (bt & 7);
2913 if (base != REG_NUM_EBP && o == 0 &&
2914 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002915 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002916 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002917 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002918 mod = 1;
2919 else
2920 mod = 2;
2921 }
2922
2923 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002924 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2925 output->modrm = GEN_MODRM(mod, rfield, 4);
2926 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002927 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002928 /*
2929 * it must be a 32/64-bit memory reference. Firstly we have
2930 * to check that all registers involved are type E/Rxx.
2931 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002932 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002933 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002934
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002935 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002936 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2937 sok &= ix;
2938 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002939 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002940 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002941
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002942 if (bt != -1) {
2943 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002944 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002945 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002946 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002947 sok &= bx;
2948 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002949
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002950 /*
2951 * While we're here, ensure the user didn't specify
2952 * WORD or QWORD
2953 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002954 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002955 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002956
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002957 if (addrbits == 16 ||
2958 (addrbits == 32 && !(sok & BITS32)) ||
2959 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002960 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002961
Keith Kaniosb7a89542007-04-12 02:40:54 +00002962 /* now reorganize base/index */
2963 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002964 ((hb == b && ht == EAH_NOTBASE) ||
2965 (hb == i && ht == EAH_MAKEBASE))) {
2966 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002967 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002968 x = bx, bx = ix, ix = x;
2969 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002970
Jin Kyu Song164d6072013-10-15 19:10:13 -07002971 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002972 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002973 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002974 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002975 if (eaflags & EAF_MIB) {
2976 /* only for mib operands */
2977 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2978 /*
2979 * make a single reg index [reg*1].
2980 * gas uses this form for an explicit index register.
2981 */
2982 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2983 }
2984 if ((ht == EAH_SUMMED) && bt == -1) {
2985 /* separate once summed index into [base, index] */
2986 bt = it, bx = ix, s--;
2987 }
2988 } else {
2989 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002990 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002991 s == 3 || s == 5 || s == 9) && bt == -1) {
2992 /* convert 3*EAX to EAX+2*EAX */
2993 bt = it, bx = ix, s--;
2994 }
2995 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002996 (eaflags & EAF_TIMESTWO) &&
2997 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002998 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002999 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003000 * to sib format with 0x0 displacement - [EAX*1+0].
3001 */
3002 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
3003 }
3004 }
Keith Kanios48af1772007-08-17 07:37:52 +00003005 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003006 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003007 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003008 x = ix, ix = bx, bx = x;
3009 }
3010 if (it == REG_NUM_ESP ||
3011 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003012 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003013
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003014 output->rex |= rexflags(it, ix, REX_X);
3015 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003016
Keith Kanios48af1772007-08-17 07:37:52 +00003017 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003018 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003019 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07003020
Keith Kaniosb7a89542007-04-12 02:40:54 +00003021 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003022 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003023 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003024 } else {
3025 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003026 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003027 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003028 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00003029 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003030 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00003031 mod = 1;
3032 else
3033 mod = 2;
3034 }
H. Peter Anvinea838272002-04-30 20:51:53 +00003035
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003036 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003037 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
3038 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003039 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003040 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07003042
Keith Kaniosb7a89542007-04-12 02:40:54 +00003043 if (it == -1)
3044 index = 4, s = 1;
3045 else
3046 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07003047
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 switch (s) {
3049 case 1:
3050 scale = 0;
3051 break;
3052 case 2:
3053 scale = 1;
3054 break;
3055 case 4:
3056 scale = 2;
3057 break;
3058 case 8:
3059 scale = 3;
3060 break;
3061 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003062 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003063 }
H. Peter Anvin70653092007-10-19 14:42:29 -07003064
Keith Kaniosb7a89542007-04-12 02:40:54 +00003065 if (bt == -1) {
3066 base = 5;
3067 mod = 0;
3068 } else {
3069 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003070 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003071 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003072 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00003073 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003074 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00003075 mod = 1;
3076 else
3077 mod = 2;
3078 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003079
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003080 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003081 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
3082 output->modrm = GEN_MODRM(mod, rfield, 4);
3083 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003084 }
3085 } else { /* it's 16-bit */
3086 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003087 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07003088
Keith Kaniosb7a89542007-04-12 02:40:54 +00003089 /* check for 64-bit long mode */
3090 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003091 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003092
H. Peter Anvine2c80182005-01-15 22:15:51 +00003093 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003094 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
3095 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003096 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003097
Keith Kaniosb7a89542007-04-12 02:40:54 +00003098 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003099 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003100 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003101
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003103 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003104 if (b == -1 && i != -1) {
3105 int tmp = b;
3106 b = i;
3107 i = tmp;
3108 } /* swap */
3109 if ((b == R_SI || b == R_DI) && i != -1) {
3110 int tmp = b;
3111 b = i;
3112 i = tmp;
3113 }
3114 /* have BX/BP as base, SI/DI index */
3115 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003116 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003117 if (i != -1 && b != -1 &&
3118 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003119 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003120 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003121 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003122
H. Peter Anvine2c80182005-01-15 22:15:51 +00003123 rm = -1;
3124 if (i != -1)
3125 switch (i * 256 + b) {
3126 case R_SI * 256 + R_BX:
3127 rm = 0;
3128 break;
3129 case R_DI * 256 + R_BX:
3130 rm = 1;
3131 break;
3132 case R_SI * 256 + R_BP:
3133 rm = 2;
3134 break;
3135 case R_DI * 256 + R_BP:
3136 rm = 3;
3137 break;
3138 } else
3139 switch (b) {
3140 case R_SI:
3141 rm = 4;
3142 break;
3143 case R_DI:
3144 rm = 5;
3145 break;
3146 case R_BP:
3147 rm = 6;
3148 break;
3149 case R_BX:
3150 rm = 7;
3151 break;
3152 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003153 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003154 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003155
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003156 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003157 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003158 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003159 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00003160 mod = 1;
3161 else
3162 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003163
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003164 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003165 output->bytes = mod; /* bytes of offset needed */
3166 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003167 }
3168 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003169 }
H. Peter Anvin70653092007-10-19 14:42:29 -07003170
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003171 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003172 return output->type;
3173
3174err:
3175 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003176}
3177
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003178static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003179{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003180 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003181 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003182
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003183 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003184
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003185 switch (ins->prefixes[PPS_ASIZE]) {
3186 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003187 valid &= 16;
3188 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003189 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003190 valid &= 32;
3191 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003192 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003193 valid &= 64;
3194 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003195 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003196 valid &= (addrbits == 32) ? 16 : 32;
3197 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003198 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003199 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003200 }
3201
3202 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003203 if (is_class(MEMORY, ins->oprs[j].type)) {
3204 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07003205
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003206 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003207 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003208 i = 0;
3209 else
3210 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003211
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003212 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003213 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003214 b = 0;
3215 else
3216 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003217
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003218 if (ins->oprs[j].scale == 0)
3219 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003220
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003221 if (!i && !b) {
3222 int ds = ins->oprs[j].disp_size;
3223 if ((addrbits != 64 && ds > 8) ||
3224 (addrbits == 64 && ds == 16))
3225 valid &= ds;
3226 } else {
3227 if (!(REG16 & ~b))
3228 valid &= 16;
3229 if (!(REG32 & ~b))
3230 valid &= 32;
3231 if (!(REG64 & ~b))
3232 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07003233
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003234 if (!(REG16 & ~i))
3235 valid &= 16;
3236 if (!(REG32 & ~i))
3237 valid &= 32;
3238 if (!(REG64 & ~i))
3239 valid &= 64;
3240 }
3241 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003242 }
3243
3244 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003245 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003246 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003247 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04003248 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003249 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003250 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003251 /* Impossible... */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03003252 nasm_nonfatal("impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003253 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003254 }
3255
3256 defdisp = ins->addr_size == 16 ? 16 : 32;
3257
3258 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003259 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
3260 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
3261 /*
3262 * mem_offs sizes must match the address size; if not,
3263 * strip the MEM_OFFS bit and match only EA instructions
3264 */
3265 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
3266 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003267 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003268}