blob: ad6d5ea6d1569ff97b4c1a10c208b3c82a6628b7 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvin (Intel)38ddb192019-01-11 12:27:02 -08003 * Copyright 1996-2019 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040037 * Bytecode specification
38 * ----------------------
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070039 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040040 *
41 * Codes Mnemonic Explanation
42 *
43 * \0 terminates the code. (Unless it's a literal of course.)
44 * \1..\4 that many literal bytes follow in the code stream
45 * \5 add 4 to the primary operand number (b, low octdigit)
46 * \6 add 4 to the secondary operand number (a, middle octdigit)
47 * \7 add 4 to both the primary and the secondary operand number
48 * \10..\13 a literal byte follows in the code stream, to be added
49 * to the register value of operand 0..3
50 * \14..\17 the position of index register operand in MIB (BND insns)
51 * \20..\23 ib a byte immediate operand, from operand 0..3
52 * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
53 * \30..\33 iw a word immediate operand, from operand 0..3
54 * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
55 * assembly mode or the operand-size override on the operand
56 * \40..\43 id a long immediate operand, from operand 0..3
57 * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
58 * depending on the address size of the instruction.
59 * \50..\53 rel8 a byte relative operand, from operand 0..3
60 * \54..\57 iq a qword immediate operand, from operand 0..3
61 * \60..\63 rel16 a word relative operand, from operand 0..3
62 * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
63 * assembly mode or the operand-size override on the operand
64 * \70..\73 rel32 a long relative operand, from operand 0..3
65 * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
66 * \1ab a ModRM, calculated on EA in operand a, with the spare
67 * field the register value of operand b.
68 * \172\ab the register number from operand a in bits 7..4, with
69 * the 4-bit immediate from operand b in bits 3..0.
70 * \173\xab the register number from operand a in bits 7..4, with
71 * the value b in bits 3..0.
72 * \174..\177 the register number from operand 0..3 in bits 7..4, and
73 * an arbitrary value in bits 3..0 (assembled as zero.)
74 * \2ab a ModRM, calculated on EA in operand a, with the spare
75 * field equal to digit b.
76 *
77 * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
78 * V field taken from operand 0..3.
79 * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
80 * V field set to 1111b.
81 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070082 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -070084 * cc 00m mmm
85 * c = 2 for EVEX and mmmm is the M field (EVEX.P0[3:0])
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070086 * and wlp is:
87 * 00 wwl lpp
88 * [l0] ll = 0 (.128, .lz)
89 * [l1] ll = 1 (.256)
90 * [l2] ll = 2 (.512)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
92 *
93 * [w0] ww = 0 for W = 0
94 * [w1] ww = 1 for W = 1
95 * [wig] ww = 2 for W don't care (always assembled as 0)
96 * [ww] ww = 3 for W used as REX.W
97 *
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
100 * [f3] pp = 2
101 * [f2] pp = 3
102 *
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
105 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400106 * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
107 * \260..\263 this instruction uses VEX/XOP rather than REX, with the
108 * V field taken from operand 0..3.
109 * \270 this instruction uses VEX/XOP rather than REX, with the
110 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700111 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700114 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700115 * [l0] ll = 0 for L = 0 (.128, .lz)
116 * [l1] ll = 1 for L = 1 (.256)
117 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700118 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700119 * [w0] ww = 0 for W = 0
120 * [w1 ] ww = 1 for W = 1
121 * [wig] ww = 2 for W don't care (always assembled as 0)
122 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700123 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700125 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400126 * \271 hlexr instruction takes XRELEASE (F3) with or without lock
127 * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
128 * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
129 * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
130 * to the operand size (if o16/o32/o64 present) or the bit size
131 * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
132 * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
133 * \312 adf (disassembler only) invalid with non-default address size.
134 * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
135 * \314 norexb (disassembler only) invalid with REX.B
136 * \315 norexx (disassembler only) invalid with REX.X
137 * \316 norexr (disassembler only) invalid with REX.R
138 * \317 norexw (disassembler only) invalid with REX.W
139 * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
140 * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
141 * \322 odf indicates that this instruction is only valid when the
142 * operand size is the default (instruction to disassembler,
143 * generates no code in the assembler)
144 * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
145 * \324 o64 indicates 64-bit operand size requiring REX prefix.
146 * \325 nohi instruction which always uses spl/bpl/sil/dil
147 * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
148 disassembler only; for SSE instructions.
149 * \330 a literal byte follows in the code stream, to be added
150 * to the condition code value of the instruction.
151 * \331 norep instruction not valid with REP prefix. Hint for
152 * disassembler only; for SSE instructions.
153 * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
154 * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
155 * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
156 * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
157 * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
158 * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
159 * \336-\337 are still listed as prefixes in the disassembler.
160 * \340 resb reserve <operand 0> bytes of uninitialized storage.
161 * Operand 0 had better be a segmentless constant.
162 * \341 wait this instruction needs a WAIT "prefix"
Cyrill Gorcunov8a5d3e62014-08-25 20:04:30 +0400163 * \360 np no SSE prefix (== \364\331)
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400164 * \361 66 SSE prefix (== \366\331)
165 * \364 !osp operand-size prefix (0x66) not permitted
166 * \365 !asp address-size prefix (0x67) not permitted
167 * \366 operand-size prefix (0x66) used as opcode extension
168 * \367 address-size prefix (0x67) used as opcode extension
169 * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
170 * jmp8 370 is used for Jcc, 371 is used for JMP.
171 * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
172 * used for conditional jump over longer jump
173 * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
174 * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
175 * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 */
177
H. Peter Anvinfe501952007-10-02 21:53:51 -0700178#include "compiler.h"
179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180
181#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000182#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800183#include "error.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000184#include "assemble.h"
185#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700186#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800187#include "disp8.h"
H. Peter Anvin172b8402016-02-18 01:16:18 -0800188#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000189
H. Peter Anvin65289e82009-07-25 17:25:11 -0700190enum match_result {
191 /*
192 * Matching errors. These should be sorted so that more specific
193 * errors come later in the sequence.
194 */
195 MERR_INVALOP,
196 MERR_OPSIZEMISSING,
197 MERR_OPSIZEMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700198 MERR_BRNOTHERE,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700199 MERR_BRNUMMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700200 MERR_MASKNOTHERE,
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700201 MERR_DECONOTHERE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700202 MERR_BADCPU,
203 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800204 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700205 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700206 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800207 MERR_BADREPNE,
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700208 MERR_REGSETSIZE,
209 MERR_REGSET,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700210 /*
211 * Matching success; the conditional ones first
212 */
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700213 MOK_JUMP, /* Matching OK but needs jmp_match() */
214 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700215};
216
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000217typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700218 enum ea_type type; /* what kind of EA is this? */
219 int sib_present; /* is a SIB byte necessary? */
220 int bytes; /* # of bytes of offset needed */
221 int size; /* lazy - this is sib+bytes+1 */
222 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700223 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224} ea;
225
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400226#define GEN_SIB(scale, index, base) \
227 (((scale) << 6) | ((index) << 3) | ((base)))
228
229#define GEN_MODRM(mod, reg, rm) \
230 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
231
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800232static int64_t calcsize(int32_t, int64_t, int, insn *,
233 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700234static int emit_prefix(struct out_data *data, const int bits, insn *ins);
235static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700236static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400237 insn *instruction,
238 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700239static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700240static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000241static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700242static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000243static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700244static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700245static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000246
H. Peter Anvin8f622462017-04-02 19:02:29 -0700247static enum ea_type process_ea(operand *, ea *, int, int,
248 opflags_t, insn *, const char **);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700249
H. Peter Anvin164d2462017-02-20 02:39:56 -0800250static inline bool absolute_op(const struct operand *o)
251{
252 return o->segment == NO_SEG && o->wrt == NO_SEG &&
253 !(o->opflags & OPFLAG_RELATIVE);
254}
255
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400256static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000257{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700258 return ins->prefixes[pos] == prefix;
259}
260
261static void assert_no_prefix(insn * ins, enum prefix_pos pos)
262{
263 if (ins->prefixes[pos])
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300264 nasm_nonfatal("invalid %s prefix", prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265}
266
267static const char *size_name(int size)
268{
269 switch (size) {
270 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400271 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700272 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400273 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700274 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700276 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400277 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700278 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400279 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700280 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700282 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400283 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700284 case 64:
285 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700286 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400287 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000288 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700289}
290
H. Peter Anvin285222f2017-03-01 13:27:33 -0800291static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400292{
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800293 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW, "%s data exceeds bounds",
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300294 size_name(size));
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400295}
296
297static void warn_overflow_const(int64_t data, int size)
298{
299 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800300 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400301}
302
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800303static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
304{
305 bool err;
306
307 switch (sign) {
308 case OUT_WRAP:
309 err = overflow_general(data, size);
310 break;
311 case OUT_SIGNED:
312 err = overflow_signed(data, size);
313 break;
314 case OUT_UNSIGNED:
315 err = overflow_unsigned(data, size);
316 break;
317 default:
318 panic();
319 break;
320 }
321
322 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800323 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800324}
325
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000326/*
327 * This routine wrappers the real output format's output routine,
328 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800329 * generator at the same time, flatten unnecessary relocations,
330 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000331 */
H. Peter Anvin (Intel)38ddb192019-01-11 12:27:02 -0800332/*
333 * This warning is currently issued by backends, but in the future
334 * this code should be centralized.
335 *
336 *!zeroing [on] RESx in initialized section becomes zero
337 *! a \c{RESx} directive was used in a section which contains
338 *! initialized data, and the output format does not support
339 *! this. Instead, this will be replaced with explicit zero
340 *! content, which may produce a large output file.
341 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700342static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000343{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000344 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700345 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700346 union {
347 uint8_t b[8];
348 uint64_t q;
349 } xdata;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700350 size_t asize, amax;
351 uint64_t zeropad = 0;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800352 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800353 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000354
H. Peter Anvina77692b2016-09-20 14:04:33 -0700355 if (!data->size)
356 return; /* Nothing to do */
357
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700358 /*
359 * Convert addresses to RAWDATA if possible
360 * XXX: not all backends want this for global symbols!!!!
361 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700362 switch (data->type) {
363 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800364 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800365 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800366 goto address;
367
368 case OUT_RELADDR:
369 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800370 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800371 goto address;
372
373 address:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700374 nasm_assert(data->size <= 8);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700375 asize = data->size;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700376 amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
Chang S. Bae427d8e32018-05-02 08:07:52 -0700377 if ((ofmt->flags & OFMT_KEEP_ADDR) == 0 && data->tsegment == fixseg &&
378 data->twrt == NO_SEG) {
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800379 warn_overflow_out(addrval, asize, data->sign);
Martin Storsjö869087d2017-05-22 13:54:20 +0300380 xdata.q = cpu_to_le64(addrval);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700381 data->data = xdata.b;
382 data->type = OUT_RAWDATA;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700383 asize = amax = 0; /* No longer an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700384 }
385 break;
386
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700387 case OUT_SEGMENT:
388 nasm_assert(data->size <= 8);
389 asize = data->size;
390 amax = 2;
391 break;
392
H. Peter Anvina77692b2016-09-20 14:04:33 -0700393 default:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700394 asize = amax = 0; /* Not an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700395 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000396 }
397
Frank Kotlerabebb082003-09-06 04:45:37 +0000398 /*
399 * this call to src_get determines when we call the
400 * debug-format-specific "linenum" function
401 * it updates lineno and lnfname to the current values
402 * returning 0 if "same as last time", -2 if lnfname
403 * changed, and the amount by which lineno changed,
404 * if it did. thus, these variables must be static
405 */
406
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400407 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700408 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000409
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700410 if (asize > amax) {
411 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300412 nasm_nonfatal("%u-bit signed relocation unsupported by output format %s",
413 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800414 } else {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800415 /*!
416 *!zext-reloc [on] relocation zero-extended to match output format
417 *! warns that a relocation has been zero-extended due
418 *! to limitations in the output format.
419 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800420 nasm_warn(WARN_ZEXT_RELOC,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700421 "%u-bit %s relocation zero-extended from %u bits",
422 (unsigned int)(asize << 3),
423 data->type == OUT_SEGMENT ? "segment" : "unsigned",
424 (unsigned int)(amax << 3));
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800425 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700426 zeropad = data->size - amax;
427 data->size = amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800428 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700429 lfmt->output(data);
H. Peter Anvined859f72018-06-15 00:03:53 -0700430
431 if (likely(data->segment != NO_SEG)) {
432 ofmt->output(data);
433 } else {
434 /* Outputting to ABSOLUTE section - only reserve is permitted */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300435 if (data->type != OUT_RESERVE)
436 nasm_nonfatal("attempt to assemble code in [ABSOLUTE] space");
H. Peter Anvined859f72018-06-15 00:03:53 -0700437 /* No need to push to the backend */
438 }
439
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700440 data->offset += data->size;
441 data->insoffs += data->size;
442
443 if (zeropad) {
444 data->type = OUT_ZERODATA;
445 data->size = zeropad;
446 lfmt->output(data);
447 ofmt->output(data);
448 data->offset += zeropad;
449 data->insoffs += zeropad;
450 data->size += zeropad; /* Restore original size value */
451 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000452}
453
H. Peter Anvina77692b2016-09-20 14:04:33 -0700454static inline void out_rawdata(struct out_data *data, const void *rawdata,
455 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400456{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700457 data->type = OUT_RAWDATA;
458 data->data = rawdata;
459 data->size = size;
460 out(data);
461}
462
463static void out_rawbyte(struct out_data *data, uint8_t byte)
464{
465 data->type = OUT_RAWDATA;
466 data->data = &byte;
467 data->size = 1;
468 out(data);
469}
470
471static inline void out_reserve(struct out_data *data, uint64_t size)
472{
473 data->type = OUT_RESERVE;
474 data->size = size;
475 out(data);
476}
477
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700478static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700479{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700480 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300481 nasm_nonfatal("segment references cannot be relative");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700482
483 data->type = OUT_SEGMENT;
484 data->sign = OUT_UNSIGNED;
485 data->size = 2;
486 data->toffset = opx->offset;
487 data->tsegment = ofmt->segbase(opx->segment | 1);
488 data->twrt = opx->wrt;
489 out(data);
490}
491
492static void out_imm(struct out_data *data, const struct operand *opx,
493 int size, enum out_sign sign)
494{
495 if (opx->segment != NO_SEG && (opx->segment & 1)) {
496 /*
497 * This is actually a segment reference, but eval() has
498 * already called ofmt->segbase() for us. Sigh.
499 */
500 if (size < 2)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300501 nasm_nonfatal("segment reference must be 16 bits");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700502
503 data->type = OUT_SEGMENT;
504 } else {
505 data->type = (opx->opflags & OPFLAG_RELATIVE)
506 ? OUT_RELADDR : OUT_ADDRESS;
507 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700508 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700509 data->toffset = opx->offset;
510 data->tsegment = opx->segment;
511 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800512 /*
513 * XXX: improve this if at some point in the future we can
514 * distinguish the subtrahend in expressions like [foo - bar]
515 * where bar is a symbol in the current segment. However, at the
516 * current point, if OPFLAG_RELATIVE is set that subtraction has
517 * already occurred.
518 */
519 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700520 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700521 out(data);
522}
523
H. Peter Anvin164d2462017-02-20 02:39:56 -0800524static void out_reladdr(struct out_data *data, const struct operand *opx,
525 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700526{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800527 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300528 nasm_nonfatal("invalid use of self-relative expression");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800529
H. Peter Anvina77692b2016-09-20 14:04:33 -0700530 data->type = OUT_RELADDR;
531 data->sign = OUT_SIGNED;
532 data->size = size;
533 data->toffset = opx->offset;
534 data->tsegment = opx->segment;
535 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800536 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700537 out(data);
538}
539
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700540static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800541 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800543 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800544 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000545 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800546 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000547
H. Peter Anvin755f5212012-02-25 11:41:34 -0800548 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700549 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300550 if (!optimizing.level || (optimizing.flag & OPTIM_DISABLE_JMP_MATCH))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400551 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300552 if (optimizing.level < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400553 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700554
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800555 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100556
Victor van den Elzen154e5922009-02-25 17:32:00 +0100557 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100558 /* Be optimistic in pass 1 */
559 return true;
560
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700562 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000563
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700564 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800565 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
566
567 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
568 /* jmp short (opcode eb) cannot be used with bnd prefix. */
569 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800570 /*!
571 *!bnd [on] invalid BND prefixes
572 *! warns about ineffective use of the \c{BND} prefix when the
573 *! \c{JMP} instruction is converted to the \c{SHORT} form.
574 *! This should be extremely rare since the short \c{JMP} only
575 *! is applicable to jumps inside the same module, but if
576 *! it is legitimate, it may be necessary to use
H. Peter Anvin959702b2019-06-06 20:56:50 -0700577 *! \c{bnd jmp dword}.
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800578 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800579 nasm_warn(WARN_BND | ERR_PASS2 ,
H. Peter Anvin959702b2019-06-06 20:56:50 -0700580 "jmp short does not init bnd regs - bnd prefix dropped");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800581 }
582
583 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000584}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000585
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700586static inline int64_t merge_resb(insn *ins, int64_t isize)
587{
588 int nbytes = resb_bytes(ins->opcode);
589
590 if (likely(!nbytes))
591 return isize;
592
593 if (isize != nbytes * ins->oprs[0].offset)
594 return isize; /* Has prefixes of some sort */
595
596 ins->oprs[0].offset *= ins->times;
597 isize *= ins->times;
598 ins->times = 1;
599 return isize;
600}
601
H. Peter Anvin04445362016-09-21 15:56:19 -0700602/* This is totally just a wild guess what is reasonable... */
603#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
604
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800605int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000606{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700607 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000608 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700609 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300610 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000611
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700612 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700613 data.offset = start;
614 data.segment = segment;
615 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700616 data.bits = bits;
617
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700618 wsize = db_bytes(instruction->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300619 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000621
H. Peter Anvineba20a72002-04-30 20:53:55 +0000622 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700624
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700625 list_for_each(e, instruction->eops) {
626 if (e->type == EOT_DB_NUMBER) {
627 if (wsize > 8) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300628 nasm_nonfatal("integer supplied to a DT,DO,DY or DZ");
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700629 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700630 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700631 data.inslen = data.size = wsize;
632 data.toffset = e->offset;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700633 data.twrt = e->wrt;
634 data.relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700635 if (e->segment != NO_SEG && (e->segment & 1)) {
636 data.tsegment = e->segment;
637 data.type = OUT_SEGMENT;
638 data.sign = OUT_UNSIGNED;
639 } else {
640 data.tsegment = e->segment;
641 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
642 data.sign = OUT_WRAP;
643 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700644 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700646 } else if (e->type == EOT_DB_STRING ||
647 e->type == EOT_DB_STRING_FREE) {
648 int align = e->stringlen % wsize;
649 if (align)
650 align = wsize - align;
651
652 data.insoffs = 0;
653 data.inslen = e->stringlen + align;
654
655 out_rawdata(&data, e->stringval, e->stringlen);
656 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700657 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000658 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700659 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700660 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700662 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700663 off_t base = 0;
664 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700665 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700666 char *buf = NULL;
667 size_t blk = 0; /* Buffered I/O block size */
668 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000669
H. Peter Anvin94ead272017-09-27 15:22:23 -0700670 if (!t)
671 goto done;
672
H. Peter Anvind81a2352016-09-21 14:03:18 -0700673 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400674 if (!fp) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300675 nasm_nonfatal("`incbin': unable to open file `%s'",
676 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700677 goto done;
678 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000679
H. Peter Anvind81a2352016-09-21 14:03:18 -0700680 len = nasm_file_size(fp);
681
682 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300683 nasm_nonfatal("`incbin': unable to get length of file `%s'",
684 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700685 goto close_done;
686 }
687
H. Peter Anvina77692b2016-09-20 14:04:33 -0700688 if (instruction->eops->next) {
689 base = instruction->eops->next->offset;
690 if (base >= len) {
691 len = 0;
692 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000693 len -= base;
694 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700695 len > (off_t)instruction->eops->next->next->offset)
696 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000697 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000698 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700699
H. Peter Anvina77692b2016-09-20 14:04:33 -0700700 lfmt->set_offset(data.offset);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700701 lfmt->uplevel(LIST_INCBIN, len);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000702
H. Peter Anvind81a2352016-09-21 14:03:18 -0700703 if (!len)
704 goto end_incbin;
705
706 /* Try to map file data */
707 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700708 if (!map) {
709 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
710 buf = nasm_malloc(blk);
711 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700712
713 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700714 /*
715 * Consider these irrelevant for INCBIN, since it is fully
716 * possible that these might be (way) bigger than an int
717 * can hold; there is, however, no reason to widen these
718 * types just for INCBIN. data.inslen == 0 signals to the
719 * backend that these fields are meaningless, if at all
720 * needed.
721 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700722 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700723 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700724
H. Peter Anvind81a2352016-09-21 14:03:18 -0700725 if (map) {
726 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700727 } else if ((off_t)m == len) {
728 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700729 } else {
730 off_t l = len;
731
732 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300733 nasm_nonfatal("`incbin': unable to seek on file `%s'",
734 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700735 goto end_incbin;
736 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700737 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700738 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700739 if (!m || feof(fp)) {
740 /*
741 * This shouldn't happen unless the file
742 * actually changes while we are reading
743 * it.
744 */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300745 nasm_nonfatal("`incbin': unexpected EOF while"
746 " reading file `%s'", fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700747 goto end_incbin;
748 }
749 out_rawdata(&data, buf, m);
750 l -= m;
751 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700752 }
753 }
754 end_incbin:
755 lfmt->downlevel(LIST_INCBIN);
756 if (instruction->times > 1) {
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700757 lfmt->uplevel(LIST_TIMES, instruction->times);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700758 lfmt->downlevel(LIST_TIMES);
759 }
760 if (ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300761 nasm_nonfatal("`incbin': error while"
762 " reading file `%s'", fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700763 }
764 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700765 if (buf)
766 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700767 if (map)
768 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700769 fclose(fp);
770 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700771 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700772 ;
773 } else {
774 /* "Real" instruction */
775
776 /* Check to see if we need an address-size prefix */
777 add_asp(instruction, bits);
778
779 m = find_match(&temp, instruction, data.segment, data.offset, bits);
780
781 if (m == MOK_GOOD) {
782 /* Matches! */
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700783 if (unlikely(itemp_has(temp, IF_OBSOLETE))) {
784 /*
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700785 * If IF_OBSOLETE is set, warn the user. Different
786 * warning classes for "obsolete but valid for this
787 * specific CPU" and "obsolete and gone."
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700788 *
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700789 *!obsolete-removed [on] instruction obsolete and removed on the target CPU
790 *! warns for an instruction which has been removed
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700791 *! from the architecture, and is no longer included
792 *! in the CPU definition given in the \c{[CPU]}
793 *! directive, for example \c{POP CS}, the opcode for
794 *! which, \c{0Fh}, instead is an opcode prefix on
795 *! CPUs newer than the first generation 8086.
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700796 *
797 *!obsolete-valid [on] instruction obsolete but valid on the target CPU
798 *! warns for an instruction which has been removed
799 *! from the architecture, but is still valid on the
800 *! specific CPU given in the \c{CPU} directive. Code
801 *! using these instructions is not forward compatible.
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700802 */
803
804 if (iflag_cmp_cpu_level(&insns_flags[temp->iflag_idx], &cpu)) {
H. Peter Anvin (Intel)fb118892019-08-09 14:21:42 -0700805 nasm_warn(WARN_OBSOLETE_REMOVED,
806 "instruction obsolete and removed on the target CPU");
807 } else {
808 nasm_warn(WARN_OBSOLETE_VALID,
809 "instruction obsolete but valid on the target CPU");
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700810 }
811 }
812
H. Peter Anvina77692b2016-09-20 14:04:33 -0700813 data.itemp = temp;
814 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700815 data.insoffs = 0;
H. Peter Anvin (Intel)77335212019-08-06 23:22:48 -0700816
817 data.inslen = calcsize(data.segment, data.offset,
818 bits, instruction, temp);
819 nasm_assert(data.inslen >= 0);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700820 data.inslen = merge_resb(instruction, data.inslen);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700821
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700822 gencode(&data, instruction);
H. Peter Anvin (Intel)77335212019-08-06 23:22:48 -0700823 nasm_assert(data.insoffs == data.inslen);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700824 } else {
825 /* No match */
826 switch (m) {
827 case MERR_OPSIZEMISSING:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300828 nasm_nonfatal("operation size not specified");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700829 break;
830 case MERR_OPSIZEMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300831 nasm_nonfatal("mismatch in operand sizes");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700832 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700833 case MERR_BRNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300834 nasm_nonfatal("broadcast not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700835 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700836 case MERR_BRNUMMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300837 nasm_nonfatal("mismatch in the number of broadcasting elements");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700838 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700839 case MERR_MASKNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300840 nasm_nonfatal("mask not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700841 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700842 case MERR_DECONOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300843 nasm_nonfatal("unsupported mode decorator for instruction");
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700844 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700845 case MERR_BADCPU:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300846 nasm_nonfatal("no instruction for this cpu level");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700847 break;
848 case MERR_BADMODE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300849 nasm_nonfatal("instruction not supported in %d-bit mode", bits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700850 break;
851 case MERR_ENCMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300852 nasm_nonfatal("specific encoding scheme not available");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700853 break;
854 case MERR_BADBND:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300855 nasm_nonfatal("bnd prefix is not allowed");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700856 break;
857 case MERR_BADREPNE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300858 nasm_nonfatal("%s prefix is not allowed",
859 (has_prefix(instruction, PPS_REP, P_REPNE) ?
860 "repne" : "repnz"));
H. Peter Anvina77692b2016-09-20 14:04:33 -0700861 break;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700862 case MERR_REGSETSIZE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300863 nasm_nonfatal("invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700864 break;
865 case MERR_REGSET:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300866 nasm_nonfatal("register set not valid for operand");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700867 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700868 default:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300869 nasm_nonfatal("invalid combination of opcode and operands");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700870 break;
871 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700872
873 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400874 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000875 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700876 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000877}
878
H. Peter Anvin29651542018-12-18 19:14:40 -0800879static void debug_set_db_type(insn *instruction)
880{
881 /* Is this really correct? .operands doesn't mean much for Dx */
882 int32_t typeinfo = TYS_ELEMENTS(instruction->operands);
883
884 switch (instruction->opcode) {
885 case I_DB:
886 typeinfo |= TY_BYTE;
887 break;
888 case I_DW:
889 typeinfo |= TY_WORD;
890 break;
891 case I_DD:
892 if (instruction->eops_float)
893 typeinfo |= TY_FLOAT;
894 else
895 typeinfo |= TY_DWORD;
896 break;
897 case I_DQ:
898 /* What about double? */
899 typeinfo |= TY_QWORD;
900 break;
901 case I_DT:
902 /* What about long double? */
903 typeinfo |= TY_TBYTE;
904 break;
905 case I_DO:
906 typeinfo |= TY_OWORD;
907 break;
908 case I_DY:
909 typeinfo |= TY_YWORD;
910 break;
911 case I_DZ:
912 typeinfo |= TY_ZWORD;
913 break;
914 default:
915 panic();
916 }
917
918 dfmt->debug_typevalue(typeinfo);
919}
920
921static void debug_set_type(insn *instruction)
922{
923 int32_t typeinfo;
924
925 if (opcode_is_resb(instruction->opcode)) {
926 typeinfo = TYS_ELEMENTS(instruction->oprs[0].offset);
927
928 switch (instruction->opcode) {
929 case I_RESB:
930 typeinfo |= TY_BYTE;
931 break;
932 case I_RESW:
933 typeinfo |= TY_WORD;
934 break;
935 case I_RESD:
936 typeinfo |= TY_DWORD;
937 break;
938 case I_RESQ:
939 typeinfo |= TY_QWORD;
940 break;
941 case I_REST:
942 typeinfo |= TY_TBYTE;
943 break;
944 case I_RESO:
945 typeinfo |= TY_OWORD;
946 break;
947 case I_RESY:
948 typeinfo |= TY_YWORD;
949 break;
950 case I_RESZ:
951 typeinfo |= TY_ZWORD;
952 break;
953 default:
954 panic();
955 }
956 } else {
957 typeinfo = TY_LABEL;
958 }
959
960 dfmt->debug_typevalue(typeinfo);
961}
962
963
964/* Proecess an EQU directive */
965static void define_equ(insn * instruction)
966{
967 if (!instruction->label) {
968 nasm_nonfatal("EQU not preceded by label");
969 } else if (instruction->operands == 1 &&
970 (instruction->oprs[0].type & IMMEDIATE) &&
971 instruction->oprs[0].wrt == NO_SEG) {
972 define_label(instruction->label,
973 instruction->oprs[0].segment,
974 instruction->oprs[0].offset, false);
975 } else if (instruction->operands == 2
976 && (instruction->oprs[0].type & IMMEDIATE)
977 && (instruction->oprs[0].type & COLON)
978 && instruction->oprs[0].segment == NO_SEG
979 && instruction->oprs[0].wrt == NO_SEG
980 && (instruction->oprs[1].type & IMMEDIATE)
981 && instruction->oprs[1].segment == NO_SEG
982 && instruction->oprs[1].wrt == NO_SEG) {
983 define_label(instruction->label,
984 instruction->oprs[0].offset | SEG_ABS,
985 instruction->oprs[1].offset, false);
986 } else {
987 nasm_nonfatal("bad syntax for EQU");
988 }
989}
990
H. Peter Anvin0d4d4312019-08-07 00:46:27 -0700991
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800992int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000993{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000994 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700995 enum match_result m;
H. Peter Anvin29651542018-12-18 19:14:40 -0800996 int64_t isize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000997
H. Peter Anvin29651542018-12-18 19:14:40 -0800998 if (instruction->opcode == I_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 return 0;
H. Peter Anvin29651542018-12-18 19:14:40 -08001000 } else if (instruction->opcode == I_EQU) {
1001 define_equ(instruction);
1002 return 0;
1003 } else if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 extop *e;
H. Peter Anvin29651542018-12-18 19:14:40 -08001005 int32_t osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001006
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -07001007 wsize = db_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001008 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001009
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +04001010 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +00001011 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001012
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001014 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001016 warn_overflow_const(e->offset, wsize);
1017 } else if (e->type == EOT_DB_STRING ||
1018 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001019 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001020
H. Peter Anvine2c80182005-01-15 22:15:51 +00001021 align = (-osize) % wsize;
1022 if (align < 0)
1023 align += wsize;
1024 isize += osize + align;
1025 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001026
H. Peter Anvin29651542018-12-18 19:14:40 -08001027 debug_set_db_type(instruction);
1028 return isize;
1029 } else if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001030 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001031 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001032
H. Peter Anvind81a2352016-09-21 14:03:18 -07001033 len = nasm_file_size_by_path(fname);
1034 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001035 nasm_nonfatal("`incbin': unable to get length of file `%s'",
1036 fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001037 return 0;
1038 }
1039
1040 if (instruction->eops->next) {
1041 if (len <= (off_t)instruction->eops->next->offset) {
1042 len = 0;
1043 } else {
1044 len -= instruction->eops->next->offset;
1045 if (instruction->eops->next->next &&
1046 len > (off_t)instruction->eops->next->next->offset) {
1047 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001048 }
1049 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001051
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001052 len *= instruction->times;
1053 instruction->times = 1; /* Tell the upper layer to not iterate */
1054
H. Peter Anvind81a2352016-09-21 14:03:18 -07001055 return len;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001056 } else {
H. Peter Anvin29651542018-12-18 19:14:40 -08001057 /* Normal instruction, or RESx */
1058
1059 /* Check to see if we need an address-size prefix */
1060 add_asp(instruction, bits);
1061
1062 m = find_match(&temp, instruction, segment, offset, bits);
1063 if (m != MOK_GOOD)
1064 return -1; /* No match */
1065
1066 isize = calcsize(segment, offset, bits, instruction, temp);
1067 debug_set_type(instruction);
H. Peter Anvin0d4d4312019-08-07 00:46:27 -07001068 isize = merge_resb(instruction, isize);
H. Peter Anvin29651542018-12-18 19:14:40 -08001069
1070 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001071 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001072}
1073
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001074static void bad_hle_warn(const insn * ins, uint8_t hleok)
1075{
1076 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001077 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001078 static const enum whatwarn warn[2][4] =
1079 {
1080 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
1081 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
1082 };
1083 unsigned int n;
1084
1085 n = (unsigned int)rep_pfx - P_XACQUIRE;
1086 if (n > 1)
1087 return; /* Not XACQUIRE/XRELEASE */
1088
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001089 ww = warn[n][hleok];
1090 if (!is_class(MEMORY, ins->oprs[0].type))
1091 ww = w_inval; /* HLE requires operand 0 to be memory */
1092
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001093 /*!
1094 *!hle [on] invalid HLE prefixes
1095 *! warns about invalid use of the HLE \c{XACQUIRE} or \c{XRELEASE}
1096 *! prefixes.
1097 */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001098 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001099 case w_none:
1100 break;
1101
1102 case w_lock:
1103 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001104 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001105 "%s with this instruction requires lock",
1106 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001107 }
1108 break;
1109
1110 case w_inval:
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001111 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001112 "%s invalid with this instruction",
1113 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001114 break;
1115 }
1116}
1117
H. Peter Anvin507ae032008-10-09 15:37:10 -07001118/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +04001119#define case3(x) case (x): case (x)+1: case (x)+2
1120#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -07001121
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001122static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001123 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001124{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001125 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001126 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001127 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001128 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001129 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001130 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001131 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001132 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001133 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001134 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -07001135 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -07001136 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001137
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001138 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001139 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001140 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001141
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001142 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001143 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001144
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 (void)segment; /* Don't warn that this parameter is unused */
1146 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001147
H. Peter Anvin839eca22007-10-29 23:12:47 -07001148 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001149 c = *codes++;
1150 op1 = (c & 3) + ((opex & 1) << 2);
1151 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1152 opx = &ins->oprs[op1];
1153 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001154
H. Peter Anvin839eca22007-10-29 23:12:47 -07001155 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001156 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 codes += c, length += c;
1158 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001159
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001160 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001161 opex = c;
1162 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001163
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001164 case4(010):
1165 ins->rex |=
1166 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 codes++, length++;
1168 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001169
Jin Kyu Song164d6072013-10-15 19:10:13 -07001170 case4(014):
1171 /* this is an index reg of MIB operand */
1172 mib_index = opx->basereg;
1173 break;
1174
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001175 case4(020):
1176 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 length++;
1178 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001179
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001180 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 length += 2;
1182 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001183
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001184 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001185 if (opx->type & (BITS16 | BITS32 | BITS64))
1186 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 else
1188 length += (bits == 16) ? 2 : 4;
1189 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001190
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001191 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001192 length += 4;
1193 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001194
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001195 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001196 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001198
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001199 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001200 length++;
1201 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001202
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001203 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001204 length += 8; /* MOV reg64/imm */
1205 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001206
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001207 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001208 length += 2;
1209 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001210
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001211 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001212 if (opx->type & (BITS16 | BITS32 | BITS64))
1213 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 else
1215 length += (bits == 16) ? 2 : 4;
1216 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001217
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001218 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001219 length += 4;
1220 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001221
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001222 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001223 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001225
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001226 case 0172:
1227 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001228 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001229 length++;
1230 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001231
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001232 case4(0174):
1233 length++;
1234 break;
1235
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001236 case4(0240):
1237 ins->rex |= REX_EV;
1238 ins->vexreg = regval(opx);
1239 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1240 ins->vex_cm = *codes++;
1241 ins->vex_wlp = *codes++;
1242 ins->evex_tuple = (*codes++ - 0300);
1243 break;
1244
1245 case 0250:
1246 ins->rex |= REX_EV;
1247 ins->vexreg = 0;
1248 ins->vex_cm = *codes++;
1249 ins->vex_wlp = *codes++;
1250 ins->evex_tuple = (*codes++ - 0300);
1251 break;
1252
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001253 case4(0254):
1254 length += 4;
1255 break;
1256
1257 case4(0260):
1258 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001259 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001260 ins->vex_cm = *codes++;
1261 ins->vex_wlp = *codes++;
1262 break;
1263
1264 case 0270:
1265 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001266 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001267 ins->vex_cm = *codes++;
1268 ins->vex_wlp = *codes++;
1269 break;
1270
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001271 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001272 hleok = c & 3;
1273 break;
1274
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001275 case4(0274):
1276 length++;
1277 break;
1278
1279 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001280 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001281
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001283 if (bits == 64)
1284 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001285 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001287
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001289 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001291
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001293 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001294
Keith Kaniosb7a89542007-04-12 02:40:54 +00001295 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001296 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1297 has_prefix(ins, PPS_ASIZE, P_A32))
1298 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001300
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001301 case4(0314):
1302 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001303
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001305 {
1306 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1307 if (pfx == P_O16)
1308 break;
1309 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001310 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001311 else
1312 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001313 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001314 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001315
H. Peter Anvine2c80182005-01-15 22:15:51 +00001316 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001317 {
1318 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1319 if (pfx == P_O32)
1320 break;
1321 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001322 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001323 else
1324 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001326 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001327
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 case 0322:
1329 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001330
Keith Kaniosb7a89542007-04-12 02:40:54 +00001331 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001332 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001333 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001334
Keith Kaniosb7a89542007-04-12 02:40:54 +00001335 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001336 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001337 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001338
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001339 case 0325:
1340 ins->rex |= REX_NH;
1341 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001342
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001343 case 0326:
1344 break;
1345
H. Peter Anvine2c80182005-01-15 22:15:51 +00001346 case 0330:
1347 codes++, length++;
1348 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001349
H. Peter Anvine2c80182005-01-15 22:15:51 +00001350 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001352
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001353 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001354 case 0333:
1355 length++;
1356 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001357
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001358 case 0334:
1359 ins->rex |= REX_L;
1360 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001361
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001362 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001363 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001364
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001365 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001366 if (!ins->prefixes[PPS_REP])
1367 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001368 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001369
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001370 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001371 if (!ins->prefixes[PPS_REP])
1372 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001373 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001374
H. Peter Anvine2c80182005-01-15 22:15:51 +00001375 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001376 if (!absolute_op(&ins->oprs[0]))
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001377 nasm_nonfatal("attempt to reserve non-constant"
1378 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001379 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001380 nasm_warn(WARN_OTHER, "forward reference in RESx "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001381 "can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 else
H. Peter Anvin5358b982018-12-18 18:06:26 -08001383 length += ins->oprs[0].offset * resb_bytes(ins->opcode);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001384 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001385
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001386 case 0341:
1387 if (!ins->prefixes[PPS_WAIT])
1388 ins->prefixes[PPS_WAIT] = P_WAIT;
1389 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001390
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001391 case 0360:
1392 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001393
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001394 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001395 length++;
1396 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001397
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001398 case 0364:
1399 case 0365:
1400 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001401
Keith Kanios48af1772007-08-17 07:37:52 +00001402 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001403 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001404 length++;
1405 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001406
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001407 case 0370:
1408 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001409 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001410
H. Peter Anvine2c80182005-01-15 22:15:51 +00001411 case 0373:
1412 length++;
1413 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001414
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001415 case 0374:
1416 eat = EA_XMMVSIB;
1417 break;
1418
1419 case 0375:
1420 eat = EA_YMMVSIB;
1421 break;
1422
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001423 case 0376:
1424 eat = EA_ZMMVSIB;
1425 break;
1426
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001427 case4(0100):
1428 case4(0110):
1429 case4(0120):
1430 case4(0130):
1431 case4(0200):
1432 case4(0204):
1433 case4(0210):
1434 case4(0214):
1435 case4(0220):
1436 case4(0224):
1437 case4(0230):
1438 case4(0234):
1439 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001440 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001441 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001442 opflags_t rflags;
1443 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001444 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001445
Keith Kaniosb7a89542007-04-12 02:40:54 +00001446 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001447
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001448 if (c <= 0177) {
1449 /* pick rfield from operand b (opx) */
1450 rflags = regflag(opx);
1451 rfield = nasm_regvals[opx->basereg];
1452 } else {
1453 rflags = 0;
1454 rfield = c & 7;
1455 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001456
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001457 /* EVEX.b1 : evex_brerop contains the operand position */
1458 op_er_sae = (ins->evex_brerop >= 0 ?
1459 &ins->oprs[ins->evex_brerop] : NULL);
1460
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001461 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1462 /* set EVEX.b */
1463 ins->evex_p[2] |= EVEX_P2B;
1464 if (op_er_sae->decoflags & ER) {
1465 /* set EVEX.RC (rounding control) */
1466 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1467 & EVEX_P2RC;
1468 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001469 } else {
1470 /* set EVEX.L'L (vector length) */
1471 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001472 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001473 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001474 /* set EVEX.b */
1475 ins->evex_p[2] |= EVEX_P2B;
1476 }
1477 }
1478
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001479 if (itemp_has(temp, IF_MIB)) {
1480 opy->eaflags |= EAF_MIB;
1481 /*
1482 * if a separate form of MIB (ICC style) is used,
1483 * the index reg info is merged into mem operand
1484 */
1485 if (mib_index != R_none) {
1486 opy->indexreg = mib_index;
1487 opy->scale = 1;
1488 opy->hintbase = mib_index;
1489 opy->hinttype = EAH_NOTBASE;
1490 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001491 }
1492
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001493 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001494 rfield, rflags, ins, &errmsg) != eat) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001495 nasm_nonfatal("%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001496 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001497 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001498 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001499 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001500 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001501 }
1502 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001503
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001504 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001505 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001506 ": instruction code \\%o (0x%02X) given", c, c);
1507 break;
1508 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001509 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001510
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001511 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001512
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001513 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001514 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001515 nasm_nonfatal("instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001516 return -1;
1517 }
1518 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001519 }
1520
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001521 switch (ins->prefixes[PPS_VEX]) {
1522 case P_EVEX:
1523 if (!(ins->rex & REX_EV))
1524 return -1;
1525 break;
1526 case P_VEX3:
1527 case P_VEX2:
1528 if (!(ins->rex & REX_V))
1529 return -1;
1530 break;
1531 default:
1532 break;
1533 }
1534
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001535 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001536 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001537
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001538 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001539 nasm_nonfatal("cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001540 return -1;
1541 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001542 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001543 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001544 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001545 ins->rex &= ~REX_W;
1546 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001547 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001548 ins->rex |= REX_W;
1549 bad32 &= ~REX_W;
1550 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001551 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001552 /* Follow REX_W */
1553 break;
1554 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001555
H. Peter Anvinfc561202011-07-07 16:58:22 -07001556 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001557 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001558 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001559 } else if (!(ins->rex & REX_EV) &&
1560 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001561 nasm_nonfatal("invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001562 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001563 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001564 if (ins->rex & REX_EV)
1565 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001566 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1567 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001568 length += 3;
1569 else
1570 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001571 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001572 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001573 nasm_nonfatal("cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001574 return -1;
1575 } else if (bits == 64) {
1576 length++;
1577 } else if ((ins->rex & REX_L) &&
1578 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001579 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001580 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001581 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001582 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001583 length++;
1584 } else {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001585 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001586 return -1;
1587 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001588 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001589
1590 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001591 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001592 /*!
H. Peter Anvin (Intel)be99ebd2018-12-13 22:12:37 -08001593 *!lock [on] LOCK prefix on unlockable instructions
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001594 *! warns about \c{LOCK} prefixes on unlockable instructions.
1595 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001596 nasm_warn(WARN_LOCK | ERR_PASS2 , "instruction is not lockable");
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001597 }
1598
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001599 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001600
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001601 /*
1602 * when BND prefix is set by DEFAULT directive,
1603 * BND prefix is added to every appropriate instruction line
1604 * unless it is overridden by NOBND prefix.
1605 */
1606 if (globalbnd &&
1607 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1608 ins->prefixes[PPS_REP] = P_BND;
1609
H. Peter Anvina77692b2016-09-20 14:04:33 -07001610 /*
1611 * Add length of legacy prefixes
1612 */
1613 length += emit_prefix(NULL, bits, ins);
1614
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001615 return length;
1616}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001617
H. Peter Anvina77692b2016-09-20 14:04:33 -07001618static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001619{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001620 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001621 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001622 !(ins->rex & (REX_V | REX_EV)) &&
1623 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001624 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1625 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001626 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001627 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001628 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001629}
1630
H. Peter Anvina77692b2016-09-20 14:04:33 -07001631static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1632{
1633 int bytes = 0;
1634 int j;
1635
1636 for (j = 0; j < MAXPREFIX; j++) {
1637 uint8_t c = 0;
1638 switch (ins->prefixes[j]) {
1639 case P_WAIT:
1640 c = 0x9B;
1641 break;
1642 case P_LOCK:
1643 c = 0xF0;
1644 break;
1645 case P_REPNE:
1646 case P_REPNZ:
1647 case P_XACQUIRE:
1648 case P_BND:
1649 c = 0xF2;
1650 break;
1651 case P_REPE:
1652 case P_REPZ:
1653 case P_REP:
1654 case P_XRELEASE:
1655 c = 0xF3;
1656 break;
1657 case R_CS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001658 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001659 nasm_warn(WARN_OTHER|ERR_PASS2, "cs segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001660 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001661 c = 0x2E;
1662 break;
1663 case R_DS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001664 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001665 nasm_warn(WARN_OTHER|ERR_PASS2, "ds segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001666 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001667 c = 0x3E;
1668 break;
1669 case R_ES:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001670 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001671 nasm_warn(WARN_OTHER|ERR_PASS2, "es segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001672 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001673 c = 0x26;
1674 break;
1675 case R_FS:
1676 c = 0x64;
1677 break;
1678 case R_GS:
1679 c = 0x65;
1680 break;
1681 case R_SS:
1682 if (bits == 64) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001683 nasm_warn(WARN_OTHER|ERR_PASS2, "ss segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001684 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001685 }
1686 c = 0x36;
1687 break;
1688 case R_SEGR6:
1689 case R_SEGR7:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001690 nasm_nonfatal("segr6 and segr7 cannot be used as prefixes");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001691 break;
1692 case P_A16:
1693 if (bits == 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001694 nasm_nonfatal("16-bit addressing is not supported "
1695 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001696 } else if (bits != 16)
1697 c = 0x67;
1698 break;
1699 case P_A32:
1700 if (bits != 32)
1701 c = 0x67;
1702 break;
1703 case P_A64:
1704 if (bits != 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001705 nasm_nonfatal("64-bit addressing is only supported "
1706 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001707 }
1708 break;
1709 case P_ASP:
1710 c = 0x67;
1711 break;
1712 case P_O16:
1713 if (bits != 16)
1714 c = 0x66;
1715 break;
1716 case P_O32:
1717 if (bits == 16)
1718 c = 0x66;
1719 break;
1720 case P_O64:
1721 /* REX.W */
1722 break;
1723 case P_OSP:
1724 c = 0x66;
1725 break;
1726 case P_EVEX:
1727 case P_VEX3:
1728 case P_VEX2:
1729 case P_NOBND:
1730 case P_none:
1731 break;
1732 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001733 nasm_panic("invalid instruction prefix");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001734 }
1735 if (c) {
1736 if (data)
1737 out_rawbyte(data, c);
1738 bytes++;
1739 }
1740 }
1741 return bytes;
1742}
1743
1744static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001745{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001746 uint8_t c;
1747 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001748 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001749 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001750 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001751 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001752 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001753 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001754 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001755 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001756 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001757
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001758 ins->rex_done = false;
1759
H. Peter Anvina77692b2016-09-20 14:04:33 -07001760 emit_prefix(data, bits, ins);
1761
H. Peter Anvin839eca22007-10-29 23:12:47 -07001762 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001763 c = *codes++;
1764 op1 = (c & 3) + ((opex & 1) << 2);
1765 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1766 opx = &ins->oprs[op1];
1767 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001768
H. Peter Anvina77692b2016-09-20 14:04:33 -07001769
H. Peter Anvin839eca22007-10-29 23:12:47 -07001770 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 case 01:
1772 case 02:
1773 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001774 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001775 emit_rex(data, ins);
1776 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001779
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001780 case 05:
1781 case 06:
1782 case 07:
1783 opex = c;
1784 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001785
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001786 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001787 emit_rex(data, ins);
1788 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001790
Jin Kyu Song164d6072013-10-15 19:10:13 -07001791 case4(014):
1792 break;
1793
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001794 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001795 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001797
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001798 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001799 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001801
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001802 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001803 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001805
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001806 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001807 if (opx->type & (BITS16 | BITS32))
1808 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 else
1810 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001811 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001813
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001814 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001815 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001817
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001818 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001819 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001820 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001822
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001823 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001824 if (opx->segment == data->segment) {
1825 int64_t delta = opx->offset - data->offset
1826 - (data->inslen - data->insoffs);
1827 if (delta > 127 || delta < -128)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001828 nasm_nonfatal("short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001829 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001830 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001832
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001833 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001834 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001835 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001836
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001837 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001838 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001840
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001841 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001842 if (opx->type & (BITS16 | BITS32 | BITS64))
1843 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 else
1845 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001846
1847 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001849
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001850 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001851 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001853
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001854 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001855 if (opx->segment == NO_SEG)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001856 nasm_nonfatal("value referenced by FAR is not relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001857 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001859
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001860 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001861 {
1862 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1863 const struct operand *opy;
1864
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001865 c = *codes++;
1866 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001867 opy = &ins->oprs[c & 7];
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001868 if (!absolute_op(opy))
1869 nasm_nonfatal("non-absolute expression not permitted "
1870 "as argument %d", c & 7);
1871 else if (opy->offset & ~mask)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001872 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001873 "is4 argument exceeds bounds");
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001874 c = opy->offset & mask;
1875 goto emit_is4;
1876 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001877
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001878 case 0173:
1879 c = *codes++;
1880 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001881 c &= 15;
1882 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001883
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001884 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001885 c = 0;
1886 emit_is4:
1887 r = nasm_regvals[opx->basereg];
1888 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001889 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001890
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001891 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001892 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001893 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001894 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001895 "signed dword immediate exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001896 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001897 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001898 break;
1899
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001900 case4(0240):
1901 case 0250:
1902 codes += 3;
1903 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1904 EVEX_P2Z | EVEX_P2AAA, 2);
1905 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1906 bytes[0] = 0x62;
1907 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001908 bytes[1] = ((((ins->rex & 7) << 5) |
1909 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001910 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001911 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1912 ((~ins->vexreg & 15) << 3) |
1913 (1 << 2) | (ins->vex_wlp & 3);
1914 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001915 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001916 break;
1917
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001918 case4(0260):
1919 case 0270:
1920 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001921 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1922 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001923 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1924 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1925 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001926 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001927 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001928 } else {
1929 bytes[0] = 0xc5;
1930 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001931 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001932 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001933 }
1934 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001935
H. Peter Anvine014f352012-02-25 22:35:19 -08001936 case 0271:
1937 case 0272:
1938 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001939 break;
1940
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001941 case4(0274):
1942 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001943 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001944 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001945
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001946 if (absolute_op(opx)) {
1947 if (ins->rex & REX_W)
1948 s = 64;
1949 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1950 s = 16;
1951 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1952 s = 32;
1953 else
1954 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001955
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001956 um = (uint64_t)2 << (s-1);
1957 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001958
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001959 if (uv > 127 && uv < (uint64_t)-128 &&
1960 (uv < um-128 || uv > um-1)) {
1961 /* If this wasn't explicitly byte-sized, warn as though we
1962 * had fallen through to the imm16/32/64 case.
1963 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001964 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001965 "%s value exceeds bounds",
1966 (opx->type & BITS8) ? "signed byte" :
1967 s == 16 ? "word" :
1968 s == 32 ? "dword" :
1969 "signed dword");
1970 }
1971
1972 /* Output as a raw byte to avoid byte overflow check */
1973 out_rawbyte(data, (uint8_t)uv);
1974 } else {
1975 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001976 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001977 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001978 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001979
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001980 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001982
H. Peter Anvine2c80182005-01-15 22:15:51 +00001983 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001984 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1985 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001986 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001987
H. Peter Anvine2c80182005-01-15 22:15:51 +00001988 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001989 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1990 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001991 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001992
H. Peter Anvine2c80182005-01-15 22:15:51 +00001993 case 0312:
1994 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001995
Keith Kaniosb7a89542007-04-12 02:40:54 +00001996 case 0313:
1997 ins->rex = 0;
1998 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001999
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002000 case4(0314):
2001 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08002002
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002004 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002006
H. Peter Anvine2c80182005-01-15 22:15:51 +00002007 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07002008 case 0323:
2009 break;
2010
Keith Kaniosb7a89542007-04-12 02:40:54 +00002011 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002012 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002013 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07002014
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002015 case 0325:
2016 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07002017
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04002018 case 0326:
2019 break;
2020
H. Peter Anvine2c80182005-01-15 22:15:51 +00002021 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002022 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002023 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002024
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002026 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002027
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002028 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002029 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002030 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002031 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002032
Keith Kanios48af1772007-08-17 07:37:52 +00002033 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002034 if (ins->rex & REX_R)
2035 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00002036 ins->rex &= ~(REX_L|REX_R);
2037 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00002038
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002039 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002040 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002041
H. Peter Anvin962e3052008-08-28 17:47:16 -07002042 case 0336:
2043 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002044 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07002045
H. Peter Anvine2c80182005-01-15 22:15:51 +00002046 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002047 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002048 nasm_panic("non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07002049
H. Peter Anvin5358b982018-12-18 18:06:26 -08002050 out_reserve(data, ins->oprs[0].offset * resb_bytes(ins->opcode));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002051 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002052
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002053 case 0341:
2054 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08002055
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002056 case 0360:
2057 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002058
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002059 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002060 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002061 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002062
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002063 case 0364:
2064 case 0365:
2065 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002066
Keith Kanios48af1772007-08-17 07:37:52 +00002067 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002068 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002069 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00002070 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002071
Jin Kyu Song03041092013-10-15 19:38:51 -07002072 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002073 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002074
H. Peter Anvine2c80182005-01-15 22:15:51 +00002075 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002076 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002077 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002078
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002079 case 0374:
2080 eat = EA_XMMVSIB;
2081 break;
2082
2083 case 0375:
2084 eat = EA_YMMVSIB;
2085 break;
2086
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002087 case 0376:
2088 eat = EA_ZMMVSIB;
2089 break;
2090
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002091 case4(0100):
2092 case4(0110):
2093 case4(0120):
2094 case4(0130):
2095 case4(0200):
2096 case4(0204):
2097 case4(0210):
2098 case4(0214):
2099 case4(0220):
2100 case4(0224):
2101 case4(0230):
2102 case4(0234):
2103 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002104 ea ea_data;
2105 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002106 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002107 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002108 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07002109
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002110 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002111 /* pick rfield from operand b (opx) */
2112 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07002113 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002114 } else {
2115 /* rfield is constant */
2116 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002117 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002118 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002119
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002120 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002121 rfield, rflags, ins, &errmsg) != eat)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002122 nasm_nonfatal("%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07002123
H. Peter Anvine2c80182005-01-15 22:15:51 +00002124 p = bytes;
2125 *p++ = ea_data.modrm;
2126 if (ea_data.sib_present)
2127 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07002128 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002129
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002130 /*
2131 * Make sure the address gets the right offset in case
2132 * the line breaks in the .lst file (BR 1197827)
2133 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002134
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002135 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002136 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07002137 if (ea_data.disp8) {
2138 out_rawbyte(data, ea_data.disp8);
2139 } else if (ea_data.rip) {
2140 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002141 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002142 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002143
H. Peter Anvina77692b2016-09-20 14:04:33 -07002144 if (overflow_general(opy->offset, asize) ||
2145 signed_bits(opy->offset, ins->addr_size) !=
2146 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08002147 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002148
H. Peter Anvina77692b2016-09-20 14:04:33 -07002149 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07002150 (asize > ea_data.bytes)
2151 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002152 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002153 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002154 }
2155 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07002156
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002157 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07002158 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002159 ": instruction code \\%o (0x%02X) given", c, c);
2160 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002161 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07002162 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002163}
2164
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002165static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002166{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002167 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002168 nasm_panic("invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002169 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002170}
2171
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00002172static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002173{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002174 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002175 nasm_panic("invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002176 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002177}
2178
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002179static int op_rexflags(const operand * o, int mask)
2180{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002181 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002182 int val;
2183
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002184 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002185 nasm_panic("invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002186
H. Peter Anvina4835d42008-05-20 14:21:29 -07002187 flags = nasm_reg_flags[o->basereg];
2188 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002189
2190 return rexflags(val, flags, mask);
2191}
2192
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002193static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002194{
2195 int rex = 0;
2196
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002197 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002198 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002199 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002200 rex |= REX_W;
2201 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2202 rex |= REX_H;
2203 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2204 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002205
2206 return rex & mask;
2207}
2208
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002209static int evexflags(int val, decoflags_t deco,
2210 int mask, uint8_t byte)
2211{
2212 int evex = 0;
2213
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002214 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002215 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002216 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002217 evex |= (EVEX_P0RP | EVEX_P0X);
2218 break;
2219 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002220 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002221 evex |= EVEX_P2VP;
2222 if (deco & Z)
2223 evex |= EVEX_P2Z;
2224 if (deco & OPMASK_MASK)
2225 evex |= deco & EVEX_P2AAA;
2226 break;
2227 }
2228 return evex & mask;
2229}
2230
2231static int op_evexflags(const operand * o, int mask, uint8_t byte)
2232{
2233 int val;
2234
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002235 val = nasm_regvals[o->basereg];
2236
2237 return evexflags(val, o->decoflags, mask, byte);
2238}
2239
H. Peter Anvin23595f52009-07-25 17:44:25 -07002240static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002241 insn *instruction,
2242 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002243{
2244 const struct itemplate *temp;
2245 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002246 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002247 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002248 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002249 int i;
2250
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002251 /* broadcasting uses a different data element size */
2252 for (i = 0; i < instruction->operands; i++)
2253 if (i == broadcast)
2254 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2255 else
2256 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002257
2258 merr = MERR_INVALOP;
2259
2260 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002261 temp->opcode != I_none; temp++) {
2262 m = matches(temp, instruction, bits);
2263 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002264 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002265 m = MOK_GOOD;
2266 else
2267 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002268 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002269 /*
2270 * Missing operand size and a candidate for fuzzy matching...
2271 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002272 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002273 if (i == broadcast)
2274 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2275 else
2276 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002277 opsizemissing = true;
2278 }
2279 if (m > merr)
2280 merr = m;
2281 if (merr == MOK_GOOD)
2282 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002283 }
2284
2285 /* No match, but see if we can get a fuzzy operand size match... */
2286 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002287 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002288
2289 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002290 /*
2291 * We ignore extrinsic operand sizes on registers, so we should
2292 * never try to fuzzy-match on them. This also resolves the case
2293 * when we have e.g. "xmmrm128" in two different positions.
2294 */
2295 if (is_class(REGISTER, instruction->oprs[i].type))
2296 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002297
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002298 /* This tests if xsizeflags[i] has more than one bit set */
2299 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2300 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002301
Jin Kyu Song7903c072013-10-30 03:00:12 -07002302 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002303 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002304 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2305 BITS32 : BITS64);
2306 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002307 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002308 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002309 }
2310
2311 /* Try matching again... */
2312 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002313 temp->opcode != I_none; temp++) {
2314 m = matches(temp, instruction, bits);
2315 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002316 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002317 m = MOK_GOOD;
2318 else
2319 m = MERR_INVALOP;
2320 }
2321 if (m > merr)
2322 merr = m;
2323 if (merr == MOK_GOOD)
2324 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002325 }
2326
H. Peter Anvina81655b2009-07-25 18:15:28 -07002327done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002328 *tempp = temp;
2329 return merr;
2330}
2331
Mark Charneydcaef4b2014-10-09 13:45:17 -04002332static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2333{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002334 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002335 uint8_t brcast_num;
2336
Mark Charneydcaef4b2014-10-09 13:45:17 -04002337 if (brsize > BITS64)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002338 nasm_fatal("size of broadcasting element is greater than 64 bits");
Mark Charneydcaef4b2014-10-09 13:45:17 -04002339
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002340 /*
2341 * The shift term is to take care of the extra BITS80 inserted
2342 * between BITS64 and BITS128.
2343 */
2344 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2345 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002346
2347 return brcast_num;
2348}
2349
H. Peter Anvin65289e82009-07-25 17:25:11 -07002350static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002351 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002352{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002353 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002354 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002355 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002356
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002357 /*
2358 * Check the opcode
2359 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002360 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002361 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002362
2363 /*
2364 * Count the operands
2365 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002367 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002368
2369 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002370 * Is it legal?
2371 */
Chang S. Baea5786342018-08-15 23:22:21 +03002372 if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002373 return MERR_INVALOP;
2374
2375 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002376 * {evex} available?
2377 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002378 switch (instruction->prefixes[PPS_VEX]) {
2379 case P_EVEX:
2380 if (!itemp_has(itemp, IF_EVEX))
2381 return MERR_ENCMISMATCH;
2382 break;
2383 case P_VEX3:
2384 case P_VEX2:
2385 if (!itemp_has(itemp, IF_VEX))
2386 return MERR_ENCMISMATCH;
2387 break;
2388 default:
2389 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002390 }
2391
2392 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002393 * Check that no spurious colons or TOs are present
2394 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002395 for (i = 0; i < itemp->operands; i++)
2396 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002397 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002398
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002399 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002400 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002401 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002402 switch (itemp_smask(itemp)) {
2403 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002404 asize = BITS8;
2405 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002406 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002407 asize = BITS16;
2408 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002409 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002410 asize = BITS32;
2411 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002412 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002413 asize = BITS64;
2414 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002415 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002416 asize = BITS128;
2417 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002418 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002419 asize = BITS256;
2420 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002421 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002422 asize = BITS512;
2423 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002424 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002425 switch (bits) {
2426 case 16:
2427 asize = BITS16;
2428 break;
2429 case 32:
2430 asize = BITS32;
2431 break;
2432 case 64:
2433 asize = BITS64;
2434 break;
2435 default:
2436 asize = 0;
2437 break;
2438 }
2439 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002440 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002441 asize = 0;
2442 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002443 }
2444
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002445 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002446 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002447 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002448 memset(size, 0, sizeof size);
2449 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002450 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002451 /* S- flags apply to all operands */
2452 for (i = 0; i < MAX_OPERANDS; i++)
2453 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002454 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002455
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002456 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002457 * Check that the operand flags all match up,
2458 * it's a bit tricky so lets be verbose:
2459 *
2460 * 1) Find out the size of operand. If instruction
2461 * doesn't have one specified -- we're trying to
2462 * guess it either from template (IF_S* flag) or
2463 * from code bits.
2464 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002465 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002466 * template has an operand size specified AND this size differ
2467 * from which instruction has (perhaps we got it from code bits)
2468 * we are:
2469 * a) Check that only size of instruction and operand is differ
2470 * other characteristics do match
2471 * b) Perhaps it's a register specified in instruction so
2472 * for such a case we just mark that operand as "size
2473 * missing" and this will turn on fuzzy operand size
2474 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002475 */
2476 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002477 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002478 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002479 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002480 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002481 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002482 opflags_t template_opsize, insn_opsize;
2483
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002484 if (!(type & SIZE_MASK))
2485 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002486
Jin Kyu Song7903c072013-10-30 03:00:12 -07002487 insn_opsize = type & SIZE_MASK;
2488 if (!is_broadcast) {
2489 template_opsize = itemp->opd[i] & SIZE_MASK;
2490 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002491 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2492
2493 if (~ideco & BRDCAST_MASK)
2494 return MERR_BRNOTHERE;
2495
Jin Kyu Song7903c072013-10-30 03:00:12 -07002496 /*
2497 * when broadcasting, the element size depends on
2498 * the instruction type. decorator flag should match.
2499 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002500 if (deco_brsize) {
2501 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002502 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002503 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002504 } else {
2505 template_opsize = 0;
2506 }
2507 }
2508
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002509 if (~ideco & deco & OPMASK_MASK)
2510 return MERR_MASKNOTHERE;
2511
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002512 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2513 return MERR_DECONOTHERE;
2514
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002515 if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002516 return MERR_INVALOP;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002517
2518 if (~itemp->opd[i] & type & REGSET_MASK)
2519 return (itemp->opd[i] & REGSET_MASK)
2520 ? MERR_REGSETSIZE : MERR_REGSET;
2521
2522 if (template_opsize) {
Jin Kyu Song7903c072013-10-30 03:00:12 -07002523 if (template_opsize != insn_opsize) {
2524 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002525 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002526 } else if (!is_class(REGISTER, type)) {
2527 /*
2528 * Note: we don't honor extrinsic operand sizes for registers,
2529 * so "missing operand size" for a register should be
2530 * considered a wildcard match rather than an error.
2531 */
2532 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002533 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002534 } else if (is_broadcast &&
2535 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002536 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002537 /*
2538 * broadcasting opsize matches but the number of repeated memory
2539 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002540 * if 64b double precision float is broadcasted to ymm (256b),
2541 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002542 */
2543 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002544 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002545 }
2546 }
2547
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002548 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002549 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002550
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002551 /*
2552 * Check operand sizes
2553 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002554 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2555 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002556 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002557 asize = itemp->opd[i] & SIZE_MASK;
2558 if (asize) {
2559 for (i = 0; i < oprs; i++)
2560 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 break;
2562 }
2563 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002564 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002565 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002566 }
2567
Keith Kaniosb7a89542007-04-12 02:40:54 +00002568 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002569 if (!(itemp->opd[i] & SIZE_MASK) &&
2570 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002571 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002572 }
2573
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002574 /*
2575 * Check template is okay at the set cpu level
2576 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002577 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002578 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002579
Keith Kaniosb7a89542007-04-12 02:40:54 +00002580 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002581 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002582 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002583 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002584 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002585
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002586 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002587 * If we have a HLE prefix, look for the NOHLE flag
2588 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002589 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002590 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2591 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2592 return MERR_BADHLE;
2593
2594 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002595 * Check if special handling needed for Jumps
2596 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002597 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002598 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002599
Jin Kyu Song03041092013-10-15 19:38:51 -07002600 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002601 * Check if BND prefix is allowed.
2602 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002603 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002604 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002605 (has_prefix(instruction, PPS_REP, P_BND) ||
2606 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002607 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002608 else if (itemp_has(itemp, IF_BND) &&
2609 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2610 has_prefix(instruction, PPS_REP, P_REPNZ)))
2611 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002612
H. Peter Anvin60926242009-07-26 16:25:38 -07002613 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002614}
2615
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002616/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002617 * Check if ModR/M.mod should/can be 01.
2618 * - EAF_BYTEOFFS is set
2619 * - offset can fit in a byte when EVEX is not used
2620 * - offset can be compressed when EVEX is used
2621 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002622#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2623 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2624 is_disp8n(input, ins, &output->disp8) : \
2625 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2626 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002627
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002628static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002629 int rfield, opflags_t rflags, insn *ins,
2630 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002631{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002632 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002633 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002634 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002635
H. Peter Anvin8f622462017-04-02 19:02:29 -07002636 *errmsg = "invalid effective address"; /* Default error message */
2637
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002638 output->type = EA_SCALAR;
2639 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002640 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002641
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002642 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002643 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002644 /* EVEX.R' flag for the REG operand */
2645 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002646
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002647 if (is_class(REGISTER, input->type)) {
2648 /*
2649 * It's a direct register.
2650 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002651 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002652 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002653
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002654 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002655 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002656
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002657 /* broadcasting is not available with a direct register operand. */
2658 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002659 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002660 goto err;
2661 }
2662
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002663 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002664 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002665 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002666 output->bytes = 0; /* no offset necessary either */
2667 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2668 } else {
2669 /*
2670 * It's a memory reference.
2671 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002672
2673 /* Embedded rounding or SAE is not available with a mem ref operand. */
2674 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002675 *errmsg = "embedded rounding is available only with "
2676 "register-register operations";
2677 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002678 }
2679
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002680 if (input->basereg == -1 &&
2681 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002682 /*
2683 * It's a pure offset.
2684 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002685 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002686 if (input->segment == NO_SEG ||
2687 (input->opflags & OPFLAG_RELATIVE)) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08002688 nasm_warn(WARN_OTHER|ERR_PASS2, "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002689 input->type &= ~IP_REL;
2690 input->type |= MEMORY;
2691 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002692 }
2693
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002694 if (bits == 64 &&
2695 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002696 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002697 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002698 }
2699
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002700 if (eaflags & EAF_BYTEOFFS ||
2701 (eaflags & EAF_WORDOFFS &&
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002702 input->disp_size != (addrbits != 16 ? 32 : 16)))
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002703 nasm_warn(WARN_OTHER, "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002704
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002705 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002706 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002707 output->sib = GEN_SIB(0, 4, 5);
2708 output->bytes = 4;
2709 output->modrm = GEN_MODRM(0, rfield, 4);
2710 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002711 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002712 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002713 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002714 output->modrm = GEN_MODRM(0, rfield,
2715 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002716 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002717 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002718 } else {
2719 /*
2720 * It's an indirection.
2721 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002723 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002724 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002725 int t, it, bt; /* register numbers */
2726 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002727
H. Peter Anvine2c80182005-01-15 22:15:51 +00002728 if (s == 0)
2729 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002730
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002731 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002732 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002733 ix = nasm_reg_flags[i];
2734 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002735 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002736 ix = 0;
2737 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002738
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002739 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002740 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002741 bx = nasm_reg_flags[b];
2742 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002743 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002744 bx = 0;
2745 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002746
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002747 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002748 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002749 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002750 int32_t o = input->offset;
2751 int mod, scale, index, base;
2752
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002753 /*
2754 * For a vector SIB, one has to be a vector and the other,
2755 * if present, a GPR. The vector must be the index operand.
2756 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002757 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002758 if (s == 0)
2759 s = 1;
2760 else if (s != 1)
2761 goto err;
2762
2763 t = bt, bt = it, it = t;
2764 x = bx, bx = ix, ix = x;
2765 }
2766
2767 if (bt != -1) {
2768 if (REG_GPR & ~bx)
2769 goto err;
2770 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2771 sok &= bx;
2772 else
2773 goto err;
2774 }
2775
2776 /*
2777 * While we're here, ensure the user didn't specify
2778 * WORD or QWORD
2779 */
2780 if (input->disp_size == 16 || input->disp_size == 64)
2781 goto err;
2782
2783 if (addrbits == 16 ||
2784 (addrbits == 32 && !(sok & BITS32)) ||
2785 (addrbits == 64 && !(sok & BITS64)))
2786 goto err;
2787
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002788 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2789 : ((ix & YMMREG & ~REG_EA)
2790 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002791
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002792 output->rex |= rexflags(it, ix, REX_X);
2793 output->rex |= rexflags(bt, bx, REX_B);
2794 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002795
2796 index = it & 7; /* it is known to be != -1 */
2797
2798 switch (s) {
2799 case 1:
2800 scale = 0;
2801 break;
2802 case 2:
2803 scale = 1;
2804 break;
2805 case 4:
2806 scale = 2;
2807 break;
2808 case 8:
2809 scale = 3;
2810 break;
2811 default: /* then what the smeg is it? */
2812 goto err; /* panic */
2813 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002814
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002815 if (bt == -1) {
2816 base = 5;
2817 mod = 0;
2818 } else {
2819 base = (bt & 7);
2820 if (base != REG_NUM_EBP && o == 0 &&
2821 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002822 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002823 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002824 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002825 mod = 1;
2826 else
2827 mod = 2;
2828 }
2829
2830 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002831 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2832 output->modrm = GEN_MODRM(mod, rfield, 4);
2833 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002834 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002835 /*
2836 * it must be a 32/64-bit memory reference. Firstly we have
2837 * to check that all registers involved are type E/Rxx.
2838 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002839 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002840 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002841
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002842 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002843 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2844 sok &= ix;
2845 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002846 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002847 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002848
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002849 if (bt != -1) {
2850 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002851 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002852 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002853 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002854 sok &= bx;
2855 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002856
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002857 /*
2858 * While we're here, ensure the user didn't specify
2859 * WORD or QWORD
2860 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002861 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002862 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002863
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002864 if (addrbits == 16 ||
2865 (addrbits == 32 && !(sok & BITS32)) ||
2866 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002867 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002868
Keith Kaniosb7a89542007-04-12 02:40:54 +00002869 /* now reorganize base/index */
2870 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002871 ((hb == b && ht == EAH_NOTBASE) ||
2872 (hb == i && ht == EAH_MAKEBASE))) {
2873 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002874 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002875 x = bx, bx = ix, ix = x;
2876 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002877
Jin Kyu Song164d6072013-10-15 19:10:13 -07002878 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002879 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002880 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002881 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002882 if (eaflags & EAF_MIB) {
2883 /* only for mib operands */
2884 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2885 /*
2886 * make a single reg index [reg*1].
2887 * gas uses this form for an explicit index register.
2888 */
2889 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2890 }
2891 if ((ht == EAH_SUMMED) && bt == -1) {
2892 /* separate once summed index into [base, index] */
2893 bt = it, bx = ix, s--;
2894 }
2895 } else {
2896 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002897 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002898 s == 3 || s == 5 || s == 9) && bt == -1) {
2899 /* convert 3*EAX to EAX+2*EAX */
2900 bt = it, bx = ix, s--;
2901 }
2902 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002903 (eaflags & EAF_TIMESTWO) &&
2904 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002905 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002906 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002907 * to sib format with 0x0 displacement - [EAX*1+0].
2908 */
2909 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2910 }
2911 }
Keith Kanios48af1772007-08-17 07:37:52 +00002912 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002913 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002914 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002915 x = ix, ix = bx, bx = x;
2916 }
2917 if (it == REG_NUM_ESP ||
2918 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002919 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002920
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002921 output->rex |= rexflags(it, ix, REX_X);
2922 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002923
Keith Kanios48af1772007-08-17 07:37:52 +00002924 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002925 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002926 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002927
Keith Kaniosb7a89542007-04-12 02:40:54 +00002928 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002929 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002930 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002931 } else {
2932 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002933 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002934 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002935 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002936 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002937 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002938 mod = 1;
2939 else
2940 mod = 2;
2941 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002942
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002943 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002944 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2945 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002946 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002947 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002948 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002949
Keith Kaniosb7a89542007-04-12 02:40:54 +00002950 if (it == -1)
2951 index = 4, s = 1;
2952 else
2953 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002954
H. Peter Anvine2c80182005-01-15 22:15:51 +00002955 switch (s) {
2956 case 1:
2957 scale = 0;
2958 break;
2959 case 2:
2960 scale = 1;
2961 break;
2962 case 4:
2963 scale = 2;
2964 break;
2965 case 8:
2966 scale = 3;
2967 break;
2968 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002969 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002970 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002971
Keith Kaniosb7a89542007-04-12 02:40:54 +00002972 if (bt == -1) {
2973 base = 5;
2974 mod = 0;
2975 } else {
2976 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002977 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002978 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002979 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002980 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002981 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002982 mod = 1;
2983 else
2984 mod = 2;
2985 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002986
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002987 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002988 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2989 output->modrm = GEN_MODRM(mod, rfield, 4);
2990 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002991 }
2992 } else { /* it's 16-bit */
2993 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002994 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002995
Keith Kaniosb7a89542007-04-12 02:40:54 +00002996 /* check for 64-bit long mode */
2997 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002998 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002999
H. Peter Anvine2c80182005-01-15 22:15:51 +00003000 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003001 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
3002 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003003 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003004
Keith Kaniosb7a89542007-04-12 02:40:54 +00003005 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003006 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003007 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003008
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003010 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003011 if (b == -1 && i != -1) {
3012 int tmp = b;
3013 b = i;
3014 i = tmp;
3015 } /* swap */
3016 if ((b == R_SI || b == R_DI) && i != -1) {
3017 int tmp = b;
3018 b = i;
3019 i = tmp;
3020 }
3021 /* have BX/BP as base, SI/DI index */
3022 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003023 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00003024 if (i != -1 && b != -1 &&
3025 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003026 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003027 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003028 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003029
H. Peter Anvine2c80182005-01-15 22:15:51 +00003030 rm = -1;
3031 if (i != -1)
3032 switch (i * 256 + b) {
3033 case R_SI * 256 + R_BX:
3034 rm = 0;
3035 break;
3036 case R_DI * 256 + R_BX:
3037 rm = 1;
3038 break;
3039 case R_SI * 256 + R_BP:
3040 rm = 2;
3041 break;
3042 case R_DI * 256 + R_BP:
3043 rm = 3;
3044 break;
3045 } else
3046 switch (b) {
3047 case R_SI:
3048 rm = 4;
3049 break;
3050 case R_DI:
3051 rm = 5;
3052 break;
3053 case R_BP:
3054 rm = 6;
3055 break;
3056 case R_BX:
3057 rm = 7;
3058 break;
3059 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003060 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003061 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003062
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003063 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003064 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003065 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003066 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00003067 mod = 1;
3068 else
3069 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003070
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003071 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003072 output->bytes = mod; /* bytes of offset needed */
3073 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003074 }
3075 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003076 }
H. Peter Anvin70653092007-10-19 14:42:29 -07003077
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003078 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003079 return output->type;
3080
3081err:
3082 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003083}
3084
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003085static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003086{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003087 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003088 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003089
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003090 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003091
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003092 switch (ins->prefixes[PPS_ASIZE]) {
3093 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003094 valid &= 16;
3095 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003096 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003097 valid &= 32;
3098 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003099 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003100 valid &= 64;
3101 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003102 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003103 valid &= (addrbits == 32) ? 16 : 32;
3104 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003105 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003106 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003107 }
3108
3109 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003110 if (is_class(MEMORY, ins->oprs[j].type)) {
3111 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07003112
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003113 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003114 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003115 i = 0;
3116 else
3117 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003118
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003119 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003120 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003121 b = 0;
3122 else
3123 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003124
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003125 if (ins->oprs[j].scale == 0)
3126 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003127
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003128 if (!i && !b) {
3129 int ds = ins->oprs[j].disp_size;
3130 if ((addrbits != 64 && ds > 8) ||
3131 (addrbits == 64 && ds == 16))
3132 valid &= ds;
3133 } else {
3134 if (!(REG16 & ~b))
3135 valid &= 16;
3136 if (!(REG32 & ~b))
3137 valid &= 32;
3138 if (!(REG64 & ~b))
3139 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07003140
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003141 if (!(REG16 & ~i))
3142 valid &= 16;
3143 if (!(REG32 & ~i))
3144 valid &= 32;
3145 if (!(REG64 & ~i))
3146 valid &= 64;
3147 }
3148 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003149 }
3150
3151 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003152 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003153 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003154 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04003155 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003156 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003157 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003158 /* Impossible... */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03003159 nasm_nonfatal("impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003160 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003161 }
3162
3163 defdisp = ins->addr_size == 16 ? 16 : 32;
3164
3165 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003166 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
3167 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
3168 /*
3169 * mem_offs sizes must match the address size; if not,
3170 * strip the MEM_OFFS bit and match only EA instructions
3171 */
3172 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
3173 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003174 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003175}