blob: fc216a8f04d001bec00e6f8e2842a8f9df41630d [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvina7ecf262018-02-06 14:43:07 -08003 * Copyright 1996-2018 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 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400213 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 Anvina77692b2016-09-20 14:04:33 -0700332static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000333{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000334 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700335 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700336 union {
337 uint8_t b[8];
338 uint64_t q;
339 } xdata;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700340 size_t asize, amax;
341 uint64_t zeropad = 0;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800342 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800343 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000344
H. Peter Anvina77692b2016-09-20 14:04:33 -0700345 if (!data->size)
346 return; /* Nothing to do */
347
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700348 /*
349 * Convert addresses to RAWDATA if possible
350 * XXX: not all backends want this for global symbols!!!!
351 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700352 switch (data->type) {
353 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800354 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800355 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800356 goto address;
357
358 case OUT_RELADDR:
359 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800360 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800361 goto address;
362
363 address:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700364 nasm_assert(data->size <= 8);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700365 asize = data->size;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700366 amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
Chang S. Bae427d8e32018-05-02 08:07:52 -0700367 if ((ofmt->flags & OFMT_KEEP_ADDR) == 0 && data->tsegment == fixseg &&
368 data->twrt == NO_SEG) {
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800369 warn_overflow_out(addrval, asize, data->sign);
Martin Storsjö869087d2017-05-22 13:54:20 +0300370 xdata.q = cpu_to_le64(addrval);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700371 data->data = xdata.b;
372 data->type = OUT_RAWDATA;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700373 asize = amax = 0; /* No longer an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700374 }
375 break;
376
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700377 case OUT_SEGMENT:
378 nasm_assert(data->size <= 8);
379 asize = data->size;
380 amax = 2;
381 break;
382
H. Peter Anvina77692b2016-09-20 14:04:33 -0700383 default:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700384 asize = amax = 0; /* Not an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700385 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000386 }
387
Frank Kotlerabebb082003-09-06 04:45:37 +0000388 /*
389 * this call to src_get determines when we call the
390 * debug-format-specific "linenum" function
391 * it updates lineno and lnfname to the current values
392 * returning 0 if "same as last time", -2 if lnfname
393 * changed, and the amount by which lineno changed,
394 * if it did. thus, these variables must be static
395 */
396
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400397 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700398 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000399
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700400 if (asize > amax) {
401 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300402 nasm_nonfatal("%u-bit signed relocation unsupported by output format %s",
403 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800404 } else {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800405 /*!
406 *!zext-reloc [on] relocation zero-extended to match output format
407 *! warns that a relocation has been zero-extended due
408 *! to limitations in the output format.
409 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800410 nasm_warn(WARN_ZEXT_RELOC,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700411 "%u-bit %s relocation zero-extended from %u bits",
412 (unsigned int)(asize << 3),
413 data->type == OUT_SEGMENT ? "segment" : "unsigned",
414 (unsigned int)(amax << 3));
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800415 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700416 zeropad = data->size - amax;
417 data->size = amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800418 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700419 lfmt->output(data);
H. Peter Anvined859f72018-06-15 00:03:53 -0700420
421 if (likely(data->segment != NO_SEG)) {
422 ofmt->output(data);
423 } else {
424 /* Outputting to ABSOLUTE section - only reserve is permitted */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300425 if (data->type != OUT_RESERVE)
426 nasm_nonfatal("attempt to assemble code in [ABSOLUTE] space");
H. Peter Anvined859f72018-06-15 00:03:53 -0700427 /* No need to push to the backend */
428 }
429
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700430 data->offset += data->size;
431 data->insoffs += data->size;
432
433 if (zeropad) {
434 data->type = OUT_ZERODATA;
435 data->size = zeropad;
436 lfmt->output(data);
437 ofmt->output(data);
438 data->offset += zeropad;
439 data->insoffs += zeropad;
440 data->size += zeropad; /* Restore original size value */
441 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000442}
443
H. Peter Anvina77692b2016-09-20 14:04:33 -0700444static inline void out_rawdata(struct out_data *data, const void *rawdata,
445 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400446{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700447 data->type = OUT_RAWDATA;
448 data->data = rawdata;
449 data->size = size;
450 out(data);
451}
452
453static void out_rawbyte(struct out_data *data, uint8_t byte)
454{
455 data->type = OUT_RAWDATA;
456 data->data = &byte;
457 data->size = 1;
458 out(data);
459}
460
461static inline void out_reserve(struct out_data *data, uint64_t size)
462{
463 data->type = OUT_RESERVE;
464 data->size = size;
465 out(data);
466}
467
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700468static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700469{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700470 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300471 nasm_nonfatal("segment references cannot be relative");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700472
473 data->type = OUT_SEGMENT;
474 data->sign = OUT_UNSIGNED;
475 data->size = 2;
476 data->toffset = opx->offset;
477 data->tsegment = ofmt->segbase(opx->segment | 1);
478 data->twrt = opx->wrt;
479 out(data);
480}
481
482static void out_imm(struct out_data *data, const struct operand *opx,
483 int size, enum out_sign sign)
484{
485 if (opx->segment != NO_SEG && (opx->segment & 1)) {
486 /*
487 * This is actually a segment reference, but eval() has
488 * already called ofmt->segbase() for us. Sigh.
489 */
490 if (size < 2)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300491 nasm_nonfatal("segment reference must be 16 bits");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700492
493 data->type = OUT_SEGMENT;
494 } else {
495 data->type = (opx->opflags & OPFLAG_RELATIVE)
496 ? OUT_RELADDR : OUT_ADDRESS;
497 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700498 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700499 data->toffset = opx->offset;
500 data->tsegment = opx->segment;
501 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800502 /*
503 * XXX: improve this if at some point in the future we can
504 * distinguish the subtrahend in expressions like [foo - bar]
505 * where bar is a symbol in the current segment. However, at the
506 * current point, if OPFLAG_RELATIVE is set that subtraction has
507 * already occurred.
508 */
509 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700510 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700511 out(data);
512}
513
H. Peter Anvin164d2462017-02-20 02:39:56 -0800514static void out_reladdr(struct out_data *data, const struct operand *opx,
515 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700516{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800517 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300518 nasm_nonfatal("invalid use of self-relative expression");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800519
H. Peter Anvina77692b2016-09-20 14:04:33 -0700520 data->type = OUT_RELADDR;
521 data->sign = OUT_SIGNED;
522 data->size = size;
523 data->toffset = opx->offset;
524 data->tsegment = opx->segment;
525 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800526 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700527 out(data);
528}
529
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700530static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800531 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000532{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800533 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800534 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000535 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800536 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000537
H. Peter Anvin755f5212012-02-25 11:41:34 -0800538 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700539 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300540 if (!optimizing.level || (optimizing.flag & OPTIM_DISABLE_JMP_MATCH))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400541 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300542 if (optimizing.level < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400543 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700544
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800545 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100546
Victor van den Elzen154e5922009-02-25 17:32:00 +0100547 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100548 /* Be optimistic in pass 1 */
549 return true;
550
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700552 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000553
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700554 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800555 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
556
557 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
558 /* jmp short (opcode eb) cannot be used with bnd prefix. */
559 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -0800560 /*!
561 *!bnd [on] invalid BND prefixes
562 *! warns about ineffective use of the \c{BND} prefix when the
563 *! \c{JMP} instruction is converted to the \c{SHORT} form.
564 *! This should be extremely rare since the short \c{JMP} only
565 *! is applicable to jumps inside the same module, but if
566 *! it is legitimate, it may be necessary to use
567 *! \c{BND JMP DWORD}...
568 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -0800569 nasm_warn(WARN_BND | ERR_PASS2 ,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300570 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800571 }
572
573 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000575
H. Peter Anvin04445362016-09-21 15:56:19 -0700576/* This is totally just a wild guess what is reasonable... */
577#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
578
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800579int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000580{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700581 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000582 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700583 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300584 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000585
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700586 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700587 data.offset = start;
588 data.segment = segment;
589 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700590 data.bits = bits;
591
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700592 wsize = db_bytes(instruction->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300593 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000595
H. Peter Anvineba20a72002-04-30 20:53:55 +0000596 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700598
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700599 list_for_each(e, instruction->eops) {
600 if (e->type == EOT_DB_NUMBER) {
601 if (wsize > 8) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300602 nasm_nonfatal("integer supplied to a DT,DO,DY or DZ");
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700603 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700604 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700605 data.inslen = data.size = wsize;
606 data.toffset = e->offset;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700607 data.twrt = e->wrt;
608 data.relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700609 if (e->segment != NO_SEG && (e->segment & 1)) {
610 data.tsegment = e->segment;
611 data.type = OUT_SEGMENT;
612 data.sign = OUT_UNSIGNED;
613 } else {
614 data.tsegment = e->segment;
615 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
616 data.sign = OUT_WRAP;
617 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700618 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700620 } else if (e->type == EOT_DB_STRING ||
621 e->type == EOT_DB_STRING_FREE) {
622 int align = e->stringlen % wsize;
623 if (align)
624 align = wsize - align;
625
626 data.insoffs = 0;
627 data.inslen = e->stringlen + align;
628
629 out_rawdata(&data, e->stringval, e->stringlen);
630 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700631 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700633 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700634 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700636 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700637 off_t base = 0;
638 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700639 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700640 char *buf = NULL;
641 size_t blk = 0; /* Buffered I/O block size */
642 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000643
H. Peter Anvin94ead272017-09-27 15:22:23 -0700644 if (!t)
645 goto done;
646
H. Peter Anvind81a2352016-09-21 14:03:18 -0700647 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400648 if (!fp) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300649 nasm_nonfatal("`incbin': unable to open file `%s'",
650 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700651 goto done;
652 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000653
H. Peter Anvind81a2352016-09-21 14:03:18 -0700654 len = nasm_file_size(fp);
655
656 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300657 nasm_nonfatal("`incbin': unable to get length of file `%s'",
658 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700659 goto close_done;
660 }
661
H. Peter Anvina77692b2016-09-20 14:04:33 -0700662 if (instruction->eops->next) {
663 base = instruction->eops->next->offset;
664 if (base >= len) {
665 len = 0;
666 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000667 len -= base;
668 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700669 len > (off_t)instruction->eops->next->next->offset)
670 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000671 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000672 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700673
H. Peter Anvina77692b2016-09-20 14:04:33 -0700674 lfmt->set_offset(data.offset);
675 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000676
H. Peter Anvind81a2352016-09-21 14:03:18 -0700677 if (!len)
678 goto end_incbin;
679
680 /* Try to map file data */
681 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700682 if (!map) {
683 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
684 buf = nasm_malloc(blk);
685 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700686
687 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700688 /*
689 * Consider these irrelevant for INCBIN, since it is fully
690 * possible that these might be (way) bigger than an int
691 * can hold; there is, however, no reason to widen these
692 * types just for INCBIN. data.inslen == 0 signals to the
693 * backend that these fields are meaningless, if at all
694 * needed.
695 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700696 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700697 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700698
H. Peter Anvind81a2352016-09-21 14:03:18 -0700699 if (map) {
700 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700701 } else if ((off_t)m == len) {
702 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700703 } else {
704 off_t l = len;
705
706 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300707 nasm_nonfatal("`incbin': unable to seek on file `%s'",
708 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700709 goto end_incbin;
710 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700711 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700712 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700713 if (!m || feof(fp)) {
714 /*
715 * This shouldn't happen unless the file
716 * actually changes while we are reading
717 * it.
718 */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300719 nasm_nonfatal("`incbin': unexpected EOF while"
720 " reading file `%s'", fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700721 goto end_incbin;
722 }
723 out_rawdata(&data, buf, m);
724 l -= m;
725 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700726 }
727 }
728 end_incbin:
729 lfmt->downlevel(LIST_INCBIN);
730 if (instruction->times > 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700731 lfmt->uplevel(LIST_TIMES);
732 lfmt->downlevel(LIST_TIMES);
733 }
734 if (ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300735 nasm_nonfatal("`incbin': error while"
736 " reading file `%s'", fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700737 }
738 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700739 if (buf)
740 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700741 if (map)
742 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700743 fclose(fp);
744 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700745 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700746 ;
747 } else {
748 /* "Real" instruction */
749
750 /* Check to see if we need an address-size prefix */
751 add_asp(instruction, bits);
752
753 m = find_match(&temp, instruction, data.segment, data.offset, bits);
754
755 if (m == MOK_GOOD) {
756 /* Matches! */
757 int64_t insn_size = calcsize(data.segment, data.offset,
758 bits, instruction, temp);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700759 nasm_assert(insn_size >= 0);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700760
761 data.itemp = temp;
762 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700763 data.insoffs = 0;
764 data.inslen = insn_size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700765
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700766 gencode(&data, instruction);
767 nasm_assert(data.insoffs == insn_size);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700768 } else {
769 /* No match */
770 switch (m) {
771 case MERR_OPSIZEMISSING:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300772 nasm_nonfatal("operation size not specified");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700773 break;
774 case MERR_OPSIZEMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300775 nasm_nonfatal("mismatch in operand sizes");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700776 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700777 case MERR_BRNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300778 nasm_nonfatal("broadcast not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700779 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700780 case MERR_BRNUMMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300781 nasm_nonfatal("mismatch in the number of broadcasting elements");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700782 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700783 case MERR_MASKNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300784 nasm_nonfatal("mask not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700785 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700786 case MERR_DECONOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300787 nasm_nonfatal("unsupported mode decorator for instruction");
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700788 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700789 case MERR_BADCPU:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300790 nasm_nonfatal("no instruction for this cpu level");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700791 break;
792 case MERR_BADMODE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300793 nasm_nonfatal("instruction not supported in %d-bit mode", bits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700794 break;
795 case MERR_ENCMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300796 nasm_nonfatal("specific encoding scheme not available");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700797 break;
798 case MERR_BADBND:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300799 nasm_nonfatal("bnd prefix is not allowed");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700800 break;
801 case MERR_BADREPNE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300802 nasm_nonfatal("%s prefix is not allowed",
803 (has_prefix(instruction, PPS_REP, P_REPNE) ?
804 "repne" : "repnz"));
H. Peter Anvina77692b2016-09-20 14:04:33 -0700805 break;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700806 case MERR_REGSETSIZE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300807 nasm_nonfatal("invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700808 break;
809 case MERR_REGSET:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300810 nasm_nonfatal("register set not valid for operand");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700811 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700812 default:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300813 nasm_nonfatal("invalid combination of opcode and operands");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700814 break;
815 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700816
817 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400818 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000819 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700820 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000821}
822
H. Peter Anvin29651542018-12-18 19:14:40 -0800823static void debug_set_db_type(insn *instruction)
824{
825 /* Is this really correct? .operands doesn't mean much for Dx */
826 int32_t typeinfo = TYS_ELEMENTS(instruction->operands);
827
828 switch (instruction->opcode) {
829 case I_DB:
830 typeinfo |= TY_BYTE;
831 break;
832 case I_DW:
833 typeinfo |= TY_WORD;
834 break;
835 case I_DD:
836 if (instruction->eops_float)
837 typeinfo |= TY_FLOAT;
838 else
839 typeinfo |= TY_DWORD;
840 break;
841 case I_DQ:
842 /* What about double? */
843 typeinfo |= TY_QWORD;
844 break;
845 case I_DT:
846 /* What about long double? */
847 typeinfo |= TY_TBYTE;
848 break;
849 case I_DO:
850 typeinfo |= TY_OWORD;
851 break;
852 case I_DY:
853 typeinfo |= TY_YWORD;
854 break;
855 case I_DZ:
856 typeinfo |= TY_ZWORD;
857 break;
858 default:
859 panic();
860 }
861
862 dfmt->debug_typevalue(typeinfo);
863}
864
865static void debug_set_type(insn *instruction)
866{
867 int32_t typeinfo;
868
869 if (opcode_is_resb(instruction->opcode)) {
870 typeinfo = TYS_ELEMENTS(instruction->oprs[0].offset);
871
872 switch (instruction->opcode) {
873 case I_RESB:
874 typeinfo |= TY_BYTE;
875 break;
876 case I_RESW:
877 typeinfo |= TY_WORD;
878 break;
879 case I_RESD:
880 typeinfo |= TY_DWORD;
881 break;
882 case I_RESQ:
883 typeinfo |= TY_QWORD;
884 break;
885 case I_REST:
886 typeinfo |= TY_TBYTE;
887 break;
888 case I_RESO:
889 typeinfo |= TY_OWORD;
890 break;
891 case I_RESY:
892 typeinfo |= TY_YWORD;
893 break;
894 case I_RESZ:
895 typeinfo |= TY_ZWORD;
896 break;
897 default:
898 panic();
899 }
900 } else {
901 typeinfo = TY_LABEL;
902 }
903
904 dfmt->debug_typevalue(typeinfo);
905}
906
907
908/* Proecess an EQU directive */
909static void define_equ(insn * instruction)
910{
911 if (!instruction->label) {
912 nasm_nonfatal("EQU not preceded by label");
913 } else if (instruction->operands == 1 &&
914 (instruction->oprs[0].type & IMMEDIATE) &&
915 instruction->oprs[0].wrt == NO_SEG) {
916 define_label(instruction->label,
917 instruction->oprs[0].segment,
918 instruction->oprs[0].offset, false);
919 } else if (instruction->operands == 2
920 && (instruction->oprs[0].type & IMMEDIATE)
921 && (instruction->oprs[0].type & COLON)
922 && instruction->oprs[0].segment == NO_SEG
923 && instruction->oprs[0].wrt == NO_SEG
924 && (instruction->oprs[1].type & IMMEDIATE)
925 && instruction->oprs[1].segment == NO_SEG
926 && instruction->oprs[1].wrt == NO_SEG) {
927 define_label(instruction->label,
928 instruction->oprs[0].offset | SEG_ABS,
929 instruction->oprs[1].offset, false);
930 } else {
931 nasm_nonfatal("bad syntax for EQU");
932 }
933}
934
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800935int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000936{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000937 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700938 enum match_result m;
H. Peter Anvin29651542018-12-18 19:14:40 -0800939 int64_t isize = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000940
H. Peter Anvin29651542018-12-18 19:14:40 -0800941 if (instruction->opcode == I_none) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000942 return 0;
H. Peter Anvin29651542018-12-18 19:14:40 -0800943 } else if (instruction->opcode == I_EQU) {
944 define_equ(instruction);
945 return 0;
946 } else if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947 extop *e;
H. Peter Anvin29651542018-12-18 19:14:40 -0800948 int32_t osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000949
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700950 wsize = db_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700951 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000952
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400953 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000954 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000955
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400957 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400959 warn_overflow_const(e->offset, wsize);
960 } else if (e->type == EOT_DB_STRING ||
961 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000963
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 align = (-osize) % wsize;
965 if (align < 0)
966 align += wsize;
967 isize += osize + align;
968 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000969
H. Peter Anvin29651542018-12-18 19:14:40 -0800970 debug_set_db_type(instruction);
971 return isize;
972 } else if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400973 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700974 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000975
H. Peter Anvind81a2352016-09-21 14:03:18 -0700976 len = nasm_file_size_by_path(fname);
977 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300978 nasm_nonfatal("`incbin': unable to get length of file `%s'",
979 fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700980 return 0;
981 }
982
983 if (instruction->eops->next) {
984 if (len <= (off_t)instruction->eops->next->offset) {
985 len = 0;
986 } else {
987 len -= instruction->eops->next->offset;
988 if (instruction->eops->next->next &&
989 len > (off_t)instruction->eops->next->next->offset) {
990 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 }
992 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700994
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700995 len *= instruction->times;
996 instruction->times = 1; /* Tell the upper layer to not iterate */
997
H. Peter Anvind81a2352016-09-21 14:03:18 -0700998 return len;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700999 } else {
H. Peter Anvin29651542018-12-18 19:14:40 -08001000 /* Normal instruction, or RESx */
1001
1002 /* Check to see if we need an address-size prefix */
1003 add_asp(instruction, bits);
1004
1005 m = find_match(&temp, instruction, segment, offset, bits);
1006 if (m != MOK_GOOD)
1007 return -1; /* No match */
1008
1009 isize = calcsize(segment, offset, bits, instruction, temp);
1010 debug_set_type(instruction);
1011
1012 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001013 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001014}
1015
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001016static void bad_hle_warn(const insn * ins, uint8_t hleok)
1017{
1018 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001019 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001020 static const enum whatwarn warn[2][4] =
1021 {
1022 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
1023 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
1024 };
1025 unsigned int n;
1026
1027 n = (unsigned int)rep_pfx - P_XACQUIRE;
1028 if (n > 1)
1029 return; /* Not XACQUIRE/XRELEASE */
1030
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001031 ww = warn[n][hleok];
1032 if (!is_class(MEMORY, ins->oprs[0].type))
1033 ww = w_inval; /* HLE requires operand 0 to be memory */
1034
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001035 /*!
1036 *!hle [on] invalid HLE prefixes
1037 *! warns about invalid use of the HLE \c{XACQUIRE} or \c{XRELEASE}
1038 *! prefixes.
1039 */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001040 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001041 case w_none:
1042 break;
1043
1044 case w_lock:
1045 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001046 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001047 "%s with this instruction requires lock",
1048 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001049 }
1050 break;
1051
1052 case w_inval:
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001053 nasm_warn(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001054 "%s invalid with this instruction",
1055 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001056 break;
1057 }
1058}
1059
H. Peter Anvin507ae032008-10-09 15:37:10 -07001060/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +04001061#define case3(x) case (x): case (x)+1: case (x)+2
1062#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -07001063
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001064static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001065 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001066{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001067 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001068 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001069 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001070 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001071 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001072 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001073 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001074 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001075 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001076 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -07001077 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -07001078 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001079
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001080 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001081 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001082 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -07001083
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001084 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001085 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001086
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 (void)segment; /* Don't warn that this parameter is unused */
1088 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001089
H. Peter Anvin839eca22007-10-29 23:12:47 -07001090 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001091 c = *codes++;
1092 op1 = (c & 3) + ((opex & 1) << 2);
1093 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1094 opx = &ins->oprs[op1];
1095 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001096
H. Peter Anvin839eca22007-10-29 23:12:47 -07001097 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001098 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 codes += c, length += c;
1100 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001101
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001102 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001103 opex = c;
1104 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001105
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001106 case4(010):
1107 ins->rex |=
1108 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 codes++, length++;
1110 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001111
Jin Kyu Song164d6072013-10-15 19:10:13 -07001112 case4(014):
1113 /* this is an index reg of MIB operand */
1114 mib_index = opx->basereg;
1115 break;
1116
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001117 case4(020):
1118 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 length++;
1120 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001121
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001122 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 length += 2;
1124 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001125
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001126 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001127 if (opx->type & (BITS16 | BITS32 | BITS64))
1128 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 else
1130 length += (bits == 16) ? 2 : 4;
1131 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001132
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001133 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 length += 4;
1135 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001136
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001137 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001138 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001140
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001141 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 length++;
1143 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001144
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001145 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001146 length += 8; /* MOV reg64/imm */
1147 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001148
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001149 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 length += 2;
1151 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001152
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001153 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001154 if (opx->type & (BITS16 | BITS32 | BITS64))
1155 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 else
1157 length += (bits == 16) ? 2 : 4;
1158 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001159
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001160 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 length += 4;
1162 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001163
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001164 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001165 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001167
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001168 case 0172:
1169 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001170 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001171 length++;
1172 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001173
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001174 case4(0174):
1175 length++;
1176 break;
1177
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001178 case4(0240):
1179 ins->rex |= REX_EV;
1180 ins->vexreg = regval(opx);
1181 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1182 ins->vex_cm = *codes++;
1183 ins->vex_wlp = *codes++;
1184 ins->evex_tuple = (*codes++ - 0300);
1185 break;
1186
1187 case 0250:
1188 ins->rex |= REX_EV;
1189 ins->vexreg = 0;
1190 ins->vex_cm = *codes++;
1191 ins->vex_wlp = *codes++;
1192 ins->evex_tuple = (*codes++ - 0300);
1193 break;
1194
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001195 case4(0254):
1196 length += 4;
1197 break;
1198
1199 case4(0260):
1200 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001201 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001202 ins->vex_cm = *codes++;
1203 ins->vex_wlp = *codes++;
1204 break;
1205
1206 case 0270:
1207 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001208 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001209 ins->vex_cm = *codes++;
1210 ins->vex_wlp = *codes++;
1211 break;
1212
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001213 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001214 hleok = c & 3;
1215 break;
1216
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001217 case4(0274):
1218 length++;
1219 break;
1220
1221 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001223
H. Peter Anvine2c80182005-01-15 22:15:51 +00001224 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001225 if (bits == 64)
1226 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001227 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001229
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001231 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001232 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001233
H. Peter Anvine2c80182005-01-15 22:15:51 +00001234 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001235 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001236
Keith Kaniosb7a89542007-04-12 02:40:54 +00001237 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001238 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1239 has_prefix(ins, PPS_ASIZE, P_A32))
1240 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001241 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001242
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001243 case4(0314):
1244 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001245
H. Peter Anvine2c80182005-01-15 22:15:51 +00001246 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001247 {
1248 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1249 if (pfx == P_O16)
1250 break;
1251 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001252 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001253 else
1254 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001255 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001256 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001257
H. Peter Anvine2c80182005-01-15 22:15:51 +00001258 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001259 {
1260 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1261 if (pfx == P_O32)
1262 break;
1263 if (pfx != P_none)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001264 nasm_warn(WARN_OTHER|ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001265 else
1266 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001267 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001268 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001269
H. Peter Anvine2c80182005-01-15 22:15:51 +00001270 case 0322:
1271 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001272
Keith Kaniosb7a89542007-04-12 02:40:54 +00001273 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001274 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001275 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001276
Keith Kaniosb7a89542007-04-12 02:40:54 +00001277 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001278 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001279 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001280
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001281 case 0325:
1282 ins->rex |= REX_NH;
1283 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001284
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001285 case 0326:
1286 break;
1287
H. Peter Anvine2c80182005-01-15 22:15:51 +00001288 case 0330:
1289 codes++, length++;
1290 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001291
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001293 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001294
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001295 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001296 case 0333:
1297 length++;
1298 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001299
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001300 case 0334:
1301 ins->rex |= REX_L;
1302 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001303
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001304 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001305 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001306
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001307 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001308 if (!ins->prefixes[PPS_REP])
1309 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001310 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001311
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001312 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001313 if (!ins->prefixes[PPS_REP])
1314 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001315 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001316
H. Peter Anvine2c80182005-01-15 22:15:51 +00001317 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001318 if (!absolute_op(&ins->oprs[0]))
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001319 nasm_nonfatal("attempt to reserve non-constant"
1320 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001321 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08001322 nasm_warn(WARN_OTHER, "forward reference in RESx "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001323 "can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001324 else
H. Peter Anvin5358b982018-12-18 18:06:26 -08001325 length += ins->oprs[0].offset * resb_bytes(ins->opcode);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001326 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001327
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001328 case 0341:
1329 if (!ins->prefixes[PPS_WAIT])
1330 ins->prefixes[PPS_WAIT] = P_WAIT;
1331 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001332
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001333 case 0360:
1334 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001335
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001336 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001337 length++;
1338 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001339
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001340 case 0364:
1341 case 0365:
1342 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001343
Keith Kanios48af1772007-08-17 07:37:52 +00001344 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001345 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001346 length++;
1347 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001348
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001349 case 0370:
1350 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001351 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001352
H. Peter Anvine2c80182005-01-15 22:15:51 +00001353 case 0373:
1354 length++;
1355 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001356
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001357 case 0374:
1358 eat = EA_XMMVSIB;
1359 break;
1360
1361 case 0375:
1362 eat = EA_YMMVSIB;
1363 break;
1364
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001365 case 0376:
1366 eat = EA_ZMMVSIB;
1367 break;
1368
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001369 case4(0100):
1370 case4(0110):
1371 case4(0120):
1372 case4(0130):
1373 case4(0200):
1374 case4(0204):
1375 case4(0210):
1376 case4(0214):
1377 case4(0220):
1378 case4(0224):
1379 case4(0230):
1380 case4(0234):
1381 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001382 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001383 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001384 opflags_t rflags;
1385 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001386 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001387
Keith Kaniosb7a89542007-04-12 02:40:54 +00001388 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001389
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001390 if (c <= 0177) {
1391 /* pick rfield from operand b (opx) */
1392 rflags = regflag(opx);
1393 rfield = nasm_regvals[opx->basereg];
1394 } else {
1395 rflags = 0;
1396 rfield = c & 7;
1397 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001398
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001399 /* EVEX.b1 : evex_brerop contains the operand position */
1400 op_er_sae = (ins->evex_brerop >= 0 ?
1401 &ins->oprs[ins->evex_brerop] : NULL);
1402
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001403 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1404 /* set EVEX.b */
1405 ins->evex_p[2] |= EVEX_P2B;
1406 if (op_er_sae->decoflags & ER) {
1407 /* set EVEX.RC (rounding control) */
1408 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1409 & EVEX_P2RC;
1410 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001411 } else {
1412 /* set EVEX.L'L (vector length) */
1413 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001414 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001415 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001416 /* set EVEX.b */
1417 ins->evex_p[2] |= EVEX_P2B;
1418 }
1419 }
1420
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001421 if (itemp_has(temp, IF_MIB)) {
1422 opy->eaflags |= EAF_MIB;
1423 /*
1424 * if a separate form of MIB (ICC style) is used,
1425 * the index reg info is merged into mem operand
1426 */
1427 if (mib_index != R_none) {
1428 opy->indexreg = mib_index;
1429 opy->scale = 1;
1430 opy->hintbase = mib_index;
1431 opy->hinttype = EAH_NOTBASE;
1432 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001433 }
1434
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001435 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001436 rfield, rflags, ins, &errmsg) != eat) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001437 nasm_nonfatal("%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001438 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001439 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001440 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001441 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001442 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001443 }
1444 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001445
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001446 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001447 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001448 ": instruction code \\%o (0x%02X) given", c, c);
1449 break;
1450 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001451 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001452
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001453 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001454
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001455 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001456 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001457 nasm_nonfatal("instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001458 return -1;
1459 }
1460 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001461 }
1462
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001463 switch (ins->prefixes[PPS_VEX]) {
1464 case P_EVEX:
1465 if (!(ins->rex & REX_EV))
1466 return -1;
1467 break;
1468 case P_VEX3:
1469 case P_VEX2:
1470 if (!(ins->rex & REX_V))
1471 return -1;
1472 break;
1473 default:
1474 break;
1475 }
1476
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001477 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001478 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001479
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001480 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001481 nasm_nonfatal("cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001482 return -1;
1483 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001484 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001485 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001486 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001487 ins->rex &= ~REX_W;
1488 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001489 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001490 ins->rex |= REX_W;
1491 bad32 &= ~REX_W;
1492 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001493 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001494 /* Follow REX_W */
1495 break;
1496 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001497
H. Peter Anvinfc561202011-07-07 16:58:22 -07001498 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001499 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001500 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001501 } else if (!(ins->rex & REX_EV) &&
1502 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001503 nasm_nonfatal("invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001504 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001505 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001506 if (ins->rex & REX_EV)
1507 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001508 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1509 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001510 length += 3;
1511 else
1512 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001513 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001514 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001515 nasm_nonfatal("cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001516 return -1;
1517 } else if (bits == 64) {
1518 length++;
1519 } else if ((ins->rex & REX_L) &&
1520 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001521 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001522 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001523 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001524 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001525 length++;
1526 } else {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001527 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001528 return -1;
1529 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001530 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001531
1532 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001533 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001534 /*!
H. Peter Anvin (Intel)be99ebd2018-12-13 22:12:37 -08001535 *!lock [on] LOCK prefix on unlockable instructions
H. Peter Anvin (Intel)723ab482018-12-13 21:53:31 -08001536 *! warns about \c{LOCK} prefixes on unlockable instructions.
1537 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001538 nasm_warn(WARN_LOCK | ERR_PASS2 , "instruction is not lockable");
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001539 }
1540
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001541 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001542
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001543 /*
1544 * when BND prefix is set by DEFAULT directive,
1545 * BND prefix is added to every appropriate instruction line
1546 * unless it is overridden by NOBND prefix.
1547 */
1548 if (globalbnd &&
1549 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1550 ins->prefixes[PPS_REP] = P_BND;
1551
H. Peter Anvina77692b2016-09-20 14:04:33 -07001552 /*
1553 * Add length of legacy prefixes
1554 */
1555 length += emit_prefix(NULL, bits, ins);
1556
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001557 return length;
1558}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001559
H. Peter Anvina77692b2016-09-20 14:04:33 -07001560static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001561{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001562 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001563 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001564 !(ins->rex & (REX_V | REX_EV)) &&
1565 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001566 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1567 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001568 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001569 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001570 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001571}
1572
H. Peter Anvina77692b2016-09-20 14:04:33 -07001573static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1574{
1575 int bytes = 0;
1576 int j;
1577
1578 for (j = 0; j < MAXPREFIX; j++) {
1579 uint8_t c = 0;
1580 switch (ins->prefixes[j]) {
1581 case P_WAIT:
1582 c = 0x9B;
1583 break;
1584 case P_LOCK:
1585 c = 0xF0;
1586 break;
1587 case P_REPNE:
1588 case P_REPNZ:
1589 case P_XACQUIRE:
1590 case P_BND:
1591 c = 0xF2;
1592 break;
1593 case P_REPE:
1594 case P_REPZ:
1595 case P_REP:
1596 case P_XRELEASE:
1597 c = 0xF3;
1598 break;
1599 case R_CS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001600 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001601 nasm_warn(WARN_OTHER|ERR_PASS2, "cs segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001602 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001603 c = 0x2E;
1604 break;
1605 case R_DS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001606 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001607 nasm_warn(WARN_OTHER|ERR_PASS2, "ds segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001608 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001609 c = 0x3E;
1610 break;
1611 case R_ES:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001612 if (bits == 64)
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001613 nasm_warn(WARN_OTHER|ERR_PASS2, "es segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001614 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001615 c = 0x26;
1616 break;
1617 case R_FS:
1618 c = 0x64;
1619 break;
1620 case R_GS:
1621 c = 0x65;
1622 break;
1623 case R_SS:
1624 if (bits == 64) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08001625 nasm_warn(WARN_OTHER|ERR_PASS2, "ss segment base generated, "
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001626 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001627 }
1628 c = 0x36;
1629 break;
1630 case R_SEGR6:
1631 case R_SEGR7:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001632 nasm_nonfatal("segr6 and segr7 cannot be used as prefixes");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001633 break;
1634 case P_A16:
1635 if (bits == 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001636 nasm_nonfatal("16-bit addressing is not supported "
1637 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001638 } else if (bits != 16)
1639 c = 0x67;
1640 break;
1641 case P_A32:
1642 if (bits != 32)
1643 c = 0x67;
1644 break;
1645 case P_A64:
1646 if (bits != 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001647 nasm_nonfatal("64-bit addressing is only supported "
1648 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001649 }
1650 break;
1651 case P_ASP:
1652 c = 0x67;
1653 break;
1654 case P_O16:
1655 if (bits != 16)
1656 c = 0x66;
1657 break;
1658 case P_O32:
1659 if (bits == 16)
1660 c = 0x66;
1661 break;
1662 case P_O64:
1663 /* REX.W */
1664 break;
1665 case P_OSP:
1666 c = 0x66;
1667 break;
1668 case P_EVEX:
1669 case P_VEX3:
1670 case P_VEX2:
1671 case P_NOBND:
1672 case P_none:
1673 break;
1674 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001675 nasm_panic("invalid instruction prefix");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001676 }
1677 if (c) {
1678 if (data)
1679 out_rawbyte(data, c);
1680 bytes++;
1681 }
1682 }
1683 return bytes;
1684}
1685
1686static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001687{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001688 uint8_t c;
1689 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001690 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001691 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001692 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001693 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001694 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001695 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001696 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001697 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001698 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001699
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001700 ins->rex_done = false;
1701
H. Peter Anvina77692b2016-09-20 14:04:33 -07001702 emit_prefix(data, bits, ins);
1703
H. Peter Anvin839eca22007-10-29 23:12:47 -07001704 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001705 c = *codes++;
1706 op1 = (c & 3) + ((opex & 1) << 2);
1707 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1708 opx = &ins->oprs[op1];
1709 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001710
H. Peter Anvina77692b2016-09-20 14:04:33 -07001711
H. Peter Anvin839eca22007-10-29 23:12:47 -07001712 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001713 case 01:
1714 case 02:
1715 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001716 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001717 emit_rex(data, ins);
1718 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001719 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001720 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001721
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001722 case 05:
1723 case 06:
1724 case 07:
1725 opex = c;
1726 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001727
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001728 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001729 emit_rex(data, ins);
1730 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001731 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001732
Jin Kyu Song164d6072013-10-15 19:10:13 -07001733 case4(014):
1734 break;
1735
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001736 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001737 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001738 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001739
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001740 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001741 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001743
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001744 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001745 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001747
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001748 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001749 if (opx->type & (BITS16 | BITS32))
1750 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 else
1752 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001753 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001755
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001756 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001757 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001758 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001759
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001760 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001761 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001762 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001764
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001765 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001766 if (opx->segment == data->segment) {
1767 int64_t delta = opx->offset - data->offset
1768 - (data->inslen - data->insoffs);
1769 if (delta > 127 || delta < -128)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001770 nasm_nonfatal("short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001771 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001772 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001774
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001775 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001776 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001777 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001778
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001779 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001780 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001782
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001783 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001784 if (opx->type & (BITS16 | BITS32 | BITS64))
1785 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 else
1787 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001788
1789 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001791
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001792 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001793 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001795
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001796 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001797 if (opx->segment == NO_SEG)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001798 nasm_nonfatal("value referenced by FAR is not relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001799 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001801
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001802 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001803 {
1804 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1805 const struct operand *opy;
1806
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001807 c = *codes++;
1808 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001809 opy = &ins->oprs[c & 7];
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001810 if (!absolute_op(opy))
1811 nasm_nonfatal("non-absolute expression not permitted "
1812 "as argument %d", c & 7);
1813 else if (opy->offset & ~mask)
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001814 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001815 "is4 argument exceeds bounds");
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001816 c = opy->offset & mask;
1817 goto emit_is4;
1818 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001819
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001820 case 0173:
1821 c = *codes++;
1822 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001823 c &= 15;
1824 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001825
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001826 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001827 c = 0;
1828 emit_is4:
1829 r = nasm_regvals[opx->basereg];
1830 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001831 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001832
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001833 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001834 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001835 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001836 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001837 "signed dword immediate exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001838 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001839 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001840 break;
1841
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001842 case4(0240):
1843 case 0250:
1844 codes += 3;
1845 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1846 EVEX_P2Z | EVEX_P2AAA, 2);
1847 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1848 bytes[0] = 0x62;
1849 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001850 bytes[1] = ((((ins->rex & 7) << 5) |
1851 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001852 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001853 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1854 ((~ins->vexreg & 15) << 3) |
1855 (1 << 2) | (ins->vex_wlp & 3);
1856 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001857 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001858 break;
1859
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001860 case4(0260):
1861 case 0270:
1862 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001863 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1864 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001865 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1866 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1867 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001868 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001869 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001870 } else {
1871 bytes[0] = 0xc5;
1872 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001873 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001874 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001875 }
1876 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001877
H. Peter Anvine014f352012-02-25 22:35:19 -08001878 case 0271:
1879 case 0272:
1880 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001881 break;
1882
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001883 case4(0274):
1884 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001885 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001886 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001887
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001888 if (absolute_op(opx)) {
1889 if (ins->rex & REX_W)
1890 s = 64;
1891 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1892 s = 16;
1893 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1894 s = 32;
1895 else
1896 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001897
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001898 um = (uint64_t)2 << (s-1);
1899 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001900
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001901 if (uv > 127 && uv < (uint64_t)-128 &&
1902 (uv < um-128 || uv > um-1)) {
1903 /* If this wasn't explicitly byte-sized, warn as though we
1904 * had fallen through to the imm16/32/64 case.
1905 */
H. Peter Anvin (Intel)80c4f232018-12-14 13:33:24 -08001906 nasm_warn(ERR_PASS2 | WARN_NUMBER_OVERFLOW,
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001907 "%s value exceeds bounds",
1908 (opx->type & BITS8) ? "signed byte" :
1909 s == 16 ? "word" :
1910 s == 32 ? "dword" :
1911 "signed dword");
1912 }
1913
1914 /* Output as a raw byte to avoid byte overflow check */
1915 out_rawbyte(data, (uint8_t)uv);
1916 } else {
1917 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001918 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001919 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001920 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001921
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001922 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001924
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001926 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1927 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001929
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001931 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1932 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001934
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 case 0312:
1936 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001937
Keith Kaniosb7a89542007-04-12 02:40:54 +00001938 case 0313:
1939 ins->rex = 0;
1940 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001941
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001942 case4(0314):
1943 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001944
H. Peter Anvine2c80182005-01-15 22:15:51 +00001945 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001947 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001948
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001950 case 0323:
1951 break;
1952
Keith Kaniosb7a89542007-04-12 02:40:54 +00001953 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001954 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001956
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001957 case 0325:
1958 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001959
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001960 case 0326:
1961 break;
1962
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001964 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
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 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001969
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001970 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001972 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001973 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001974
Keith Kanios48af1772007-08-17 07:37:52 +00001975 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001976 if (ins->rex & REX_R)
1977 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001978 ins->rex &= ~(REX_L|REX_R);
1979 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001980
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001981 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001982 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001983
H. Peter Anvin962e3052008-08-28 17:47:16 -07001984 case 0336:
1985 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001986 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001987
H. Peter Anvine2c80182005-01-15 22:15:51 +00001988 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001989 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001990 nasm_panic("non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001991
H. Peter Anvin5358b982018-12-18 18:06:26 -08001992 out_reserve(data, ins->oprs[0].offset * resb_bytes(ins->opcode));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001993 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001994
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001995 case 0341:
1996 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001997
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001998 case 0360:
1999 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002000
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002001 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002002 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002003 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07002004
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002005 case 0364:
2006 case 0365:
2007 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002008
Keith Kanios48af1772007-08-17 07:37:52 +00002009 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002010 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002011 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00002012 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00002013
Jin Kyu Song03041092013-10-15 19:38:51 -07002014 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00002015 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002016
H. Peter Anvine2c80182005-01-15 22:15:51 +00002017 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07002018 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002019 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00002020
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002021 case 0374:
2022 eat = EA_XMMVSIB;
2023 break;
2024
2025 case 0375:
2026 eat = EA_YMMVSIB;
2027 break;
2028
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002029 case 0376:
2030 eat = EA_ZMMVSIB;
2031 break;
2032
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002033 case4(0100):
2034 case4(0110):
2035 case4(0120):
2036 case4(0130):
2037 case4(0200):
2038 case4(0204):
2039 case4(0210):
2040 case4(0214):
2041 case4(0220):
2042 case4(0224):
2043 case4(0230):
2044 case4(0234):
2045 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002046 ea ea_data;
2047 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002048 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002049 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002050 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07002051
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002052 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002053 /* pick rfield from operand b (opx) */
2054 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07002055 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002056 } else {
2057 /* rfield is constant */
2058 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002059 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002060 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002061
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002062 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002063 rfield, rflags, ins, &errmsg) != eat)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002064 nasm_nonfatal("%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07002065
H. Peter Anvine2c80182005-01-15 22:15:51 +00002066 p = bytes;
2067 *p++ = ea_data.modrm;
2068 if (ea_data.sib_present)
2069 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07002070 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002071
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002072 /*
2073 * Make sure the address gets the right offset in case
2074 * the line breaks in the .lst file (BR 1197827)
2075 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02002076
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002077 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002078 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07002079 if (ea_data.disp8) {
2080 out_rawbyte(data, ea_data.disp8);
2081 } else if (ea_data.rip) {
2082 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002083 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002084 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08002085
H. Peter Anvina77692b2016-09-20 14:04:33 -07002086 if (overflow_general(opy->offset, asize) ||
2087 signed_bits(opy->offset, ins->addr_size) !=
2088 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08002089 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002090
H. Peter Anvina77692b2016-09-20 14:04:33 -07002091 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07002092 (asize > ea_data.bytes)
2093 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002094 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00002095 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002096 }
2097 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07002098
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002099 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07002100 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002101 ": instruction code \\%o (0x%02X) given", c, c);
2102 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002103 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07002104 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002105}
2106
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002107static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002108{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002109 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002110 nasm_panic("invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002111 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002112}
2113
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00002114static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002115{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002116 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002117 nasm_panic("invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002118 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002119}
2120
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002121static int op_rexflags(const operand * o, int mask)
2122{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002123 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002124 int val;
2125
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002126 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002127 nasm_panic("invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002128
H. Peter Anvina4835d42008-05-20 14:21:29 -07002129 flags = nasm_reg_flags[o->basereg];
2130 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002131
2132 return rexflags(val, flags, mask);
2133}
2134
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002135static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002136{
2137 int rex = 0;
2138
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002139 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002140 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002141 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002142 rex |= REX_W;
2143 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2144 rex |= REX_H;
2145 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2146 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002147
2148 return rex & mask;
2149}
2150
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002151static int evexflags(int val, decoflags_t deco,
2152 int mask, uint8_t byte)
2153{
2154 int evex = 0;
2155
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002156 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002157 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002158 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002159 evex |= (EVEX_P0RP | EVEX_P0X);
2160 break;
2161 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002162 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002163 evex |= EVEX_P2VP;
2164 if (deco & Z)
2165 evex |= EVEX_P2Z;
2166 if (deco & OPMASK_MASK)
2167 evex |= deco & EVEX_P2AAA;
2168 break;
2169 }
2170 return evex & mask;
2171}
2172
2173static int op_evexflags(const operand * o, int mask, uint8_t byte)
2174{
2175 int val;
2176
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002177 val = nasm_regvals[o->basereg];
2178
2179 return evexflags(val, o->decoflags, mask, byte);
2180}
2181
H. Peter Anvin23595f52009-07-25 17:44:25 -07002182static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002183 insn *instruction,
2184 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002185{
2186 const struct itemplate *temp;
2187 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002188 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002189 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002190 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002191 int i;
2192
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002193 /* broadcasting uses a different data element size */
2194 for (i = 0; i < instruction->operands; i++)
2195 if (i == broadcast)
2196 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2197 else
2198 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002199
2200 merr = MERR_INVALOP;
2201
2202 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002203 temp->opcode != I_none; temp++) {
2204 m = matches(temp, instruction, bits);
2205 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002206 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002207 m = MOK_GOOD;
2208 else
2209 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002210 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002211 /*
2212 * Missing operand size and a candidate for fuzzy matching...
2213 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002214 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002215 if (i == broadcast)
2216 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2217 else
2218 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002219 opsizemissing = true;
2220 }
2221 if (m > merr)
2222 merr = m;
2223 if (merr == MOK_GOOD)
2224 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002225 }
2226
2227 /* No match, but see if we can get a fuzzy operand size match... */
2228 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002229 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002230
2231 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002232 /*
2233 * We ignore extrinsic operand sizes on registers, so we should
2234 * never try to fuzzy-match on them. This also resolves the case
2235 * when we have e.g. "xmmrm128" in two different positions.
2236 */
2237 if (is_class(REGISTER, instruction->oprs[i].type))
2238 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002239
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002240 /* This tests if xsizeflags[i] has more than one bit set */
2241 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2242 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002243
Jin Kyu Song7903c072013-10-30 03:00:12 -07002244 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002245 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002246 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2247 BITS32 : BITS64);
2248 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002249 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002250 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002251 }
2252
2253 /* Try matching again... */
2254 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002255 temp->opcode != I_none; temp++) {
2256 m = matches(temp, instruction, bits);
2257 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002258 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002259 m = MOK_GOOD;
2260 else
2261 m = MERR_INVALOP;
2262 }
2263 if (m > merr)
2264 merr = m;
2265 if (merr == MOK_GOOD)
2266 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002267 }
2268
H. Peter Anvina81655b2009-07-25 18:15:28 -07002269done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002270 *tempp = temp;
2271 return merr;
2272}
2273
Mark Charneydcaef4b2014-10-09 13:45:17 -04002274static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2275{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002276 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002277 uint8_t brcast_num;
2278
Mark Charneydcaef4b2014-10-09 13:45:17 -04002279 if (brsize > BITS64)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002280 nasm_fatal("size of broadcasting element is greater than 64 bits");
Mark Charneydcaef4b2014-10-09 13:45:17 -04002281
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002282 /*
2283 * The shift term is to take care of the extra BITS80 inserted
2284 * between BITS64 and BITS128.
2285 */
2286 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2287 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002288
2289 return brcast_num;
2290}
2291
H. Peter Anvin65289e82009-07-25 17:25:11 -07002292static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002293 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002294{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002295 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002296 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002297 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002298
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002299 /*
2300 * Check the opcode
2301 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002302 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002303 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002304
2305 /*
2306 * Count the operands
2307 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002308 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002309 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002310
2311 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002312 * Is it legal?
2313 */
Chang S. Baea5786342018-08-15 23:22:21 +03002314 if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002315 return MERR_INVALOP;
2316
2317 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002318 * {evex} available?
2319 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002320 switch (instruction->prefixes[PPS_VEX]) {
2321 case P_EVEX:
2322 if (!itemp_has(itemp, IF_EVEX))
2323 return MERR_ENCMISMATCH;
2324 break;
2325 case P_VEX3:
2326 case P_VEX2:
2327 if (!itemp_has(itemp, IF_VEX))
2328 return MERR_ENCMISMATCH;
2329 break;
2330 default:
2331 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002332 }
2333
2334 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002335 * Check that no spurious colons or TOs are present
2336 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002337 for (i = 0; i < itemp->operands; i++)
2338 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002339 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002340
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002341 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002342 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002343 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002344 switch (itemp_smask(itemp)) {
2345 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002346 asize = BITS8;
2347 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002348 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002349 asize = BITS16;
2350 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002351 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002352 asize = BITS32;
2353 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002354 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002355 asize = BITS64;
2356 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002357 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002358 asize = BITS128;
2359 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002360 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002361 asize = BITS256;
2362 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002363 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002364 asize = BITS512;
2365 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002366 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002367 switch (bits) {
2368 case 16:
2369 asize = BITS16;
2370 break;
2371 case 32:
2372 asize = BITS32;
2373 break;
2374 case 64:
2375 asize = BITS64;
2376 break;
2377 default:
2378 asize = 0;
2379 break;
2380 }
2381 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002382 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002383 asize = 0;
2384 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002385 }
2386
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002387 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002388 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002389 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002390 memset(size, 0, sizeof size);
2391 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002392 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002393 /* S- flags apply to all operands */
2394 for (i = 0; i < MAX_OPERANDS; i++)
2395 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002396 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002397
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002398 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002399 * Check that the operand flags all match up,
2400 * it's a bit tricky so lets be verbose:
2401 *
2402 * 1) Find out the size of operand. If instruction
2403 * doesn't have one specified -- we're trying to
2404 * guess it either from template (IF_S* flag) or
2405 * from code bits.
2406 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002407 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002408 * template has an operand size specified AND this size differ
2409 * from which instruction has (perhaps we got it from code bits)
2410 * we are:
2411 * a) Check that only size of instruction and operand is differ
2412 * other characteristics do match
2413 * b) Perhaps it's a register specified in instruction so
2414 * for such a case we just mark that operand as "size
2415 * missing" and this will turn on fuzzy operand size
2416 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002417 */
2418 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002419 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002420 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002421 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002422 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002423 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002424 opflags_t template_opsize, insn_opsize;
2425
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002426 if (!(type & SIZE_MASK))
2427 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002428
Jin Kyu Song7903c072013-10-30 03:00:12 -07002429 insn_opsize = type & SIZE_MASK;
2430 if (!is_broadcast) {
2431 template_opsize = itemp->opd[i] & SIZE_MASK;
2432 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002433 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2434
2435 if (~ideco & BRDCAST_MASK)
2436 return MERR_BRNOTHERE;
2437
Jin Kyu Song7903c072013-10-30 03:00:12 -07002438 /*
2439 * when broadcasting, the element size depends on
2440 * the instruction type. decorator flag should match.
2441 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002442 if (deco_brsize) {
2443 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002444 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002445 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002446 } else {
2447 template_opsize = 0;
2448 }
2449 }
2450
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002451 if (~ideco & deco & OPMASK_MASK)
2452 return MERR_MASKNOTHERE;
2453
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002454 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2455 return MERR_DECONOTHERE;
2456
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002457 if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002458 return MERR_INVALOP;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002459
2460 if (~itemp->opd[i] & type & REGSET_MASK)
2461 return (itemp->opd[i] & REGSET_MASK)
2462 ? MERR_REGSETSIZE : MERR_REGSET;
2463
2464 if (template_opsize) {
Jin Kyu Song7903c072013-10-30 03:00:12 -07002465 if (template_opsize != insn_opsize) {
2466 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002467 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002468 } else if (!is_class(REGISTER, type)) {
2469 /*
2470 * Note: we don't honor extrinsic operand sizes for registers,
2471 * so "missing operand size" for a register should be
2472 * considered a wildcard match rather than an error.
2473 */
2474 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002475 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002476 } else if (is_broadcast &&
2477 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002478 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002479 /*
2480 * broadcasting opsize matches but the number of repeated memory
2481 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002482 * if 64b double precision float is broadcasted to ymm (256b),
2483 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002484 */
2485 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002486 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002487 }
2488 }
2489
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002490 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002491 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002492
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002493 /*
2494 * Check operand sizes
2495 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002496 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2497 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002498 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002499 asize = itemp->opd[i] & SIZE_MASK;
2500 if (asize) {
2501 for (i = 0; i < oprs; i++)
2502 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002503 break;
2504 }
2505 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002506 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002508 }
2509
Keith Kaniosb7a89542007-04-12 02:40:54 +00002510 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 if (!(itemp->opd[i] & SIZE_MASK) &&
2512 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002513 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002514 }
2515
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002516 /*
2517 * Check template is okay at the set cpu level
2518 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002519 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002520 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002521
Keith Kaniosb7a89542007-04-12 02:40:54 +00002522 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002523 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002524 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002525 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002526 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002528 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002529 * If we have a HLE prefix, look for the NOHLE flag
2530 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002531 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002532 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2533 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2534 return MERR_BADHLE;
2535
2536 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002537 * Check if special handling needed for Jumps
2538 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002539 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002540 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002541
Jin Kyu Song03041092013-10-15 19:38:51 -07002542 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002543 * Check if BND prefix is allowed.
2544 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002545 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002546 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002547 (has_prefix(instruction, PPS_REP, P_BND) ||
2548 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002549 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002550 else if (itemp_has(itemp, IF_BND) &&
2551 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2552 has_prefix(instruction, PPS_REP, P_REPNZ)))
2553 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002554
H. Peter Anvin60926242009-07-26 16:25:38 -07002555 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002556}
2557
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002558/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002559 * Check if ModR/M.mod should/can be 01.
2560 * - EAF_BYTEOFFS is set
2561 * - offset can fit in a byte when EVEX is not used
2562 * - offset can be compressed when EVEX is used
2563 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002564#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2565 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2566 is_disp8n(input, ins, &output->disp8) : \
2567 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2568 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002569
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002570static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002571 int rfield, opflags_t rflags, insn *ins,
2572 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002573{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002574 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002575 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002576 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002577
H. Peter Anvin8f622462017-04-02 19:02:29 -07002578 *errmsg = "invalid effective address"; /* Default error message */
2579
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002580 output->type = EA_SCALAR;
2581 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002582 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002583
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002584 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002585 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002586 /* EVEX.R' flag for the REG operand */
2587 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002588
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002589 if (is_class(REGISTER, input->type)) {
2590 /*
2591 * It's a direct register.
2592 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002593 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002594 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002595
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002596 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002597 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002598
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002599 /* broadcasting is not available with a direct register operand. */
2600 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002601 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002602 goto err;
2603 }
2604
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002605 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002606 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002607 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002608 output->bytes = 0; /* no offset necessary either */
2609 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2610 } else {
2611 /*
2612 * It's a memory reference.
2613 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002614
2615 /* Embedded rounding or SAE is not available with a mem ref operand. */
2616 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002617 *errmsg = "embedded rounding is available only with "
2618 "register-register operations";
2619 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002620 }
2621
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002622 if (input->basereg == -1 &&
2623 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002624 /*
2625 * It's a pure offset.
2626 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002627 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002628 if (input->segment == NO_SEG ||
2629 (input->opflags & OPFLAG_RELATIVE)) {
H. Peter Anvin (Intel)c3c6cea2018-12-14 13:44:35 -08002630 nasm_warn(WARN_OTHER|ERR_PASS2, "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002631 input->type &= ~IP_REL;
2632 input->type |= MEMORY;
2633 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002634 }
2635
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002636 if (bits == 64 &&
2637 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002638 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002639 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002640 }
2641
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002642 if (eaflags & EAF_BYTEOFFS ||
2643 (eaflags & EAF_WORDOFFS &&
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002644 input->disp_size != (addrbits != 16 ? 32 : 16)))
H. Peter Anvin (Intel)5df6ca72018-12-18 12:25:11 -08002645 nasm_warn(WARN_OTHER, "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002646
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002647 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002648 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002649 output->sib = GEN_SIB(0, 4, 5);
2650 output->bytes = 4;
2651 output->modrm = GEN_MODRM(0, rfield, 4);
2652 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002653 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002654 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002655 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002656 output->modrm = GEN_MODRM(0, rfield,
2657 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002658 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002659 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002660 } else {
2661 /*
2662 * It's an indirection.
2663 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002664 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002665 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002666 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002667 int t, it, bt; /* register numbers */
2668 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002669
H. Peter Anvine2c80182005-01-15 22:15:51 +00002670 if (s == 0)
2671 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002672
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002673 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002674 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002675 ix = nasm_reg_flags[i];
2676 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002677 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002678 ix = 0;
2679 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002680
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002681 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002682 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002683 bx = nasm_reg_flags[b];
2684 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002685 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002686 bx = 0;
2687 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002688
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002689 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002690 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002691 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002692 int32_t o = input->offset;
2693 int mod, scale, index, base;
2694
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002695 /*
2696 * For a vector SIB, one has to be a vector and the other,
2697 * if present, a GPR. The vector must be the index operand.
2698 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002699 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002700 if (s == 0)
2701 s = 1;
2702 else if (s != 1)
2703 goto err;
2704
2705 t = bt, bt = it, it = t;
2706 x = bx, bx = ix, ix = x;
2707 }
2708
2709 if (bt != -1) {
2710 if (REG_GPR & ~bx)
2711 goto err;
2712 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2713 sok &= bx;
2714 else
2715 goto err;
2716 }
2717
2718 /*
2719 * While we're here, ensure the user didn't specify
2720 * WORD or QWORD
2721 */
2722 if (input->disp_size == 16 || input->disp_size == 64)
2723 goto err;
2724
2725 if (addrbits == 16 ||
2726 (addrbits == 32 && !(sok & BITS32)) ||
2727 (addrbits == 64 && !(sok & BITS64)))
2728 goto err;
2729
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002730 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2731 : ((ix & YMMREG & ~REG_EA)
2732 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002733
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002734 output->rex |= rexflags(it, ix, REX_X);
2735 output->rex |= rexflags(bt, bx, REX_B);
2736 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002737
2738 index = it & 7; /* it is known to be != -1 */
2739
2740 switch (s) {
2741 case 1:
2742 scale = 0;
2743 break;
2744 case 2:
2745 scale = 1;
2746 break;
2747 case 4:
2748 scale = 2;
2749 break;
2750 case 8:
2751 scale = 3;
2752 break;
2753 default: /* then what the smeg is it? */
2754 goto err; /* panic */
2755 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002756
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002757 if (bt == -1) {
2758 base = 5;
2759 mod = 0;
2760 } else {
2761 base = (bt & 7);
2762 if (base != REG_NUM_EBP && o == 0 &&
2763 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002764 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002765 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002766 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002767 mod = 1;
2768 else
2769 mod = 2;
2770 }
2771
2772 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002773 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2774 output->modrm = GEN_MODRM(mod, rfield, 4);
2775 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002776 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002777 /*
2778 * it must be a 32/64-bit memory reference. Firstly we have
2779 * to check that all registers involved are type E/Rxx.
2780 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002781 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002782 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002783
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002784 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002785 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2786 sok &= ix;
2787 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002788 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002789 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002790
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002791 if (bt != -1) {
2792 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002793 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002794 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002795 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002796 sok &= bx;
2797 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002798
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002799 /*
2800 * While we're here, ensure the user didn't specify
2801 * WORD or QWORD
2802 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002803 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002804 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002805
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002806 if (addrbits == 16 ||
2807 (addrbits == 32 && !(sok & BITS32)) ||
2808 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002809 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002810
Keith Kaniosb7a89542007-04-12 02:40:54 +00002811 /* now reorganize base/index */
2812 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002813 ((hb == b && ht == EAH_NOTBASE) ||
2814 (hb == i && ht == EAH_MAKEBASE))) {
2815 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002816 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002817 x = bx, bx = ix, ix = x;
2818 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002819
Jin Kyu Song164d6072013-10-15 19:10:13 -07002820 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002821 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002822 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002823 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002824 if (eaflags & EAF_MIB) {
2825 /* only for mib operands */
2826 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2827 /*
2828 * make a single reg index [reg*1].
2829 * gas uses this form for an explicit index register.
2830 */
2831 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2832 }
2833 if ((ht == EAH_SUMMED) && bt == -1) {
2834 /* separate once summed index into [base, index] */
2835 bt = it, bx = ix, s--;
2836 }
2837 } else {
2838 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002839 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002840 s == 3 || s == 5 || s == 9) && bt == -1) {
2841 /* convert 3*EAX to EAX+2*EAX */
2842 bt = it, bx = ix, s--;
2843 }
2844 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002845 (eaflags & EAF_TIMESTWO) &&
2846 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002847 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002848 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002849 * to sib format with 0x0 displacement - [EAX*1+0].
2850 */
2851 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2852 }
2853 }
Keith Kanios48af1772007-08-17 07:37:52 +00002854 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002855 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002856 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002857 x = ix, ix = bx, bx = x;
2858 }
2859 if (it == REG_NUM_ESP ||
2860 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002861 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002862
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002863 output->rex |= rexflags(it, ix, REX_X);
2864 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002865
Keith Kanios48af1772007-08-17 07:37:52 +00002866 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002867 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002868 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002869
Keith Kaniosb7a89542007-04-12 02:40:54 +00002870 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002872 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002873 } else {
2874 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002875 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002876 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002877 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002878 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002879 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002880 mod = 1;
2881 else
2882 mod = 2;
2883 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002884
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002885 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002886 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2887 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002888 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002889 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002890 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002891
Keith Kaniosb7a89542007-04-12 02:40:54 +00002892 if (it == -1)
2893 index = 4, s = 1;
2894 else
2895 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002896
H. Peter Anvine2c80182005-01-15 22:15:51 +00002897 switch (s) {
2898 case 1:
2899 scale = 0;
2900 break;
2901 case 2:
2902 scale = 1;
2903 break;
2904 case 4:
2905 scale = 2;
2906 break;
2907 case 8:
2908 scale = 3;
2909 break;
2910 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002911 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002912 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002913
Keith Kaniosb7a89542007-04-12 02:40:54 +00002914 if (bt == -1) {
2915 base = 5;
2916 mod = 0;
2917 } else {
2918 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002919 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002920 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002921 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002922 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002923 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002924 mod = 1;
2925 else
2926 mod = 2;
2927 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002928
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002929 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002930 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2931 output->modrm = GEN_MODRM(mod, rfield, 4);
2932 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002933 }
2934 } else { /* it's 16-bit */
2935 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002936 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002937
Keith Kaniosb7a89542007-04-12 02:40:54 +00002938 /* check for 64-bit long mode */
2939 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002940 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002941
H. Peter Anvine2c80182005-01-15 22:15:51 +00002942 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002943 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2944 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002945 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002946
Keith Kaniosb7a89542007-04-12 02:40:54 +00002947 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002948 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002949 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002950
H. Peter Anvine2c80182005-01-15 22:15:51 +00002951 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002952 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002953 if (b == -1 && i != -1) {
2954 int tmp = b;
2955 b = i;
2956 i = tmp;
2957 } /* swap */
2958 if ((b == R_SI || b == R_DI) && i != -1) {
2959 int tmp = b;
2960 b = i;
2961 i = tmp;
2962 }
2963 /* have BX/BP as base, SI/DI index */
2964 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002965 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002966 if (i != -1 && b != -1 &&
2967 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002968 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002969 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002970 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002971
H. Peter Anvine2c80182005-01-15 22:15:51 +00002972 rm = -1;
2973 if (i != -1)
2974 switch (i * 256 + b) {
2975 case R_SI * 256 + R_BX:
2976 rm = 0;
2977 break;
2978 case R_DI * 256 + R_BX:
2979 rm = 1;
2980 break;
2981 case R_SI * 256 + R_BP:
2982 rm = 2;
2983 break;
2984 case R_DI * 256 + R_BP:
2985 rm = 3;
2986 break;
2987 } else
2988 switch (b) {
2989 case R_SI:
2990 rm = 4;
2991 break;
2992 case R_DI:
2993 rm = 5;
2994 break;
2995 case R_BP:
2996 rm = 6;
2997 break;
2998 case R_BX:
2999 rm = 7;
3000 break;
3001 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003002 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003003 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003004
H. Peter Anvinab5bd052010-07-25 12:43:30 -07003005 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08003006 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00003007 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07003008 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00003009 mod = 1;
3010 else
3011 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003012
H. Peter Anvin6867acc2007-10-10 14:58:45 -07003013 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04003014 output->bytes = mod; /* bytes of offset needed */
3015 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00003016 }
3017 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003018 }
H. Peter Anvin70653092007-10-19 14:42:29 -07003019
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003020 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07003021 return output->type;
3022
3023err:
3024 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003025}
3026
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003027static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00003028{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003029 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003030 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00003031
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003032 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003033
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003034 switch (ins->prefixes[PPS_ASIZE]) {
3035 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003036 valid &= 16;
3037 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003038 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003039 valid &= 32;
3040 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003041 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003042 valid &= 64;
3043 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003044 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003045 valid &= (addrbits == 32) ? 16 : 32;
3046 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003047 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003048 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003049 }
3050
3051 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003052 if (is_class(MEMORY, ins->oprs[j].type)) {
3053 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07003054
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003055 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003056 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003057 i = 0;
3058 else
3059 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003060
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003061 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04003062 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003063 b = 0;
3064 else
3065 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07003066
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003067 if (ins->oprs[j].scale == 0)
3068 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003069
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003070 if (!i && !b) {
3071 int ds = ins->oprs[j].disp_size;
3072 if ((addrbits != 64 && ds > 8) ||
3073 (addrbits == 64 && ds == 16))
3074 valid &= ds;
3075 } else {
3076 if (!(REG16 & ~b))
3077 valid &= 16;
3078 if (!(REG32 & ~b))
3079 valid &= 32;
3080 if (!(REG64 & ~b))
3081 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07003082
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003083 if (!(REG16 & ~i))
3084 valid &= 16;
3085 if (!(REG32 & ~i))
3086 valid &= 32;
3087 if (!(REG64 & ~i))
3088 valid &= 64;
3089 }
3090 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003091 }
3092
3093 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003094 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07003095 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003096 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04003097 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003098 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003099 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003100 /* Impossible... */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03003101 nasm_nonfatal("impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003102 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07003103 }
3104
3105 defdisp = ins->addr_size == 16 ? 16 : 32;
3106
3107 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04003108 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
3109 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
3110 /*
3111 * mem_offs sizes must match the address size; if not,
3112 * strip the MEM_OFFS bit and match only EA instructions
3113 */
3114 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
3115 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003116 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003117}