blob: 49faa6b867fa57a3e4a12364092830c22fcd7d95 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvin (Intel)0d4ce8d2020-07-09 21:10:42 -07003 * Copyright 1996-2020 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,
H. Peter Anvin (Intel)0d4ce8d2020-07-09 21:10:42 -0700404 * let the debug backend know. Some backends really don't
405 * like being given a NULL filename as can happen if we
406 * use -Lb and expand a macro, so filter out that case.
Frank Kotlerabebb082003-09-06 04:45:37 +0000407 */
H. Peter Anvin (Intel)2f171dd2020-07-09 20:03:55 -0700408 data->where = src_where();
H. Peter Anvin (Intel)0d4ce8d2020-07-09 21:10:42 -0700409 if (data->where.filename &&
410 (!src_location_same(data->where, dbg.where) |
411 (data->segment != dbg.segment))) {
H. Peter Anvin (Intel)2f171dd2020-07-09 20:03:55 -0700412 dbg.where = data->where;
413 dbg.segment = data->segment;
414 dfmt->linenum(dbg.where.filename, dbg.where.lineno, data->segment);
415 }
H. Peter Anvineba20a72002-04-30 20:53:55 +0000416
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700417 if (asize > amax) {
418 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300419 nasm_nonfatal("%u-bit signed relocation unsupported by output format %s",
420 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800421 } else {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800422 /*!
423 *!zext-reloc [on] relocation zero-extended to match output format
424 *! warns that a relocation has been zero-extended due
425 *! to limitations in the output format.
426 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800427 nasm_warn(WARN_ZEXT_RELOC,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700428 "%u-bit %s relocation zero-extended from %u bits",
429 (unsigned int)(asize << 3),
430 data->type == OUT_SEGMENT ? "segment" : "unsigned",
431 (unsigned int)(amax << 3));
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800432 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700433 zeropad = data->size - amax;
434 data->size = amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800435 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700436 lfmt->output(data);
H. Peter Anvined859f72018-06-15 00:03:53 -0700437
438 if (likely(data->segment != NO_SEG)) {
439 ofmt->output(data);
440 } else {
441 /* Outputting to ABSOLUTE section - only reserve is permitted */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300442 if (data->type != OUT_RESERVE)
443 nasm_nonfatal("attempt to assemble code in [ABSOLUTE] space");
H. Peter Anvined859f72018-06-15 00:03:53 -0700444 /* No need to push to the backend */
445 }
446
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700447 data->offset += data->size;
448 data->insoffs += data->size;
449
450 if (zeropad) {
451 data->type = OUT_ZERODATA;
452 data->size = zeropad;
453 lfmt->output(data);
454 ofmt->output(data);
455 data->offset += zeropad;
456 data->insoffs += zeropad;
457 data->size += zeropad; /* Restore original size value */
458 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000459}
460
H. Peter Anvina77692b2016-09-20 14:04:33 -0700461static inline void out_rawdata(struct out_data *data, const void *rawdata,
462 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400463{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700464 data->type = OUT_RAWDATA;
465 data->data = rawdata;
466 data->size = size;
467 out(data);
468}
469
470static void out_rawbyte(struct out_data *data, uint8_t byte)
471{
472 data->type = OUT_RAWDATA;
473 data->data = &byte;
474 data->size = 1;
475 out(data);
476}
477
478static inline void out_reserve(struct out_data *data, uint64_t size)
479{
480 data->type = OUT_RESERVE;
481 data->size = size;
482 out(data);
483}
484
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700485static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700486{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700487 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300488 nasm_nonfatal("segment references cannot be relative");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700489
490 data->type = OUT_SEGMENT;
491 data->sign = OUT_UNSIGNED;
492 data->size = 2;
493 data->toffset = opx->offset;
494 data->tsegment = ofmt->segbase(opx->segment | 1);
495 data->twrt = opx->wrt;
496 out(data);
497}
498
499static void out_imm(struct out_data *data, const struct operand *opx,
500 int size, enum out_sign sign)
501{
502 if (opx->segment != NO_SEG && (opx->segment & 1)) {
503 /*
504 * This is actually a segment reference, but eval() has
505 * already called ofmt->segbase() for us. Sigh.
506 */
507 if (size < 2)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300508 nasm_nonfatal("segment reference must be 16 bits");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700509
510 data->type = OUT_SEGMENT;
511 } else {
512 data->type = (opx->opflags & OPFLAG_RELATIVE)
513 ? OUT_RELADDR : OUT_ADDRESS;
514 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700515 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700516 data->toffset = opx->offset;
517 data->tsegment = opx->segment;
518 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800519 /*
520 * XXX: improve this if at some point in the future we can
521 * distinguish the subtrahend in expressions like [foo - bar]
522 * where bar is a symbol in the current segment. However, at the
523 * current point, if OPFLAG_RELATIVE is set that subtraction has
524 * already occurred.
525 */
526 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700527 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700528 out(data);
529}
530
H. Peter Anvin164d2462017-02-20 02:39:56 -0800531static void out_reladdr(struct out_data *data, const struct operand *opx,
532 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700533{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800534 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300535 nasm_nonfatal("invalid use of self-relative expression");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800536
H. Peter Anvina77692b2016-09-20 14:04:33 -0700537 data->type = OUT_RELADDR;
538 data->sign = OUT_SIGNED;
539 data->size = size;
540 data->toffset = opx->offset;
541 data->tsegment = opx->segment;
542 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800543 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700544 out(data);
545}
546
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700547static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800548 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800550 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800551 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000552 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800553 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000554
H. Peter Anvin755f5212012-02-25 11:41:34 -0800555 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700556 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300557 if (!optimizing.level || (optimizing.flag & OPTIM_DISABLE_JMP_MATCH))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400558 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300559 if (optimizing.level < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400560 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700561
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800562 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100563
Victor van den Elzen154e5922009-02-25 17:32:00 +0100564 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100565 /* Be optimistic in pass 1 */
566 return true;
567
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700569 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000570
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700571 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800572 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
573
574 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
575 /* jmp short (opcode eb) cannot be used with bnd prefix. */
576 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800577 /*!
578 *!bnd [on] invalid BND prefixes
579 *! warns about ineffective use of the \c{BND} prefix when the
580 *! \c{JMP} instruction is converted to the \c{SHORT} form.
581 *! This should be extremely rare since the short \c{JMP} only
582 *! is applicable to jumps inside the same module, but if
583 *! it is legitimate, it may be necessary to use
H. Peter Anvin959702b2019-06-06 20:56:50 -0700584 *! \c{bnd jmp dword}.
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800585 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800586 nasm_warn(WARN_BND | ERR_PASS2 ,
H. Peter Anvin959702b2019-06-06 20:56:50 -0700587 "jmp short does not init bnd regs - bnd prefix dropped");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800588 }
589
590 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000592
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700593static inline int64_t merge_resb(insn *ins, int64_t isize)
594{
595 int nbytes = resb_bytes(ins->opcode);
596
597 if (likely(!nbytes))
598 return isize;
599
600 if (isize != nbytes * ins->oprs[0].offset)
601 return isize; /* Has prefixes of some sort */
602
603 ins->oprs[0].offset *= ins->times;
604 isize *= ins->times;
605 ins->times = 1;
606 return isize;
607}
608
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700609/* This must be handle non-power-of-2 alignment values */
610static inline size_t pad_bytes(size_t len, size_t align)
611{
612 size_t partial = len % align;
613 return partial ? align - partial : 0;
614}
615
616static void out_eops(struct out_data *data, const extop *e)
617{
618 while (e) {
619 size_t dup = e->dup;
620
621 switch (e->type) {
622 case EOT_NOTHING:
623 break;
624
625 case EOT_EXTOP:
626 while (dup--)
627 out_eops(data, e->val.subexpr);
628 break;
629
630 case EOT_DB_NUMBER:
631 if (e->elem > 8) {
632 nasm_nonfatal("integer supplied as %d-bit data",
633 e->elem << 3);
634 } else {
635 while (dup--) {
636 data->insoffs = 0;
637 data->inslen = data->size = e->elem;
638 data->tsegment = e->val.num.segment;
639 data->toffset = e->val.num.offset;
640 data->twrt = e->val.num.wrt;
641 data->relbase = 0;
642 if (e->val.num.segment != NO_SEG &&
643 (e->val.num.segment & 1)) {
644 data->type = OUT_SEGMENT;
645 data->sign = OUT_UNSIGNED;
646 } else {
647 data->type = e->val.num.relative
648 ? OUT_RELADDR : OUT_ADDRESS;
649 data->sign = OUT_WRAP;
650 }
651 out(data);
652 }
653 }
654 break;
655
656 case EOT_DB_FLOAT:
657 case EOT_DB_STRING:
658 case EOT_DB_STRING_FREE:
659 {
660 size_t pad, len;
661
662 pad = pad_bytes(e->val.string.len, e->elem);
663 len = e->val.string.len + pad;
664
665 while (dup--) {
666 data->insoffs = 0;
667 data->inslen = len;
668 out_rawdata(data, e->val.string.data, e->val.string.len);
669 if (pad)
670 out_rawdata(data, zero_buffer, pad);
671 }
672 break;
673 }
674
675 case EOT_DB_RESERVE:
676 data->insoffs = 0;
677 data->inslen = dup * e->elem;
678 out_reserve(data, data->inslen);
679 break;
680 }
681
682 e = e->next;
683 }
684}
685
H. Peter Anvin04445362016-09-21 15:56:19 -0700686/* This is totally just a wild guess what is reasonable... */
687#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
688
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800689int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000690{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700691 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000692 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700693 enum match_result m;
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700694
695 if (instruction->opcode == I_none)
696 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000697
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700698 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700699 data.offset = start;
700 data.segment = segment;
701 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700702 data.bits = bits;
703
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700704 if (opcode_is_db(instruction->opcode)) {
705 out_eops(&data, instruction->eops);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700706 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700707 const char *fname = instruction->eops->val.string.data;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000708 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700709 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700710 off_t base = 0;
711 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700712 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700713 char *buf = NULL;
714 size_t blk = 0; /* Buffered I/O block size */
715 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000716
H. Peter Anvin94ead272017-09-27 15:22:23 -0700717 if (!t)
718 goto done;
719
H. Peter Anvind81a2352016-09-21 14:03:18 -0700720 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400721 if (!fp) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300722 nasm_nonfatal("`incbin': unable to open file `%s'",
723 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700724 goto done;
725 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000726
H. Peter Anvind81a2352016-09-21 14:03:18 -0700727 len = nasm_file_size(fp);
728
729 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300730 nasm_nonfatal("`incbin': unable to get length of file `%s'",
731 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700732 goto close_done;
733 }
734
H. Peter Anvina77692b2016-09-20 14:04:33 -0700735 if (instruction->eops->next) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700736 base = instruction->eops->next->val.num.offset;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700737 if (base >= len) {
738 len = 0;
739 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000740 len -= base;
741 if (instruction->eops->next->next &&
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700742 len > (off_t)instruction->eops->next->next->val.num.offset)
743 len = (off_t)instruction->eops->next->next->val.num.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000744 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000745 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700746
H. Peter Anvina77692b2016-09-20 14:04:33 -0700747 lfmt->set_offset(data.offset);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700748 lfmt->uplevel(LIST_INCBIN, len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000749
H. Peter Anvind81a2352016-09-21 14:03:18 -0700750 if (!len)
751 goto end_incbin;
752
753 /* Try to map file data */
754 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700755 if (!map) {
756 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
757 buf = nasm_malloc(blk);
758 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700759
760 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700761 /*
762 * Consider these irrelevant for INCBIN, since it is fully
763 * possible that these might be (way) bigger than an int
764 * can hold; there is, however, no reason to widen these
765 * types just for INCBIN. data.inslen == 0 signals to the
766 * backend that these fields are meaningless, if at all
767 * needed.
768 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700769 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700770 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700771
H. Peter Anvind81a2352016-09-21 14:03:18 -0700772 if (map) {
773 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700774 } else if ((off_t)m == len) {
775 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700776 } else {
777 off_t l = len;
778
779 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300780 nasm_nonfatal("`incbin': unable to seek on file `%s'",
781 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700782 goto end_incbin;
783 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700784 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700785 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700786 if (!m || feof(fp)) {
787 /*
788 * This shouldn't happen unless the file
789 * actually changes while we are reading
790 * it.
791 */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300792 nasm_nonfatal("`incbin': unexpected EOF while"
793 " reading file `%s'", fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700794 goto end_incbin;
795 }
796 out_rawdata(&data, buf, m);
797 l -= m;
798 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700799 }
800 }
801 end_incbin:
802 lfmt->downlevel(LIST_INCBIN);
803 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700804 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700805 lfmt->downlevel(LIST_TIMES);
806 }
807 if (ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300808 nasm_nonfatal("`incbin': error while"
809 " reading file `%s'", fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700810 }
811 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700812 if (buf)
813 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700814 if (map)
815 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700816 fclose(fp);
817 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700818 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700819 ;
820 } else {
821 /* "Real" instruction */
822
823 /* Check to see if we need an address-size prefix */
824 add_asp(instruction, bits);
825
826 m = find_match(&temp, instruction, data.segment, data.offset, bits);
827
828 if (m == MOK_GOOD) {
829 /* Matches! */
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700830 if (unlikely(itemp_has(temp, IF_OBSOLETE))) {
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700831 errflags warning;
832 const char *whathappened;
833 const char *validity;
834 bool never = itemp_has(temp, IF_NEVER);
835
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700836 /*
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700837 * If IF_OBSOLETE is set, warn the user. Different
838 * warning classes for "obsolete but valid for this
839 * specific CPU" and "obsolete and gone."
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700840 *
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700841 *!obsolete-removed [on] instruction obsolete and removed on the target CPU
842 *! warns for an instruction which has been removed
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700843 *! from the architecture, and is no longer included
844 *! in the CPU definition given in the \c{[CPU]}
845 *! directive, for example \c{POP CS}, the opcode for
846 *! which, \c{0Fh}, instead is an opcode prefix on
847 *! CPUs newer than the first generation 8086.
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700848 *
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700849 *!obsolete-nop [on] instruction obsolete and is a noop on the target CPU
850 *! warns for an instruction which has been removed
851 *! from the architecture, but has been architecturally
852 *! defined to be a noop for future CPUs.
853 *
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700854 *!obsolete-valid [on] instruction obsolete but valid on the target CPU
855 *! warns for an instruction which has been removed
856 *! from the architecture, but is still valid on the
857 *! specific CPU given in the \c{CPU} directive. Code
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700858 *! using these instructions is most likely not
859 *! forward compatible.
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700860 */
861
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700862 whathappened = never ? "never implemented" : "obsolete";
863
864 if (!never && !iflag_cmp_cpu_level(&insns_flags[temp->iflag_idx], &cpu)) {
865 warning = WARN_OBSOLETE_VALID;
866 validity = "but valid on";
867 } else if (itemp_has(temp, IF_NOP)) {
868 warning = WARN_OBSOLETE_NOP;
869 validity = "and is a noop on";
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700870 } else {
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700871 warning = WARN_OBSOLETE_REMOVED;
H. Peter Anvin (Intel)fb118ae2019-08-09 15:01:28 -0700872 validity = never ? "and invalid on" : "and removed from";
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700873 }
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700874
875 nasm_warn(warning, "instruction %s %s the target CPU",
876 whathappened, validity);
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700877 }
878
H. Peter Anvina77692b2016-09-20 14:04:33 -0700879 data.itemp = temp;
880 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700881 data.insoffs = 0;
H. Peter Anvin (Intel)77335212019-08-06 23:22:48 -0700882
883 data.inslen = calcsize(data.segment, data.offset,
884 bits, instruction, temp);
885 nasm_assert(data.inslen >= 0);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700886 data.inslen = merge_resb(instruction, data.inslen);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700887
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700888 gencode(&data, instruction);
H. Peter Anvin (Intel)77335212019-08-06 23:22:48 -0700889 nasm_assert(data.insoffs == data.inslen);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700890 } else {
891 /* No match */
892 switch (m) {
893 case MERR_OPSIZEMISSING:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300894 nasm_nonfatal("operation size not specified");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700895 break;
896 case MERR_OPSIZEMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300897 nasm_nonfatal("mismatch in operand sizes");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700898 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700899 case MERR_BRNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300900 nasm_nonfatal("broadcast not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700901 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700902 case MERR_BRNUMMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300903 nasm_nonfatal("mismatch in the number of broadcasting elements");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700904 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700905 case MERR_MASKNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300906 nasm_nonfatal("mask not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700907 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700908 case MERR_DECONOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300909 nasm_nonfatal("unsupported mode decorator for instruction");
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700910 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700911 case MERR_BADCPU:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300912 nasm_nonfatal("no instruction for this cpu level");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700913 break;
914 case MERR_BADMODE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300915 nasm_nonfatal("instruction not supported in %d-bit mode", bits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700916 break;
917 case MERR_ENCMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300918 nasm_nonfatal("specific encoding scheme not available");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700919 break;
920 case MERR_BADBND:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300921 nasm_nonfatal("bnd prefix is not allowed");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700922 break;
923 case MERR_BADREPNE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300924 nasm_nonfatal("%s prefix is not allowed",
925 (has_prefix(instruction, PPS_REP, P_REPNE) ?
926 "repne" : "repnz"));
H. Peter Anvina77692b2016-09-20 14:04:33 -0700927 break;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700928 case MERR_REGSETSIZE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300929 nasm_nonfatal("invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700930 break;
931 case MERR_REGSET:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300932 nasm_nonfatal("register set not valid for operand");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700933 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700934 default:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300935 nasm_nonfatal("invalid combination of opcode and operands");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700936 break;
937 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700938
939 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400940 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000941 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700942 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000943}
944
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700945static int32_t eops_typeinfo(const extop *e)
H. Peter Anvin29651542018-12-18 19:14:40 -0800946{
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700947 int32_t typeinfo = 0;
H. Peter Anvin29651542018-12-18 19:14:40 -0800948
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700949 while (e) {
950 switch (e->type) {
951 case EOT_NOTHING:
952 break;
953
954 case EOT_EXTOP:
955 typeinfo |= eops_typeinfo(e->val.subexpr);
956 break;
957
958 case EOT_DB_FLOAT:
959 switch (e->elem) {
960 case 1: typeinfo |= TY_BYTE; break;
961 case 2: typeinfo |= TY_WORD; break;
962 case 4: typeinfo |= TY_FLOAT; break;
963 case 8: typeinfo |= TY_QWORD; break; /* double? */
964 case 10: typeinfo |= TY_TBYTE; break; /* long double? */
965 case 16: typeinfo |= TY_YWORD; break;
966 case 32: typeinfo |= TY_ZWORD; break;
967 default: break;
968 }
969 break;
970
971 default:
972 switch (e->elem) {
973 case 1: typeinfo |= TY_BYTE; break;
974 case 2: typeinfo |= TY_WORD; break;
975 case 4: typeinfo |= TY_DWORD; break;
976 case 8: typeinfo |= TY_QWORD; break;
977 case 10: typeinfo |= TY_TBYTE; break;
978 case 16: typeinfo |= TY_YWORD; break;
979 case 32: typeinfo |= TY_ZWORD; break;
980 default: break;
981 }
982 break;
983 }
984 e = e->next;
H. Peter Anvin29651542018-12-18 19:14:40 -0800985 }
986
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700987 return typeinfo;
988}
989
990static inline void debug_set_db_type(insn *instruction)
991{
992
993 int32_t typeinfo = TYS_ELEMENTS(instruction->operands);
994
995 typeinfo |= eops_typeinfo(instruction->eops);
H. Peter Anvin29651542018-12-18 19:14:40 -0800996 dfmt->debug_typevalue(typeinfo);
997}
998
999static void debug_set_type(insn *instruction)
1000{
1001 int32_t typeinfo;
1002
1003 if (opcode_is_resb(instruction->opcode)) {
1004 typeinfo = TYS_ELEMENTS(instruction->oprs[0].offset);
1005
1006 switch (instruction->opcode) {
1007 case I_RESB:
1008 typeinfo |= TY_BYTE;
1009 break;
1010 case I_RESW:
1011 typeinfo |= TY_WORD;
1012 break;
1013 case I_RESD:
1014 typeinfo |= TY_DWORD;
1015 break;
1016 case I_RESQ:
1017 typeinfo |= TY_QWORD;
1018 break;
1019 case I_REST:
1020 typeinfo |= TY_TBYTE;
1021 break;
1022 case I_RESO:
1023 typeinfo |= TY_OWORD;
1024 break;
1025 case I_RESY:
1026 typeinfo |= TY_YWORD;
1027 break;
1028 case I_RESZ:
1029 typeinfo |= TY_ZWORD;
1030 break;
1031 default:
1032 panic();
1033 }
1034 } else {
1035 typeinfo = TY_LABEL;
1036 }
1037
1038 dfmt->debug_typevalue(typeinfo);
1039}
1040
1041
1042/* Proecess an EQU directive */
1043static void define_equ(insn * instruction)
1044{
1045 if (!instruction->label) {
1046 nasm_nonfatal("EQU not preceded by label");
1047 } else if (instruction->operands == 1 &&
1048 (instruction->oprs[0].type & IMMEDIATE) &&
1049 instruction->oprs[0].wrt == NO_SEG) {
1050 define_label(instruction->label,
1051 instruction->oprs[0].segment,
1052 instruction->oprs[0].offset, false);
1053 } else if (instruction->operands == 2
1054 && (instruction->oprs[0].type & IMMEDIATE)
1055 && (instruction->oprs[0].type & COLON)
1056 && instruction->oprs[0].segment == NO_SEG
1057 && instruction->oprs[0].wrt == NO_SEG
1058 && (instruction->oprs[1].type & IMMEDIATE)
1059 && instruction->oprs[1].segment == NO_SEG
1060 && instruction->oprs[1].wrt == NO_SEG) {
1061 define_label(instruction->label,
1062 instruction->oprs[0].offset | SEG_ABS,
1063 instruction->oprs[1].offset, false);
1064 } else {
1065 nasm_nonfatal("bad syntax for EQU");
1066 }
1067}
1068
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001069static int64_t len_extops(const extop *e)
1070{
1071 int64_t isize = 0;
1072 size_t pad;
1073
1074 while (e) {
1075 switch (e->type) {
1076 case EOT_NOTHING:
1077 break;
1078
1079 case EOT_EXTOP:
1080 isize += e->dup * len_extops(e->val.subexpr);
1081 break;
1082
1083 case EOT_DB_STRING:
1084 case EOT_DB_STRING_FREE:
1085 case EOT_DB_FLOAT:
1086 pad = pad_bytes(e->val.string.len, e->elem);
1087 isize += e->dup * (e->val.string.len + pad);
1088 break;
1089
1090 case EOT_DB_NUMBER:
1091 warn_overflow_const(e->val.num.offset, e->elem);
1092 isize += e->dup * e->elem;
1093 break;
1094
1095 case EOT_DB_RESERVE:
1096 isize += e->dup;
1097 break;
1098 }
1099
1100 e = e->next;
1101 }
1102
1103 return isize;
1104}
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001105
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001106int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001107{
H. Peter Anvin3360d792007-09-11 04:16:57 +00001108 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001109 enum match_result m;
H. Peter Anvin29651542018-12-18 19:14:40 -08001110 int64_t isize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001111
H. Peter Anvin29651542018-12-18 19:14:40 -08001112 if (instruction->opcode == I_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 return 0;
H. Peter Anvin29651542018-12-18 19:14:40 -08001114 } else if (instruction->opcode == I_EQU) {
1115 define_equ(instruction);
1116 return 0;
1117 } else if (opcode_is_db(instruction->opcode)) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001118 isize = len_extops(instruction->eops);
H. Peter Anvin29651542018-12-18 19:14:40 -08001119 debug_set_db_type(instruction);
1120 return isize;
1121 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001122 const extop *e = instruction->eops;
1123 const char *fname = e->val.string.data;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001124 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001125
H. Peter Anvind81a2352016-09-21 14:03:18 -07001126 len = nasm_file_size_by_path(fname);
1127 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001128 nasm_nonfatal("`incbin': unable to get length of file `%s'",
1129 fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001130 return 0;
1131 }
1132
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001133 e = e->next;
1134 if (e) {
1135 if (len <= (off_t)e->val.num.offset) {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001136 len = 0;
1137 } else {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001138 len -= e->val.num.offset;
1139 e = e->next;
1140 if (e && len > (off_t)e->val.num.offset) {
1141 len = (off_t)e->val.num.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 }
1143 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001145
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001146 len *= instruction->times;
1147 instruction->times = 1; /* Tell the upper layer to not iterate */
1148
H. Peter Anvind81a2352016-09-21 14:03:18 -07001149 return len;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001150 } else {
H. Peter Anvin29651542018-12-18 19:14:40 -08001151 /* Normal instruction, or RESx */
1152
1153 /* Check to see if we need an address-size prefix */
1154 add_asp(instruction, bits);
1155
1156 m = find_match(&temp, instruction, segment, offset, bits);
1157 if (m != MOK_GOOD)
1158 return -1; /* No match */
1159
1160 isize = calcsize(segment, offset, bits, instruction, temp);
1161 debug_set_type(instruction);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001162 isize = merge_resb(instruction, isize);
H. Peter Anvin29651542018-12-18 19:14:40 -08001163
1164 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001165 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001166}
1167
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001168static void bad_hle_warn(const insn * ins, uint8_t hleok)
1169{
1170 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001171 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001172 static const enum whatwarn warn[2][4] =
1173 {
1174 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
1175 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
1176 };
1177 unsigned int n;
1178
1179 n = (unsigned int)rep_pfx - P_XACQUIRE;
1180 if (n > 1)
1181 return; /* Not XACQUIRE/XRELEASE */
1182
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001183 ww = warn[n][hleok];
1184 if (!is_class(MEMORY, ins->oprs[0].type))
1185 ww = w_inval; /* HLE requires operand 0 to be memory */
1186
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001187 /*!
1188 *!hle [on] invalid HLE prefixes
1189 *! warns about invalid use of the HLE \c{XACQUIRE} or \c{XRELEASE}
1190 *! prefixes.
1191 */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001192 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001193 case w_none:
1194 break;
1195
1196 case w_lock:
1197 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001198 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001199 "%s with this instruction requires lock",
1200 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001201 }
1202 break;
1203
1204 case w_inval:
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001205 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001206 "%s invalid with this instruction",
1207 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001208 break;
1209 }
1210}
1211
H. Peter Anvin507ae032008-10-09 15:37:10 -07001212/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +04001213#define case3(x) case (x): case (x)+1: case (x)+2
1214#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -07001215
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001216static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001217 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001218{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001219 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001220 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001221 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001222 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001223 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001224 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001225 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001226 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001227 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001228 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -07001229 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -07001230 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001231
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001232 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001233 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001234 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001235
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001236 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001237 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001238
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 (void)segment; /* Don't warn that this parameter is unused */
1240 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001241
H. Peter Anvin839eca22007-10-29 23:12:47 -07001242 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001243 c = *codes++;
1244 op1 = (c & 3) + ((opex & 1) << 2);
1245 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1246 opx = &ins->oprs[op1];
1247 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001248
H. Peter Anvin839eca22007-10-29 23:12:47 -07001249 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001250 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001251 codes += c, length += c;
1252 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001253
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001254 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001255 opex = c;
1256 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001257
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001258 case4(010):
1259 ins->rex |=
1260 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001261 codes++, length++;
1262 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001263
Jin Kyu Song164d6072013-10-15 19:10:13 -07001264 case4(014):
1265 /* this is an index reg of MIB operand */
1266 mib_index = opx->basereg;
1267 break;
1268
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001269 case4(020):
1270 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001271 length++;
1272 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001273
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001274 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001275 length += 2;
1276 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001277
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001278 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001279 if (opx->type & (BITS16 | BITS32 | BITS64))
1280 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 else
1282 length += (bits == 16) ? 2 : 4;
1283 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001284
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001285 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 length += 4;
1287 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001288
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001289 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001290 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001291 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001292
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001293 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 length++;
1295 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001296
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001297 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001298 length += 8; /* MOV reg64/imm */
1299 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001300
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001301 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 length += 2;
1303 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001304
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001305 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001306 if (opx->type & (BITS16 | BITS32 | BITS64))
1307 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 else
1309 length += (bits == 16) ? 2 : 4;
1310 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001311
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001312 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 length += 4;
1314 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001315
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001316 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001317 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001318 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001319
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001320 case 0172:
1321 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001322 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001323 length++;
1324 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001325
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001326 case4(0174):
1327 length++;
1328 break;
1329
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001330 case4(0240):
1331 ins->rex |= REX_EV;
1332 ins->vexreg = regval(opx);
1333 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1334 ins->vex_cm = *codes++;
1335 ins->vex_wlp = *codes++;
1336 ins->evex_tuple = (*codes++ - 0300);
1337 break;
1338
1339 case 0250:
1340 ins->rex |= REX_EV;
1341 ins->vexreg = 0;
1342 ins->vex_cm = *codes++;
1343 ins->vex_wlp = *codes++;
1344 ins->evex_tuple = (*codes++ - 0300);
1345 break;
1346
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001347 case4(0254):
1348 length += 4;
1349 break;
1350
1351 case4(0260):
1352 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001353 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001354 ins->vex_cm = *codes++;
1355 ins->vex_wlp = *codes++;
1356 break;
1357
1358 case 0270:
1359 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001360 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001361 ins->vex_cm = *codes++;
1362 ins->vex_wlp = *codes++;
1363 break;
1364
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001365 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001366 hleok = c & 3;
1367 break;
1368
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001369 case4(0274):
1370 length++;
1371 break;
1372
1373 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001374 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001375
H. Peter Anvine2c80182005-01-15 22:15:51 +00001376 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001377 if (bits == 64)
1378 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001379 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001380 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001381
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001383 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001385
H. Peter Anvine2c80182005-01-15 22:15:51 +00001386 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001387 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001388
Keith Kaniosb7a89542007-04-12 02:40:54 +00001389 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001390 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1391 has_prefix(ins, PPS_ASIZE, P_A32))
1392 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001394
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001395 case4(0314):
1396 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001397
H. Peter Anvine2c80182005-01-15 22:15:51 +00001398 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001399 {
1400 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1401 if (pfx == P_O16)
1402 break;
1403 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001404 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001405 else
1406 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001407 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001408 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001409
H. Peter Anvine2c80182005-01-15 22:15:51 +00001410 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001411 {
1412 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1413 if (pfx == P_O32)
1414 break;
1415 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001416 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001417 else
1418 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001419 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001420 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001421
H. Peter Anvine2c80182005-01-15 22:15:51 +00001422 case 0322:
1423 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001424
Keith Kaniosb7a89542007-04-12 02:40:54 +00001425 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001426 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001427 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001428
Keith Kaniosb7a89542007-04-12 02:40:54 +00001429 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001430 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001431 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001432
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001433 case 0325:
1434 ins->rex |= REX_NH;
1435 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001436
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001437 case 0326:
1438 break;
1439
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 case 0330:
1441 codes++, length++;
1442 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001443
H. Peter Anvine2c80182005-01-15 22:15:51 +00001444 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001445 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001446
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001447 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001448 case 0333:
1449 length++;
1450 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001451
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001452 case 0334:
1453 ins->rex |= REX_L;
1454 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001455
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001456 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001457 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001458
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001459 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001460 if (!ins->prefixes[PPS_REP])
1461 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001462 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001463
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001464 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001465 if (!ins->prefixes[PPS_REP])
1466 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001467 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001468
H. Peter Anvine2c80182005-01-15 22:15:51 +00001469 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001470 if (!absolute_op(&ins->oprs[0]))
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001471 nasm_nonfatal("attempt to reserve non-constant"
1472 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001473 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001474 nasm_warn(WARN_OTHER, "forward reference in RESx "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001475 "can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001476 else
H. Peter Anvin5358b982018-12-18 18:06:26 -08001477 length += ins->oprs[0].offset * resb_bytes(ins->opcode);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001478 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001479
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001480 case 0341:
1481 if (!ins->prefixes[PPS_WAIT])
1482 ins->prefixes[PPS_WAIT] = P_WAIT;
1483 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001484
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001485 case 0360:
1486 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001487
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001488 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001489 length++;
1490 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001491
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001492 case 0364:
1493 case 0365:
1494 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001495
Keith Kanios48af1772007-08-17 07:37:52 +00001496 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001497 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001498 length++;
1499 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001500
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001501 case 0370:
1502 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001504
H. Peter Anvine2c80182005-01-15 22:15:51 +00001505 case 0373:
1506 length++;
1507 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001508
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001509 case 0374:
1510 eat = EA_XMMVSIB;
1511 break;
1512
1513 case 0375:
1514 eat = EA_YMMVSIB;
1515 break;
1516
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001517 case 0376:
1518 eat = EA_ZMMVSIB;
1519 break;
1520
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001521 case4(0100):
1522 case4(0110):
1523 case4(0120):
1524 case4(0130):
1525 case4(0200):
1526 case4(0204):
1527 case4(0210):
1528 case4(0214):
1529 case4(0220):
1530 case4(0224):
1531 case4(0230):
1532 case4(0234):
1533 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001534 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001535 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001536 opflags_t rflags;
1537 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001538 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001539
Keith Kaniosb7a89542007-04-12 02:40:54 +00001540 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001541
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001542 if (c <= 0177) {
1543 /* pick rfield from operand b (opx) */
1544 rflags = regflag(opx);
1545 rfield = nasm_regvals[opx->basereg];
1546 } else {
1547 rflags = 0;
1548 rfield = c & 7;
1549 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001550
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001551 /* EVEX.b1 : evex_brerop contains the operand position */
1552 op_er_sae = (ins->evex_brerop >= 0 ?
1553 &ins->oprs[ins->evex_brerop] : NULL);
1554
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001555 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1556 /* set EVEX.b */
1557 ins->evex_p[2] |= EVEX_P2B;
1558 if (op_er_sae->decoflags & ER) {
1559 /* set EVEX.RC (rounding control) */
1560 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1561 & EVEX_P2RC;
1562 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001563 } else {
1564 /* set EVEX.L'L (vector length) */
1565 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001566 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001567 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001568 /* set EVEX.b */
1569 ins->evex_p[2] |= EVEX_P2B;
1570 }
1571 }
1572
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001573 if (itemp_has(temp, IF_MIB)) {
1574 opy->eaflags |= EAF_MIB;
1575 /*
1576 * if a separate form of MIB (ICC style) is used,
1577 * the index reg info is merged into mem operand
1578 */
1579 if (mib_index != R_none) {
1580 opy->indexreg = mib_index;
1581 opy->scale = 1;
1582 opy->hintbase = mib_index;
1583 opy->hinttype = EAH_NOTBASE;
1584 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001585 }
1586
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001587 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001588 rfield, rflags, ins, &errmsg) != eat) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001589 nasm_nonfatal("%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001591 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001592 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001594 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001595 }
1596 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001597
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001598 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001599 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001600 ": instruction code \\%o (0x%02X) given", c, c);
1601 break;
1602 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001603 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001604
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001605 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001606
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001607 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001608 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001609 nasm_nonfatal("instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001610 return -1;
1611 }
1612 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001613 }
1614
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001615 switch (ins->prefixes[PPS_VEX]) {
1616 case P_EVEX:
1617 if (!(ins->rex & REX_EV))
1618 return -1;
1619 break;
1620 case P_VEX3:
1621 case P_VEX2:
1622 if (!(ins->rex & REX_V))
1623 return -1;
1624 break;
1625 default:
1626 break;
1627 }
1628
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001629 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001630 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001631
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001632 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001633 nasm_nonfatal("cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001634 return -1;
1635 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001636 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001637 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001638 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001639 ins->rex &= ~REX_W;
1640 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001641 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001642 ins->rex |= REX_W;
1643 bad32 &= ~REX_W;
1644 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001645 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001646 /* Follow REX_W */
1647 break;
1648 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001649
H. Peter Anvinfc561202011-07-07 16:58:22 -07001650 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001651 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001653 } else if (!(ins->rex & REX_EV) &&
1654 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001655 nasm_nonfatal("invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001656 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001657 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001658 if (ins->rex & REX_EV)
1659 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001660 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1661 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001662 length += 3;
1663 else
1664 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001665 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001666 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001667 nasm_nonfatal("cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001668 return -1;
1669 } else if (bits == 64) {
1670 length++;
1671 } else if ((ins->rex & REX_L) &&
1672 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001673 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001674 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001675 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001676 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001677 length++;
1678 } else {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001679 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001680 return -1;
1681 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001682 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001683
1684 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001685 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001686 /*!
H. Peter Anvin (Intel)be99ebd2018-12-13 22:12:37 -08001687 *!lock [on] LOCK prefix on unlockable instructions
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001688 *! warns about \c{LOCK} prefixes on unlockable instructions.
1689 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001690 nasm_warn(WARN_LOCK | ERR_PASS2 , "instruction is not lockable");
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001691 }
1692
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001693 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001694
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001695 /*
1696 * when BND prefix is set by DEFAULT directive,
1697 * BND prefix is added to every appropriate instruction line
1698 * unless it is overridden by NOBND prefix.
1699 */
1700 if (globalbnd &&
1701 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1702 ins->prefixes[PPS_REP] = P_BND;
1703
H. Peter Anvina77692b2016-09-20 14:04:33 -07001704 /*
1705 * Add length of legacy prefixes
1706 */
1707 length += emit_prefix(NULL, bits, ins);
1708
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001709 return length;
1710}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001711
H. Peter Anvina77692b2016-09-20 14:04:33 -07001712static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001713{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001714 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001715 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001716 !(ins->rex & (REX_V | REX_EV)) &&
1717 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001718 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1719 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001720 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001721 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001722 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001723}
1724
H. Peter Anvina77692b2016-09-20 14:04:33 -07001725static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1726{
1727 int bytes = 0;
1728 int j;
1729
1730 for (j = 0; j < MAXPREFIX; j++) {
1731 uint8_t c = 0;
1732 switch (ins->prefixes[j]) {
1733 case P_WAIT:
1734 c = 0x9B;
1735 break;
1736 case P_LOCK:
1737 c = 0xF0;
1738 break;
1739 case P_REPNE:
1740 case P_REPNZ:
1741 case P_XACQUIRE:
1742 case P_BND:
1743 c = 0xF2;
1744 break;
1745 case P_REPE:
1746 case P_REPZ:
1747 case P_REP:
1748 case P_XRELEASE:
1749 c = 0xF3;
1750 break;
1751 case R_CS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001752 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001753 nasm_warn(WARN_OTHER|ERR_PASS2, "cs segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001754 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001755 c = 0x2E;
1756 break;
1757 case R_DS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001758 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001759 nasm_warn(WARN_OTHER|ERR_PASS2, "ds segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001760 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001761 c = 0x3E;
1762 break;
1763 case R_ES:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001764 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001765 nasm_warn(WARN_OTHER|ERR_PASS2, "es segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001766 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001767 c = 0x26;
1768 break;
1769 case R_FS:
1770 c = 0x64;
1771 break;
1772 case R_GS:
1773 c = 0x65;
1774 break;
1775 case R_SS:
1776 if (bits == 64) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001777 nasm_warn(WARN_OTHER|ERR_PASS2, "ss segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001778 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001779 }
1780 c = 0x36;
1781 break;
1782 case R_SEGR6:
1783 case R_SEGR7:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001784 nasm_nonfatal("segr6 and segr7 cannot be used as prefixes");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001785 break;
1786 case P_A16:
1787 if (bits == 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001788 nasm_nonfatal("16-bit addressing is not supported "
1789 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001790 } else if (bits != 16)
1791 c = 0x67;
1792 break;
1793 case P_A32:
1794 if (bits != 32)
1795 c = 0x67;
1796 break;
1797 case P_A64:
1798 if (bits != 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001799 nasm_nonfatal("64-bit addressing is only supported "
1800 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001801 }
1802 break;
1803 case P_ASP:
1804 c = 0x67;
1805 break;
1806 case P_O16:
1807 if (bits != 16)
1808 c = 0x66;
1809 break;
1810 case P_O32:
1811 if (bits == 16)
1812 c = 0x66;
1813 break;
1814 case P_O64:
1815 /* REX.W */
1816 break;
1817 case P_OSP:
1818 c = 0x66;
1819 break;
1820 case P_EVEX:
1821 case P_VEX3:
1822 case P_VEX2:
1823 case P_NOBND:
1824 case P_none:
1825 break;
1826 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001827 nasm_panic("invalid instruction prefix");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001828 }
1829 if (c) {
1830 if (data)
1831 out_rawbyte(data, c);
1832 bytes++;
1833 }
1834 }
1835 return bytes;
1836}
1837
1838static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001839{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001840 uint8_t c;
1841 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001842 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001843 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001844 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001845 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001846 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001847 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001848 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001849 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001850 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001851
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001852 ins->rex_done = false;
1853
H. Peter Anvina77692b2016-09-20 14:04:33 -07001854 emit_prefix(data, bits, ins);
1855
H. Peter Anvin839eca22007-10-29 23:12:47 -07001856 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001857 c = *codes++;
1858 op1 = (c & 3) + ((opex & 1) << 2);
1859 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1860 opx = &ins->oprs[op1];
1861 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001862
H. Peter Anvina77692b2016-09-20 14:04:33 -07001863
H. Peter Anvin839eca22007-10-29 23:12:47 -07001864 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 case 01:
1866 case 02:
1867 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001868 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001869 emit_rex(data, ins);
1870 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001873
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001874 case 05:
1875 case 06:
1876 case 07:
1877 opex = c;
1878 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001879
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001880 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001881 emit_rex(data, ins);
1882 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001883 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001884
Jin Kyu Song164d6072013-10-15 19:10:13 -07001885 case4(014):
1886 break;
1887
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001888 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001889 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001890 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001891
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001892 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001893 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001894 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001895
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001896 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001897 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001898 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001899
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001900 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001901 if (opx->type & (BITS16 | BITS32))
1902 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001903 else
1904 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001905 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001906 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001907
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001908 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001909 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001911
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001912 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001913 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001914 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001916
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001917 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001918 if (opx->segment == data->segment) {
1919 int64_t delta = opx->offset - data->offset
1920 - (data->inslen - data->insoffs);
1921 if (delta > 127 || delta < -128)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001922 nasm_nonfatal("short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001923 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001924 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001926
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001927 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001928 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001929 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001930
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001931 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001932 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001934
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001935 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001936 if (opx->type & (BITS16 | BITS32 | BITS64))
1937 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001938 else
1939 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001940
1941 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001943
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001944 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001945 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001947
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001948 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001949 if (opx->segment == NO_SEG)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001950 nasm_nonfatal("value referenced by FAR is not relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001951 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001952 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001953
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001954 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001955 {
1956 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1957 const struct operand *opy;
1958
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001959 c = *codes++;
1960 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001961 opy = &ins->oprs[c & 7];
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001962 if (!absolute_op(opy))
1963 nasm_nonfatal("non-absolute expression not permitted "
1964 "as argument %d", c & 7);
1965 else if (opy->offset & ~mask)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001966 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001967 "is4 argument exceeds bounds");
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001968 c = opy->offset & mask;
1969 goto emit_is4;
1970 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001971
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001972 case 0173:
1973 c = *codes++;
1974 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001975 c &= 15;
1976 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001977
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001978 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001979 c = 0;
1980 emit_is4:
1981 r = nasm_regvals[opx->basereg];
1982 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001983 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001984
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001985 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001986 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001987 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001988 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001989 "signed dword immediate exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001990 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001991 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001992 break;
1993
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001994 case4(0240):
1995 case 0250:
1996 codes += 3;
1997 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1998 EVEX_P2Z | EVEX_P2AAA, 2);
1999 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
2000 bytes[0] = 0x62;
2001 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002002 bytes[1] = ((((ins->rex & 7) << 5) |
2003 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07002004 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002005 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
2006 ((~ins->vexreg & 15) << 3) |
2007 (1 << 2) | (ins->vex_wlp & 3);
2008 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07002009 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002010 break;
2011
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002012 case4(0260):
2013 case 0270:
2014 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002015 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
2016 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002017 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
2018 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
2019 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07002020 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07002021 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002022 } else {
2023 bytes[0] = 0xc5;
2024 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07002025 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07002026 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002027 }
2028 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002029
H. Peter Anvine014f352012-02-25 22:35:19 -08002030 case 0271:
2031 case 0272:
2032 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08002033 break;
2034
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002035 case4(0274):
2036 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08002037 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002038 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07002039
H. Peter Anvin64e87d02017-03-01 13:45:02 -08002040 if (absolute_op(opx)) {
2041 if (ins->rex & REX_W)
2042 s = 64;
2043 else if (ins->prefixes[PPS_OSIZE] == P_O16)
2044 s = 16;
2045 else if (ins->prefixes[PPS_OSIZE] == P_O32)
2046 s = 32;
2047 else
2048 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07002049
H. Peter Anvin64e87d02017-03-01 13:45:02 -08002050 um = (uint64_t)2 << (s-1);
2051 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08002052
H. Peter Anvin64e87d02017-03-01 13:45:02 -08002053 if (uv > 127 && uv < (uint64_t)-128 &&
2054 (uv < um-128 || uv > um-1)) {
2055 /* If this wasn't explicitly byte-sized, warn as though we
2056 * had fallen through to the imm16/32/64 case.
2057 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08002058 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin64e87d02017-03-01 13:45:02 -08002059 "%s value exceeds bounds",
2060 (opx->type & BITS8) ? "signed byte" :
2061 s == 16 ? "word" :
2062 s == 32 ? "dword" :
2063 "signed dword");
2064 }
2065
2066 /* Output as a raw byte to avoid byte overflow check */
2067 out_rawbyte(data, (uint8_t)uv);
2068 } else {
2069 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002070 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07002071 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002072 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07002073
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002074 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002075 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002076
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002078 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
2079 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002080 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002081
H. Peter Anvine2c80182005-01-15 22:15:51 +00002082 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002083 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
2084 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002085 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002086
H. Peter Anvine2c80182005-01-15 22:15:51 +00002087 case 0312:
2088 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002089
Keith Kaniosb7a89542007-04-12 02:40:54 +00002090 case 0313:
Keith Kaniosb7a89542007-04-12 02:40:54 +00002091 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002092
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002093 case4(0314):
2094 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08002095
H. Peter Anvine2c80182005-01-15 22:15:51 +00002096 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002097 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002098 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002099
H. Peter Anvine2c80182005-01-15 22:15:51 +00002100 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07002101 case 0323:
2102 break;
2103
Keith Kaniosb7a89542007-04-12 02:40:54 +00002104 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002105 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002106 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07002107
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002108 case 0325:
2109 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07002110
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04002111 case 0326:
2112 break;
2113
H. Peter Anvine2c80182005-01-15 22:15:51 +00002114 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002115 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002116 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002117
H. Peter Anvine2c80182005-01-15 22:15:51 +00002118 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002119 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002120
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002121 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002122 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002123 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002124 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002125
Keith Kanios48af1772007-08-17 07:37:52 +00002126 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002127 if (ins->rex & REX_R)
2128 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00002129 ins->rex &= ~(REX_L|REX_R);
2130 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00002131
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002132 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002133 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002134
H. Peter Anvin962e3052008-08-28 17:47:16 -07002135 case 0336:
2136 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002137 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07002138
H. Peter Anvine2c80182005-01-15 22:15:51 +00002139 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002140 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002141 nasm_panic("non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07002142
H. Peter Anvin5358b982018-12-18 18:06:26 -08002143 out_reserve(data, ins->oprs[0].offset * resb_bytes(ins->opcode));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002144 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002145
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002146 case 0341:
2147 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08002148
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002149 case 0360:
2150 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002151
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002152 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002153 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002154 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002155
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002156 case 0364:
2157 case 0365:
2158 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002159
Keith Kanios48af1772007-08-17 07:37:52 +00002160 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002161 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002162 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00002163 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002164
Jin Kyu Song03041092013-10-15 19:38:51 -07002165 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002166 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002167
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002169 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002170 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002171
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002172 case 0374:
2173 eat = EA_XMMVSIB;
2174 break;
2175
2176 case 0375:
2177 eat = EA_YMMVSIB;
2178 break;
2179
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002180 case 0376:
2181 eat = EA_ZMMVSIB;
2182 break;
2183
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002184 case4(0100):
2185 case4(0110):
2186 case4(0120):
2187 case4(0130):
2188 case4(0200):
2189 case4(0204):
2190 case4(0210):
2191 case4(0214):
2192 case4(0220):
2193 case4(0224):
2194 case4(0230):
2195 case4(0234):
2196 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002197 ea ea_data;
2198 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002199 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002200 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002201 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07002202
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002203 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002204 /* pick rfield from operand b (opx) */
2205 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07002206 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002207 } else {
2208 /* rfield is constant */
2209 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002210 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002211 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002212
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002213 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002214 rfield, rflags, ins, &errmsg) != eat)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002215 nasm_nonfatal("%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07002216
H. Peter Anvine2c80182005-01-15 22:15:51 +00002217 p = bytes;
2218 *p++ = ea_data.modrm;
2219 if (ea_data.sib_present)
2220 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07002221 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002222
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002223 /*
2224 * Make sure the address gets the right offset in case
2225 * the line breaks in the .lst file (BR 1197827)
2226 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002227
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002228 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002229 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07002230 if (ea_data.disp8) {
2231 out_rawbyte(data, ea_data.disp8);
2232 } else if (ea_data.rip) {
2233 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002234 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002235 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002236
H. Peter Anvina77692b2016-09-20 14:04:33 -07002237 if (overflow_general(opy->offset, asize) ||
2238 signed_bits(opy->offset, ins->addr_size) !=
2239 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08002240 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002241
H. Peter Anvina77692b2016-09-20 14:04:33 -07002242 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07002243 (asize > ea_data.bytes)
2244 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002245 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002246 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002247 }
2248 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07002249
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002250 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07002251 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002252 ": instruction code \\%o (0x%02X) given", c, c);
2253 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002254 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07002255 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002256}
2257
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002258static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002259{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002260 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002261 nasm_panic("invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002262 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002263}
2264
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00002265static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002266{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002267 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002268 nasm_panic("invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002269 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002270}
2271
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002272static int op_rexflags(const operand * o, int mask)
2273{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002274 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002275 int val;
2276
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002277 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002278 nasm_panic("invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002279
H. Peter Anvina4835d42008-05-20 14:21:29 -07002280 flags = nasm_reg_flags[o->basereg];
2281 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002282
2283 return rexflags(val, flags, mask);
2284}
2285
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002286static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002287{
2288 int rex = 0;
2289
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002290 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002291 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002292 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002293 rex |= REX_W;
2294 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2295 rex |= REX_H;
2296 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2297 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002298
2299 return rex & mask;
2300}
2301
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002302static int evexflags(int val, decoflags_t deco,
2303 int mask, uint8_t byte)
2304{
2305 int evex = 0;
2306
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002307 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002308 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002309 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002310 evex |= (EVEX_P0RP | EVEX_P0X);
2311 break;
2312 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002313 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002314 evex |= EVEX_P2VP;
2315 if (deco & Z)
2316 evex |= EVEX_P2Z;
2317 if (deco & OPMASK_MASK)
2318 evex |= deco & EVEX_P2AAA;
2319 break;
2320 }
2321 return evex & mask;
2322}
2323
2324static int op_evexflags(const operand * o, int mask, uint8_t byte)
2325{
2326 int val;
2327
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002328 val = nasm_regvals[o->basereg];
2329
2330 return evexflags(val, o->decoflags, mask, byte);
2331}
2332
H. Peter Anvin23595f52009-07-25 17:44:25 -07002333static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002334 insn *instruction,
2335 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002336{
2337 const struct itemplate *temp;
2338 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002339 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002340 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002341 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002342 int i;
2343
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002344 /* broadcasting uses a different data element size */
2345 for (i = 0; i < instruction->operands; i++)
2346 if (i == broadcast)
2347 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2348 else
2349 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002350
2351 merr = MERR_INVALOP;
2352
2353 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002354 temp->opcode != I_none; temp++) {
2355 m = matches(temp, instruction, bits);
2356 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002357 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002358 m = MOK_GOOD;
2359 else
2360 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002361 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002362 /*
2363 * Missing operand size and a candidate for fuzzy matching...
2364 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002365 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002366 if (i == broadcast)
2367 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2368 else
2369 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002370 opsizemissing = true;
2371 }
2372 if (m > merr)
2373 merr = m;
2374 if (merr == MOK_GOOD)
2375 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002376 }
2377
2378 /* No match, but see if we can get a fuzzy operand size match... */
2379 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002380 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002381
2382 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002383 /*
2384 * We ignore extrinsic operand sizes on registers, so we should
2385 * never try to fuzzy-match on them. This also resolves the case
2386 * when we have e.g. "xmmrm128" in two different positions.
2387 */
2388 if (is_class(REGISTER, instruction->oprs[i].type))
2389 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002390
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002391 /* This tests if xsizeflags[i] has more than one bit set */
2392 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2393 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002394
Jin Kyu Song7903c072013-10-30 03:00:12 -07002395 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002396 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002397 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2398 BITS32 : BITS64);
2399 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002400 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002401 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002402 }
2403
2404 /* Try matching again... */
2405 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002406 temp->opcode != I_none; temp++) {
2407 m = matches(temp, instruction, bits);
2408 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002409 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002410 m = MOK_GOOD;
2411 else
2412 m = MERR_INVALOP;
2413 }
2414 if (m > merr)
2415 merr = m;
2416 if (merr == MOK_GOOD)
2417 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002418 }
2419
H. Peter Anvina81655b2009-07-25 18:15:28 -07002420done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002421 *tempp = temp;
2422 return merr;
2423}
2424
Mark Charneydcaef4b2014-10-09 13:45:17 -04002425static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2426{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002427 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002428 uint8_t brcast_num;
2429
Mark Charneydcaef4b2014-10-09 13:45:17 -04002430 if (brsize > BITS64)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002431 nasm_fatal("size of broadcasting element is greater than 64 bits");
Mark Charneydcaef4b2014-10-09 13:45:17 -04002432
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002433 /*
2434 * The shift term is to take care of the extra BITS80 inserted
2435 * between BITS64 and BITS128.
2436 */
2437 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2438 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002439
2440 return brcast_num;
2441}
2442
H. Peter Anvin65289e82009-07-25 17:25:11 -07002443static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002444 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002445{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002446 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002447 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002448 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002449
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002450 /*
2451 * Check the opcode
2452 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002453 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002454 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002455
2456 /*
2457 * Count the operands
2458 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002459 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002460 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002461
2462 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002463 * Is it legal?
2464 */
Chang S. Baea5786342018-08-15 23:22:21 +03002465 if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002466 return MERR_INVALOP;
2467
2468 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002469 * {evex} available?
2470 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002471 switch (instruction->prefixes[PPS_VEX]) {
2472 case P_EVEX:
2473 if (!itemp_has(itemp, IF_EVEX))
2474 return MERR_ENCMISMATCH;
2475 break;
2476 case P_VEX3:
2477 case P_VEX2:
2478 if (!itemp_has(itemp, IF_VEX))
2479 return MERR_ENCMISMATCH;
2480 break;
2481 default:
2482 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002483 }
2484
2485 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002486 * Check that no spurious colons or TOs are present
2487 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002488 for (i = 0; i < itemp->operands; i++)
2489 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002490 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002491
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002492 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002493 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002494 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002495 switch (itemp_smask(itemp)) {
2496 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002497 asize = BITS8;
2498 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002499 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002500 asize = BITS16;
2501 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002502 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002503 asize = BITS32;
2504 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002505 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002506 asize = BITS64;
2507 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002508 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002509 asize = BITS128;
2510 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002511 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002512 asize = BITS256;
2513 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002514 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002515 asize = BITS512;
2516 break;
H. Peter Anvin (Intel)02b60dd2019-08-14 15:23:00 -07002517 case IF_GENBIT(IF_ANYSIZE):
2518 asize = SIZE_MASK;
2519 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002520 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002521 switch (bits) {
2522 case 16:
2523 asize = BITS16;
2524 break;
2525 case 32:
2526 asize = BITS32;
2527 break;
2528 case 64:
2529 asize = BITS64;
2530 break;
2531 default:
2532 asize = 0;
2533 break;
2534 }
2535 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002536 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002537 asize = 0;
2538 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002539 }
2540
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002541 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002542 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002543 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002544 memset(size, 0, sizeof size);
2545 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002546 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002547 /* S- flags apply to all operands */
2548 for (i = 0; i < MAX_OPERANDS; i++)
2549 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002550 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002551
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002552 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002553 * Check that the operand flags all match up,
2554 * it's a bit tricky so lets be verbose:
2555 *
2556 * 1) Find out the size of operand. If instruction
2557 * doesn't have one specified -- we're trying to
2558 * guess it either from template (IF_S* flag) or
2559 * from code bits.
2560 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002561 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002562 * template has an operand size specified AND this size differ
2563 * from which instruction has (perhaps we got it from code bits)
2564 * we are:
2565 * a) Check that only size of instruction and operand is differ
2566 * other characteristics do match
2567 * b) Perhaps it's a register specified in instruction so
2568 * for such a case we just mark that operand as "size
2569 * missing" and this will turn on fuzzy operand size
2570 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002571 */
2572 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002573 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002574 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002575 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002576 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002577 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002578 opflags_t template_opsize, insn_opsize;
2579
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002580 if (!(type & SIZE_MASK))
2581 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002582
Jin Kyu Song7903c072013-10-30 03:00:12 -07002583 insn_opsize = type & SIZE_MASK;
2584 if (!is_broadcast) {
2585 template_opsize = itemp->opd[i] & SIZE_MASK;
2586 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002587 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2588
2589 if (~ideco & BRDCAST_MASK)
2590 return MERR_BRNOTHERE;
2591
Jin Kyu Song7903c072013-10-30 03:00:12 -07002592 /*
2593 * when broadcasting, the element size depends on
2594 * the instruction type. decorator flag should match.
2595 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002596 if (deco_brsize) {
2597 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002598 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002599 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002600 } else {
2601 template_opsize = 0;
2602 }
2603 }
2604
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002605 if (~ideco & deco & OPMASK_MASK)
2606 return MERR_MASKNOTHERE;
2607
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002608 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2609 return MERR_DECONOTHERE;
2610
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002611 if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002612 return MERR_INVALOP;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002613
2614 if (~itemp->opd[i] & type & REGSET_MASK)
2615 return (itemp->opd[i] & REGSET_MASK)
2616 ? MERR_REGSETSIZE : MERR_REGSET;
2617
2618 if (template_opsize) {
Jin Kyu Song7903c072013-10-30 03:00:12 -07002619 if (template_opsize != insn_opsize) {
2620 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002621 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002622 } else if (!is_class(REGISTER, type)) {
2623 /*
2624 * Note: we don't honor extrinsic operand sizes for registers,
2625 * so "missing operand size" for a register should be
2626 * considered a wildcard match rather than an error.
2627 */
2628 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002629 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002630 } else if (is_broadcast &&
2631 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002632 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002633 /*
2634 * broadcasting opsize matches but the number of repeated memory
2635 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002636 * if 64b double precision float is broadcasted to ymm (256b),
2637 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002638 */
2639 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002640 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002641 }
2642 }
2643
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002644 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002645 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002646
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002647 /*
2648 * Check operand sizes
2649 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002650 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2651 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002653 asize = itemp->opd[i] & SIZE_MASK;
2654 if (asize) {
2655 for (i = 0; i < oprs; i++)
2656 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002657 break;
2658 }
2659 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002660 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002661 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002662 }
2663
Keith Kaniosb7a89542007-04-12 02:40:54 +00002664 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002665 if (!(itemp->opd[i] & SIZE_MASK) &&
2666 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002667 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002668 }
2669
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002670 /*
2671 * Check template is okay at the set cpu level
2672 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002673 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002674 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002675
Keith Kaniosb7a89542007-04-12 02:40:54 +00002676 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002677 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002678 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002679 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002680 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002681
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002682 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002683 * If we have a HLE prefix, look for the NOHLE flag
2684 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002685 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002686 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2687 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2688 return MERR_BADHLE;
2689
2690 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002691 * Check if special handling needed for Jumps
2692 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002693 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002694 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002695
Jin Kyu Song03041092013-10-15 19:38:51 -07002696 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002697 * Check if BND prefix is allowed.
2698 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002699 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002700 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002701 (has_prefix(instruction, PPS_REP, P_BND) ||
2702 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002703 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002704 else if (itemp_has(itemp, IF_BND) &&
2705 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2706 has_prefix(instruction, PPS_REP, P_REPNZ)))
2707 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002708
H. Peter Anvin60926242009-07-26 16:25:38 -07002709 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002710}
2711
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002712/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002713 * Check if ModR/M.mod should/can be 01.
2714 * - EAF_BYTEOFFS is set
2715 * - offset can fit in a byte when EVEX is not used
2716 * - offset can be compressed when EVEX is used
2717 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002718#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2719 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2720 is_disp8n(input, ins, &output->disp8) : \
2721 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2722 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002723
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002724static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002725 int rfield, opflags_t rflags, insn *ins,
2726 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002727{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002728 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002729 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002730 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002731
H. Peter Anvin8f622462017-04-02 19:02:29 -07002732 *errmsg = "invalid effective address"; /* Default error message */
2733
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002734 output->type = EA_SCALAR;
2735 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002736 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002737
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002738 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002739 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002740 /* EVEX.R' flag for the REG operand */
2741 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002742
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002743 if (is_class(REGISTER, input->type)) {
2744 /*
2745 * It's a direct register.
2746 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002747 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002748 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002749
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002750 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002751 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002752
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002753 /* broadcasting is not available with a direct register operand. */
2754 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002755 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002756 goto err;
2757 }
2758
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002759 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002760 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002761 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002762 output->bytes = 0; /* no offset necessary either */
2763 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2764 } else {
2765 /*
2766 * It's a memory reference.
2767 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002768
2769 /* Embedded rounding or SAE is not available with a mem ref operand. */
2770 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002771 *errmsg = "embedded rounding is available only with "
2772 "register-register operations";
2773 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002774 }
2775
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002776 if (input->basereg == -1 &&
2777 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002778 /*
H. Peter Anvin (Intel)254a56a2020-07-10 02:44:33 -07002779 * It's a pure offset. If it is an IMMEDIATE, it is a pattern
2780 * in insns.dat which allows an immediate to be used as a memory
2781 * address, in which case apply the default REL/ABS.
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002782 */
H. Peter Anvin (Intel)254a56a2020-07-10 02:44:33 -07002783 if (bits == 64) {
2784 if (is_class(IMMEDIATE, input->type)) {
2785 if (!(input->eaflags & EAF_ABS) &&
2786 ((input->eaflags & EAF_REL) || globalrel))
2787 input->type |= IP_REL;
2788 }
2789 if ((input->type & IP_REL) == IP_REL) {
2790 if (input->segment == NO_SEG ||
2791 (input->opflags & OPFLAG_RELATIVE)) {
2792 nasm_warn(WARN_OTHER|ERR_PASS2, "absolute address can not be RIP-relative");
2793 input->type &= ~IP_REL;
2794 input->type |= MEMORY;
2795 }
H. Peter Anvin164d2462017-02-20 02:39:56 -08002796 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002797 }
2798
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002799 if (bits == 64 &&
2800 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002801 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002802 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002803 }
2804
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002805 if (eaflags & EAF_BYTEOFFS ||
2806 (eaflags & EAF_WORDOFFS &&
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002807 input->disp_size != (addrbits != 16 ? 32 : 16)))
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002808 nasm_warn(WARN_OTHER, "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002809
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002810 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002811 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002812 output->sib = GEN_SIB(0, 4, 5);
2813 output->bytes = 4;
2814 output->modrm = GEN_MODRM(0, rfield, 4);
2815 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002816 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002817 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002818 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002819 output->modrm = GEN_MODRM(0, rfield,
2820 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002821 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002822 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002823 } else {
2824 /*
2825 * It's an indirection.
2826 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002828 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002829 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002830 int t, it, bt; /* register numbers */
2831 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002832
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 if (s == 0)
2834 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002835
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002836 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002837 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002838 ix = nasm_reg_flags[i];
2839 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002840 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002841 ix = 0;
2842 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002843
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002844 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002845 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002846 bx = nasm_reg_flags[b];
2847 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002848 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002849 bx = 0;
2850 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002851
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002852 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002853 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002854 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002855 int32_t o = input->offset;
2856 int mod, scale, index, base;
2857
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002858 /*
2859 * For a vector SIB, one has to be a vector and the other,
2860 * if present, a GPR. The vector must be the index operand.
2861 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002862 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002863 if (s == 0)
2864 s = 1;
2865 else if (s != 1)
2866 goto err;
2867
2868 t = bt, bt = it, it = t;
2869 x = bx, bx = ix, ix = x;
2870 }
2871
2872 if (bt != -1) {
2873 if (REG_GPR & ~bx)
2874 goto err;
2875 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2876 sok &= bx;
2877 else
2878 goto err;
2879 }
2880
2881 /*
2882 * While we're here, ensure the user didn't specify
2883 * WORD or QWORD
2884 */
2885 if (input->disp_size == 16 || input->disp_size == 64)
2886 goto err;
2887
2888 if (addrbits == 16 ||
2889 (addrbits == 32 && !(sok & BITS32)) ||
2890 (addrbits == 64 && !(sok & BITS64)))
2891 goto err;
2892
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002893 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2894 : ((ix & YMMREG & ~REG_EA)
2895 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002896
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002897 output->rex |= rexflags(it, ix, REX_X);
2898 output->rex |= rexflags(bt, bx, REX_B);
2899 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002900
2901 index = it & 7; /* it is known to be != -1 */
2902
2903 switch (s) {
2904 case 1:
2905 scale = 0;
2906 break;
2907 case 2:
2908 scale = 1;
2909 break;
2910 case 4:
2911 scale = 2;
2912 break;
2913 case 8:
2914 scale = 3;
2915 break;
2916 default: /* then what the smeg is it? */
2917 goto err; /* panic */
2918 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002919
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002920 if (bt == -1) {
2921 base = 5;
2922 mod = 0;
2923 } else {
2924 base = (bt & 7);
2925 if (base != REG_NUM_EBP && o == 0 &&
2926 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002927 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002928 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002929 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002930 mod = 1;
2931 else
2932 mod = 2;
2933 }
2934
2935 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002936 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2937 output->modrm = GEN_MODRM(mod, rfield, 4);
2938 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002939 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002940 /*
2941 * it must be a 32/64-bit memory reference. Firstly we have
2942 * to check that all registers involved are type E/Rxx.
2943 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002944 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002945 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002946
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002947 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002948 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2949 sok &= ix;
2950 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002951 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002952 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002953
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002954 if (bt != -1) {
2955 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002956 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002957 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002958 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002959 sok &= bx;
2960 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002961
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002962 /*
2963 * While we're here, ensure the user didn't specify
2964 * WORD or QWORD
2965 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002966 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002967 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002968
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002969 if (addrbits == 16 ||
2970 (addrbits == 32 && !(sok & BITS32)) ||
2971 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002972 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002973
Keith Kaniosb7a89542007-04-12 02:40:54 +00002974 /* now reorganize base/index */
2975 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002976 ((hb == b && ht == EAH_NOTBASE) ||
2977 (hb == i && ht == EAH_MAKEBASE))) {
2978 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002979 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002980 x = bx, bx = ix, ix = x;
2981 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002982
Jin Kyu Song164d6072013-10-15 19:10:13 -07002983 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002984 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002985 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002986 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002987 if (eaflags & EAF_MIB) {
2988 /* only for mib operands */
2989 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2990 /*
2991 * make a single reg index [reg*1].
2992 * gas uses this form for an explicit index register.
2993 */
2994 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2995 }
2996 if ((ht == EAH_SUMMED) && bt == -1) {
2997 /* separate once summed index into [base, index] */
2998 bt = it, bx = ix, s--;
2999 }
3000 } else {
3001 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08003002 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003003 s == 3 || s == 5 || s == 9) && bt == -1) {
3004 /* convert 3*EAX to EAX+2*EAX */
3005 bt = it, bx = ix, s--;
3006 }
3007 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08003008 (eaflags & EAF_TIMESTWO) &&
3009 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003010 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08003011 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003012 * to sib format with 0x0 displacement - [EAX*1+0].
3013 */
3014 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
3015 }
3016 }
Keith Kanios48af1772007-08-17 07:37:52 +00003017 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003018 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003019 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003020 x = ix, ix = bx, bx = x;
3021 }
3022 if (it == REG_NUM_ESP ||
3023 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003024 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003025
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003026 output->rex |= rexflags(it, ix, REX_X);
3027 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003028
Keith Kanios48af1772007-08-17 07:37:52 +00003029 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003030 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003031 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07003032
Keith Kaniosb7a89542007-04-12 02:40:54 +00003033 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003034 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003035 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003036 } else {
3037 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003038 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003039 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003040 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00003041 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003042 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00003043 mod = 1;
3044 else
3045 mod = 2;
3046 }
H. Peter Anvinea838272002-04-30 20:51:53 +00003047
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003048 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003049 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
3050 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003051 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003052 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003053 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07003054
Keith Kaniosb7a89542007-04-12 02:40:54 +00003055 if (it == -1)
3056 index = 4, s = 1;
3057 else
3058 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07003059
H. Peter Anvine2c80182005-01-15 22:15:51 +00003060 switch (s) {
3061 case 1:
3062 scale = 0;
3063 break;
3064 case 2:
3065 scale = 1;
3066 break;
3067 case 4:
3068 scale = 2;
3069 break;
3070 case 8:
3071 scale = 3;
3072 break;
3073 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003074 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003075 }
H. Peter Anvin70653092007-10-19 14:42:29 -07003076
Keith Kaniosb7a89542007-04-12 02:40:54 +00003077 if (bt == -1) {
3078 base = 5;
3079 mod = 0;
3080 } else {
3081 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003082 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003083 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003084 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00003085 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003086 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00003087 mod = 1;
3088 else
3089 mod = 2;
3090 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003091
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003092 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003093 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
3094 output->modrm = GEN_MODRM(mod, rfield, 4);
3095 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003096 }
3097 } else { /* it's 16-bit */
3098 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003099 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07003100
Keith Kaniosb7a89542007-04-12 02:40:54 +00003101 /* check for 64-bit long mode */
3102 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003103 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003104
H. Peter Anvine2c80182005-01-15 22:15:51 +00003105 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003106 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
3107 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003108 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003109
Keith Kaniosb7a89542007-04-12 02:40:54 +00003110 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003111 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003112 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003113
H. Peter Anvine2c80182005-01-15 22:15:51 +00003114 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003115 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003116 if (b == -1 && i != -1) {
3117 int tmp = b;
3118 b = i;
3119 i = tmp;
3120 } /* swap */
3121 if ((b == R_SI || b == R_DI) && i != -1) {
3122 int tmp = b;
3123 b = i;
3124 i = tmp;
3125 }
3126 /* have BX/BP as base, SI/DI index */
3127 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003128 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003129 if (i != -1 && b != -1 &&
3130 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003131 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003132 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003133 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003134
H. Peter Anvine2c80182005-01-15 22:15:51 +00003135 rm = -1;
3136 if (i != -1)
3137 switch (i * 256 + b) {
3138 case R_SI * 256 + R_BX:
3139 rm = 0;
3140 break;
3141 case R_DI * 256 + R_BX:
3142 rm = 1;
3143 break;
3144 case R_SI * 256 + R_BP:
3145 rm = 2;
3146 break;
3147 case R_DI * 256 + R_BP:
3148 rm = 3;
3149 break;
3150 } else
3151 switch (b) {
3152 case R_SI:
3153 rm = 4;
3154 break;
3155 case R_DI:
3156 rm = 5;
3157 break;
3158 case R_BP:
3159 rm = 6;
3160 break;
3161 case R_BX:
3162 rm = 7;
3163 break;
3164 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003165 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003166 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003167
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003168 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003169 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003170 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003171 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00003172 mod = 1;
3173 else
3174 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003175
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003176 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003177 output->bytes = mod; /* bytes of offset needed */
3178 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003179 }
3180 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003181 }
H. Peter Anvin70653092007-10-19 14:42:29 -07003182
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003183 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003184 return output->type;
3185
3186err:
3187 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003188}
3189
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003190static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003191{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003192 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003193 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003194
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003195 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003196
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003197 switch (ins->prefixes[PPS_ASIZE]) {
3198 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003199 valid &= 16;
3200 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003201 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003202 valid &= 32;
3203 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003204 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003205 valid &= 64;
3206 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003207 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003208 valid &= (addrbits == 32) ? 16 : 32;
3209 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003210 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003211 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003212 }
3213
3214 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003215 if (is_class(MEMORY, ins->oprs[j].type)) {
3216 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07003217
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003218 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003219 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003220 i = 0;
3221 else
3222 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003223
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003224 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003225 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003226 b = 0;
3227 else
3228 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003229
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003230 if (ins->oprs[j].scale == 0)
3231 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003232
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003233 if (!i && !b) {
3234 int ds = ins->oprs[j].disp_size;
3235 if ((addrbits != 64 && ds > 8) ||
3236 (addrbits == 64 && ds == 16))
3237 valid &= ds;
3238 } else {
3239 if (!(REG16 & ~b))
3240 valid &= 16;
3241 if (!(REG32 & ~b))
3242 valid &= 32;
3243 if (!(REG64 & ~b))
3244 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07003245
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003246 if (!(REG16 & ~i))
3247 valid &= 16;
3248 if (!(REG32 & ~i))
3249 valid &= 32;
3250 if (!(REG64 & ~i))
3251 valid &= 64;
3252 }
3253 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003254 }
3255
3256 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003257 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003258 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003259 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04003260 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003261 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003262 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003263 /* Impossible... */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03003264 nasm_nonfatal("impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003265 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003266 }
3267
3268 defdisp = ins->addr_size == 16 ? 16 : 32;
3269
3270 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003271 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
3272 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
3273 /*
3274 * mem_offs sizes must match the address size; if not,
3275 * strip the MEM_OFFS bit and match only EA instructions
3276 */
3277 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
3278 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003279 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003280}