blob: e5d5682cfe78501e2e0dc233ee51d67599ed4495 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvin (Intel)38ddb192019-01-11 12:27:02 -08003 * Copyright 1996-2019 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040037 * Bytecode specification
38 * ----------------------
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070039 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040040 *
41 * Codes Mnemonic Explanation
42 *
43 * \0 terminates the code. (Unless it's a literal of course.)
44 * \1..\4 that many literal bytes follow in the code stream
45 * \5 add 4 to the primary operand number (b, low octdigit)
46 * \6 add 4 to the secondary operand number (a, middle octdigit)
47 * \7 add 4 to both the primary and the secondary operand number
48 * \10..\13 a literal byte follows in the code stream, to be added
49 * to the register value of operand 0..3
50 * \14..\17 the position of index register operand in MIB (BND insns)
51 * \20..\23 ib a byte immediate operand, from operand 0..3
52 * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
53 * \30..\33 iw a word immediate operand, from operand 0..3
54 * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
55 * assembly mode or the operand-size override on the operand
56 * \40..\43 id a long immediate operand, from operand 0..3
57 * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
58 * depending on the address size of the instruction.
59 * \50..\53 rel8 a byte relative operand, from operand 0..3
60 * \54..\57 iq a qword immediate operand, from operand 0..3
61 * \60..\63 rel16 a word relative operand, from operand 0..3
62 * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
63 * assembly mode or the operand-size override on the operand
64 * \70..\73 rel32 a long relative operand, from operand 0..3
65 * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
66 * \1ab a ModRM, calculated on EA in operand a, with the spare
67 * field the register value of operand b.
68 * \172\ab the register number from operand a in bits 7..4, with
69 * the 4-bit immediate from operand b in bits 3..0.
70 * \173\xab the register number from operand a in bits 7..4, with
71 * the value b in bits 3..0.
72 * \174..\177 the register number from operand 0..3 in bits 7..4, and
73 * an arbitrary value in bits 3..0 (assembled as zero.)
74 * \2ab a ModRM, calculated on EA in operand a, with the spare
75 * field equal to digit b.
76 *
77 * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
78 * V field taken from operand 0..3.
79 * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
80 * V field set to 1111b.
81 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070082 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -070084 * cc 00m mmm
85 * c = 2 for EVEX and mmmm is the M field (EVEX.P0[3:0])
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070086 * and wlp is:
87 * 00 wwl lpp
88 * [l0] ll = 0 (.128, .lz)
89 * [l1] ll = 1 (.256)
90 * [l2] ll = 2 (.512)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
92 *
93 * [w0] ww = 0 for W = 0
94 * [w1] ww = 1 for W = 1
95 * [wig] ww = 2 for W don't care (always assembled as 0)
96 * [ww] ww = 3 for W used as REX.W
97 *
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
100 * [f3] pp = 2
101 * [f2] pp = 3
102 *
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
105 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400106 * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
107 * \260..\263 this instruction uses VEX/XOP rather than REX, with the
108 * V field taken from operand 0..3.
109 * \270 this instruction uses VEX/XOP rather than REX, with the
110 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700111 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700114 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700115 * [l0] ll = 0 for L = 0 (.128, .lz)
116 * [l1] ll = 1 for L = 1 (.256)
117 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700118 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700119 * [w0] ww = 0 for W = 0
120 * [w1 ] ww = 1 for W = 1
121 * [wig] ww = 2 for W don't care (always assembled as 0)
122 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700123 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700125 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400126 * \271 hlexr instruction takes XRELEASE (F3) with or without lock
127 * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
128 * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
129 * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
130 * to the operand size (if o16/o32/o64 present) or the bit size
131 * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
132 * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
133 * \312 adf (disassembler only) invalid with non-default address size.
134 * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
135 * \314 norexb (disassembler only) invalid with REX.B
136 * \315 norexx (disassembler only) invalid with REX.X
137 * \316 norexr (disassembler only) invalid with REX.R
138 * \317 norexw (disassembler only) invalid with REX.W
139 * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
140 * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
141 * \322 odf indicates that this instruction is only valid when the
142 * operand size is the default (instruction to disassembler,
143 * generates no code in the assembler)
144 * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
145 * \324 o64 indicates 64-bit operand size requiring REX prefix.
146 * \325 nohi instruction which always uses spl/bpl/sil/dil
147 * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
148 disassembler only; for SSE instructions.
149 * \330 a literal byte follows in the code stream, to be added
150 * to the condition code value of the instruction.
151 * \331 norep instruction not valid with REP prefix. Hint for
152 * disassembler only; for SSE instructions.
153 * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
154 * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
155 * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
156 * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
157 * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
158 * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
159 * \336-\337 are still listed as prefixes in the disassembler.
160 * \340 resb reserve <operand 0> bytes of uninitialized storage.
161 * Operand 0 had better be a segmentless constant.
162 * \341 wait this instruction needs a WAIT "prefix"
Cyrill Gorcunov8a5d3e62014-08-25 20:04:30 +0400163 * \360 np no SSE prefix (== \364\331)
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400164 * \361 66 SSE prefix (== \366\331)
165 * \364 !osp operand-size prefix (0x66) not permitted
166 * \365 !asp address-size prefix (0x67) not permitted
167 * \366 operand-size prefix (0x66) used as opcode extension
168 * \367 address-size prefix (0x67) used as opcode extension
169 * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
170 * jmp8 370 is used for Jcc, 371 is used for JMP.
171 * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
172 * used for conditional jump over longer jump
173 * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
174 * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
175 * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 */
177
H. Peter Anvinfe501952007-10-02 21:53:51 -0700178#include "compiler.h"
179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180
181#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000182#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800183#include "error.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000184#include "assemble.h"
185#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700186#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800187#include "disp8.h"
H. Peter Anvin172b8402016-02-18 01:16:18 -0800188#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000189
H. Peter Anvin65289e82009-07-25 17:25:11 -0700190enum match_result {
191 /*
192 * Matching errors. These should be sorted so that more specific
193 * errors come later in the sequence.
194 */
195 MERR_INVALOP,
196 MERR_OPSIZEMISSING,
197 MERR_OPSIZEMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700198 MERR_BRNOTHERE,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700199 MERR_BRNUMMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700200 MERR_MASKNOTHERE,
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700201 MERR_DECONOTHERE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700202 MERR_BADCPU,
203 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800204 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700205 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700206 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800207 MERR_BADREPNE,
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700208 MERR_REGSETSIZE,
209 MERR_REGSET,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700210 /*
211 * Matching success; the conditional ones first
212 */
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700213 MOK_JUMP, /* Matching OK but needs jmp_match() */
214 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700215};
216
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000217typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700218 enum ea_type type; /* what kind of EA is this? */
219 int sib_present; /* is a SIB byte necessary? */
220 int bytes; /* # of bytes of offset needed */
221 int size; /* lazy - this is sib+bytes+1 */
222 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700223 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224} ea;
225
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400226#define GEN_SIB(scale, index, base) \
227 (((scale) << 6) | ((index) << 3) | ((base)))
228
229#define GEN_MODRM(mod, reg, rm) \
230 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
231
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800232static int64_t calcsize(int32_t, int64_t, int, insn *,
233 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700234static int emit_prefix(struct out_data *data, const int bits, insn *ins);
235static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700236static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400237 insn *instruction,
238 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700239static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700240static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000241static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700242static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000243static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700244static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700245static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000246
H. Peter Anvin8f622462017-04-02 19:02:29 -0700247static enum ea_type process_ea(operand *, ea *, int, int,
248 opflags_t, insn *, const char **);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700249
H. Peter Anvin164d2462017-02-20 02:39:56 -0800250static inline bool absolute_op(const struct operand *o)
251{
252 return o->segment == NO_SEG && o->wrt == NO_SEG &&
253 !(o->opflags & OPFLAG_RELATIVE);
254}
255
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400256static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000257{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700258 return ins->prefixes[pos] == prefix;
259}
260
261static void assert_no_prefix(insn * ins, enum prefix_pos pos)
262{
263 if (ins->prefixes[pos])
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300264 nasm_nonfatal("invalid %s prefix", prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265}
266
267static const char *size_name(int size)
268{
269 switch (size) {
270 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400271 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700272 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400273 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700274 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700276 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400277 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700278 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400279 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700280 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700282 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400283 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700284 case 64:
285 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700286 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400287 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000288 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700289}
290
H. Peter Anvin285222f2017-03-01 13:27:33 -0800291static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400292{
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800293 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW, "%s data exceeds bounds",
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300294 size_name(size));
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400295}
296
297static void warn_overflow_const(int64_t data, int size)
298{
299 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800300 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400301}
302
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800303static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
304{
305 bool err;
306
307 switch (sign) {
308 case OUT_WRAP:
309 err = overflow_general(data, size);
310 break;
311 case OUT_SIGNED:
312 err = overflow_signed(data, size);
313 break;
314 case OUT_UNSIGNED:
315 err = overflow_unsigned(data, size);
316 break;
317 default:
318 panic();
319 break;
320 }
321
322 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800323 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800324}
325
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000326/*
327 * This routine wrappers the real output format's output routine,
328 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800329 * generator at the same time, flatten unnecessary relocations,
330 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000331 */
H. Peter Anvin (Intel)38ddb192019-01-11 12:27:02 -0800332/*
333 * This warning is currently issued by backends, but in the future
334 * this code should be centralized.
335 *
336 *!zeroing [on] RESx in initialized section becomes zero
337 *! a \c{RESx} directive was used in a section which contains
338 *! initialized data, and the output format does not support
339 *! this. Instead, this will be replaced with explicit zero
340 *! content, which may produce a large output file.
341 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700342static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000343{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000344 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700345 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700346 union {
347 uint8_t b[8];
348 uint64_t q;
349 } xdata;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700350 size_t asize, amax;
351 uint64_t zeropad = 0;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800352 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800353 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000354
H. Peter Anvina77692b2016-09-20 14:04:33 -0700355 if (!data->size)
356 return; /* Nothing to do */
357
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700358 /*
359 * Convert addresses to RAWDATA if possible
360 * XXX: not all backends want this for global symbols!!!!
361 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700362 switch (data->type) {
363 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800364 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800365 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800366 goto address;
367
368 case OUT_RELADDR:
369 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800370 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800371 goto address;
372
373 address:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700374 nasm_assert(data->size <= 8);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700375 asize = data->size;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700376 amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
Chang S. Bae427d8e32018-05-02 08:07:52 -0700377 if ((ofmt->flags & OFMT_KEEP_ADDR) == 0 && data->tsegment == fixseg &&
378 data->twrt == NO_SEG) {
H. Peter Anvin86b2e932019-09-12 18:20:07 -0700379 if (asize >= (size_t)(data->bits >> 3))
380 data->sign = OUT_WRAP; /* Support address space wrapping for low-bit modes */
381 warn_overflow_out(addrval, asize, data->sign);
Martin Storsjö869087d2017-05-22 13:54:20 +0300382 xdata.q = cpu_to_le64(addrval);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700383 data->data = xdata.b;
384 data->type = OUT_RAWDATA;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700385 asize = amax = 0; /* No longer an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700386 }
387 break;
388
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700389 case OUT_SEGMENT:
390 nasm_assert(data->size <= 8);
391 asize = data->size;
392 amax = 2;
393 break;
394
H. Peter Anvina77692b2016-09-20 14:04:33 -0700395 default:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700396 asize = amax = 0; /* Not an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700397 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000398 }
399
Frank Kotlerabebb082003-09-06 04:45:37 +0000400 /*
401 * this call to src_get determines when we call the
402 * debug-format-specific "linenum" function
403 * it updates lineno and lnfname to the current values
404 * returning 0 if "same as last time", -2 if lnfname
405 * changed, and the amount by which lineno changed,
406 * if it did. thus, these variables must be static
407 */
408
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400409 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700410 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000411
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700412 if (asize > amax) {
413 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300414 nasm_nonfatal("%u-bit signed relocation unsupported by output format %s",
415 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800416 } else {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800417 /*!
418 *!zext-reloc [on] relocation zero-extended to match output format
419 *! warns that a relocation has been zero-extended due
420 *! to limitations in the output format.
421 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800422 nasm_warn(WARN_ZEXT_RELOC,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700423 "%u-bit %s relocation zero-extended from %u bits",
424 (unsigned int)(asize << 3),
425 data->type == OUT_SEGMENT ? "segment" : "unsigned",
426 (unsigned int)(amax << 3));
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800427 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700428 zeropad = data->size - amax;
429 data->size = amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800430 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700431 lfmt->output(data);
H. Peter Anvined859f72018-06-15 00:03:53 -0700432
433 if (likely(data->segment != NO_SEG)) {
434 ofmt->output(data);
435 } else {
436 /* Outputting to ABSOLUTE section - only reserve is permitted */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300437 if (data->type != OUT_RESERVE)
438 nasm_nonfatal("attempt to assemble code in [ABSOLUTE] space");
H. Peter Anvined859f72018-06-15 00:03:53 -0700439 /* No need to push to the backend */
440 }
441
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700442 data->offset += data->size;
443 data->insoffs += data->size;
444
445 if (zeropad) {
446 data->type = OUT_ZERODATA;
447 data->size = zeropad;
448 lfmt->output(data);
449 ofmt->output(data);
450 data->offset += zeropad;
451 data->insoffs += zeropad;
452 data->size += zeropad; /* Restore original size value */
453 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000454}
455
H. Peter Anvina77692b2016-09-20 14:04:33 -0700456static inline void out_rawdata(struct out_data *data, const void *rawdata,
457 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400458{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700459 data->type = OUT_RAWDATA;
460 data->data = rawdata;
461 data->size = size;
462 out(data);
463}
464
465static void out_rawbyte(struct out_data *data, uint8_t byte)
466{
467 data->type = OUT_RAWDATA;
468 data->data = &byte;
469 data->size = 1;
470 out(data);
471}
472
473static inline void out_reserve(struct out_data *data, uint64_t size)
474{
475 data->type = OUT_RESERVE;
476 data->size = size;
477 out(data);
478}
479
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700480static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700481{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700482 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300483 nasm_nonfatal("segment references cannot be relative");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700484
485 data->type = OUT_SEGMENT;
486 data->sign = OUT_UNSIGNED;
487 data->size = 2;
488 data->toffset = opx->offset;
489 data->tsegment = ofmt->segbase(opx->segment | 1);
490 data->twrt = opx->wrt;
491 out(data);
492}
493
494static void out_imm(struct out_data *data, const struct operand *opx,
495 int size, enum out_sign sign)
496{
497 if (opx->segment != NO_SEG && (opx->segment & 1)) {
498 /*
499 * This is actually a segment reference, but eval() has
500 * already called ofmt->segbase() for us. Sigh.
501 */
502 if (size < 2)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300503 nasm_nonfatal("segment reference must be 16 bits");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700504
505 data->type = OUT_SEGMENT;
506 } else {
507 data->type = (opx->opflags & OPFLAG_RELATIVE)
508 ? OUT_RELADDR : OUT_ADDRESS;
509 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700510 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700511 data->toffset = opx->offset;
512 data->tsegment = opx->segment;
513 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800514 /*
515 * XXX: improve this if at some point in the future we can
516 * distinguish the subtrahend in expressions like [foo - bar]
517 * where bar is a symbol in the current segment. However, at the
518 * current point, if OPFLAG_RELATIVE is set that subtraction has
519 * already occurred.
520 */
521 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700522 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700523 out(data);
524}
525
H. Peter Anvin164d2462017-02-20 02:39:56 -0800526static void out_reladdr(struct out_data *data, const struct operand *opx,
527 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700528{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800529 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300530 nasm_nonfatal("invalid use of self-relative expression");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800531
H. Peter Anvina77692b2016-09-20 14:04:33 -0700532 data->type = OUT_RELADDR;
533 data->sign = OUT_SIGNED;
534 data->size = size;
535 data->toffset = opx->offset;
536 data->tsegment = opx->segment;
537 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800538 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700539 out(data);
540}
541
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700542static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800543 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800545 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800546 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000547 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800548 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000549
H. Peter Anvin755f5212012-02-25 11:41:34 -0800550 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700551 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300552 if (!optimizing.level || (optimizing.flag & OPTIM_DISABLE_JMP_MATCH))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400553 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300554 if (optimizing.level < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400555 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700556
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800557 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100558
Victor van den Elzen154e5922009-02-25 17:32:00 +0100559 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100560 /* Be optimistic in pass 1 */
561 return true;
562
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700564 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000565
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700566 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800567 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
568
569 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
570 /* jmp short (opcode eb) cannot be used with bnd prefix. */
571 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800572 /*!
573 *!bnd [on] invalid BND prefixes
574 *! warns about ineffective use of the \c{BND} prefix when the
575 *! \c{JMP} instruction is converted to the \c{SHORT} form.
576 *! This should be extremely rare since the short \c{JMP} only
577 *! is applicable to jumps inside the same module, but if
578 *! it is legitimate, it may be necessary to use
H. Peter Anvin959702b2019-06-06 20:56:50 -0700579 *! \c{bnd jmp dword}.
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800580 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800581 nasm_warn(WARN_BND | ERR_PASS2 ,
H. Peter Anvin959702b2019-06-06 20:56:50 -0700582 "jmp short does not init bnd regs - bnd prefix dropped");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800583 }
584
585 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000587
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700588static inline int64_t merge_resb(insn *ins, int64_t isize)
589{
590 int nbytes = resb_bytes(ins->opcode);
591
592 if (likely(!nbytes))
593 return isize;
594
595 if (isize != nbytes * ins->oprs[0].offset)
596 return isize; /* Has prefixes of some sort */
597
598 ins->oprs[0].offset *= ins->times;
599 isize *= ins->times;
600 ins->times = 1;
601 return isize;
602}
603
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700604/* This must be handle non-power-of-2 alignment values */
605static inline size_t pad_bytes(size_t len, size_t align)
606{
607 size_t partial = len % align;
608 return partial ? align - partial : 0;
609}
610
611static void out_eops(struct out_data *data, const extop *e)
612{
613 while (e) {
614 size_t dup = e->dup;
615
616 switch (e->type) {
617 case EOT_NOTHING:
618 break;
619
620 case EOT_EXTOP:
621 while (dup--)
622 out_eops(data, e->val.subexpr);
623 break;
624
625 case EOT_DB_NUMBER:
626 if (e->elem > 8) {
627 nasm_nonfatal("integer supplied as %d-bit data",
628 e->elem << 3);
629 } else {
630 while (dup--) {
631 data->insoffs = 0;
632 data->inslen = data->size = e->elem;
633 data->tsegment = e->val.num.segment;
634 data->toffset = e->val.num.offset;
635 data->twrt = e->val.num.wrt;
636 data->relbase = 0;
637 if (e->val.num.segment != NO_SEG &&
638 (e->val.num.segment & 1)) {
639 data->type = OUT_SEGMENT;
640 data->sign = OUT_UNSIGNED;
641 } else {
642 data->type = e->val.num.relative
643 ? OUT_RELADDR : OUT_ADDRESS;
644 data->sign = OUT_WRAP;
645 }
646 out(data);
647 }
648 }
649 break;
650
651 case EOT_DB_FLOAT:
652 case EOT_DB_STRING:
653 case EOT_DB_STRING_FREE:
654 {
655 size_t pad, len;
656
657 pad = pad_bytes(e->val.string.len, e->elem);
658 len = e->val.string.len + pad;
659
660 while (dup--) {
661 data->insoffs = 0;
662 data->inslen = len;
663 out_rawdata(data, e->val.string.data, e->val.string.len);
664 if (pad)
665 out_rawdata(data, zero_buffer, pad);
666 }
667 break;
668 }
669
670 case EOT_DB_RESERVE:
671 data->insoffs = 0;
672 data->inslen = dup * e->elem;
673 out_reserve(data, data->inslen);
674 break;
675 }
676
677 e = e->next;
678 }
679}
680
H. Peter Anvin04445362016-09-21 15:56:19 -0700681/* This is totally just a wild guess what is reasonable... */
682#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
683
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800684int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000685{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700686 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000687 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700688 enum match_result m;
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700689
690 if (instruction->opcode == I_none)
691 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000692
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700693 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700694 data.offset = start;
695 data.segment = segment;
696 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700697 data.bits = bits;
698
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700699 if (opcode_is_db(instruction->opcode)) {
700 out_eops(&data, instruction->eops);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700701 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700702 const char *fname = instruction->eops->val.string.data;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000703 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700704 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700705 off_t base = 0;
706 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700707 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700708 char *buf = NULL;
709 size_t blk = 0; /* Buffered I/O block size */
710 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000711
H. Peter Anvin94ead272017-09-27 15:22:23 -0700712 if (!t)
713 goto done;
714
H. Peter Anvind81a2352016-09-21 14:03:18 -0700715 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400716 if (!fp) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300717 nasm_nonfatal("`incbin': unable to open file `%s'",
718 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700719 goto done;
720 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000721
H. Peter Anvind81a2352016-09-21 14:03:18 -0700722 len = nasm_file_size(fp);
723
724 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300725 nasm_nonfatal("`incbin': unable to get length of file `%s'",
726 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700727 goto close_done;
728 }
729
H. Peter Anvina77692b2016-09-20 14:04:33 -0700730 if (instruction->eops->next) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700731 base = instruction->eops->next->val.num.offset;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700732 if (base >= len) {
733 len = 0;
734 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000735 len -= base;
736 if (instruction->eops->next->next &&
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700737 len > (off_t)instruction->eops->next->next->val.num.offset)
738 len = (off_t)instruction->eops->next->next->val.num.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000739 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000740 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700741
H. Peter Anvina77692b2016-09-20 14:04:33 -0700742 lfmt->set_offset(data.offset);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700743 lfmt->uplevel(LIST_INCBIN, len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000744
H. Peter Anvind81a2352016-09-21 14:03:18 -0700745 if (!len)
746 goto end_incbin;
747
748 /* Try to map file data */
749 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700750 if (!map) {
751 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
752 buf = nasm_malloc(blk);
753 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700754
755 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700756 /*
757 * Consider these irrelevant for INCBIN, since it is fully
758 * possible that these might be (way) bigger than an int
759 * can hold; there is, however, no reason to widen these
760 * types just for INCBIN. data.inslen == 0 signals to the
761 * backend that these fields are meaningless, if at all
762 * needed.
763 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700764 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700765 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700766
H. Peter Anvind81a2352016-09-21 14:03:18 -0700767 if (map) {
768 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700769 } else if ((off_t)m == len) {
770 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700771 } else {
772 off_t l = len;
773
774 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300775 nasm_nonfatal("`incbin': unable to seek on file `%s'",
776 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700777 goto end_incbin;
778 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700779 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700780 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700781 if (!m || feof(fp)) {
782 /*
783 * This shouldn't happen unless the file
784 * actually changes while we are reading
785 * it.
786 */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300787 nasm_nonfatal("`incbin': unexpected EOF while"
788 " reading file `%s'", fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700789 goto end_incbin;
790 }
791 out_rawdata(&data, buf, m);
792 l -= m;
793 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700794 }
795 }
796 end_incbin:
797 lfmt->downlevel(LIST_INCBIN);
798 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700799 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700800 lfmt->downlevel(LIST_TIMES);
801 }
802 if (ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300803 nasm_nonfatal("`incbin': error while"
804 " reading file `%s'", fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700805 }
806 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700807 if (buf)
808 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700809 if (map)
810 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700811 fclose(fp);
812 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700813 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700814 ;
815 } else {
816 /* "Real" instruction */
817
818 /* Check to see if we need an address-size prefix */
819 add_asp(instruction, bits);
820
821 m = find_match(&temp, instruction, data.segment, data.offset, bits);
822
823 if (m == MOK_GOOD) {
824 /* Matches! */
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700825 if (unlikely(itemp_has(temp, IF_OBSOLETE))) {
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700826 errflags warning;
827 const char *whathappened;
828 const char *validity;
829 bool never = itemp_has(temp, IF_NEVER);
830
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700831 /*
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700832 * If IF_OBSOLETE is set, warn the user. Different
833 * warning classes for "obsolete but valid for this
834 * specific CPU" and "obsolete and gone."
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700835 *
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700836 *!obsolete-removed [on] instruction obsolete and removed on the target CPU
837 *! warns for an instruction which has been removed
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700838 *! from the architecture, and is no longer included
839 *! in the CPU definition given in the \c{[CPU]}
840 *! directive, for example \c{POP CS}, the opcode for
841 *! which, \c{0Fh}, instead is an opcode prefix on
842 *! CPUs newer than the first generation 8086.
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700843 *
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700844 *!obsolete-nop [on] instruction obsolete and is a noop on the target CPU
845 *! warns for an instruction which has been removed
846 *! from the architecture, but has been architecturally
847 *! defined to be a noop for future CPUs.
848 *
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700849 *!obsolete-valid [on] instruction obsolete but valid on the target CPU
850 *! warns for an instruction which has been removed
851 *! from the architecture, but is still valid on the
852 *! specific CPU given in the \c{CPU} directive. Code
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700853 *! using these instructions is most likely not
854 *! forward compatible.
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700855 */
856
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700857 whathappened = never ? "never implemented" : "obsolete";
858
859 if (!never && !iflag_cmp_cpu_level(&insns_flags[temp->iflag_idx], &cpu)) {
860 warning = WARN_OBSOLETE_VALID;
861 validity = "but valid on";
862 } else if (itemp_has(temp, IF_NOP)) {
863 warning = WARN_OBSOLETE_NOP;
864 validity = "and is a noop on";
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700865 } else {
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700866 warning = WARN_OBSOLETE_REMOVED;
H. Peter Anvin (Intel)fb118ae2019-08-09 15:01:28 -0700867 validity = never ? "and invalid on" : "and removed from";
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700868 }
H. Peter Anvin (Intel)5b394612019-08-09 14:52:16 -0700869
870 nasm_warn(warning, "instruction %s %s the target CPU",
871 whathappened, validity);
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700872 }
873
H. Peter Anvina77692b2016-09-20 14:04:33 -0700874 data.itemp = temp;
875 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700876 data.insoffs = 0;
H. Peter Anvin (Intel)77335212019-08-06 23:22:48 -0700877
878 data.inslen = calcsize(data.segment, data.offset,
879 bits, instruction, temp);
880 nasm_assert(data.inslen >= 0);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700881 data.inslen = merge_resb(instruction, data.inslen);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700882
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700883 gencode(&data, instruction);
H. Peter Anvin (Intel)77335212019-08-06 23:22:48 -0700884 nasm_assert(data.insoffs == data.inslen);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700885 } else {
886 /* No match */
887 switch (m) {
888 case MERR_OPSIZEMISSING:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300889 nasm_nonfatal("operation size not specified");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700890 break;
891 case MERR_OPSIZEMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300892 nasm_nonfatal("mismatch in operand sizes");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700893 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700894 case MERR_BRNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300895 nasm_nonfatal("broadcast not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700896 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700897 case MERR_BRNUMMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300898 nasm_nonfatal("mismatch in the number of broadcasting elements");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700899 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700900 case MERR_MASKNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300901 nasm_nonfatal("mask not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700902 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700903 case MERR_DECONOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300904 nasm_nonfatal("unsupported mode decorator for instruction");
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700905 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700906 case MERR_BADCPU:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300907 nasm_nonfatal("no instruction for this cpu level");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700908 break;
909 case MERR_BADMODE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300910 nasm_nonfatal("instruction not supported in %d-bit mode", bits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700911 break;
912 case MERR_ENCMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300913 nasm_nonfatal("specific encoding scheme not available");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700914 break;
915 case MERR_BADBND:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300916 nasm_nonfatal("bnd prefix is not allowed");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700917 break;
918 case MERR_BADREPNE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300919 nasm_nonfatal("%s prefix is not allowed",
920 (has_prefix(instruction, PPS_REP, P_REPNE) ?
921 "repne" : "repnz"));
H. Peter Anvina77692b2016-09-20 14:04:33 -0700922 break;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700923 case MERR_REGSETSIZE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300924 nasm_nonfatal("invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700925 break;
926 case MERR_REGSET:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300927 nasm_nonfatal("register set not valid for operand");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700928 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700929 default:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300930 nasm_nonfatal("invalid combination of opcode and operands");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700931 break;
932 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700933
934 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400935 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000936 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700937 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000938}
939
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700940static int32_t eops_typeinfo(const extop *e)
H. Peter Anvin29651542018-12-18 19:14:40 -0800941{
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700942 int32_t typeinfo = 0;
H. Peter Anvin29651542018-12-18 19:14:40 -0800943
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700944 while (e) {
945 switch (e->type) {
946 case EOT_NOTHING:
947 break;
948
949 case EOT_EXTOP:
950 typeinfo |= eops_typeinfo(e->val.subexpr);
951 break;
952
953 case EOT_DB_FLOAT:
954 switch (e->elem) {
955 case 1: typeinfo |= TY_BYTE; break;
956 case 2: typeinfo |= TY_WORD; break;
957 case 4: typeinfo |= TY_FLOAT; break;
958 case 8: typeinfo |= TY_QWORD; break; /* double? */
959 case 10: typeinfo |= TY_TBYTE; break; /* long double? */
960 case 16: typeinfo |= TY_YWORD; break;
961 case 32: typeinfo |= TY_ZWORD; break;
962 default: break;
963 }
964 break;
965
966 default:
967 switch (e->elem) {
968 case 1: typeinfo |= TY_BYTE; break;
969 case 2: typeinfo |= TY_WORD; break;
970 case 4: typeinfo |= TY_DWORD; break;
971 case 8: typeinfo |= TY_QWORD; break;
972 case 10: typeinfo |= TY_TBYTE; break;
973 case 16: typeinfo |= TY_YWORD; break;
974 case 32: typeinfo |= TY_ZWORD; break;
975 default: break;
976 }
977 break;
978 }
979 e = e->next;
H. Peter Anvin29651542018-12-18 19:14:40 -0800980 }
981
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -0700982 return typeinfo;
983}
984
985static inline void debug_set_db_type(insn *instruction)
986{
987
988 int32_t typeinfo = TYS_ELEMENTS(instruction->operands);
989
990 typeinfo |= eops_typeinfo(instruction->eops);
H. Peter Anvin29651542018-12-18 19:14:40 -0800991 dfmt->debug_typevalue(typeinfo);
992}
993
994static void debug_set_type(insn *instruction)
995{
996 int32_t typeinfo;
997
998 if (opcode_is_resb(instruction->opcode)) {
999 typeinfo = TYS_ELEMENTS(instruction->oprs[0].offset);
1000
1001 switch (instruction->opcode) {
1002 case I_RESB:
1003 typeinfo |= TY_BYTE;
1004 break;
1005 case I_RESW:
1006 typeinfo |= TY_WORD;
1007 break;
1008 case I_RESD:
1009 typeinfo |= TY_DWORD;
1010 break;
1011 case I_RESQ:
1012 typeinfo |= TY_QWORD;
1013 break;
1014 case I_REST:
1015 typeinfo |= TY_TBYTE;
1016 break;
1017 case I_RESO:
1018 typeinfo |= TY_OWORD;
1019 break;
1020 case I_RESY:
1021 typeinfo |= TY_YWORD;
1022 break;
1023 case I_RESZ:
1024 typeinfo |= TY_ZWORD;
1025 break;
1026 default:
1027 panic();
1028 }
1029 } else {
1030 typeinfo = TY_LABEL;
1031 }
1032
1033 dfmt->debug_typevalue(typeinfo);
1034}
1035
1036
1037/* Proecess an EQU directive */
1038static void define_equ(insn * instruction)
1039{
1040 if (!instruction->label) {
1041 nasm_nonfatal("EQU not preceded by label");
1042 } else if (instruction->operands == 1 &&
1043 (instruction->oprs[0].type & IMMEDIATE) &&
1044 instruction->oprs[0].wrt == NO_SEG) {
1045 define_label(instruction->label,
1046 instruction->oprs[0].segment,
1047 instruction->oprs[0].offset, false);
1048 } else if (instruction->operands == 2
1049 && (instruction->oprs[0].type & IMMEDIATE)
1050 && (instruction->oprs[0].type & COLON)
1051 && instruction->oprs[0].segment == NO_SEG
1052 && instruction->oprs[0].wrt == NO_SEG
1053 && (instruction->oprs[1].type & IMMEDIATE)
1054 && instruction->oprs[1].segment == NO_SEG
1055 && instruction->oprs[1].wrt == NO_SEG) {
1056 define_label(instruction->label,
1057 instruction->oprs[0].offset | SEG_ABS,
1058 instruction->oprs[1].offset, false);
1059 } else {
1060 nasm_nonfatal("bad syntax for EQU");
1061 }
1062}
1063
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001064static int64_t len_extops(const extop *e)
1065{
1066 int64_t isize = 0;
1067 size_t pad;
1068
1069 while (e) {
1070 switch (e->type) {
1071 case EOT_NOTHING:
1072 break;
1073
1074 case EOT_EXTOP:
1075 isize += e->dup * len_extops(e->val.subexpr);
1076 break;
1077
1078 case EOT_DB_STRING:
1079 case EOT_DB_STRING_FREE:
1080 case EOT_DB_FLOAT:
1081 pad = pad_bytes(e->val.string.len, e->elem);
1082 isize += e->dup * (e->val.string.len + pad);
1083 break;
1084
1085 case EOT_DB_NUMBER:
1086 warn_overflow_const(e->val.num.offset, e->elem);
1087 isize += e->dup * e->elem;
1088 break;
1089
1090 case EOT_DB_RESERVE:
1091 isize += e->dup;
1092 break;
1093 }
1094
1095 e = e->next;
1096 }
1097
1098 return isize;
1099}
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001100
H. Peter Anvinb20bc732017-03-07 19:23:03 -08001101int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001102{
H. Peter Anvin3360d792007-09-11 04:16:57 +00001103 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001104 enum match_result m;
H. Peter Anvin29651542018-12-18 19:14:40 -08001105 int64_t isize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001106
H. Peter Anvin29651542018-12-18 19:14:40 -08001107 if (instruction->opcode == I_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 return 0;
H. Peter Anvin29651542018-12-18 19:14:40 -08001109 } else if (instruction->opcode == I_EQU) {
1110 define_equ(instruction);
1111 return 0;
1112 } else if (opcode_is_db(instruction->opcode)) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001113 isize = len_extops(instruction->eops);
H. Peter Anvin29651542018-12-18 19:14:40 -08001114 debug_set_db_type(instruction);
1115 return isize;
1116 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001117 const extop *e = instruction->eops;
1118 const char *fname = e->val.string.data;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001119 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001120
H. Peter Anvind81a2352016-09-21 14:03:18 -07001121 len = nasm_file_size_by_path(fname);
1122 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001123 nasm_nonfatal("`incbin': unable to get length of file `%s'",
1124 fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001125 return 0;
1126 }
1127
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001128 e = e->next;
1129 if (e) {
1130 if (len <= (off_t)e->val.num.offset) {
H. Peter Anvind81a2352016-09-21 14:03:18 -07001131 len = 0;
1132 } else {
H. Peter Anvin (Intel)84b852b2019-10-16 14:29:16 -07001133 len -= e->val.num.offset;
1134 e = e->next;
1135 if (e && len > (off_t)e->val.num.offset) {
1136 len = (off_t)e->val.num.offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 }
1138 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001140
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001141 len *= instruction->times;
1142 instruction->times = 1; /* Tell the upper layer to not iterate */
1143
H. Peter Anvind81a2352016-09-21 14:03:18 -07001144 return len;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001145 } else {
H. Peter Anvin29651542018-12-18 19:14:40 -08001146 /* Normal instruction, or RESx */
1147
1148 /* Check to see if we need an address-size prefix */
1149 add_asp(instruction, bits);
1150
1151 m = find_match(&temp, instruction, segment, offset, bits);
1152 if (m != MOK_GOOD)
1153 return -1; /* No match */
1154
1155 isize = calcsize(segment, offset, bits, instruction, temp);
1156 debug_set_type(instruction);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001157 isize = merge_resb(instruction, isize);
H. Peter Anvin29651542018-12-18 19:14:40 -08001158
1159 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001160 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001161}
1162
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001163static void bad_hle_warn(const insn * ins, uint8_t hleok)
1164{
1165 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001166 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001167 static const enum whatwarn warn[2][4] =
1168 {
1169 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
1170 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
1171 };
1172 unsigned int n;
1173
1174 n = (unsigned int)rep_pfx - P_XACQUIRE;
1175 if (n > 1)
1176 return; /* Not XACQUIRE/XRELEASE */
1177
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001178 ww = warn[n][hleok];
1179 if (!is_class(MEMORY, ins->oprs[0].type))
1180 ww = w_inval; /* HLE requires operand 0 to be memory */
1181
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001182 /*!
1183 *!hle [on] invalid HLE prefixes
1184 *! warns about invalid use of the HLE \c{XACQUIRE} or \c{XRELEASE}
1185 *! prefixes.
1186 */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001187 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001188 case w_none:
1189 break;
1190
1191 case w_lock:
1192 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001193 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001194 "%s with this instruction requires lock",
1195 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001196 }
1197 break;
1198
1199 case w_inval:
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001200 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001201 "%s invalid with this instruction",
1202 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001203 break;
1204 }
1205}
1206
H. Peter Anvin507ae032008-10-09 15:37:10 -07001207/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +04001208#define case3(x) case (x): case (x)+1: case (x)+2
1209#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -07001210
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001211static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001212 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001213{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001214 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001215 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001216 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001217 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001218 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001219 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001220 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001221 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001222 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001223 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -07001224 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -07001225 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001226
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001227 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001228 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001229 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001230
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001231 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001232 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001233
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 (void)segment; /* Don't warn that this parameter is unused */
1235 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001236
H. Peter Anvin839eca22007-10-29 23:12:47 -07001237 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001238 c = *codes++;
1239 op1 = (c & 3) + ((opex & 1) << 2);
1240 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1241 opx = &ins->oprs[op1];
1242 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001243
H. Peter Anvin839eca22007-10-29 23:12:47 -07001244 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001245 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 codes += c, length += c;
1247 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001248
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001249 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001250 opex = c;
1251 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001252
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001253 case4(010):
1254 ins->rex |=
1255 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 codes++, length++;
1257 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001258
Jin Kyu Song164d6072013-10-15 19:10:13 -07001259 case4(014):
1260 /* this is an index reg of MIB operand */
1261 mib_index = opx->basereg;
1262 break;
1263
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001264 case4(020):
1265 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 length++;
1267 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001268
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001269 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 length += 2;
1271 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001272
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001273 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001274 if (opx->type & (BITS16 | BITS32 | BITS64))
1275 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 else
1277 length += (bits == 16) ? 2 : 4;
1278 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001279
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001280 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 length += 4;
1282 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001283
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001284 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001285 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001287
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001288 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 length++;
1290 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001291
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001292 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001293 length += 8; /* MOV reg64/imm */
1294 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001295
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001296 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 length += 2;
1298 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001299
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001300 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001301 if (opx->type & (BITS16 | BITS32 | BITS64))
1302 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 else
1304 length += (bits == 16) ? 2 : 4;
1305 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001306
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001307 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 length += 4;
1309 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001310
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001311 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001312 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001314
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001315 case 0172:
1316 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001317 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001318 length++;
1319 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001320
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001321 case4(0174):
1322 length++;
1323 break;
1324
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001325 case4(0240):
1326 ins->rex |= REX_EV;
1327 ins->vexreg = regval(opx);
1328 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1329 ins->vex_cm = *codes++;
1330 ins->vex_wlp = *codes++;
1331 ins->evex_tuple = (*codes++ - 0300);
1332 break;
1333
1334 case 0250:
1335 ins->rex |= REX_EV;
1336 ins->vexreg = 0;
1337 ins->vex_cm = *codes++;
1338 ins->vex_wlp = *codes++;
1339 ins->evex_tuple = (*codes++ - 0300);
1340 break;
1341
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001342 case4(0254):
1343 length += 4;
1344 break;
1345
1346 case4(0260):
1347 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001348 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001349 ins->vex_cm = *codes++;
1350 ins->vex_wlp = *codes++;
1351 break;
1352
1353 case 0270:
1354 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001355 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001356 ins->vex_cm = *codes++;
1357 ins->vex_wlp = *codes++;
1358 break;
1359
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001360 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001361 hleok = c & 3;
1362 break;
1363
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001364 case4(0274):
1365 length++;
1366 break;
1367
1368 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001369 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001370
H. Peter Anvine2c80182005-01-15 22:15:51 +00001371 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001372 if (bits == 64)
1373 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001374 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001376
H. Peter Anvine2c80182005-01-15 22:15:51 +00001377 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001378 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001379 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001380
H. Peter Anvine2c80182005-01-15 22:15:51 +00001381 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001382 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001383
Keith Kaniosb7a89542007-04-12 02:40:54 +00001384 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001385 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1386 has_prefix(ins, PPS_ASIZE, P_A32))
1387 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001388 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001389
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001390 case4(0314):
1391 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001392
H. Peter Anvine2c80182005-01-15 22:15:51 +00001393 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001394 {
1395 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1396 if (pfx == P_O16)
1397 break;
1398 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001399 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001400 else
1401 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001402 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001403 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001404
H. Peter Anvine2c80182005-01-15 22:15:51 +00001405 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001406 {
1407 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1408 if (pfx == P_O32)
1409 break;
1410 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001411 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001412 else
1413 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001415 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001416
H. Peter Anvine2c80182005-01-15 22:15:51 +00001417 case 0322:
1418 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001419
Keith Kaniosb7a89542007-04-12 02:40:54 +00001420 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001421 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001422 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001423
Keith Kaniosb7a89542007-04-12 02:40:54 +00001424 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001425 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001426 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001427
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001428 case 0325:
1429 ins->rex |= REX_NH;
1430 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001431
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001432 case 0326:
1433 break;
1434
H. Peter Anvine2c80182005-01-15 22:15:51 +00001435 case 0330:
1436 codes++, length++;
1437 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001438
H. Peter Anvine2c80182005-01-15 22:15:51 +00001439 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001441
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001442 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001443 case 0333:
1444 length++;
1445 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001446
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001447 case 0334:
1448 ins->rex |= REX_L;
1449 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001450
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001451 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001452 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001453
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001454 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001455 if (!ins->prefixes[PPS_REP])
1456 ins->prefixes[PPS_REP] = P_REP;
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 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001460 if (!ins->prefixes[PPS_REP])
1461 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001462 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001463
H. Peter Anvine2c80182005-01-15 22:15:51 +00001464 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001465 if (!absolute_op(&ins->oprs[0]))
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001466 nasm_nonfatal("attempt to reserve non-constant"
1467 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001468 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001469 nasm_warn(WARN_OTHER, "forward reference in RESx "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001470 "can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001471 else
H. Peter Anvin5358b982018-12-18 18:06:26 -08001472 length += ins->oprs[0].offset * resb_bytes(ins->opcode);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001474
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001475 case 0341:
1476 if (!ins->prefixes[PPS_WAIT])
1477 ins->prefixes[PPS_WAIT] = P_WAIT;
1478 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001479
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001480 case 0360:
1481 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001482
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001483 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001484 length++;
1485 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001486
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001487 case 0364:
1488 case 0365:
1489 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001490
Keith Kanios48af1772007-08-17 07:37:52 +00001491 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001492 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001493 length++;
1494 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001495
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001496 case 0370:
1497 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001498 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001499
H. Peter Anvine2c80182005-01-15 22:15:51 +00001500 case 0373:
1501 length++;
1502 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001503
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001504 case 0374:
1505 eat = EA_XMMVSIB;
1506 break;
1507
1508 case 0375:
1509 eat = EA_YMMVSIB;
1510 break;
1511
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001512 case 0376:
1513 eat = EA_ZMMVSIB;
1514 break;
1515
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001516 case4(0100):
1517 case4(0110):
1518 case4(0120):
1519 case4(0130):
1520 case4(0200):
1521 case4(0204):
1522 case4(0210):
1523 case4(0214):
1524 case4(0220):
1525 case4(0224):
1526 case4(0230):
1527 case4(0234):
1528 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001529 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001530 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001531 opflags_t rflags;
1532 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001533 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001534
Keith Kaniosb7a89542007-04-12 02:40:54 +00001535 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001536
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001537 if (c <= 0177) {
1538 /* pick rfield from operand b (opx) */
1539 rflags = regflag(opx);
1540 rfield = nasm_regvals[opx->basereg];
1541 } else {
1542 rflags = 0;
1543 rfield = c & 7;
1544 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001545
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001546 /* EVEX.b1 : evex_brerop contains the operand position */
1547 op_er_sae = (ins->evex_brerop >= 0 ?
1548 &ins->oprs[ins->evex_brerop] : NULL);
1549
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001550 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1551 /* set EVEX.b */
1552 ins->evex_p[2] |= EVEX_P2B;
1553 if (op_er_sae->decoflags & ER) {
1554 /* set EVEX.RC (rounding control) */
1555 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1556 & EVEX_P2RC;
1557 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001558 } else {
1559 /* set EVEX.L'L (vector length) */
1560 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001561 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001562 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001563 /* set EVEX.b */
1564 ins->evex_p[2] |= EVEX_P2B;
1565 }
1566 }
1567
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001568 if (itemp_has(temp, IF_MIB)) {
1569 opy->eaflags |= EAF_MIB;
1570 /*
1571 * if a separate form of MIB (ICC style) is used,
1572 * the index reg info is merged into mem operand
1573 */
1574 if (mib_index != R_none) {
1575 opy->indexreg = mib_index;
1576 opy->scale = 1;
1577 opy->hintbase = mib_index;
1578 opy->hinttype = EAH_NOTBASE;
1579 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001580 }
1581
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001582 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001583 rfield, rflags, ins, &errmsg) != eat) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001584 nasm_nonfatal("%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001586 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001587 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001588 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001589 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001590 }
1591 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001592
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001594 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001595 ": instruction code \\%o (0x%02X) given", c, c);
1596 break;
1597 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001598 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001599
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001600 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001601
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001602 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001603 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001604 nasm_nonfatal("instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001605 return -1;
1606 }
1607 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001608 }
1609
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001610 switch (ins->prefixes[PPS_VEX]) {
1611 case P_EVEX:
1612 if (!(ins->rex & REX_EV))
1613 return -1;
1614 break;
1615 case P_VEX3:
1616 case P_VEX2:
1617 if (!(ins->rex & REX_V))
1618 return -1;
1619 break;
1620 default:
1621 break;
1622 }
1623
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001624 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001625 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001626
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001627 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001628 nasm_nonfatal("cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001629 return -1;
1630 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001631 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001632 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001633 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001634 ins->rex &= ~REX_W;
1635 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001636 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001637 ins->rex |= REX_W;
1638 bad32 &= ~REX_W;
1639 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001640 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001641 /* Follow REX_W */
1642 break;
1643 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001644
H. Peter Anvinfc561202011-07-07 16:58:22 -07001645 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001646 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001647 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001648 } else if (!(ins->rex & REX_EV) &&
1649 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001650 nasm_nonfatal("invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001651 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001653 if (ins->rex & REX_EV)
1654 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001655 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1656 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001657 length += 3;
1658 else
1659 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001660 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001661 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001662 nasm_nonfatal("cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001663 return -1;
1664 } else if (bits == 64) {
1665 length++;
1666 } else if ((ins->rex & REX_L) &&
1667 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001668 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001669 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001670 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001671 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001672 length++;
1673 } else {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001674 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001675 return -1;
1676 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001677 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001678
1679 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001680 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001681 /*!
H. Peter Anvin (Intel)be99ebd2018-12-13 22:12:37 -08001682 *!lock [on] LOCK prefix on unlockable instructions
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001683 *! warns about \c{LOCK} prefixes on unlockable instructions.
1684 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001685 nasm_warn(WARN_LOCK | ERR_PASS2 , "instruction is not lockable");
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001686 }
1687
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001688 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001689
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001690 /*
1691 * when BND prefix is set by DEFAULT directive,
1692 * BND prefix is added to every appropriate instruction line
1693 * unless it is overridden by NOBND prefix.
1694 */
1695 if (globalbnd &&
1696 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1697 ins->prefixes[PPS_REP] = P_BND;
1698
H. Peter Anvina77692b2016-09-20 14:04:33 -07001699 /*
1700 * Add length of legacy prefixes
1701 */
1702 length += emit_prefix(NULL, bits, ins);
1703
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001704 return length;
1705}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001706
H. Peter Anvina77692b2016-09-20 14:04:33 -07001707static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001708{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001709 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001710 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001711 !(ins->rex & (REX_V | REX_EV)) &&
1712 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001713 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1714 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001715 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001716 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001717 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001718}
1719
H. Peter Anvina77692b2016-09-20 14:04:33 -07001720static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1721{
1722 int bytes = 0;
1723 int j;
1724
1725 for (j = 0; j < MAXPREFIX; j++) {
1726 uint8_t c = 0;
1727 switch (ins->prefixes[j]) {
1728 case P_WAIT:
1729 c = 0x9B;
1730 break;
1731 case P_LOCK:
1732 c = 0xF0;
1733 break;
1734 case P_REPNE:
1735 case P_REPNZ:
1736 case P_XACQUIRE:
1737 case P_BND:
1738 c = 0xF2;
1739 break;
1740 case P_REPE:
1741 case P_REPZ:
1742 case P_REP:
1743 case P_XRELEASE:
1744 c = 0xF3;
1745 break;
1746 case R_CS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001747 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001748 nasm_warn(WARN_OTHER|ERR_PASS2, "cs segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001749 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001750 c = 0x2E;
1751 break;
1752 case R_DS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001753 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001754 nasm_warn(WARN_OTHER|ERR_PASS2, "ds segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001755 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001756 c = 0x3E;
1757 break;
1758 case R_ES:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001759 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001760 nasm_warn(WARN_OTHER|ERR_PASS2, "es segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001761 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001762 c = 0x26;
1763 break;
1764 case R_FS:
1765 c = 0x64;
1766 break;
1767 case R_GS:
1768 c = 0x65;
1769 break;
1770 case R_SS:
1771 if (bits == 64) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001772 nasm_warn(WARN_OTHER|ERR_PASS2, "ss segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001773 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001774 }
1775 c = 0x36;
1776 break;
1777 case R_SEGR6:
1778 case R_SEGR7:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001779 nasm_nonfatal("segr6 and segr7 cannot be used as prefixes");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001780 break;
1781 case P_A16:
1782 if (bits == 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001783 nasm_nonfatal("16-bit addressing is not supported "
1784 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001785 } else if (bits != 16)
1786 c = 0x67;
1787 break;
1788 case P_A32:
1789 if (bits != 32)
1790 c = 0x67;
1791 break;
1792 case P_A64:
1793 if (bits != 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001794 nasm_nonfatal("64-bit addressing is only supported "
1795 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001796 }
1797 break;
1798 case P_ASP:
1799 c = 0x67;
1800 break;
1801 case P_O16:
1802 if (bits != 16)
1803 c = 0x66;
1804 break;
1805 case P_O32:
1806 if (bits == 16)
1807 c = 0x66;
1808 break;
1809 case P_O64:
1810 /* REX.W */
1811 break;
1812 case P_OSP:
1813 c = 0x66;
1814 break;
1815 case P_EVEX:
1816 case P_VEX3:
1817 case P_VEX2:
1818 case P_NOBND:
1819 case P_none:
1820 break;
1821 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001822 nasm_panic("invalid instruction prefix");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001823 }
1824 if (c) {
1825 if (data)
1826 out_rawbyte(data, c);
1827 bytes++;
1828 }
1829 }
1830 return bytes;
1831}
1832
1833static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001834{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001835 uint8_t c;
1836 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001837 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001838 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001839 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001840 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001841 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001842 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001843 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001844 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001845 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001846
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001847 ins->rex_done = false;
1848
H. Peter Anvina77692b2016-09-20 14:04:33 -07001849 emit_prefix(data, bits, ins);
1850
H. Peter Anvin839eca22007-10-29 23:12:47 -07001851 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001852 c = *codes++;
1853 op1 = (c & 3) + ((opex & 1) << 2);
1854 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1855 opx = &ins->oprs[op1];
1856 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001857
H. Peter Anvina77692b2016-09-20 14:04:33 -07001858
H. Peter Anvin839eca22007-10-29 23:12:47 -07001859 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 case 01:
1861 case 02:
1862 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001863 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001864 emit_rex(data, ins);
1865 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001868
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001869 case 05:
1870 case 06:
1871 case 07:
1872 opex = c;
1873 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001874
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001875 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001876 emit_rex(data, ins);
1877 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001879
Jin Kyu Song164d6072013-10-15 19:10:13 -07001880 case4(014):
1881 break;
1882
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001883 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001884 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001885 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001886
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001887 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001888 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001889 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001890
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001891 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001892 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001893 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001894
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001895 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001896 if (opx->type & (BITS16 | BITS32))
1897 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001898 else
1899 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001900 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001902
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001903 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001904 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001905 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001906
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001907 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001908 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001909 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001910 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001911
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001912 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001913 if (opx->segment == data->segment) {
1914 int64_t delta = opx->offset - data->offset
1915 - (data->inslen - data->insoffs);
1916 if (delta > 127 || delta < -128)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001917 nasm_nonfatal("short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001918 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001919 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001921
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001922 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001923 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001924 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001925
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001926 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001927 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001929
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001930 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001931 if (opx->type & (BITS16 | BITS32 | BITS64))
1932 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 else
1934 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001935
1936 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001938
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001939 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001940 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001941 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001942
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001943 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001944 if (opx->segment == NO_SEG)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001945 nasm_nonfatal("value referenced by FAR is not relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001946 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001947 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001948
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001949 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001950 {
1951 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1952 const struct operand *opy;
1953
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001954 c = *codes++;
1955 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001956 opy = &ins->oprs[c & 7];
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001957 if (!absolute_op(opy))
1958 nasm_nonfatal("non-absolute expression not permitted "
1959 "as argument %d", c & 7);
1960 else if (opy->offset & ~mask)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001961 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001962 "is4 argument exceeds bounds");
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001963 c = opy->offset & mask;
1964 goto emit_is4;
1965 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001966
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001967 case 0173:
1968 c = *codes++;
1969 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001970 c &= 15;
1971 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001972
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001973 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001974 c = 0;
1975 emit_is4:
1976 r = nasm_regvals[opx->basereg];
1977 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001978 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001979
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001980 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001981 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001982 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001983 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001984 "signed dword immediate exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001985 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001986 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001987 break;
1988
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001989 case4(0240):
1990 case 0250:
1991 codes += 3;
1992 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1993 EVEX_P2Z | EVEX_P2AAA, 2);
1994 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1995 bytes[0] = 0x62;
1996 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001997 bytes[1] = ((((ins->rex & 7) << 5) |
1998 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001999 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002000 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
2001 ((~ins->vexreg & 15) << 3) |
2002 (1 << 2) | (ins->vex_wlp & 3);
2003 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07002004 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002005 break;
2006
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002007 case4(0260):
2008 case 0270:
2009 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002010 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
2011 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002012 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
2013 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
2014 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07002015 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07002016 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002017 } else {
2018 bytes[0] = 0xc5;
2019 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
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, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002022 }
2023 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07002024
H. Peter Anvine014f352012-02-25 22:35:19 -08002025 case 0271:
2026 case 0272:
2027 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08002028 break;
2029
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002030 case4(0274):
2031 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08002032 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002033 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07002034
H. Peter Anvin64e87d02017-03-01 13:45:02 -08002035 if (absolute_op(opx)) {
2036 if (ins->rex & REX_W)
2037 s = 64;
2038 else if (ins->prefixes[PPS_OSIZE] == P_O16)
2039 s = 16;
2040 else if (ins->prefixes[PPS_OSIZE] == P_O32)
2041 s = 32;
2042 else
2043 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07002044
H. Peter Anvin64e87d02017-03-01 13:45:02 -08002045 um = (uint64_t)2 << (s-1);
2046 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08002047
H. Peter Anvin64e87d02017-03-01 13:45:02 -08002048 if (uv > 127 && uv < (uint64_t)-128 &&
2049 (uv < um-128 || uv > um-1)) {
2050 /* If this wasn't explicitly byte-sized, warn as though we
2051 * had fallen through to the imm16/32/64 case.
2052 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08002053 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin64e87d02017-03-01 13:45:02 -08002054 "%s value exceeds bounds",
2055 (opx->type & BITS8) ? "signed byte" :
2056 s == 16 ? "word" :
2057 s == 32 ? "dword" :
2058 "signed dword");
2059 }
2060
2061 /* Output as a raw byte to avoid byte overflow check */
2062 out_rawbyte(data, (uint8_t)uv);
2063 } else {
2064 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002065 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07002066 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002067 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07002068
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002069 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002070 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002071
H. Peter Anvine2c80182005-01-15 22:15:51 +00002072 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002073 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
2074 out_rawbyte(data, 0x67);
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 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002078 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
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 0312:
2083 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002084
Keith Kaniosb7a89542007-04-12 02:40:54 +00002085 case 0313:
Keith Kaniosb7a89542007-04-12 02:40:54 +00002086 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002087
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002088 case4(0314):
2089 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08002090
H. Peter Anvine2c80182005-01-15 22:15:51 +00002091 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002092 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002093 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002094
H. Peter Anvine2c80182005-01-15 22:15:51 +00002095 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07002096 case 0323:
2097 break;
2098
Keith Kaniosb7a89542007-04-12 02:40:54 +00002099 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002100 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002101 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07002102
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002103 case 0325:
2104 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07002105
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04002106 case 0326:
2107 break;
2108
H. Peter Anvine2c80182005-01-15 22:15:51 +00002109 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002110 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002111 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002112
H. Peter Anvine2c80182005-01-15 22:15:51 +00002113 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002114 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002115
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002116 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002117 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002118 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002119 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002120
Keith Kanios48af1772007-08-17 07:37:52 +00002121 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002122 if (ins->rex & REX_R)
2123 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00002124 ins->rex &= ~(REX_L|REX_R);
2125 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00002126
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002127 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002128 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002129
H. Peter Anvin962e3052008-08-28 17:47:16 -07002130 case 0336:
2131 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002132 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07002133
H. Peter Anvine2c80182005-01-15 22:15:51 +00002134 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002135 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002136 nasm_panic("non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07002137
H. Peter Anvin5358b982018-12-18 18:06:26 -08002138 out_reserve(data, ins->oprs[0].offset * resb_bytes(ins->opcode));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002139 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002140
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002141 case 0341:
2142 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08002143
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002144 case 0360:
2145 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002146
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002147 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002148 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002149 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002150
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002151 case 0364:
2152 case 0365:
2153 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002154
Keith Kanios48af1772007-08-17 07:37:52 +00002155 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002156 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002157 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00002158 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002159
Jin Kyu Song03041092013-10-15 19:38:51 -07002160 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002161 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002162
H. Peter Anvine2c80182005-01-15 22:15:51 +00002163 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002164 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002165 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002166
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002167 case 0374:
2168 eat = EA_XMMVSIB;
2169 break;
2170
2171 case 0375:
2172 eat = EA_YMMVSIB;
2173 break;
2174
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002175 case 0376:
2176 eat = EA_ZMMVSIB;
2177 break;
2178
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002179 case4(0100):
2180 case4(0110):
2181 case4(0120):
2182 case4(0130):
2183 case4(0200):
2184 case4(0204):
2185 case4(0210):
2186 case4(0214):
2187 case4(0220):
2188 case4(0224):
2189 case4(0230):
2190 case4(0234):
2191 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002192 ea ea_data;
2193 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002194 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002195 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002196 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07002197
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002198 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002199 /* pick rfield from operand b (opx) */
2200 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07002201 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002202 } else {
2203 /* rfield is constant */
2204 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002205 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002206 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002207
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002208 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002209 rfield, rflags, ins, &errmsg) != eat)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002210 nasm_nonfatal("%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07002211
H. Peter Anvine2c80182005-01-15 22:15:51 +00002212 p = bytes;
2213 *p++ = ea_data.modrm;
2214 if (ea_data.sib_present)
2215 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07002216 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002217
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002218 /*
2219 * Make sure the address gets the right offset in case
2220 * the line breaks in the .lst file (BR 1197827)
2221 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002222
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002223 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002224 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07002225 if (ea_data.disp8) {
2226 out_rawbyte(data, ea_data.disp8);
2227 } else if (ea_data.rip) {
2228 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002229 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002230 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002231
H. Peter Anvina77692b2016-09-20 14:04:33 -07002232 if (overflow_general(opy->offset, asize) ||
2233 signed_bits(opy->offset, ins->addr_size) !=
2234 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08002235 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002236
H. Peter Anvina77692b2016-09-20 14:04:33 -07002237 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07002238 (asize > ea_data.bytes)
2239 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002240 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002241 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002242 }
2243 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07002244
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002245 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07002246 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002247 ": instruction code \\%o (0x%02X) given", c, c);
2248 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002249 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07002250 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002251}
2252
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002253static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002254{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002255 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002256 nasm_panic("invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002257 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002258}
2259
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00002260static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002261{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002262 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002263 nasm_panic("invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002264 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002265}
2266
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002267static int op_rexflags(const operand * o, int mask)
2268{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002269 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002270 int val;
2271
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002272 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002273 nasm_panic("invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002274
H. Peter Anvina4835d42008-05-20 14:21:29 -07002275 flags = nasm_reg_flags[o->basereg];
2276 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002277
2278 return rexflags(val, flags, mask);
2279}
2280
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002281static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002282{
2283 int rex = 0;
2284
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002285 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002286 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002287 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002288 rex |= REX_W;
2289 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2290 rex |= REX_H;
2291 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2292 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002293
2294 return rex & mask;
2295}
2296
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002297static int evexflags(int val, decoflags_t deco,
2298 int mask, uint8_t byte)
2299{
2300 int evex = 0;
2301
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002302 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002303 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002304 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002305 evex |= (EVEX_P0RP | EVEX_P0X);
2306 break;
2307 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002308 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002309 evex |= EVEX_P2VP;
2310 if (deco & Z)
2311 evex |= EVEX_P2Z;
2312 if (deco & OPMASK_MASK)
2313 evex |= deco & EVEX_P2AAA;
2314 break;
2315 }
2316 return evex & mask;
2317}
2318
2319static int op_evexflags(const operand * o, int mask, uint8_t byte)
2320{
2321 int val;
2322
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002323 val = nasm_regvals[o->basereg];
2324
2325 return evexflags(val, o->decoflags, mask, byte);
2326}
2327
H. Peter Anvin23595f52009-07-25 17:44:25 -07002328static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002329 insn *instruction,
2330 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002331{
2332 const struct itemplate *temp;
2333 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002334 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002335 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002336 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002337 int i;
2338
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002339 /* broadcasting uses a different data element size */
2340 for (i = 0; i < instruction->operands; i++)
2341 if (i == broadcast)
2342 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2343 else
2344 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002345
2346 merr = MERR_INVALOP;
2347
2348 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002349 temp->opcode != I_none; temp++) {
2350 m = matches(temp, instruction, bits);
2351 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002352 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002353 m = MOK_GOOD;
2354 else
2355 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002356 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002357 /*
2358 * Missing operand size and a candidate for fuzzy matching...
2359 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002360 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002361 if (i == broadcast)
2362 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2363 else
2364 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002365 opsizemissing = true;
2366 }
2367 if (m > merr)
2368 merr = m;
2369 if (merr == MOK_GOOD)
2370 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002371 }
2372
2373 /* No match, but see if we can get a fuzzy operand size match... */
2374 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002375 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002376
2377 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002378 /*
2379 * We ignore extrinsic operand sizes on registers, so we should
2380 * never try to fuzzy-match on them. This also resolves the case
2381 * when we have e.g. "xmmrm128" in two different positions.
2382 */
2383 if (is_class(REGISTER, instruction->oprs[i].type))
2384 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002385
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002386 /* This tests if xsizeflags[i] has more than one bit set */
2387 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2388 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002389
Jin Kyu Song7903c072013-10-30 03:00:12 -07002390 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002391 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002392 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2393 BITS32 : BITS64);
2394 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002395 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002396 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002397 }
2398
2399 /* Try matching again... */
2400 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002401 temp->opcode != I_none; temp++) {
2402 m = matches(temp, instruction, bits);
2403 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002404 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002405 m = MOK_GOOD;
2406 else
2407 m = MERR_INVALOP;
2408 }
2409 if (m > merr)
2410 merr = m;
2411 if (merr == MOK_GOOD)
2412 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002413 }
2414
H. Peter Anvina81655b2009-07-25 18:15:28 -07002415done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002416 *tempp = temp;
2417 return merr;
2418}
2419
Mark Charneydcaef4b2014-10-09 13:45:17 -04002420static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2421{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002422 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002423 uint8_t brcast_num;
2424
Mark Charneydcaef4b2014-10-09 13:45:17 -04002425 if (brsize > BITS64)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002426 nasm_fatal("size of broadcasting element is greater than 64 bits");
Mark Charneydcaef4b2014-10-09 13:45:17 -04002427
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002428 /*
2429 * The shift term is to take care of the extra BITS80 inserted
2430 * between BITS64 and BITS128.
2431 */
2432 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2433 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002434
2435 return brcast_num;
2436}
2437
H. Peter Anvin65289e82009-07-25 17:25:11 -07002438static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002439 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002440{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002441 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002442 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002443 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002444
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002445 /*
2446 * Check the opcode
2447 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002448 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002449 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002450
2451 /*
2452 * Count the operands
2453 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002454 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002455 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002456
2457 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002458 * Is it legal?
2459 */
Chang S. Baea5786342018-08-15 23:22:21 +03002460 if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002461 return MERR_INVALOP;
2462
2463 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002464 * {evex} available?
2465 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002466 switch (instruction->prefixes[PPS_VEX]) {
2467 case P_EVEX:
2468 if (!itemp_has(itemp, IF_EVEX))
2469 return MERR_ENCMISMATCH;
2470 break;
2471 case P_VEX3:
2472 case P_VEX2:
2473 if (!itemp_has(itemp, IF_VEX))
2474 return MERR_ENCMISMATCH;
2475 break;
2476 default:
2477 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002478 }
2479
2480 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002481 * Check that no spurious colons or TOs are present
2482 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002483 for (i = 0; i < itemp->operands; i++)
2484 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002485 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002486
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002487 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002488 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002489 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002490 switch (itemp_smask(itemp)) {
2491 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002492 asize = BITS8;
2493 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002494 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002495 asize = BITS16;
2496 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002497 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002498 asize = BITS32;
2499 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002500 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002501 asize = BITS64;
2502 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002503 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002504 asize = BITS128;
2505 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002506 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002507 asize = BITS256;
2508 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002509 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002510 asize = BITS512;
2511 break;
H. Peter Anvin (Intel)02b60dd2019-08-14 15:23:00 -07002512 case IF_GENBIT(IF_ANYSIZE):
2513 asize = SIZE_MASK;
2514 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002515 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002516 switch (bits) {
2517 case 16:
2518 asize = BITS16;
2519 break;
2520 case 32:
2521 asize = BITS32;
2522 break;
2523 case 64:
2524 asize = BITS64;
2525 break;
2526 default:
2527 asize = 0;
2528 break;
2529 }
2530 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002531 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002532 asize = 0;
2533 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002534 }
2535
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002536 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002537 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002538 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002539 memset(size, 0, sizeof size);
2540 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002541 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002542 /* S- flags apply to all operands */
2543 for (i = 0; i < MAX_OPERANDS; i++)
2544 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002545 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002546
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002547 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002548 * Check that the operand flags all match up,
2549 * it's a bit tricky so lets be verbose:
2550 *
2551 * 1) Find out the size of operand. If instruction
2552 * doesn't have one specified -- we're trying to
2553 * guess it either from template (IF_S* flag) or
2554 * from code bits.
2555 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002556 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002557 * template has an operand size specified AND this size differ
2558 * from which instruction has (perhaps we got it from code bits)
2559 * we are:
2560 * a) Check that only size of instruction and operand is differ
2561 * other characteristics do match
2562 * b) Perhaps it's a register specified in instruction so
2563 * for such a case we just mark that operand as "size
2564 * missing" and this will turn on fuzzy operand size
2565 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002566 */
2567 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002568 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002569 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002570 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002571 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002572 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002573 opflags_t template_opsize, insn_opsize;
2574
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002575 if (!(type & SIZE_MASK))
2576 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002577
Jin Kyu Song7903c072013-10-30 03:00:12 -07002578 insn_opsize = type & SIZE_MASK;
2579 if (!is_broadcast) {
2580 template_opsize = itemp->opd[i] & SIZE_MASK;
2581 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002582 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2583
2584 if (~ideco & BRDCAST_MASK)
2585 return MERR_BRNOTHERE;
2586
Jin Kyu Song7903c072013-10-30 03:00:12 -07002587 /*
2588 * when broadcasting, the element size depends on
2589 * the instruction type. decorator flag should match.
2590 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002591 if (deco_brsize) {
2592 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002593 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002594 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002595 } else {
2596 template_opsize = 0;
2597 }
2598 }
2599
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002600 if (~ideco & deco & OPMASK_MASK)
2601 return MERR_MASKNOTHERE;
2602
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002603 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2604 return MERR_DECONOTHERE;
2605
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002606 if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002607 return MERR_INVALOP;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002608
2609 if (~itemp->opd[i] & type & REGSET_MASK)
2610 return (itemp->opd[i] & REGSET_MASK)
2611 ? MERR_REGSETSIZE : MERR_REGSET;
2612
2613 if (template_opsize) {
Jin Kyu Song7903c072013-10-30 03:00:12 -07002614 if (template_opsize != insn_opsize) {
2615 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002616 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002617 } else if (!is_class(REGISTER, type)) {
2618 /*
2619 * Note: we don't honor extrinsic operand sizes for registers,
2620 * so "missing operand size" for a register should be
2621 * considered a wildcard match rather than an error.
2622 */
2623 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002624 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002625 } else if (is_broadcast &&
2626 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002627 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002628 /*
2629 * broadcasting opsize matches but the number of repeated memory
2630 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002631 * if 64b double precision float is broadcasted to ymm (256b),
2632 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002633 */
2634 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002635 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002636 }
2637 }
2638
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002639 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002640 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002641
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002642 /*
2643 * Check operand sizes
2644 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002645 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2646 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002647 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002648 asize = itemp->opd[i] & SIZE_MASK;
2649 if (asize) {
2650 for (i = 0; i < oprs; i++)
2651 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002652 break;
2653 }
2654 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002655 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002656 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002657 }
2658
Keith Kaniosb7a89542007-04-12 02:40:54 +00002659 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002660 if (!(itemp->opd[i] & SIZE_MASK) &&
2661 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002662 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002663 }
2664
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002665 /*
2666 * Check template is okay at the set cpu level
2667 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002668 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002669 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002670
Keith Kaniosb7a89542007-04-12 02:40:54 +00002671 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002672 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002673 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002674 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002675 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002676
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002677 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002678 * If we have a HLE prefix, look for the NOHLE flag
2679 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002680 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002681 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2682 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2683 return MERR_BADHLE;
2684
2685 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002686 * Check if special handling needed for Jumps
2687 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002688 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002689 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002690
Jin Kyu Song03041092013-10-15 19:38:51 -07002691 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002692 * Check if BND prefix is allowed.
2693 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002694 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002695 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002696 (has_prefix(instruction, PPS_REP, P_BND) ||
2697 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002698 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002699 else if (itemp_has(itemp, IF_BND) &&
2700 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2701 has_prefix(instruction, PPS_REP, P_REPNZ)))
2702 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002703
H. Peter Anvin60926242009-07-26 16:25:38 -07002704 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002705}
2706
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002707/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002708 * Check if ModR/M.mod should/can be 01.
2709 * - EAF_BYTEOFFS is set
2710 * - offset can fit in a byte when EVEX is not used
2711 * - offset can be compressed when EVEX is used
2712 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002713#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2714 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2715 is_disp8n(input, ins, &output->disp8) : \
2716 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2717 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002718
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002719static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002720 int rfield, opflags_t rflags, insn *ins,
2721 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002722{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002723 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002724 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002725 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002726
H. Peter Anvin8f622462017-04-02 19:02:29 -07002727 *errmsg = "invalid effective address"; /* Default error message */
2728
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002729 output->type = EA_SCALAR;
2730 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002731 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002732
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002733 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002734 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002735 /* EVEX.R' flag for the REG operand */
2736 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002737
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002738 if (is_class(REGISTER, input->type)) {
2739 /*
2740 * It's a direct register.
2741 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002742 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002743 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002744
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002745 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002746 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002747
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002748 /* broadcasting is not available with a direct register operand. */
2749 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002750 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002751 goto err;
2752 }
2753
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002754 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002755 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002756 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002757 output->bytes = 0; /* no offset necessary either */
2758 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2759 } else {
2760 /*
2761 * It's a memory reference.
2762 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002763
2764 /* Embedded rounding or SAE is not available with a mem ref operand. */
2765 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002766 *errmsg = "embedded rounding is available only with "
2767 "register-register operations";
2768 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002769 }
2770
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002771 if (input->basereg == -1 &&
2772 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002773 /*
2774 * It's a pure offset.
2775 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002776 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002777 if (input->segment == NO_SEG ||
2778 (input->opflags & OPFLAG_RELATIVE)) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08002779 nasm_warn(WARN_OTHER|ERR_PASS2, "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002780 input->type &= ~IP_REL;
2781 input->type |= MEMORY;
2782 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002783 }
2784
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002785 if (bits == 64 &&
2786 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002787 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002788 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002789 }
2790
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002791 if (eaflags & EAF_BYTEOFFS ||
2792 (eaflags & EAF_WORDOFFS &&
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002793 input->disp_size != (addrbits != 16 ? 32 : 16)))
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002794 nasm_warn(WARN_OTHER, "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002795
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002796 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002797 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002798 output->sib = GEN_SIB(0, 4, 5);
2799 output->bytes = 4;
2800 output->modrm = GEN_MODRM(0, rfield, 4);
2801 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002802 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002803 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002804 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002805 output->modrm = GEN_MODRM(0, rfield,
2806 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002807 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002808 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002809 } else {
2810 /*
2811 * It's an indirection.
2812 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002813 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002814 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002815 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002816 int t, it, bt; /* register numbers */
2817 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002818
H. Peter Anvine2c80182005-01-15 22:15:51 +00002819 if (s == 0)
2820 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002821
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002822 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002823 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002824 ix = nasm_reg_flags[i];
2825 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002826 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002827 ix = 0;
2828 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002829
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002830 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002831 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002832 bx = nasm_reg_flags[b];
2833 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002834 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002835 bx = 0;
2836 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002837
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002838 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002839 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002840 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002841 int32_t o = input->offset;
2842 int mod, scale, index, base;
2843
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002844 /*
2845 * For a vector SIB, one has to be a vector and the other,
2846 * if present, a GPR. The vector must be the index operand.
2847 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002848 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002849 if (s == 0)
2850 s = 1;
2851 else if (s != 1)
2852 goto err;
2853
2854 t = bt, bt = it, it = t;
2855 x = bx, bx = ix, ix = x;
2856 }
2857
2858 if (bt != -1) {
2859 if (REG_GPR & ~bx)
2860 goto err;
2861 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2862 sok &= bx;
2863 else
2864 goto err;
2865 }
2866
2867 /*
2868 * While we're here, ensure the user didn't specify
2869 * WORD or QWORD
2870 */
2871 if (input->disp_size == 16 || input->disp_size == 64)
2872 goto err;
2873
2874 if (addrbits == 16 ||
2875 (addrbits == 32 && !(sok & BITS32)) ||
2876 (addrbits == 64 && !(sok & BITS64)))
2877 goto err;
2878
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002879 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2880 : ((ix & YMMREG & ~REG_EA)
2881 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002882
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002883 output->rex |= rexflags(it, ix, REX_X);
2884 output->rex |= rexflags(bt, bx, REX_B);
2885 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002886
2887 index = it & 7; /* it is known to be != -1 */
2888
2889 switch (s) {
2890 case 1:
2891 scale = 0;
2892 break;
2893 case 2:
2894 scale = 1;
2895 break;
2896 case 4:
2897 scale = 2;
2898 break;
2899 case 8:
2900 scale = 3;
2901 break;
2902 default: /* then what the smeg is it? */
2903 goto err; /* panic */
2904 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002905
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002906 if (bt == -1) {
2907 base = 5;
2908 mod = 0;
2909 } else {
2910 base = (bt & 7);
2911 if (base != REG_NUM_EBP && o == 0 &&
2912 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002913 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002914 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002915 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002916 mod = 1;
2917 else
2918 mod = 2;
2919 }
2920
2921 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002922 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2923 output->modrm = GEN_MODRM(mod, rfield, 4);
2924 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002925 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002926 /*
2927 * it must be a 32/64-bit memory reference. Firstly we have
2928 * to check that all registers involved are type E/Rxx.
2929 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002930 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002931 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002932
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002933 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002934 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2935 sok &= ix;
2936 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002937 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002938 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002939
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002940 if (bt != -1) {
2941 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002942 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002943 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002944 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002945 sok &= bx;
2946 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002947
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002948 /*
2949 * While we're here, ensure the user didn't specify
2950 * WORD or QWORD
2951 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002952 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002953 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002954
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002955 if (addrbits == 16 ||
2956 (addrbits == 32 && !(sok & BITS32)) ||
2957 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002958 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002959
Keith Kaniosb7a89542007-04-12 02:40:54 +00002960 /* now reorganize base/index */
2961 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002962 ((hb == b && ht == EAH_NOTBASE) ||
2963 (hb == i && ht == EAH_MAKEBASE))) {
2964 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002965 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002966 x = bx, bx = ix, ix = x;
2967 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002968
Jin Kyu Song164d6072013-10-15 19:10:13 -07002969 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002970 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002971 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002972 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002973 if (eaflags & EAF_MIB) {
2974 /* only for mib operands */
2975 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2976 /*
2977 * make a single reg index [reg*1].
2978 * gas uses this form for an explicit index register.
2979 */
2980 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2981 }
2982 if ((ht == EAH_SUMMED) && bt == -1) {
2983 /* separate once summed index into [base, index] */
2984 bt = it, bx = ix, s--;
2985 }
2986 } else {
2987 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002988 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002989 s == 3 || s == 5 || s == 9) && bt == -1) {
2990 /* convert 3*EAX to EAX+2*EAX */
2991 bt = it, bx = ix, s--;
2992 }
2993 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002994 (eaflags & EAF_TIMESTWO) &&
2995 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002996 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002997 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002998 * to sib format with 0x0 displacement - [EAX*1+0].
2999 */
3000 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
3001 }
3002 }
Keith Kanios48af1772007-08-17 07:37:52 +00003003 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003004 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00003005 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003006 x = ix, ix = bx, bx = x;
3007 }
3008 if (it == REG_NUM_ESP ||
3009 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003010 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003011
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003012 output->rex |= rexflags(it, ix, REX_X);
3013 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00003014
Keith Kanios48af1772007-08-17 07:37:52 +00003015 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003016 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003017 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07003018
Keith Kaniosb7a89542007-04-12 02:40:54 +00003019 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00003020 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00003021 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003022 } else {
3023 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003024 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003025 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003026 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00003027 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003028 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00003029 mod = 1;
3030 else
3031 mod = 2;
3032 }
H. Peter Anvinea838272002-04-30 20:51:53 +00003033
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003034 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003035 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
3036 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003037 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003038 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07003040
Keith Kaniosb7a89542007-04-12 02:40:54 +00003041 if (it == -1)
3042 index = 4, s = 1;
3043 else
3044 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07003045
H. Peter Anvine2c80182005-01-15 22:15:51 +00003046 switch (s) {
3047 case 1:
3048 scale = 0;
3049 break;
3050 case 2:
3051 scale = 1;
3052 break;
3053 case 4:
3054 scale = 2;
3055 break;
3056 case 8:
3057 scale = 3;
3058 break;
3059 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003060 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003061 }
H. Peter Anvin70653092007-10-19 14:42:29 -07003062
Keith Kaniosb7a89542007-04-12 02:40:54 +00003063 if (bt == -1) {
3064 base = 5;
3065 mod = 0;
3066 } else {
3067 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003068 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003069 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003070 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00003071 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003072 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00003073 mod = 1;
3074 else
3075 mod = 2;
3076 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003077
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003078 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003079 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
3080 output->modrm = GEN_MODRM(mod, rfield, 4);
3081 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003082 }
3083 } else { /* it's 16-bit */
3084 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003085 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07003086
Keith Kaniosb7a89542007-04-12 02:40:54 +00003087 /* check for 64-bit long mode */
3088 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003089 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003090
H. Peter Anvine2c80182005-01-15 22:15:51 +00003091 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003092 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
3093 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003094 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003095
Keith Kaniosb7a89542007-04-12 02:40:54 +00003096 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003097 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003098 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003099
H. Peter Anvine2c80182005-01-15 22:15:51 +00003100 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003101 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003102 if (b == -1 && i != -1) {
3103 int tmp = b;
3104 b = i;
3105 i = tmp;
3106 } /* swap */
3107 if ((b == R_SI || b == R_DI) && i != -1) {
3108 int tmp = b;
3109 b = i;
3110 i = tmp;
3111 }
3112 /* have BX/BP as base, SI/DI index */
3113 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003114 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003115 if (i != -1 && b != -1 &&
3116 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003117 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003118 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003119 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003120
H. Peter Anvine2c80182005-01-15 22:15:51 +00003121 rm = -1;
3122 if (i != -1)
3123 switch (i * 256 + b) {
3124 case R_SI * 256 + R_BX:
3125 rm = 0;
3126 break;
3127 case R_DI * 256 + R_BX:
3128 rm = 1;
3129 break;
3130 case R_SI * 256 + R_BP:
3131 rm = 2;
3132 break;
3133 case R_DI * 256 + R_BP:
3134 rm = 3;
3135 break;
3136 } else
3137 switch (b) {
3138 case R_SI:
3139 rm = 4;
3140 break;
3141 case R_DI:
3142 rm = 5;
3143 break;
3144 case R_BP:
3145 rm = 6;
3146 break;
3147 case R_BX:
3148 rm = 7;
3149 break;
3150 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003151 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003152 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003153
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003154 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003155 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003156 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003157 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00003158 mod = 1;
3159 else
3160 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003161
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003162 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003163 output->bytes = mod; /* bytes of offset needed */
3164 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003165 }
3166 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003167 }
H. Peter Anvin70653092007-10-19 14:42:29 -07003168
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003169 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003170 return output->type;
3171
3172err:
3173 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003174}
3175
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003176static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003177{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003178 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003179 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003180
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003181 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003182
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003183 switch (ins->prefixes[PPS_ASIZE]) {
3184 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003185 valid &= 16;
3186 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003187 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003188 valid &= 32;
3189 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003190 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003191 valid &= 64;
3192 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003193 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003194 valid &= (addrbits == 32) ? 16 : 32;
3195 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003196 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003197 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003198 }
3199
3200 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003201 if (is_class(MEMORY, ins->oprs[j].type)) {
3202 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07003203
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003204 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003205 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003206 i = 0;
3207 else
3208 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003209
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003210 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003211 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003212 b = 0;
3213 else
3214 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003215
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003216 if (ins->oprs[j].scale == 0)
3217 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003218
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003219 if (!i && !b) {
3220 int ds = ins->oprs[j].disp_size;
3221 if ((addrbits != 64 && ds > 8) ||
3222 (addrbits == 64 && ds == 16))
3223 valid &= ds;
3224 } else {
3225 if (!(REG16 & ~b))
3226 valid &= 16;
3227 if (!(REG32 & ~b))
3228 valid &= 32;
3229 if (!(REG64 & ~b))
3230 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07003231
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003232 if (!(REG16 & ~i))
3233 valid &= 16;
3234 if (!(REG32 & ~i))
3235 valid &= 32;
3236 if (!(REG64 & ~i))
3237 valid &= 64;
3238 }
3239 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003240 }
3241
3242 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003243 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003244 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003245 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04003246 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003247 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003248 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003249 /* Impossible... */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03003250 nasm_nonfatal("impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003251 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003252 }
3253
3254 defdisp = ins->addr_size == 16 ? 16 : 32;
3255
3256 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003257 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
3258 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
3259 /*
3260 * mem_offs sizes must match the address size; if not,
3261 * strip the MEM_OFFS bit and match only EA instructions
3262 */
3263 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
3264 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003265 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003266}