blob: 5daf4c117ca194b83adcdc35224ecdb3ddfb76a5 [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 Anvin04445362016-09-21 15:56:19 -0700586/* This is totally just a wild guess what is reasonable... */
587#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
588
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800589int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000590{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700591 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000592 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700593 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300594 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000595
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700596 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700597 data.offset = start;
598 data.segment = segment;
599 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700600 data.bits = bits;
601
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700602 wsize = db_bytes(instruction->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300603 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000605
H. Peter Anvineba20a72002-04-30 20:53:55 +0000606 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700608
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700609 list_for_each(e, instruction->eops) {
610 if (e->type == EOT_DB_NUMBER) {
611 if (wsize > 8) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300612 nasm_nonfatal("integer supplied to a DT,DO,DY or DZ");
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700613 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700614 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700615 data.inslen = data.size = wsize;
616 data.toffset = e->offset;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700617 data.twrt = e->wrt;
618 data.relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700619 if (e->segment != NO_SEG && (e->segment & 1)) {
620 data.tsegment = e->segment;
621 data.type = OUT_SEGMENT;
622 data.sign = OUT_UNSIGNED;
623 } else {
624 data.tsegment = e->segment;
625 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
626 data.sign = OUT_WRAP;
627 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700628 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000629 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700630 } else if (e->type == EOT_DB_STRING ||
631 e->type == EOT_DB_STRING_FREE) {
632 int align = e->stringlen % wsize;
633 if (align)
634 align = wsize - align;
635
636 data.insoffs = 0;
637 data.inslen = e->stringlen + align;
638
639 out_rawdata(&data, e->stringval, e->stringlen);
640 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700641 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700643 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700644 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700646 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700647 off_t base = 0;
648 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700649 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700650 char *buf = NULL;
651 size_t blk = 0; /* Buffered I/O block size */
652 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000653
H. Peter Anvin94ead272017-09-27 15:22:23 -0700654 if (!t)
655 goto done;
656
H. Peter Anvind81a2352016-09-21 14:03:18 -0700657 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400658 if (!fp) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300659 nasm_nonfatal("`incbin': unable to open file `%s'",
660 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700661 goto done;
662 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000663
H. Peter Anvind81a2352016-09-21 14:03:18 -0700664 len = nasm_file_size(fp);
665
666 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300667 nasm_nonfatal("`incbin': unable to get length of file `%s'",
668 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700669 goto close_done;
670 }
671
H. Peter Anvina77692b2016-09-20 14:04:33 -0700672 if (instruction->eops->next) {
673 base = instruction->eops->next->offset;
674 if (base >= len) {
675 len = 0;
676 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000677 len -= base;
678 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700679 len > (off_t)instruction->eops->next->next->offset)
680 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000681 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000682 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700683
H. Peter Anvina77692b2016-09-20 14:04:33 -0700684 lfmt->set_offset(data.offset);
685 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000686
H. Peter Anvind81a2352016-09-21 14:03:18 -0700687 if (!len)
688 goto end_incbin;
689
690 /* Try to map file data */
691 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700692 if (!map) {
693 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
694 buf = nasm_malloc(blk);
695 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700696
697 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700698 /*
699 * Consider these irrelevant for INCBIN, since it is fully
700 * possible that these might be (way) bigger than an int
701 * can hold; there is, however, no reason to widen these
702 * types just for INCBIN. data.inslen == 0 signals to the
703 * backend that these fields are meaningless, if at all
704 * needed.
705 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700706 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700707 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700708
H. Peter Anvind81a2352016-09-21 14:03:18 -0700709 if (map) {
710 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700711 } else if ((off_t)m == len) {
712 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700713 } else {
714 off_t l = len;
715
716 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300717 nasm_nonfatal("`incbin': unable to seek on file `%s'",
718 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700719 goto end_incbin;
720 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700721 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700722 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700723 if (!m || feof(fp)) {
724 /*
725 * This shouldn't happen unless the file
726 * actually changes while we are reading
727 * it.
728 */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300729 nasm_nonfatal("`incbin': unexpected EOF while"
730 " reading file `%s'", fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700731 goto end_incbin;
732 }
733 out_rawdata(&data, buf, m);
734 l -= m;
735 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700736 }
737 }
738 end_incbin:
739 lfmt->downlevel(LIST_INCBIN);
740 if (instruction->times > 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700741 lfmt->uplevel(LIST_TIMES);
742 lfmt->downlevel(LIST_TIMES);
743 }
744 if (ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300745 nasm_nonfatal("`incbin': error while"
746 " reading file `%s'", fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700747 }
748 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700749 if (buf)
750 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700751 if (map)
752 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700753 fclose(fp);
754 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700755 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700756 ;
757 } else {
758 /* "Real" instruction */
759
760 /* Check to see if we need an address-size prefix */
761 add_asp(instruction, bits);
762
763 m = find_match(&temp, instruction, data.segment, data.offset, bits);
764
765 if (m == MOK_GOOD) {
766 /* Matches! */
H. Peter Anvin (Intel)41bb8a82019-08-06 22:56:51 -0700767 int64_t insn_size;
768
769 if (unlikely(itemp_has(temp, IF_OBSOLETE))) {
770 /*
771 * If IF_OBSOLETE is set, warn unless we have *exactly*
772 * the correct CPU level set.
773 *
774 *!obsolete [on] instruction obsolete for the target CPU
775 *! warns if an instruction which has been removed
776 *! from the architecture, and is no longer included
777 *! in the CPU definition given in the \c{[CPU]}
778 *! directive, for example \c{POP CS}, the opcode for
779 *! which, \c{0Fh}, instead is an opcode prefix on
780 *! CPUs newer than the first generation 8086.
781 */
782
783 if (iflag_cmp_cpu_level(&insns_flags[temp->iflag_idx], &cpu)) {
784 nasm_warn(WARN_OBSOLETE,
785 "obsolete instruction invalid on the target CPU");
786 }
787 }
788
789 insn_size = calcsize(data.segment, data.offset,
790 bits, instruction, temp);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700791 nasm_assert(insn_size >= 0);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700792
793 data.itemp = temp;
794 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700795 data.insoffs = 0;
796 data.inslen = insn_size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700797
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700798 gencode(&data, instruction);
799 nasm_assert(data.insoffs == insn_size);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700800 } else {
801 /* No match */
802 switch (m) {
803 case MERR_OPSIZEMISSING:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300804 nasm_nonfatal("operation size not specified");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700805 break;
806 case MERR_OPSIZEMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300807 nasm_nonfatal("mismatch in operand sizes");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700808 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700809 case MERR_BRNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300810 nasm_nonfatal("broadcast not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700811 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700812 case MERR_BRNUMMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300813 nasm_nonfatal("mismatch in the number of broadcasting elements");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700814 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700815 case MERR_MASKNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300816 nasm_nonfatal("mask not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700817 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700818 case MERR_DECONOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300819 nasm_nonfatal("unsupported mode decorator for instruction");
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700820 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700821 case MERR_BADCPU:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300822 nasm_nonfatal("no instruction for this cpu level");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700823 break;
824 case MERR_BADMODE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300825 nasm_nonfatal("instruction not supported in %d-bit mode", bits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700826 break;
827 case MERR_ENCMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300828 nasm_nonfatal("specific encoding scheme not available");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700829 break;
830 case MERR_BADBND:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300831 nasm_nonfatal("bnd prefix is not allowed");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700832 break;
833 case MERR_BADREPNE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300834 nasm_nonfatal("%s prefix is not allowed",
835 (has_prefix(instruction, PPS_REP, P_REPNE) ?
836 "repne" : "repnz"));
H. Peter Anvina77692b2016-09-20 14:04:33 -0700837 break;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700838 case MERR_REGSETSIZE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300839 nasm_nonfatal("invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700840 break;
841 case MERR_REGSET:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300842 nasm_nonfatal("register set not valid for operand");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700843 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700844 default:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300845 nasm_nonfatal("invalid combination of opcode and operands");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700846 break;
847 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700848
849 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400850 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000851 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700852 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000853}
854
H. Peter Anvin29651542018-12-18 19:14:40 -0800855static void debug_set_db_type(insn *instruction)
856{
857 /* Is this really correct? .operands doesn't mean much for Dx */
858 int32_t typeinfo = TYS_ELEMENTS(instruction->operands);
859
860 switch (instruction->opcode) {
861 case I_DB:
862 typeinfo |= TY_BYTE;
863 break;
864 case I_DW:
865 typeinfo |= TY_WORD;
866 break;
867 case I_DD:
868 if (instruction->eops_float)
869 typeinfo |= TY_FLOAT;
870 else
871 typeinfo |= TY_DWORD;
872 break;
873 case I_DQ:
874 /* What about double? */
875 typeinfo |= TY_QWORD;
876 break;
877 case I_DT:
878 /* What about long double? */
879 typeinfo |= TY_TBYTE;
880 break;
881 case I_DO:
882 typeinfo |= TY_OWORD;
883 break;
884 case I_DY:
885 typeinfo |= TY_YWORD;
886 break;
887 case I_DZ:
888 typeinfo |= TY_ZWORD;
889 break;
890 default:
891 panic();
892 }
893
894 dfmt->debug_typevalue(typeinfo);
895}
896
897static void debug_set_type(insn *instruction)
898{
899 int32_t typeinfo;
900
901 if (opcode_is_resb(instruction->opcode)) {
902 typeinfo = TYS_ELEMENTS(instruction->oprs[0].offset);
903
904 switch (instruction->opcode) {
905 case I_RESB:
906 typeinfo |= TY_BYTE;
907 break;
908 case I_RESW:
909 typeinfo |= TY_WORD;
910 break;
911 case I_RESD:
912 typeinfo |= TY_DWORD;
913 break;
914 case I_RESQ:
915 typeinfo |= TY_QWORD;
916 break;
917 case I_REST:
918 typeinfo |= TY_TBYTE;
919 break;
920 case I_RESO:
921 typeinfo |= TY_OWORD;
922 break;
923 case I_RESY:
924 typeinfo |= TY_YWORD;
925 break;
926 case I_RESZ:
927 typeinfo |= TY_ZWORD;
928 break;
929 default:
930 panic();
931 }
932 } else {
933 typeinfo = TY_LABEL;
934 }
935
936 dfmt->debug_typevalue(typeinfo);
937}
938
939
940/* Proecess an EQU directive */
941static void define_equ(insn * instruction)
942{
943 if (!instruction->label) {
944 nasm_nonfatal("EQU not preceded by label");
945 } else if (instruction->operands == 1 &&
946 (instruction->oprs[0].type & IMMEDIATE) &&
947 instruction->oprs[0].wrt == NO_SEG) {
948 define_label(instruction->label,
949 instruction->oprs[0].segment,
950 instruction->oprs[0].offset, false);
951 } else if (instruction->operands == 2
952 && (instruction->oprs[0].type & IMMEDIATE)
953 && (instruction->oprs[0].type & COLON)
954 && instruction->oprs[0].segment == NO_SEG
955 && instruction->oprs[0].wrt == NO_SEG
956 && (instruction->oprs[1].type & IMMEDIATE)
957 && instruction->oprs[1].segment == NO_SEG
958 && instruction->oprs[1].wrt == NO_SEG) {
959 define_label(instruction->label,
960 instruction->oprs[0].offset | SEG_ABS,
961 instruction->oprs[1].offset, false);
962 } else {
963 nasm_nonfatal("bad syntax for EQU");
964 }
965}
966
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800967int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000968{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000969 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700970 enum match_result m;
H. Peter Anvin29651542018-12-18 19:14:40 -0800971 int64_t isize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000972
H. Peter Anvin29651542018-12-18 19:14:40 -0800973 if (instruction->opcode == I_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 return 0;
H. Peter Anvin29651542018-12-18 19:14:40 -0800975 } else if (instruction->opcode == I_EQU) {
976 define_equ(instruction);
977 return 0;
978 } else if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 extop *e;
H. Peter Anvin29651542018-12-18 19:14:40 -0800980 int32_t osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000981
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700982 wsize = db_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700983 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000984
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400985 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000986 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000987
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400989 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400991 warn_overflow_const(e->offset, wsize);
992 } else if (e->type == EOT_DB_STRING ||
993 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000995
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 align = (-osize) % wsize;
997 if (align < 0)
998 align += wsize;
999 isize += osize + align;
1000 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001001
H. Peter Anvin29651542018-12-18 19:14:40 -08001002 debug_set_db_type(instruction);
1003 return isize;
1004 } else if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001005 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001006 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +00001007
H. Peter Anvind81a2352016-09-21 14:03:18 -07001008 len = nasm_file_size_by_path(fname);
1009 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001010 nasm_nonfatal("`incbin': unable to get length of file `%s'",
1011 fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -07001012 return 0;
1013 }
1014
1015 if (instruction->eops->next) {
1016 if (len <= (off_t)instruction->eops->next->offset) {
1017 len = 0;
1018 } else {
1019 len -= instruction->eops->next->offset;
1020 if (instruction->eops->next->next &&
1021 len > (off_t)instruction->eops->next->next->offset) {
1022 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 }
1024 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 }
H. Peter Anvind81a2352016-09-21 14:03:18 -07001026
H. Peter Anvin3e458a82017-05-01 20:28:29 -07001027 len *= instruction->times;
1028 instruction->times = 1; /* Tell the upper layer to not iterate */
1029
H. Peter Anvind81a2352016-09-21 14:03:18 -07001030 return len;
H. Peter Anvin23595f52009-07-25 17:44:25 -07001031 } else {
H. Peter Anvin29651542018-12-18 19:14:40 -08001032 /* Normal instruction, or RESx */
1033
1034 /* Check to see if we need an address-size prefix */
1035 add_asp(instruction, bits);
1036
1037 m = find_match(&temp, instruction, segment, offset, bits);
1038 if (m != MOK_GOOD)
1039 return -1; /* No match */
1040
1041 isize = calcsize(segment, offset, bits, instruction, temp);
1042 debug_set_type(instruction);
1043
1044 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001045 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001046}
1047
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001048static void bad_hle_warn(const insn * ins, uint8_t hleok)
1049{
1050 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001051 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001052 static const enum whatwarn warn[2][4] =
1053 {
1054 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
1055 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
1056 };
1057 unsigned int n;
1058
1059 n = (unsigned int)rep_pfx - P_XACQUIRE;
1060 if (n > 1)
1061 return; /* Not XACQUIRE/XRELEASE */
1062
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001063 ww = warn[n][hleok];
1064 if (!is_class(MEMORY, ins->oprs[0].type))
1065 ww = w_inval; /* HLE requires operand 0 to be memory */
1066
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001067 /*!
1068 *!hle [on] invalid HLE prefixes
1069 *! warns about invalid use of the HLE \c{XACQUIRE} or \c{XRELEASE}
1070 *! prefixes.
1071 */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001072 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001073 case w_none:
1074 break;
1075
1076 case w_lock:
1077 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001078 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001079 "%s with this instruction requires lock",
1080 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001081 }
1082 break;
1083
1084 case w_inval:
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001085 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001086 "%s invalid with this instruction",
1087 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001088 break;
1089 }
1090}
1091
H. Peter Anvin507ae032008-10-09 15:37:10 -07001092/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +04001093#define case3(x) case (x): case (x)+1: case (x)+2
1094#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -07001095
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001096static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001097 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001098{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001099 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001100 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001101 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001102 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001103 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001104 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001105 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001106 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001107 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001108 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -07001109 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -07001110 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001111
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001112 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001113 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001114 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001115
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001116 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001117 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001118
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 (void)segment; /* Don't warn that this parameter is unused */
1120 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001121
H. Peter Anvin839eca22007-10-29 23:12:47 -07001122 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001123 c = *codes++;
1124 op1 = (c & 3) + ((opex & 1) << 2);
1125 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1126 opx = &ins->oprs[op1];
1127 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001128
H. Peter Anvin839eca22007-10-29 23:12:47 -07001129 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001130 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 codes += c, length += c;
1132 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001133
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001134 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001135 opex = c;
1136 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001137
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001138 case4(010):
1139 ins->rex |=
1140 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 codes++, length++;
1142 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001143
Jin Kyu Song164d6072013-10-15 19:10:13 -07001144 case4(014):
1145 /* this is an index reg of MIB operand */
1146 mib_index = opx->basereg;
1147 break;
1148
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001149 case4(020):
1150 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 length++;
1152 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001153
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001154 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 length += 2;
1156 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001157
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001158 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001159 if (opx->type & (BITS16 | BITS32 | BITS64))
1160 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 else
1162 length += (bits == 16) ? 2 : 4;
1163 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001164
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001165 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 length += 4;
1167 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001168
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001169 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001170 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001172
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001173 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 length++;
1175 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001176
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001177 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001178 length += 8; /* MOV reg64/imm */
1179 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001180
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001181 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 length += 2;
1183 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001184
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001185 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001186 if (opx->type & (BITS16 | BITS32 | BITS64))
1187 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 else
1189 length += (bits == 16) ? 2 : 4;
1190 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001191
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001192 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 length += 4;
1194 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001195
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001196 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001197 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001199
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001200 case 0172:
1201 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001202 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001203 length++;
1204 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001205
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001206 case4(0174):
1207 length++;
1208 break;
1209
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001210 case4(0240):
1211 ins->rex |= REX_EV;
1212 ins->vexreg = regval(opx);
1213 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1214 ins->vex_cm = *codes++;
1215 ins->vex_wlp = *codes++;
1216 ins->evex_tuple = (*codes++ - 0300);
1217 break;
1218
1219 case 0250:
1220 ins->rex |= REX_EV;
1221 ins->vexreg = 0;
1222 ins->vex_cm = *codes++;
1223 ins->vex_wlp = *codes++;
1224 ins->evex_tuple = (*codes++ - 0300);
1225 break;
1226
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001227 case4(0254):
1228 length += 4;
1229 break;
1230
1231 case4(0260):
1232 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001233 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001234 ins->vex_cm = *codes++;
1235 ins->vex_wlp = *codes++;
1236 break;
1237
1238 case 0270:
1239 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001240 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001241 ins->vex_cm = *codes++;
1242 ins->vex_wlp = *codes++;
1243 break;
1244
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001245 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001246 hleok = c & 3;
1247 break;
1248
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001249 case4(0274):
1250 length++;
1251 break;
1252
1253 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001254 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001255
H. Peter Anvine2c80182005-01-15 22:15:51 +00001256 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001257 if (bits == 64)
1258 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001259 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001261
H. Peter Anvine2c80182005-01-15 22:15:51 +00001262 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001263 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001264 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001265
H. Peter Anvine2c80182005-01-15 22:15:51 +00001266 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001267 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001268
Keith Kaniosb7a89542007-04-12 02:40:54 +00001269 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001270 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1271 has_prefix(ins, PPS_ASIZE, P_A32))
1272 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001274
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001275 case4(0314):
1276 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001277
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001279 {
1280 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1281 if (pfx == P_O16)
1282 break;
1283 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001284 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001285 else
1286 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001288 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001289
H. Peter Anvine2c80182005-01-15 22:15:51 +00001290 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001291 {
1292 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1293 if (pfx == P_O32)
1294 break;
1295 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001296 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001297 else
1298 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001300 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001301
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 case 0322:
1303 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001304
Keith Kaniosb7a89542007-04-12 02:40:54 +00001305 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001306 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001307 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001308
Keith Kaniosb7a89542007-04-12 02:40:54 +00001309 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001310 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001311 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001312
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001313 case 0325:
1314 ins->rex |= REX_NH;
1315 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001316
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001317 case 0326:
1318 break;
1319
H. Peter Anvine2c80182005-01-15 22:15:51 +00001320 case 0330:
1321 codes++, length++;
1322 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001323
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001325 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001326
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001327 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001328 case 0333:
1329 length++;
1330 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001331
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001332 case 0334:
1333 ins->rex |= REX_L;
1334 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001335
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001336 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001337 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001338
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001339 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001340 if (!ins->prefixes[PPS_REP])
1341 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001342 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001343
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001344 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001345 if (!ins->prefixes[PPS_REP])
1346 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001347 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001348
H. Peter Anvine2c80182005-01-15 22:15:51 +00001349 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001350 if (!absolute_op(&ins->oprs[0]))
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001351 nasm_nonfatal("attempt to reserve non-constant"
1352 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001353 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001354 nasm_warn(WARN_OTHER, "forward reference in RESx "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001355 "can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001356 else
H. Peter Anvin5358b982018-12-18 18:06:26 -08001357 length += ins->oprs[0].offset * resb_bytes(ins->opcode);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001358 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001359
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001360 case 0341:
1361 if (!ins->prefixes[PPS_WAIT])
1362 ins->prefixes[PPS_WAIT] = P_WAIT;
1363 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001364
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001365 case 0360:
1366 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001367
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001368 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001369 length++;
1370 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001371
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001372 case 0364:
1373 case 0365:
1374 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001375
Keith Kanios48af1772007-08-17 07:37:52 +00001376 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001377 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001378 length++;
1379 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001380
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001381 case 0370:
1382 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001383 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001384
H. Peter Anvine2c80182005-01-15 22:15:51 +00001385 case 0373:
1386 length++;
1387 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001388
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001389 case 0374:
1390 eat = EA_XMMVSIB;
1391 break;
1392
1393 case 0375:
1394 eat = EA_YMMVSIB;
1395 break;
1396
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001397 case 0376:
1398 eat = EA_ZMMVSIB;
1399 break;
1400
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001401 case4(0100):
1402 case4(0110):
1403 case4(0120):
1404 case4(0130):
1405 case4(0200):
1406 case4(0204):
1407 case4(0210):
1408 case4(0214):
1409 case4(0220):
1410 case4(0224):
1411 case4(0230):
1412 case4(0234):
1413 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001414 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001415 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001416 opflags_t rflags;
1417 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001418 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001419
Keith Kaniosb7a89542007-04-12 02:40:54 +00001420 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001421
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001422 if (c <= 0177) {
1423 /* pick rfield from operand b (opx) */
1424 rflags = regflag(opx);
1425 rfield = nasm_regvals[opx->basereg];
1426 } else {
1427 rflags = 0;
1428 rfield = c & 7;
1429 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001430
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001431 /* EVEX.b1 : evex_brerop contains the operand position */
1432 op_er_sae = (ins->evex_brerop >= 0 ?
1433 &ins->oprs[ins->evex_brerop] : NULL);
1434
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001435 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1436 /* set EVEX.b */
1437 ins->evex_p[2] |= EVEX_P2B;
1438 if (op_er_sae->decoflags & ER) {
1439 /* set EVEX.RC (rounding control) */
1440 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1441 & EVEX_P2RC;
1442 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001443 } else {
1444 /* set EVEX.L'L (vector length) */
1445 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001446 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001447 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001448 /* set EVEX.b */
1449 ins->evex_p[2] |= EVEX_P2B;
1450 }
1451 }
1452
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001453 if (itemp_has(temp, IF_MIB)) {
1454 opy->eaflags |= EAF_MIB;
1455 /*
1456 * if a separate form of MIB (ICC style) is used,
1457 * the index reg info is merged into mem operand
1458 */
1459 if (mib_index != R_none) {
1460 opy->indexreg = mib_index;
1461 opy->scale = 1;
1462 opy->hintbase = mib_index;
1463 opy->hinttype = EAH_NOTBASE;
1464 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001465 }
1466
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001467 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001468 rfield, rflags, ins, &errmsg) != eat) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001469 nasm_nonfatal("%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001470 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001471 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001472 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001473 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001474 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001475 }
1476 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001477
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001478 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001479 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001480 ": instruction code \\%o (0x%02X) given", c, c);
1481 break;
1482 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001483 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001484
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001485 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001486
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001487 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001488 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001489 nasm_nonfatal("instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001490 return -1;
1491 }
1492 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001493 }
1494
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001495 switch (ins->prefixes[PPS_VEX]) {
1496 case P_EVEX:
1497 if (!(ins->rex & REX_EV))
1498 return -1;
1499 break;
1500 case P_VEX3:
1501 case P_VEX2:
1502 if (!(ins->rex & REX_V))
1503 return -1;
1504 break;
1505 default:
1506 break;
1507 }
1508
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001509 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001510 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001511
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001512 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001513 nasm_nonfatal("cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001514 return -1;
1515 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001516 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001517 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001518 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001519 ins->rex &= ~REX_W;
1520 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001521 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001522 ins->rex |= REX_W;
1523 bad32 &= ~REX_W;
1524 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001525 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001526 /* Follow REX_W */
1527 break;
1528 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001529
H. Peter Anvinfc561202011-07-07 16:58:22 -07001530 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001531 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001532 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001533 } else if (!(ins->rex & REX_EV) &&
1534 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001535 nasm_nonfatal("invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001536 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001537 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001538 if (ins->rex & REX_EV)
1539 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001540 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1541 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001542 length += 3;
1543 else
1544 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001545 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001546 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001547 nasm_nonfatal("cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001548 return -1;
1549 } else if (bits == 64) {
1550 length++;
1551 } else if ((ins->rex & REX_L) &&
1552 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001553 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001554 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001555 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001556 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001557 length++;
1558 } else {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001559 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001560 return -1;
1561 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001562 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001563
1564 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001565 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001566 /*!
H. Peter Anvin (Intel)be99ebd2018-12-13 22:12:37 -08001567 *!lock [on] LOCK prefix on unlockable instructions
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001568 *! warns about \c{LOCK} prefixes on unlockable instructions.
1569 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001570 nasm_warn(WARN_LOCK | ERR_PASS2 , "instruction is not lockable");
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001571 }
1572
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001573 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001574
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001575 /*
1576 * when BND prefix is set by DEFAULT directive,
1577 * BND prefix is added to every appropriate instruction line
1578 * unless it is overridden by NOBND prefix.
1579 */
1580 if (globalbnd &&
1581 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1582 ins->prefixes[PPS_REP] = P_BND;
1583
H. Peter Anvina77692b2016-09-20 14:04:33 -07001584 /*
1585 * Add length of legacy prefixes
1586 */
1587 length += emit_prefix(NULL, bits, ins);
1588
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001589 return length;
1590}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001591
H. Peter Anvina77692b2016-09-20 14:04:33 -07001592static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001593{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001594 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001595 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001596 !(ins->rex & (REX_V | REX_EV)) &&
1597 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001598 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1599 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001600 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001601 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001602 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001603}
1604
H. Peter Anvina77692b2016-09-20 14:04:33 -07001605static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1606{
1607 int bytes = 0;
1608 int j;
1609
1610 for (j = 0; j < MAXPREFIX; j++) {
1611 uint8_t c = 0;
1612 switch (ins->prefixes[j]) {
1613 case P_WAIT:
1614 c = 0x9B;
1615 break;
1616 case P_LOCK:
1617 c = 0xF0;
1618 break;
1619 case P_REPNE:
1620 case P_REPNZ:
1621 case P_XACQUIRE:
1622 case P_BND:
1623 c = 0xF2;
1624 break;
1625 case P_REPE:
1626 case P_REPZ:
1627 case P_REP:
1628 case P_XRELEASE:
1629 c = 0xF3;
1630 break;
1631 case R_CS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001632 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001633 nasm_warn(WARN_OTHER|ERR_PASS2, "cs segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001634 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001635 c = 0x2E;
1636 break;
1637 case R_DS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001638 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001639 nasm_warn(WARN_OTHER|ERR_PASS2, "ds segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001640 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001641 c = 0x3E;
1642 break;
1643 case R_ES:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001644 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001645 nasm_warn(WARN_OTHER|ERR_PASS2, "es segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001646 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001647 c = 0x26;
1648 break;
1649 case R_FS:
1650 c = 0x64;
1651 break;
1652 case R_GS:
1653 c = 0x65;
1654 break;
1655 case R_SS:
1656 if (bits == 64) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001657 nasm_warn(WARN_OTHER|ERR_PASS2, "ss segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001658 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001659 }
1660 c = 0x36;
1661 break;
1662 case R_SEGR6:
1663 case R_SEGR7:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001664 nasm_nonfatal("segr6 and segr7 cannot be used as prefixes");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001665 break;
1666 case P_A16:
1667 if (bits == 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001668 nasm_nonfatal("16-bit addressing is not supported "
1669 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001670 } else if (bits != 16)
1671 c = 0x67;
1672 break;
1673 case P_A32:
1674 if (bits != 32)
1675 c = 0x67;
1676 break;
1677 case P_A64:
1678 if (bits != 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001679 nasm_nonfatal("64-bit addressing is only supported "
1680 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001681 }
1682 break;
1683 case P_ASP:
1684 c = 0x67;
1685 break;
1686 case P_O16:
1687 if (bits != 16)
1688 c = 0x66;
1689 break;
1690 case P_O32:
1691 if (bits == 16)
1692 c = 0x66;
1693 break;
1694 case P_O64:
1695 /* REX.W */
1696 break;
1697 case P_OSP:
1698 c = 0x66;
1699 break;
1700 case P_EVEX:
1701 case P_VEX3:
1702 case P_VEX2:
1703 case P_NOBND:
1704 case P_none:
1705 break;
1706 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001707 nasm_panic("invalid instruction prefix");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001708 }
1709 if (c) {
1710 if (data)
1711 out_rawbyte(data, c);
1712 bytes++;
1713 }
1714 }
1715 return bytes;
1716}
1717
1718static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001719{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001720 uint8_t c;
1721 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001722 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001723 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001724 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001725 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001726 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001727 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001728 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001729 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001730 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001731
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001732 ins->rex_done = false;
1733
H. Peter Anvina77692b2016-09-20 14:04:33 -07001734 emit_prefix(data, bits, ins);
1735
H. Peter Anvin839eca22007-10-29 23:12:47 -07001736 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001737 c = *codes++;
1738 op1 = (c & 3) + ((opex & 1) << 2);
1739 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1740 opx = &ins->oprs[op1];
1741 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001742
H. Peter Anvina77692b2016-09-20 14:04:33 -07001743
H. Peter Anvin839eca22007-10-29 23:12:47 -07001744 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 case 01:
1746 case 02:
1747 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001748 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001749 emit_rex(data, ins);
1750 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001753
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001754 case 05:
1755 case 06:
1756 case 07:
1757 opex = c;
1758 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001759
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001760 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001761 emit_rex(data, ins);
1762 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001764
Jin Kyu Song164d6072013-10-15 19:10:13 -07001765 case4(014):
1766 break;
1767
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001768 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001769 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001771
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001772 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001773 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001775
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001776 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001777 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001779
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001780 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001781 if (opx->type & (BITS16 | BITS32))
1782 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 else
1784 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001785 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001787
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001788 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001789 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001791
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001792 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001793 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001794 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001796
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001797 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001798 if (opx->segment == data->segment) {
1799 int64_t delta = opx->offset - data->offset
1800 - (data->inslen - data->insoffs);
1801 if (delta > 127 || delta < -128)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001802 nasm_nonfatal("short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001803 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001804 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001806
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001807 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001808 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001809 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001810
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001811 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001812 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001814
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001815 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001816 if (opx->type & (BITS16 | BITS32 | BITS64))
1817 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 else
1819 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001820
1821 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001823
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001824 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001825 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001827
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001828 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001829 if (opx->segment == NO_SEG)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001830 nasm_nonfatal("value referenced by FAR is not relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001831 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001833
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001834 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001835 {
1836 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1837 const struct operand *opy;
1838
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001839 c = *codes++;
1840 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001841 opy = &ins->oprs[c & 7];
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001842 if (!absolute_op(opy))
1843 nasm_nonfatal("non-absolute expression not permitted "
1844 "as argument %d", c & 7);
1845 else if (opy->offset & ~mask)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001846 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001847 "is4 argument exceeds bounds");
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001848 c = opy->offset & mask;
1849 goto emit_is4;
1850 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001851
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001852 case 0173:
1853 c = *codes++;
1854 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001855 c &= 15;
1856 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001857
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001858 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001859 c = 0;
1860 emit_is4:
1861 r = nasm_regvals[opx->basereg];
1862 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001863 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001864
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001865 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001866 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001867 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001868 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001869 "signed dword immediate exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001870 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001871 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001872 break;
1873
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001874 case4(0240):
1875 case 0250:
1876 codes += 3;
1877 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1878 EVEX_P2Z | EVEX_P2AAA, 2);
1879 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1880 bytes[0] = 0x62;
1881 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001882 bytes[1] = ((((ins->rex & 7) << 5) |
1883 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001884 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001885 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1886 ((~ins->vexreg & 15) << 3) |
1887 (1 << 2) | (ins->vex_wlp & 3);
1888 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001889 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001890 break;
1891
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001892 case4(0260):
1893 case 0270:
1894 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001895 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1896 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001897 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1898 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1899 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001900 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001901 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001902 } else {
1903 bytes[0] = 0xc5;
1904 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001905 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001906 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001907 }
1908 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001909
H. Peter Anvine014f352012-02-25 22:35:19 -08001910 case 0271:
1911 case 0272:
1912 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001913 break;
1914
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001915 case4(0274):
1916 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001917 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001918 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001919
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001920 if (absolute_op(opx)) {
1921 if (ins->rex & REX_W)
1922 s = 64;
1923 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1924 s = 16;
1925 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1926 s = 32;
1927 else
1928 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001929
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001930 um = (uint64_t)2 << (s-1);
1931 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001932
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001933 if (uv > 127 && uv < (uint64_t)-128 &&
1934 (uv < um-128 || uv > um-1)) {
1935 /* If this wasn't explicitly byte-sized, warn as though we
1936 * had fallen through to the imm16/32/64 case.
1937 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001938 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001939 "%s value exceeds bounds",
1940 (opx->type & BITS8) ? "signed byte" :
1941 s == 16 ? "word" :
1942 s == 32 ? "dword" :
1943 "signed dword");
1944 }
1945
1946 /* Output as a raw byte to avoid byte overflow check */
1947 out_rawbyte(data, (uint8_t)uv);
1948 } else {
1949 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001950 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001951 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001952 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001953
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001954 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001956
H. Peter Anvine2c80182005-01-15 22:15:51 +00001957 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001958 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1959 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001961
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001963 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1964 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001965 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001966
H. Peter Anvine2c80182005-01-15 22:15:51 +00001967 case 0312:
1968 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001969
Keith Kaniosb7a89542007-04-12 02:40:54 +00001970 case 0313:
1971 ins->rex = 0;
1972 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001973
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001974 case4(0314):
1975 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001976
H. Peter Anvine2c80182005-01-15 22:15:51 +00001977 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001978 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001980
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001982 case 0323:
1983 break;
1984
Keith Kaniosb7a89542007-04-12 02:40:54 +00001985 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001986 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001987 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001988
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001989 case 0325:
1990 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001991
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001992 case 0326:
1993 break;
1994
H. Peter Anvine2c80182005-01-15 22:15:51 +00001995 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001996 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001997 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001998
H. Peter Anvine2c80182005-01-15 22:15:51 +00001999 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002000 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002001
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002002 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002003 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002004 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002005 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002006
Keith Kanios48af1772007-08-17 07:37:52 +00002007 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002008 if (ins->rex & REX_R)
2009 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00002010 ins->rex &= ~(REX_L|REX_R);
2011 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00002012
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002013 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002014 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07002015
H. Peter Anvin962e3052008-08-28 17:47:16 -07002016 case 0336:
2017 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002018 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07002019
H. Peter Anvine2c80182005-01-15 22:15:51 +00002020 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00002021 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvinc5136902018-06-15 18:20:17 -07002022 nasm_panic("non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07002023
H. Peter Anvin5358b982018-12-18 18:06:26 -08002024 out_reserve(data, ins->oprs[0].offset * resb_bytes(ins->opcode));
H. Peter Anvine2c80182005-01-15 22:15:51 +00002025 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002026
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002027 case 0341:
2028 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08002029
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002030 case 0360:
2031 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002032
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002033 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002034 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002035 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002036
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002037 case 0364:
2038 case 0365:
2039 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002040
Keith Kanios48af1772007-08-17 07:37:52 +00002041 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002042 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002043 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00002044 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002045
Jin Kyu Song03041092013-10-15 19:38:51 -07002046 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002047 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002048
H. Peter Anvine2c80182005-01-15 22:15:51 +00002049 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002050 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002051 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002052
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002053 case 0374:
2054 eat = EA_XMMVSIB;
2055 break;
2056
2057 case 0375:
2058 eat = EA_YMMVSIB;
2059 break;
2060
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002061 case 0376:
2062 eat = EA_ZMMVSIB;
2063 break;
2064
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002065 case4(0100):
2066 case4(0110):
2067 case4(0120):
2068 case4(0130):
2069 case4(0200):
2070 case4(0204):
2071 case4(0210):
2072 case4(0214):
2073 case4(0220):
2074 case4(0224):
2075 case4(0230):
2076 case4(0234):
2077 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002078 ea ea_data;
2079 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002080 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002081 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002082 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07002083
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002084 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002085 /* pick rfield from operand b (opx) */
2086 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07002087 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002088 } else {
2089 /* rfield is constant */
2090 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002091 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002092 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002093
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002094 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002095 rfield, rflags, ins, &errmsg) != eat)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002096 nasm_nonfatal("%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07002097
H. Peter Anvine2c80182005-01-15 22:15:51 +00002098 p = bytes;
2099 *p++ = ea_data.modrm;
2100 if (ea_data.sib_present)
2101 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07002102 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002103
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002104 /*
2105 * Make sure the address gets the right offset in case
2106 * the line breaks in the .lst file (BR 1197827)
2107 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002108
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002109 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002110 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07002111 if (ea_data.disp8) {
2112 out_rawbyte(data, ea_data.disp8);
2113 } else if (ea_data.rip) {
2114 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002115 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002116 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002117
H. Peter Anvina77692b2016-09-20 14:04:33 -07002118 if (overflow_general(opy->offset, asize) ||
2119 signed_bits(opy->offset, ins->addr_size) !=
2120 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08002121 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002122
H. Peter Anvina77692b2016-09-20 14:04:33 -07002123 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07002124 (asize > ea_data.bytes)
2125 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002126 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002127 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002128 }
2129 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07002130
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002131 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07002132 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002133 ": instruction code \\%o (0x%02X) given", c, c);
2134 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002135 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07002136 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002137}
2138
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002139static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002140{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002141 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002142 nasm_panic("invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002143 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002144}
2145
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00002146static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002147{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002148 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002149 nasm_panic("invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002150 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002151}
2152
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002153static int op_rexflags(const operand * o, int mask)
2154{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002155 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002156 int val;
2157
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002158 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002159 nasm_panic("invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002160
H. Peter Anvina4835d42008-05-20 14:21:29 -07002161 flags = nasm_reg_flags[o->basereg];
2162 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002163
2164 return rexflags(val, flags, mask);
2165}
2166
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002167static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002168{
2169 int rex = 0;
2170
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002171 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002172 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002173 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002174 rex |= REX_W;
2175 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2176 rex |= REX_H;
2177 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2178 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002179
2180 return rex & mask;
2181}
2182
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002183static int evexflags(int val, decoflags_t deco,
2184 int mask, uint8_t byte)
2185{
2186 int evex = 0;
2187
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002188 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002189 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002190 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002191 evex |= (EVEX_P0RP | EVEX_P0X);
2192 break;
2193 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002194 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002195 evex |= EVEX_P2VP;
2196 if (deco & Z)
2197 evex |= EVEX_P2Z;
2198 if (deco & OPMASK_MASK)
2199 evex |= deco & EVEX_P2AAA;
2200 break;
2201 }
2202 return evex & mask;
2203}
2204
2205static int op_evexflags(const operand * o, int mask, uint8_t byte)
2206{
2207 int val;
2208
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002209 val = nasm_regvals[o->basereg];
2210
2211 return evexflags(val, o->decoflags, mask, byte);
2212}
2213
H. Peter Anvin23595f52009-07-25 17:44:25 -07002214static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002215 insn *instruction,
2216 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002217{
2218 const struct itemplate *temp;
2219 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002220 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002221 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002222 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002223 int i;
2224
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002225 /* broadcasting uses a different data element size */
2226 for (i = 0; i < instruction->operands; i++)
2227 if (i == broadcast)
2228 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2229 else
2230 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002231
2232 merr = MERR_INVALOP;
2233
2234 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002235 temp->opcode != I_none; temp++) {
2236 m = matches(temp, instruction, bits);
2237 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002238 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002239 m = MOK_GOOD;
2240 else
2241 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002242 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002243 /*
2244 * Missing operand size and a candidate for fuzzy matching...
2245 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002246 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002247 if (i == broadcast)
2248 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2249 else
2250 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002251 opsizemissing = true;
2252 }
2253 if (m > merr)
2254 merr = m;
2255 if (merr == MOK_GOOD)
2256 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002257 }
2258
2259 /* No match, but see if we can get a fuzzy operand size match... */
2260 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002261 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002262
2263 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002264 /*
2265 * We ignore extrinsic operand sizes on registers, so we should
2266 * never try to fuzzy-match on them. This also resolves the case
2267 * when we have e.g. "xmmrm128" in two different positions.
2268 */
2269 if (is_class(REGISTER, instruction->oprs[i].type))
2270 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002271
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002272 /* This tests if xsizeflags[i] has more than one bit set */
2273 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2274 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002275
Jin Kyu Song7903c072013-10-30 03:00:12 -07002276 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002277 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002278 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2279 BITS32 : BITS64);
2280 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002281 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002282 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002283 }
2284
2285 /* Try matching again... */
2286 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002287 temp->opcode != I_none; temp++) {
2288 m = matches(temp, instruction, bits);
2289 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002290 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002291 m = MOK_GOOD;
2292 else
2293 m = MERR_INVALOP;
2294 }
2295 if (m > merr)
2296 merr = m;
2297 if (merr == MOK_GOOD)
2298 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002299 }
2300
H. Peter Anvina81655b2009-07-25 18:15:28 -07002301done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002302 *tempp = temp;
2303 return merr;
2304}
2305
Mark Charneydcaef4b2014-10-09 13:45:17 -04002306static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2307{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002308 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002309 uint8_t brcast_num;
2310
Mark Charneydcaef4b2014-10-09 13:45:17 -04002311 if (brsize > BITS64)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002312 nasm_fatal("size of broadcasting element is greater than 64 bits");
Mark Charneydcaef4b2014-10-09 13:45:17 -04002313
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002314 /*
2315 * The shift term is to take care of the extra BITS80 inserted
2316 * between BITS64 and BITS128.
2317 */
2318 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2319 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002320
2321 return brcast_num;
2322}
2323
H. Peter Anvin65289e82009-07-25 17:25:11 -07002324static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002325 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002326{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002327 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002328 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002329 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002330
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002331 /*
2332 * Check the opcode
2333 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002334 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002335 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002336
2337 /*
2338 * Count the operands
2339 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002341 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002342
2343 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002344 * Is it legal?
2345 */
Chang S. Baea5786342018-08-15 23:22:21 +03002346 if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002347 return MERR_INVALOP;
2348
2349 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002350 * {evex} available?
2351 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002352 switch (instruction->prefixes[PPS_VEX]) {
2353 case P_EVEX:
2354 if (!itemp_has(itemp, IF_EVEX))
2355 return MERR_ENCMISMATCH;
2356 break;
2357 case P_VEX3:
2358 case P_VEX2:
2359 if (!itemp_has(itemp, IF_VEX))
2360 return MERR_ENCMISMATCH;
2361 break;
2362 default:
2363 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002364 }
2365
2366 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002367 * Check that no spurious colons or TOs are present
2368 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 for (i = 0; i < itemp->operands; i++)
2370 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002371 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002372
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002373 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002374 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002375 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002376 switch (itemp_smask(itemp)) {
2377 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002378 asize = BITS8;
2379 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002380 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002381 asize = BITS16;
2382 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002383 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002384 asize = BITS32;
2385 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002386 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002387 asize = BITS64;
2388 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002389 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002390 asize = BITS128;
2391 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002392 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002393 asize = BITS256;
2394 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002395 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002396 asize = BITS512;
2397 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002398 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002399 switch (bits) {
2400 case 16:
2401 asize = BITS16;
2402 break;
2403 case 32:
2404 asize = BITS32;
2405 break;
2406 case 64:
2407 asize = BITS64;
2408 break;
2409 default:
2410 asize = 0;
2411 break;
2412 }
2413 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002414 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002415 asize = 0;
2416 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002417 }
2418
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002419 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002420 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002421 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002422 memset(size, 0, sizeof size);
2423 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002424 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002425 /* S- flags apply to all operands */
2426 for (i = 0; i < MAX_OPERANDS; i++)
2427 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002428 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002429
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002430 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002431 * Check that the operand flags all match up,
2432 * it's a bit tricky so lets be verbose:
2433 *
2434 * 1) Find out the size of operand. If instruction
2435 * doesn't have one specified -- we're trying to
2436 * guess it either from template (IF_S* flag) or
2437 * from code bits.
2438 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002439 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002440 * template has an operand size specified AND this size differ
2441 * from which instruction has (perhaps we got it from code bits)
2442 * we are:
2443 * a) Check that only size of instruction and operand is differ
2444 * other characteristics do match
2445 * b) Perhaps it's a register specified in instruction so
2446 * for such a case we just mark that operand as "size
2447 * missing" and this will turn on fuzzy operand size
2448 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002449 */
2450 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002451 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002452 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002453 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002454 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002455 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002456 opflags_t template_opsize, insn_opsize;
2457
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002458 if (!(type & SIZE_MASK))
2459 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002460
Jin Kyu Song7903c072013-10-30 03:00:12 -07002461 insn_opsize = type & SIZE_MASK;
2462 if (!is_broadcast) {
2463 template_opsize = itemp->opd[i] & SIZE_MASK;
2464 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002465 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2466
2467 if (~ideco & BRDCAST_MASK)
2468 return MERR_BRNOTHERE;
2469
Jin Kyu Song7903c072013-10-30 03:00:12 -07002470 /*
2471 * when broadcasting, the element size depends on
2472 * the instruction type. decorator flag should match.
2473 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002474 if (deco_brsize) {
2475 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002476 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002477 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002478 } else {
2479 template_opsize = 0;
2480 }
2481 }
2482
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002483 if (~ideco & deco & OPMASK_MASK)
2484 return MERR_MASKNOTHERE;
2485
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002486 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2487 return MERR_DECONOTHERE;
2488
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002489 if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002490 return MERR_INVALOP;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002491
2492 if (~itemp->opd[i] & type & REGSET_MASK)
2493 return (itemp->opd[i] & REGSET_MASK)
2494 ? MERR_REGSETSIZE : MERR_REGSET;
2495
2496 if (template_opsize) {
Jin Kyu Song7903c072013-10-30 03:00:12 -07002497 if (template_opsize != insn_opsize) {
2498 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002499 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002500 } else if (!is_class(REGISTER, type)) {
2501 /*
2502 * Note: we don't honor extrinsic operand sizes for registers,
2503 * so "missing operand size" for a register should be
2504 * considered a wildcard match rather than an error.
2505 */
2506 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002507 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002508 } else if (is_broadcast &&
2509 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002510 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002511 /*
2512 * broadcasting opsize matches but the number of repeated memory
2513 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002514 * if 64b double precision float is broadcasted to ymm (256b),
2515 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002516 */
2517 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002518 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002519 }
2520 }
2521
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002522 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002523 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002524
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002525 /*
2526 * Check operand sizes
2527 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002528 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2529 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002531 asize = itemp->opd[i] & SIZE_MASK;
2532 if (asize) {
2533 for (i = 0; i < oprs; i++)
2534 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002535 break;
2536 }
2537 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002538 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002539 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002540 }
2541
Keith Kaniosb7a89542007-04-12 02:40:54 +00002542 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002543 if (!(itemp->opd[i] & SIZE_MASK) &&
2544 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002545 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002546 }
2547
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002548 /*
2549 * Check template is okay at the set cpu level
2550 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002551 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002552 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002553
Keith Kaniosb7a89542007-04-12 02:40:54 +00002554 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002555 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002556 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002557 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002558 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002559
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002560 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002561 * If we have a HLE prefix, look for the NOHLE flag
2562 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002563 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002564 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2565 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2566 return MERR_BADHLE;
2567
2568 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002569 * Check if special handling needed for Jumps
2570 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002571 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002572 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002573
Jin Kyu Song03041092013-10-15 19:38:51 -07002574 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002575 * Check if BND prefix is allowed.
2576 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002577 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002578 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002579 (has_prefix(instruction, PPS_REP, P_BND) ||
2580 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002581 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002582 else if (itemp_has(itemp, IF_BND) &&
2583 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2584 has_prefix(instruction, PPS_REP, P_REPNZ)))
2585 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002586
H. Peter Anvin60926242009-07-26 16:25:38 -07002587 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002588}
2589
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002590/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002591 * Check if ModR/M.mod should/can be 01.
2592 * - EAF_BYTEOFFS is set
2593 * - offset can fit in a byte when EVEX is not used
2594 * - offset can be compressed when EVEX is used
2595 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002596#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2597 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2598 is_disp8n(input, ins, &output->disp8) : \
2599 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2600 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002601
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002602static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002603 int rfield, opflags_t rflags, insn *ins,
2604 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002605{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002606 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002607 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002608 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002609
H. Peter Anvin8f622462017-04-02 19:02:29 -07002610 *errmsg = "invalid effective address"; /* Default error message */
2611
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002612 output->type = EA_SCALAR;
2613 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002614 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002615
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002616 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002617 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002618 /* EVEX.R' flag for the REG operand */
2619 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002620
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002621 if (is_class(REGISTER, input->type)) {
2622 /*
2623 * It's a direct register.
2624 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002625 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002626 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002627
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002628 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002629 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002630
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002631 /* broadcasting is not available with a direct register operand. */
2632 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002633 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002634 goto err;
2635 }
2636
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002637 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002638 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002639 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002640 output->bytes = 0; /* no offset necessary either */
2641 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2642 } else {
2643 /*
2644 * It's a memory reference.
2645 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002646
2647 /* Embedded rounding or SAE is not available with a mem ref operand. */
2648 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002649 *errmsg = "embedded rounding is available only with "
2650 "register-register operations";
2651 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002652 }
2653
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002654 if (input->basereg == -1 &&
2655 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002656 /*
2657 * It's a pure offset.
2658 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002659 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002660 if (input->segment == NO_SEG ||
2661 (input->opflags & OPFLAG_RELATIVE)) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08002662 nasm_warn(WARN_OTHER|ERR_PASS2, "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002663 input->type &= ~IP_REL;
2664 input->type |= MEMORY;
2665 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002666 }
2667
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002668 if (bits == 64 &&
2669 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002670 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002671 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002672 }
2673
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002674 if (eaflags & EAF_BYTEOFFS ||
2675 (eaflags & EAF_WORDOFFS &&
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002676 input->disp_size != (addrbits != 16 ? 32 : 16)))
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002677 nasm_warn(WARN_OTHER, "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002678
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002679 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002680 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002681 output->sib = GEN_SIB(0, 4, 5);
2682 output->bytes = 4;
2683 output->modrm = GEN_MODRM(0, rfield, 4);
2684 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002685 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002686 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002687 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002688 output->modrm = GEN_MODRM(0, rfield,
2689 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002690 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002691 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002692 } else {
2693 /*
2694 * It's an indirection.
2695 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002696 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002697 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002698 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002699 int t, it, bt; /* register numbers */
2700 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002701
H. Peter Anvine2c80182005-01-15 22:15:51 +00002702 if (s == 0)
2703 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002704
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002705 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002706 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002707 ix = nasm_reg_flags[i];
2708 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002709 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002710 ix = 0;
2711 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002712
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002713 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002714 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002715 bx = nasm_reg_flags[b];
2716 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002717 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002718 bx = 0;
2719 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002720
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002721 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002722 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002723 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002724 int32_t o = input->offset;
2725 int mod, scale, index, base;
2726
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002727 /*
2728 * For a vector SIB, one has to be a vector and the other,
2729 * if present, a GPR. The vector must be the index operand.
2730 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002731 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002732 if (s == 0)
2733 s = 1;
2734 else if (s != 1)
2735 goto err;
2736
2737 t = bt, bt = it, it = t;
2738 x = bx, bx = ix, ix = x;
2739 }
2740
2741 if (bt != -1) {
2742 if (REG_GPR & ~bx)
2743 goto err;
2744 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2745 sok &= bx;
2746 else
2747 goto err;
2748 }
2749
2750 /*
2751 * While we're here, ensure the user didn't specify
2752 * WORD or QWORD
2753 */
2754 if (input->disp_size == 16 || input->disp_size == 64)
2755 goto err;
2756
2757 if (addrbits == 16 ||
2758 (addrbits == 32 && !(sok & BITS32)) ||
2759 (addrbits == 64 && !(sok & BITS64)))
2760 goto err;
2761
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002762 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2763 : ((ix & YMMREG & ~REG_EA)
2764 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002765
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002766 output->rex |= rexflags(it, ix, REX_X);
2767 output->rex |= rexflags(bt, bx, REX_B);
2768 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002769
2770 index = it & 7; /* it is known to be != -1 */
2771
2772 switch (s) {
2773 case 1:
2774 scale = 0;
2775 break;
2776 case 2:
2777 scale = 1;
2778 break;
2779 case 4:
2780 scale = 2;
2781 break;
2782 case 8:
2783 scale = 3;
2784 break;
2785 default: /* then what the smeg is it? */
2786 goto err; /* panic */
2787 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002788
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002789 if (bt == -1) {
2790 base = 5;
2791 mod = 0;
2792 } else {
2793 base = (bt & 7);
2794 if (base != REG_NUM_EBP && o == 0 &&
2795 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002796 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002797 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002798 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002799 mod = 1;
2800 else
2801 mod = 2;
2802 }
2803
2804 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002805 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2806 output->modrm = GEN_MODRM(mod, rfield, 4);
2807 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002808 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002809 /*
2810 * it must be a 32/64-bit memory reference. Firstly we have
2811 * to check that all registers involved are type E/Rxx.
2812 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002813 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002814 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002815
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002816 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002817 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2818 sok &= ix;
2819 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002820 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002821 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002822
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002823 if (bt != -1) {
2824 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002825 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002826 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002827 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002828 sok &= bx;
2829 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002830
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002831 /*
2832 * While we're here, ensure the user didn't specify
2833 * WORD or QWORD
2834 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002835 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002836 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002837
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002838 if (addrbits == 16 ||
2839 (addrbits == 32 && !(sok & BITS32)) ||
2840 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002841 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002842
Keith Kaniosb7a89542007-04-12 02:40:54 +00002843 /* now reorganize base/index */
2844 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002845 ((hb == b && ht == EAH_NOTBASE) ||
2846 (hb == i && ht == EAH_MAKEBASE))) {
2847 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002848 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002849 x = bx, bx = ix, ix = x;
2850 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002851
Jin Kyu Song164d6072013-10-15 19:10:13 -07002852 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002853 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002854 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002855 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002856 if (eaflags & EAF_MIB) {
2857 /* only for mib operands */
2858 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2859 /*
2860 * make a single reg index [reg*1].
2861 * gas uses this form for an explicit index register.
2862 */
2863 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2864 }
2865 if ((ht == EAH_SUMMED) && bt == -1) {
2866 /* separate once summed index into [base, index] */
2867 bt = it, bx = ix, s--;
2868 }
2869 } else {
2870 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002871 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002872 s == 3 || s == 5 || s == 9) && bt == -1) {
2873 /* convert 3*EAX to EAX+2*EAX */
2874 bt = it, bx = ix, s--;
2875 }
2876 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002877 (eaflags & EAF_TIMESTWO) &&
2878 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002879 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002880 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002881 * to sib format with 0x0 displacement - [EAX*1+0].
2882 */
2883 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2884 }
2885 }
Keith Kanios48af1772007-08-17 07:37:52 +00002886 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002887 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002888 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002889 x = ix, ix = bx, bx = x;
2890 }
2891 if (it == REG_NUM_ESP ||
2892 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002893 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002894
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002895 output->rex |= rexflags(it, ix, REX_X);
2896 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002897
Keith Kanios48af1772007-08-17 07:37:52 +00002898 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002899 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002901
Keith Kaniosb7a89542007-04-12 02:40:54 +00002902 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002903 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002904 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002905 } else {
2906 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002907 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002908 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002909 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002910 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002911 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002912 mod = 1;
2913 else
2914 mod = 2;
2915 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002916
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002917 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002918 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2919 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002920 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002921 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002922 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002923
Keith Kaniosb7a89542007-04-12 02:40:54 +00002924 if (it == -1)
2925 index = 4, s = 1;
2926 else
2927 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002928
H. Peter Anvine2c80182005-01-15 22:15:51 +00002929 switch (s) {
2930 case 1:
2931 scale = 0;
2932 break;
2933 case 2:
2934 scale = 1;
2935 break;
2936 case 4:
2937 scale = 2;
2938 break;
2939 case 8:
2940 scale = 3;
2941 break;
2942 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002943 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002944 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002945
Keith Kaniosb7a89542007-04-12 02:40:54 +00002946 if (bt == -1) {
2947 base = 5;
2948 mod = 0;
2949 } else {
2950 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002951 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002952 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002953 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002954 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002955 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002956 mod = 1;
2957 else
2958 mod = 2;
2959 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002960
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002961 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002962 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2963 output->modrm = GEN_MODRM(mod, rfield, 4);
2964 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002965 }
2966 } else { /* it's 16-bit */
2967 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002968 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002969
Keith Kaniosb7a89542007-04-12 02:40:54 +00002970 /* check for 64-bit long mode */
2971 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002972 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002973
H. Peter Anvine2c80182005-01-15 22:15:51 +00002974 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002975 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2976 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002977 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002978
Keith Kaniosb7a89542007-04-12 02:40:54 +00002979 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002980 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002981 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002982
H. Peter Anvine2c80182005-01-15 22:15:51 +00002983 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002984 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002985 if (b == -1 && i != -1) {
2986 int tmp = b;
2987 b = i;
2988 i = tmp;
2989 } /* swap */
2990 if ((b == R_SI || b == R_DI) && i != -1) {
2991 int tmp = b;
2992 b = i;
2993 i = tmp;
2994 }
2995 /* have BX/BP as base, SI/DI index */
2996 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002997 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002998 if (i != -1 && b != -1 &&
2999 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003000 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003001 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003002 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003003
H. Peter Anvine2c80182005-01-15 22:15:51 +00003004 rm = -1;
3005 if (i != -1)
3006 switch (i * 256 + b) {
3007 case R_SI * 256 + R_BX:
3008 rm = 0;
3009 break;
3010 case R_DI * 256 + R_BX:
3011 rm = 1;
3012 break;
3013 case R_SI * 256 + R_BP:
3014 rm = 2;
3015 break;
3016 case R_DI * 256 + R_BP:
3017 rm = 3;
3018 break;
3019 } else
3020 switch (b) {
3021 case R_SI:
3022 rm = 4;
3023 break;
3024 case R_DI:
3025 rm = 5;
3026 break;
3027 case R_BP:
3028 rm = 6;
3029 break;
3030 case R_BX:
3031 rm = 7;
3032 break;
3033 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003034 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003035 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003036
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003037 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003038 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003039 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003040 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00003041 mod = 1;
3042 else
3043 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003044
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003045 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003046 output->bytes = mod; /* bytes of offset needed */
3047 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003048 }
3049 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003050 }
H. Peter Anvin70653092007-10-19 14:42:29 -07003051
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003052 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003053 return output->type;
3054
3055err:
3056 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003057}
3058
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003059static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003060{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003061 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003062 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003063
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003064 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003065
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003066 switch (ins->prefixes[PPS_ASIZE]) {
3067 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003068 valid &= 16;
3069 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003070 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003071 valid &= 32;
3072 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003073 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003074 valid &= 64;
3075 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003076 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003077 valid &= (addrbits == 32) ? 16 : 32;
3078 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003079 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003080 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003081 }
3082
3083 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003084 if (is_class(MEMORY, ins->oprs[j].type)) {
3085 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07003086
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003087 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003088 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003089 i = 0;
3090 else
3091 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003092
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003093 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003094 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003095 b = 0;
3096 else
3097 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003098
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003099 if (ins->oprs[j].scale == 0)
3100 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003101
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003102 if (!i && !b) {
3103 int ds = ins->oprs[j].disp_size;
3104 if ((addrbits != 64 && ds > 8) ||
3105 (addrbits == 64 && ds == 16))
3106 valid &= ds;
3107 } else {
3108 if (!(REG16 & ~b))
3109 valid &= 16;
3110 if (!(REG32 & ~b))
3111 valid &= 32;
3112 if (!(REG64 & ~b))
3113 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07003114
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003115 if (!(REG16 & ~i))
3116 valid &= 16;
3117 if (!(REG32 & ~i))
3118 valid &= 32;
3119 if (!(REG64 & ~i))
3120 valid &= 64;
3121 }
3122 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003123 }
3124
3125 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003126 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003127 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003128 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04003129 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003130 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003131 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003132 /* Impossible... */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03003133 nasm_nonfatal("impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003134 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003135 }
3136
3137 defdisp = ins->addr_size == 16 ? 16 : 32;
3138
3139 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003140 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
3141 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
3142 /*
3143 * mem_offs sizes must match the address size; if not,
3144 * strip the MEM_OFFS bit and match only EA instructions
3145 */
3146 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
3147 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003148 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003149}