blob: da58d1284749be25558a94c9c8154bcfdbf3dd5c [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#include <stdio.h>
181#include <string.h>
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800182#include <stdlib.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000183
184#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000185#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800186#include "error.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000187#include "assemble.h"
188#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700189#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800190#include "disp8.h"
H. Peter Anvin172b8402016-02-18 01:16:18 -0800191#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000192
H. Peter Anvin65289e82009-07-25 17:25:11 -0700193enum match_result {
194 /*
195 * Matching errors. These should be sorted so that more specific
196 * errors come later in the sequence.
197 */
198 MERR_INVALOP,
199 MERR_OPSIZEMISSING,
200 MERR_OPSIZEMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700201 MERR_BRNOTHERE,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700202 MERR_BRNUMMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700203 MERR_MASKNOTHERE,
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700204 MERR_DECONOTHERE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700205 MERR_BADCPU,
206 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800207 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700208 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700209 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800210 MERR_BADREPNE,
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700211 MERR_REGSETSIZE,
212 MERR_REGSET,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700213 /*
214 * Matching success; the conditional ones first
215 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400216 MOK_JUMP, /* Matching OK but needs jmp_match() */
217 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700218};
219
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000220typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700221 enum ea_type type; /* what kind of EA is this? */
222 int sib_present; /* is a SIB byte necessary? */
223 int bytes; /* # of bytes of offset needed */
224 int size; /* lazy - this is sib+bytes+1 */
225 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700226 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000227} ea;
228
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400229#define GEN_SIB(scale, index, base) \
230 (((scale) << 6) | ((index) << 3) | ((base)))
231
232#define GEN_MODRM(mod, reg, rm) \
233 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
234
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800235static int64_t calcsize(int32_t, int64_t, int, insn *,
236 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700237static int emit_prefix(struct out_data *data, const int bits, insn *ins);
238static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700239static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400240 insn *instruction,
241 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700242static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700243static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000244static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700245static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000246static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700247static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700248static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000249
H. Peter Anvin8f622462017-04-02 19:02:29 -0700250static enum ea_type process_ea(operand *, ea *, int, int,
251 opflags_t, insn *, const char **);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700252
H. Peter Anvin164d2462017-02-20 02:39:56 -0800253static inline bool absolute_op(const struct operand *o)
254{
255 return o->segment == NO_SEG && o->wrt == NO_SEG &&
256 !(o->opflags & OPFLAG_RELATIVE);
257}
258
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400259static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000260{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700261 return ins->prefixes[pos] == prefix;
262}
263
264static void assert_no_prefix(insn * ins, enum prefix_pos pos)
265{
266 if (ins->prefixes[pos])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800267 nasm_error(ERR_NONFATAL, "invalid %s prefix",
268 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700269}
270
271static const char *size_name(int size)
272{
273 switch (size) {
274 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700276 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400277 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700278 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400279 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700280 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700282 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400283 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700284 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400285 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700286 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400287 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700288 case 64:
289 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700290 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400291 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000292 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700293}
294
H. Peter Anvin285222f2017-03-01 13:27:33 -0800295static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400296{
H. Peter Anvin285222f2017-03-01 13:27:33 -0800297 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400298 "%s data exceeds bounds", size_name(size));
299}
300
301static void warn_overflow_const(int64_t data, int size)
302{
303 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800304 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400305}
306
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800307static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
308{
309 bool err;
310
311 switch (sign) {
312 case OUT_WRAP:
313 err = overflow_general(data, size);
314 break;
315 case OUT_SIGNED:
316 err = overflow_signed(data, size);
317 break;
318 case OUT_UNSIGNED:
319 err = overflow_unsigned(data, size);
320 break;
321 default:
322 panic();
323 break;
324 }
325
326 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800327 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800328}
329
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000330/*
331 * This routine wrappers the real output format's output routine,
332 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800333 * generator at the same time, flatten unnecessary relocations,
334 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000335 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700336static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000337{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000338 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700339 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700340 union {
341 uint8_t b[8];
342 uint64_t q;
343 } xdata;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700344 size_t asize, amax;
345 uint64_t zeropad = 0;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800346 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800347 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000348
H. Peter Anvina77692b2016-09-20 14:04:33 -0700349 if (!data->size)
350 return; /* Nothing to do */
351
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700352 /*
353 * Convert addresses to RAWDATA if possible
354 * XXX: not all backends want this for global symbols!!!!
355 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700356 switch (data->type) {
357 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800358 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800359 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800360 goto address;
361
362 case OUT_RELADDR:
363 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800364 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800365 goto address;
366
367 address:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700368 nasm_assert(data->size <= 8);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700369 asize = data->size;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700370 amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
Chang S. Bae427d8e32018-05-02 08:07:52 -0700371 if ((ofmt->flags & OFMT_KEEP_ADDR) == 0 && data->tsegment == fixseg &&
372 data->twrt == NO_SEG) {
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800373 warn_overflow_out(addrval, asize, data->sign);
Martin Storsjö869087d2017-05-22 13:54:20 +0300374 xdata.q = cpu_to_le64(addrval);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700375 data->data = xdata.b;
376 data->type = OUT_RAWDATA;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700377 asize = amax = 0; /* No longer an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700378 }
379 break;
380
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700381 case OUT_SEGMENT:
382 nasm_assert(data->size <= 8);
383 asize = data->size;
384 amax = 2;
385 break;
386
H. Peter Anvina77692b2016-09-20 14:04:33 -0700387 default:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700388 asize = amax = 0; /* Not an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700389 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000390 }
391
Frank Kotlerabebb082003-09-06 04:45:37 +0000392 /*
393 * this call to src_get determines when we call the
394 * debug-format-specific "linenum" function
395 * it updates lineno and lnfname to the current values
396 * returning 0 if "same as last time", -2 if lnfname
397 * changed, and the amount by which lineno changed,
398 * if it did. thus, these variables must be static
399 */
400
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400401 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700402 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000403
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700404 if (asize > amax) {
405 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800406 nasm_error(ERR_NONFATAL,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700407 "%u-bit signed relocation unsupported by output format %s",
408 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800409 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800410 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
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 */
425 if (data->type != OUT_RESERVE) {
426 nasm_error(ERR_NONFATAL, "attempt to assemble code in [ABSOLUTE]"
427 " space");
428 }
429 /* No need to push to the backend */
430 }
431
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700432 data->offset += data->size;
433 data->insoffs += data->size;
434
435 if (zeropad) {
436 data->type = OUT_ZERODATA;
437 data->size = zeropad;
438 lfmt->output(data);
439 ofmt->output(data);
440 data->offset += zeropad;
441 data->insoffs += zeropad;
442 data->size += zeropad; /* Restore original size value */
443 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000444}
445
H. Peter Anvina77692b2016-09-20 14:04:33 -0700446static inline void out_rawdata(struct out_data *data, const void *rawdata,
447 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400448{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700449 data->type = OUT_RAWDATA;
450 data->data = rawdata;
451 data->size = size;
452 out(data);
453}
454
455static void out_rawbyte(struct out_data *data, uint8_t byte)
456{
457 data->type = OUT_RAWDATA;
458 data->data = &byte;
459 data->size = 1;
460 out(data);
461}
462
463static inline void out_reserve(struct out_data *data, uint64_t size)
464{
465 data->type = OUT_RESERVE;
466 data->size = size;
467 out(data);
468}
469
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700470static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700471{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700472 if (opx->opflags & OPFLAG_RELATIVE)
473 nasm_error(ERR_NONFATAL, "segment references cannot be relative");
474
475 data->type = OUT_SEGMENT;
476 data->sign = OUT_UNSIGNED;
477 data->size = 2;
478 data->toffset = opx->offset;
479 data->tsegment = ofmt->segbase(opx->segment | 1);
480 data->twrt = opx->wrt;
481 out(data);
482}
483
484static void out_imm(struct out_data *data, const struct operand *opx,
485 int size, enum out_sign sign)
486{
487 if (opx->segment != NO_SEG && (opx->segment & 1)) {
488 /*
489 * This is actually a segment reference, but eval() has
490 * already called ofmt->segbase() for us. Sigh.
491 */
492 if (size < 2)
493 nasm_error(ERR_NONFATAL, "segment reference must be 16 bits");
494
495 data->type = OUT_SEGMENT;
496 } else {
497 data->type = (opx->opflags & OPFLAG_RELATIVE)
498 ? OUT_RELADDR : OUT_ADDRESS;
499 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700500 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700501 data->toffset = opx->offset;
502 data->tsegment = opx->segment;
503 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800504 /*
505 * XXX: improve this if at some point in the future we can
506 * distinguish the subtrahend in expressions like [foo - bar]
507 * where bar is a symbol in the current segment. However, at the
508 * current point, if OPFLAG_RELATIVE is set that subtraction has
509 * already occurred.
510 */
511 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700512 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700513 out(data);
514}
515
H. Peter Anvin164d2462017-02-20 02:39:56 -0800516static void out_reladdr(struct out_data *data, const struct operand *opx,
517 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700518{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800519 if (opx->opflags & OPFLAG_RELATIVE)
520 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
521
H. Peter Anvina77692b2016-09-20 14:04:33 -0700522 data->type = OUT_RELADDR;
523 data->sign = OUT_SIGNED;
524 data->size = size;
525 data->toffset = opx->offset;
526 data->tsegment = opx->segment;
527 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800528 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700529 out(data);
530}
531
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700532static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800533 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800535 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800536 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000537 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800538 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000539
H. Peter Anvin755f5212012-02-25 11:41:34 -0800540 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700541 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300542 if (!optimizing.level || (optimizing.flag & OPTIM_DISABLE_JMP_MATCH))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400543 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300544 if (optimizing.level < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400545 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700546
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800547 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100548
Victor van den Elzen154e5922009-02-25 17:32:00 +0100549 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100550 /* Be optimistic in pass 1 */
551 return true;
552
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700554 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000555
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700556 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800557 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
558
559 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
560 /* jmp short (opcode eb) cannot be used with bnd prefix. */
561 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800562 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800563 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800564 }
565
566 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000568
H. Peter Anvin04445362016-09-21 15:56:19 -0700569/* This is totally just a wild guess what is reasonable... */
570#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
571
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800572int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000573{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700574 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000575 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700576 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300577 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000578
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700579 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700580 data.offset = start;
581 data.segment = segment;
582 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700583 data.bits = bits;
584
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700585 wsize = db_bytes(instruction->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300586 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000588
H. Peter Anvineba20a72002-04-30 20:53:55 +0000589 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700591
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700592 list_for_each(e, instruction->eops) {
593 if (e->type == EOT_DB_NUMBER) {
594 if (wsize > 8) {
595 nasm_error(ERR_NONFATAL,
596 "integer supplied to a DT, DO, DY or DZ"
597 " instruction");
598 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700599 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700600 data.inslen = data.size = wsize;
601 data.toffset = e->offset;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700602 data.twrt = e->wrt;
603 data.relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700604 if (e->segment != NO_SEG && (e->segment & 1)) {
605 data.tsegment = e->segment;
606 data.type = OUT_SEGMENT;
607 data.sign = OUT_UNSIGNED;
608 } else {
609 data.tsegment = e->segment;
610 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
611 data.sign = OUT_WRAP;
612 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700613 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700615 } else if (e->type == EOT_DB_STRING ||
616 e->type == EOT_DB_STRING_FREE) {
617 int align = e->stringlen % wsize;
618 if (align)
619 align = wsize - align;
620
621 data.insoffs = 0;
622 data.inslen = e->stringlen + align;
623
624 out_rawdata(&data, e->stringval, e->stringlen);
625 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700626 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700628 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700629 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700631 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700632 off_t base = 0;
633 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700634 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700635 char *buf = NULL;
636 size_t blk = 0; /* Buffered I/O block size */
637 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000638
H. Peter Anvin94ead272017-09-27 15:22:23 -0700639 if (!t)
640 goto done;
641
H. Peter Anvind81a2352016-09-21 14:03:18 -0700642 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400643 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800644 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000645 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700646 goto done;
647 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000648
H. Peter Anvind81a2352016-09-21 14:03:18 -0700649 len = nasm_file_size(fp);
650
651 if (len == (off_t)-1) {
652 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700653 fname);
654 goto close_done;
655 }
656
H. Peter Anvina77692b2016-09-20 14:04:33 -0700657 if (instruction->eops->next) {
658 base = instruction->eops->next->offset;
659 if (base >= len) {
660 len = 0;
661 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000662 len -= base;
663 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700664 len > (off_t)instruction->eops->next->next->offset)
665 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000666 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000667 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700668
H. Peter Anvina77692b2016-09-20 14:04:33 -0700669 lfmt->set_offset(data.offset);
670 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000671
H. Peter Anvind81a2352016-09-21 14:03:18 -0700672 if (!len)
673 goto end_incbin;
674
675 /* Try to map file data */
676 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700677 if (!map) {
678 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
679 buf = nasm_malloc(blk);
680 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700681
682 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700683 /*
684 * Consider these irrelevant for INCBIN, since it is fully
685 * possible that these might be (way) bigger than an int
686 * can hold; there is, however, no reason to widen these
687 * types just for INCBIN. data.inslen == 0 signals to the
688 * backend that these fields are meaningless, if at all
689 * needed.
690 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700691 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700692 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700693
H. Peter Anvind81a2352016-09-21 14:03:18 -0700694 if (map) {
695 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700696 } else if ((off_t)m == len) {
697 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700698 } else {
699 off_t l = len;
700
701 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700702 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700703 "`incbin': unable to seek on file `%s'",
704 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700705 goto end_incbin;
706 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700707 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700708 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700709 if (!m || feof(fp)) {
710 /*
711 * This shouldn't happen unless the file
712 * actually changes while we are reading
713 * it.
714 */
715 nasm_error(ERR_NONFATAL,
716 "`incbin': unexpected EOF while"
717 " reading file `%s'", fname);
718 goto end_incbin;
719 }
720 out_rawdata(&data, buf, m);
721 l -= m;
722 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700723 }
724 }
725 end_incbin:
726 lfmt->downlevel(LIST_INCBIN);
727 if (instruction->times > 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700728 lfmt->uplevel(LIST_TIMES);
729 lfmt->downlevel(LIST_TIMES);
730 }
731 if (ferror(fp)) {
732 nasm_error(ERR_NONFATAL,
733 "`incbin': error while"
734 " reading file `%s'", fname);
735 }
736 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700737 if (buf)
738 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700739 if (map)
740 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700741 fclose(fp);
742 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700743 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700744 ;
745 } else {
746 /* "Real" instruction */
747
748 /* Check to see if we need an address-size prefix */
749 add_asp(instruction, bits);
750
751 m = find_match(&temp, instruction, data.segment, data.offset, bits);
752
753 if (m == MOK_GOOD) {
754 /* Matches! */
755 int64_t insn_size = calcsize(data.segment, data.offset,
756 bits, instruction, temp);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700757 nasm_assert(insn_size >= 0);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700758
759 data.itemp = temp;
760 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700761 data.insoffs = 0;
762 data.inslen = insn_size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700763
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700764 gencode(&data, instruction);
765 nasm_assert(data.insoffs == insn_size);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700766 } else {
767 /* No match */
768 switch (m) {
769 case MERR_OPSIZEMISSING:
770 nasm_error(ERR_NONFATAL, "operation size not specified");
771 break;
772 case MERR_OPSIZEMISMATCH:
773 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
774 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700775 case MERR_BRNOTHERE:
776 nasm_error(ERR_NONFATAL,
777 "broadcast not permitted on this operand");
778 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700779 case MERR_BRNUMMISMATCH:
780 nasm_error(ERR_NONFATAL,
781 "mismatch in the number of broadcasting elements");
782 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700783 case MERR_MASKNOTHERE:
784 nasm_error(ERR_NONFATAL,
785 "mask not permitted on this operand");
786 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700787 case MERR_DECONOTHERE:
788 nasm_error(ERR_NONFATAL, "unsupported mode decorator for instruction");
789 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700790 case MERR_BADCPU:
791 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
792 break;
793 case MERR_BADMODE:
794 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
795 bits);
796 break;
797 case MERR_ENCMISMATCH:
798 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
799 break;
800 case MERR_BADBND:
801 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
802 break;
803 case MERR_BADREPNE:
804 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
805 (has_prefix(instruction, PPS_REP, P_REPNE) ?
806 "repne" : "repnz"));
807 break;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700808 case MERR_REGSETSIZE:
809 nasm_error(ERR_NONFATAL, "invalid register set size");
810 break;
811 case MERR_REGSET:
812 nasm_error(ERR_NONFATAL, "register set not valid for operand");
813 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700814 default:
815 nasm_error(ERR_NONFATAL,
816 "invalid combination of opcode and operands");
817 break;
818 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700819
820 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400821 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000822 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700823 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000824}
825
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800826int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000827{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000828 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700829 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000830
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400831 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000833
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700834 if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300836 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000837
H. Peter Anvine2c80182005-01-15 22:15:51 +0000838 isize = 0;
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700839 wsize = db_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700840 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000841
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400842 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000843 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000844
H. Peter Anvine2c80182005-01-15 22:15:51 +0000845 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400846 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400848 warn_overflow_const(e->offset, wsize);
849 } else if (e->type == EOT_DB_STRING ||
850 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000852
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 align = (-osize) % wsize;
854 if (align < 0)
855 align += wsize;
856 isize += osize + align;
857 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700858 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000859 }
860
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400862 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700863 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000864
H. Peter Anvind81a2352016-09-21 14:03:18 -0700865 len = nasm_file_size_by_path(fname);
866 if (len == (off_t)-1) {
867 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
868 fname);
869 return 0;
870 }
871
872 if (instruction->eops->next) {
873 if (len <= (off_t)instruction->eops->next->offset) {
874 len = 0;
875 } else {
876 len -= instruction->eops->next->offset;
877 if (instruction->eops->next->next &&
878 len > (off_t)instruction->eops->next->next->offset) {
879 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000880 }
881 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000882 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700883
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700884 len *= instruction->times;
885 instruction->times = 1; /* Tell the upper layer to not iterate */
886
H. Peter Anvind81a2352016-09-21 14:03:18 -0700887 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000888 }
889
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700890 /* Check to see if we need an address-size prefix */
891 add_asp(instruction, bits);
892
H. Peter Anvin23595f52009-07-25 17:44:25 -0700893 m = find_match(&temp, instruction, segment, offset, bits);
894 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400895 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700896 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700897 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400898 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000899 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000900}
901
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800902static void bad_hle_warn(const insn * ins, uint8_t hleok)
903{
904 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800905 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800906 static const enum whatwarn warn[2][4] =
907 {
908 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
909 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
910 };
911 unsigned int n;
912
913 n = (unsigned int)rep_pfx - P_XACQUIRE;
914 if (n > 1)
915 return; /* Not XACQUIRE/XRELEASE */
916
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800917 ww = warn[n][hleok];
918 if (!is_class(MEMORY, ins->oprs[0].type))
919 ww = w_inval; /* HLE requires operand 0 to be memory */
920
921 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800922 case w_none:
923 break;
924
925 case w_lock:
926 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800927 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800928 "%s with this instruction requires lock",
929 prefix_name(rep_pfx));
930 }
931 break;
932
933 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800934 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800935 "%s invalid with this instruction",
936 prefix_name(rep_pfx));
937 break;
938 }
939}
940
H. Peter Anvin507ae032008-10-09 15:37:10 -0700941/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400942#define case3(x) case (x): case (x)+1: case (x)+2
943#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700944
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800945static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800946 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000947{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800948 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800949 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000950 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000951 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700952 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700953 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700954 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700955 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800956 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800957 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700958 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -0700959 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000960
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700961 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700962 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700963 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700964
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700965 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400966 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700967
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 (void)segment; /* Don't warn that this parameter is unused */
969 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000970
H. Peter Anvin839eca22007-10-29 23:12:47 -0700971 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400972 c = *codes++;
973 op1 = (c & 3) + ((opex & 1) << 2);
974 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
975 opx = &ins->oprs[op1];
976 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700977
H. Peter Anvin839eca22007-10-29 23:12:47 -0700978 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400979 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 codes += c, length += c;
981 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700982
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400983 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400984 opex = c;
985 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700986
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400987 case4(010):
988 ins->rex |=
989 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 codes++, length++;
991 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700992
Jin Kyu Song164d6072013-10-15 19:10:13 -0700993 case4(014):
994 /* this is an index reg of MIB operand */
995 mib_index = opx->basereg;
996 break;
997
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400998 case4(020):
999 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 length++;
1001 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001002
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001003 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 length += 2;
1005 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001006
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001007 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001008 if (opx->type & (BITS16 | BITS32 | BITS64))
1009 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 else
1011 length += (bits == 16) ? 2 : 4;
1012 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001013
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001014 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 length += 4;
1016 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001017
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001018 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001019 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001021
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001022 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 length++;
1024 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001025
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001026 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001027 length += 8; /* MOV reg64/imm */
1028 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001029
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001030 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 length += 2;
1032 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001033
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001034 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001035 if (opx->type & (BITS16 | BITS32 | BITS64))
1036 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 else
1038 length += (bits == 16) ? 2 : 4;
1039 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001040
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001041 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 length += 4;
1043 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001044
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001045 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001046 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001048
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001049 case 0172:
1050 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001051 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001052 length++;
1053 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001054
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001055 case4(0174):
1056 length++;
1057 break;
1058
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001059 case4(0240):
1060 ins->rex |= REX_EV;
1061 ins->vexreg = regval(opx);
1062 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1063 ins->vex_cm = *codes++;
1064 ins->vex_wlp = *codes++;
1065 ins->evex_tuple = (*codes++ - 0300);
1066 break;
1067
1068 case 0250:
1069 ins->rex |= REX_EV;
1070 ins->vexreg = 0;
1071 ins->vex_cm = *codes++;
1072 ins->vex_wlp = *codes++;
1073 ins->evex_tuple = (*codes++ - 0300);
1074 break;
1075
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001076 case4(0254):
1077 length += 4;
1078 break;
1079
1080 case4(0260):
1081 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001082 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001083 ins->vex_cm = *codes++;
1084 ins->vex_wlp = *codes++;
1085 break;
1086
1087 case 0270:
1088 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001089 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001090 ins->vex_cm = *codes++;
1091 ins->vex_wlp = *codes++;
1092 break;
1093
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001094 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001095 hleok = c & 3;
1096 break;
1097
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001098 case4(0274):
1099 length++;
1100 break;
1101
1102 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001104
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001106 if (bits == 64)
1107 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001108 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001110
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001112 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001114
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001116 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001117
Keith Kaniosb7a89542007-04-12 02:40:54 +00001118 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001119 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1120 has_prefix(ins, PPS_ASIZE, P_A32))
1121 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001123
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001124 case4(0314):
1125 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001126
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001128 {
1129 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1130 if (pfx == P_O16)
1131 break;
1132 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001133 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001134 else
1135 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001137 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001138
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001140 {
1141 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1142 if (pfx == P_O32)
1143 break;
1144 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001145 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001146 else
1147 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001149 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001150
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 case 0322:
1152 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001153
Keith Kaniosb7a89542007-04-12 02:40:54 +00001154 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001155 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001156 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001157
Keith Kaniosb7a89542007-04-12 02:40:54 +00001158 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001159 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001160 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001161
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001162 case 0325:
1163 ins->rex |= REX_NH;
1164 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001165
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001166 case 0326:
1167 break;
1168
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 case 0330:
1170 codes++, length++;
1171 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001172
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001175
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001176 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 case 0333:
1178 length++;
1179 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001180
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001181 case 0334:
1182 ins->rex |= REX_L;
1183 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001184
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001185 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001186 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001187
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001188 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001189 if (!ins->prefixes[PPS_REP])
1190 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001191 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001192
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001193 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001194 if (!ins->prefixes[PPS_REP])
1195 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001196 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001197
H. Peter Anvine2c80182005-01-15 22:15:51 +00001198 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001199 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001200 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001202 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1203 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001204 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001205 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001206 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001208
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001209 case 0341:
1210 if (!ins->prefixes[PPS_WAIT])
1211 ins->prefixes[PPS_WAIT] = P_WAIT;
1212 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001213
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001214 case 0360:
1215 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001216
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001217 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001218 length++;
1219 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001220
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001221 case 0364:
1222 case 0365:
1223 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001224
Keith Kanios48af1772007-08-17 07:37:52 +00001225 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001226 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001227 length++;
1228 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001229
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001230 case 0370:
1231 case 0371:
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 0373:
1235 length++;
1236 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001237
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001238 case 0374:
1239 eat = EA_XMMVSIB;
1240 break;
1241
1242 case 0375:
1243 eat = EA_YMMVSIB;
1244 break;
1245
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001246 case 0376:
1247 eat = EA_ZMMVSIB;
1248 break;
1249
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001250 case4(0100):
1251 case4(0110):
1252 case4(0120):
1253 case4(0130):
1254 case4(0200):
1255 case4(0204):
1256 case4(0210):
1257 case4(0214):
1258 case4(0220):
1259 case4(0224):
1260 case4(0230):
1261 case4(0234):
1262 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001264 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001265 opflags_t rflags;
1266 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001267 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001268
Keith Kaniosb7a89542007-04-12 02:40:54 +00001269 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001270
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001271 if (c <= 0177) {
1272 /* pick rfield from operand b (opx) */
1273 rflags = regflag(opx);
1274 rfield = nasm_regvals[opx->basereg];
1275 } else {
1276 rflags = 0;
1277 rfield = c & 7;
1278 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001279
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001280 /* EVEX.b1 : evex_brerop contains the operand position */
1281 op_er_sae = (ins->evex_brerop >= 0 ?
1282 &ins->oprs[ins->evex_brerop] : NULL);
1283
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001284 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1285 /* set EVEX.b */
1286 ins->evex_p[2] |= EVEX_P2B;
1287 if (op_er_sae->decoflags & ER) {
1288 /* set EVEX.RC (rounding control) */
1289 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1290 & EVEX_P2RC;
1291 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001292 } else {
1293 /* set EVEX.L'L (vector length) */
1294 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001295 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001296 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001297 /* set EVEX.b */
1298 ins->evex_p[2] |= EVEX_P2B;
1299 }
1300 }
1301
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001302 if (itemp_has(temp, IF_MIB)) {
1303 opy->eaflags |= EAF_MIB;
1304 /*
1305 * if a separate form of MIB (ICC style) is used,
1306 * the index reg info is merged into mem operand
1307 */
1308 if (mib_index != R_none) {
1309 opy->indexreg = mib_index;
1310 opy->scale = 1;
1311 opy->hintbase = mib_index;
1312 opy->hinttype = EAH_NOTBASE;
1313 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001314 }
1315
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001316 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001317 rfield, rflags, ins, &errmsg) != eat) {
1318 nasm_error(ERR_NONFATAL, "%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001319 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001320 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001321 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001322 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001323 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001324 }
1325 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001326
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001327 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001328 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001329 ": instruction code \\%o (0x%02X) given", c, c);
1330 break;
1331 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001332 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001333
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001334 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001335
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001336 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001337 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001338 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001339 return -1;
1340 }
1341 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001342 }
1343
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001344 switch (ins->prefixes[PPS_VEX]) {
1345 case P_EVEX:
1346 if (!(ins->rex & REX_EV))
1347 return -1;
1348 break;
1349 case P_VEX3:
1350 case P_VEX2:
1351 if (!(ins->rex & REX_V))
1352 return -1;
1353 break;
1354 default:
1355 break;
1356 }
1357
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001358 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001359 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001360
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001361 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001362 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001363 return -1;
1364 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001365 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001366 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001367 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001368 ins->rex &= ~REX_W;
1369 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001370 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001371 ins->rex |= REX_W;
1372 bad32 &= ~REX_W;
1373 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001374 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001375 /* Follow REX_W */
1376 break;
1377 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001378
H. Peter Anvinfc561202011-07-07 16:58:22 -07001379 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001380 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001381 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001382 } else if (!(ins->rex & REX_EV) &&
1383 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001384 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001385 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001386 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001387 if (ins->rex & REX_EV)
1388 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001389 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1390 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001391 length += 3;
1392 else
1393 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001394 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001395 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001396 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001397 return -1;
1398 } else if (bits == 64) {
1399 length++;
1400 } else if ((ins->rex & REX_L) &&
1401 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001402 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001403 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001404 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001405 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001406 length++;
1407 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001408 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001409 return -1;
1410 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001411 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001412
1413 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001414 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001415 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001416 "instruction is not lockable");
1417 }
1418
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001419 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001420
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001421 /*
1422 * when BND prefix is set by DEFAULT directive,
1423 * BND prefix is added to every appropriate instruction line
1424 * unless it is overridden by NOBND prefix.
1425 */
1426 if (globalbnd &&
1427 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1428 ins->prefixes[PPS_REP] = P_BND;
1429
H. Peter Anvina77692b2016-09-20 14:04:33 -07001430 /*
1431 * Add length of legacy prefixes
1432 */
1433 length += emit_prefix(NULL, bits, ins);
1434
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001435 return length;
1436}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001437
H. Peter Anvina77692b2016-09-20 14:04:33 -07001438static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001439{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001440 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001441 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001442 !(ins->rex & (REX_V | REX_EV)) &&
1443 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001444 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1445 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001446 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001447 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001448 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001449}
1450
H. Peter Anvina77692b2016-09-20 14:04:33 -07001451static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1452{
1453 int bytes = 0;
1454 int j;
1455
1456 for (j = 0; j < MAXPREFIX; j++) {
1457 uint8_t c = 0;
1458 switch (ins->prefixes[j]) {
1459 case P_WAIT:
1460 c = 0x9B;
1461 break;
1462 case P_LOCK:
1463 c = 0xF0;
1464 break;
1465 case P_REPNE:
1466 case P_REPNZ:
1467 case P_XACQUIRE:
1468 case P_BND:
1469 c = 0xF2;
1470 break;
1471 case P_REPE:
1472 case P_REPZ:
1473 case P_REP:
1474 case P_XRELEASE:
1475 c = 0xF3;
1476 break;
1477 case R_CS:
1478 if (bits == 64) {
1479 nasm_error(ERR_WARNING | ERR_PASS2,
1480 "cs segment base generated, but will be ignored in 64-bit mode");
1481 }
1482 c = 0x2E;
1483 break;
1484 case R_DS:
1485 if (bits == 64) {
1486 nasm_error(ERR_WARNING | ERR_PASS2,
1487 "ds segment base generated, but will be ignored in 64-bit mode");
1488 }
1489 c = 0x3E;
1490 break;
1491 case R_ES:
1492 if (bits == 64) {
1493 nasm_error(ERR_WARNING | ERR_PASS2,
1494 "es segment base generated, but will be ignored in 64-bit mode");
1495 }
1496 c = 0x26;
1497 break;
1498 case R_FS:
1499 c = 0x64;
1500 break;
1501 case R_GS:
1502 c = 0x65;
1503 break;
1504 case R_SS:
1505 if (bits == 64) {
1506 nasm_error(ERR_WARNING | ERR_PASS2,
1507 "ss segment base generated, but will be ignored in 64-bit mode");
1508 }
1509 c = 0x36;
1510 break;
1511 case R_SEGR6:
1512 case R_SEGR7:
1513 nasm_error(ERR_NONFATAL,
1514 "segr6 and segr7 cannot be used as prefixes");
1515 break;
1516 case P_A16:
1517 if (bits == 64) {
1518 nasm_error(ERR_NONFATAL,
1519 "16-bit addressing is not supported "
1520 "in 64-bit mode");
1521 } else if (bits != 16)
1522 c = 0x67;
1523 break;
1524 case P_A32:
1525 if (bits != 32)
1526 c = 0x67;
1527 break;
1528 case P_A64:
1529 if (bits != 64) {
1530 nasm_error(ERR_NONFATAL,
1531 "64-bit addressing is only supported "
1532 "in 64-bit mode");
1533 }
1534 break;
1535 case P_ASP:
1536 c = 0x67;
1537 break;
1538 case P_O16:
1539 if (bits != 16)
1540 c = 0x66;
1541 break;
1542 case P_O32:
1543 if (bits == 16)
1544 c = 0x66;
1545 break;
1546 case P_O64:
1547 /* REX.W */
1548 break;
1549 case P_OSP:
1550 c = 0x66;
1551 break;
1552 case P_EVEX:
1553 case P_VEX3:
1554 case P_VEX2:
1555 case P_NOBND:
1556 case P_none:
1557 break;
1558 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001559 nasm_panic("invalid instruction prefix");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001560 }
1561 if (c) {
1562 if (data)
1563 out_rawbyte(data, c);
1564 bytes++;
1565 }
1566 }
1567 return bytes;
1568}
1569
1570static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001571{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001572 uint8_t c;
1573 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001574 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001575 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001576 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001577 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001578 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001579 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001580 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001581 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001582 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001583
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001584 ins->rex_done = false;
1585
H. Peter Anvina77692b2016-09-20 14:04:33 -07001586 emit_prefix(data, bits, ins);
1587
H. Peter Anvin839eca22007-10-29 23:12:47 -07001588 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001589 c = *codes++;
1590 op1 = (c & 3) + ((opex & 1) << 2);
1591 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1592 opx = &ins->oprs[op1];
1593 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001594
H. Peter Anvina77692b2016-09-20 14:04:33 -07001595
H. Peter Anvin839eca22007-10-29 23:12:47 -07001596 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 case 01:
1598 case 02:
1599 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001600 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001601 emit_rex(data, ins);
1602 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001605
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001606 case 05:
1607 case 06:
1608 case 07:
1609 opex = c;
1610 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001611
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001612 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001613 emit_rex(data, ins);
1614 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001616
Jin Kyu Song164d6072013-10-15 19:10:13 -07001617 case4(014):
1618 break;
1619
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001620 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001621 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001623
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001624 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001625 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001627
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001628 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001629 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001631
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001632 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001633 if (opx->type & (BITS16 | BITS32))
1634 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 else
1636 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001637 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001639
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001640 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001641 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001643
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001644 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001645 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001646 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001648
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001649 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001650 if (opx->segment == data->segment) {
1651 int64_t delta = opx->offset - data->offset
1652 - (data->inslen - data->insoffs);
1653 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001654 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001655 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001656 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001658
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001659 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001660 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001661 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001662
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001663 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001664 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001666
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001667 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001668 if (opx->type & (BITS16 | BITS32 | BITS64))
1669 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 else
1671 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001672
1673 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001675
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001676 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001677 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001678 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001679
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001680 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001681 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001682 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001683 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001684 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001685 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001686
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001687 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001688 {
1689 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1690 const struct operand *opy;
1691
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001692 c = *codes++;
1693 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001694 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001695 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001696 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001697 "non-absolute expression not permitted as argument %d",
1698 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001699 } else if (opy->offset & ~mask) {
1700 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1701 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001702 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001703 c = opy->offset & mask;
1704 goto emit_is4;
1705 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001706
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001707 case 0173:
1708 c = *codes++;
1709 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001710 c &= 15;
1711 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001712
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001713 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001714 c = 0;
1715 emit_is4:
1716 r = nasm_regvals[opx->basereg];
1717 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001718 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001719
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001720 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001721 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001722 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001723 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001724 "signed dword immediate exceeds bounds");
1725 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001726 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001727 break;
1728
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001729 case4(0240):
1730 case 0250:
1731 codes += 3;
1732 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1733 EVEX_P2Z | EVEX_P2AAA, 2);
1734 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1735 bytes[0] = 0x62;
1736 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001737 bytes[1] = ((((ins->rex & 7) << 5) |
1738 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001739 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001740 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1741 ((~ins->vexreg & 15) << 3) |
1742 (1 << 2) | (ins->vex_wlp & 3);
1743 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001744 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001745 break;
1746
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001747 case4(0260):
1748 case 0270:
1749 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001750 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1751 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001752 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1753 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1754 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001755 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001756 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001757 } else {
1758 bytes[0] = 0xc5;
1759 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001760 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001761 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001762 }
1763 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001764
H. Peter Anvine014f352012-02-25 22:35:19 -08001765 case 0271:
1766 case 0272:
1767 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001768 break;
1769
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001770 case4(0274):
1771 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001772 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001773 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001774
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001775 if (absolute_op(opx)) {
1776 if (ins->rex & REX_W)
1777 s = 64;
1778 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1779 s = 16;
1780 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1781 s = 32;
1782 else
1783 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001784
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001785 um = (uint64_t)2 << (s-1);
1786 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001787
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001788 if (uv > 127 && uv < (uint64_t)-128 &&
1789 (uv < um-128 || uv > um-1)) {
1790 /* If this wasn't explicitly byte-sized, warn as though we
1791 * had fallen through to the imm16/32/64 case.
1792 */
1793 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1794 "%s value exceeds bounds",
1795 (opx->type & BITS8) ? "signed byte" :
1796 s == 16 ? "word" :
1797 s == 32 ? "dword" :
1798 "signed dword");
1799 }
1800
1801 /* Output as a raw byte to avoid byte overflow check */
1802 out_rawbyte(data, (uint8_t)uv);
1803 } else {
1804 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001805 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001806 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001807 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001808
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001809 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001811
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001813 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1814 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001816
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001818 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1819 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001821
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 case 0312:
1823 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001824
Keith Kaniosb7a89542007-04-12 02:40:54 +00001825 case 0313:
1826 ins->rex = 0;
1827 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001828
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001829 case4(0314):
1830 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001831
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001833 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001835
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001837 case 0323:
1838 break;
1839
Keith Kaniosb7a89542007-04-12 02:40:54 +00001840 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001841 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001842 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001843
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001844 case 0325:
1845 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001846
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001847 case 0326:
1848 break;
1849
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001851 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001853
H. Peter Anvine2c80182005-01-15 22:15:51 +00001854 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001856
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001857 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001859 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001861
Keith Kanios48af1772007-08-17 07:37:52 +00001862 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001863 if (ins->rex & REX_R)
1864 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001865 ins->rex &= ~(REX_L|REX_R);
1866 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001867
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001868 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001869 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001870
H. Peter Anvin962e3052008-08-28 17:47:16 -07001871 case 0336:
1872 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001873 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001874
H. Peter Anvine2c80182005-01-15 22:15:51 +00001875 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001877 nasm_panic("non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001878
1879 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001881
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001882 case 0341:
1883 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001884
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001885 case 0360:
1886 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001887
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001888 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001889 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001890 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001891
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001892 case 0364:
1893 case 0365:
1894 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001895
Keith Kanios48af1772007-08-17 07:37:52 +00001896 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001897 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001898 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001899 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001900
Jin Kyu Song03041092013-10-15 19:38:51 -07001901 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001902 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001903
H. Peter Anvine2c80182005-01-15 22:15:51 +00001904 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001905 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001906 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001907
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001908 case 0374:
1909 eat = EA_XMMVSIB;
1910 break;
1911
1912 case 0375:
1913 eat = EA_YMMVSIB;
1914 break;
1915
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001916 case 0376:
1917 eat = EA_ZMMVSIB;
1918 break;
1919
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001920 case4(0100):
1921 case4(0110):
1922 case4(0120):
1923 case4(0130):
1924 case4(0200):
1925 case4(0204):
1926 case4(0210):
1927 case4(0214):
1928 case4(0220):
1929 case4(0224):
1930 case4(0230):
1931 case4(0234):
1932 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 ea ea_data;
1934 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001935 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001936 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001937 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001938
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001939 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001940 /* pick rfield from operand b (opx) */
1941 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001942 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001943 } else {
1944 /* rfield is constant */
1945 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001946 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001947 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001948
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001949 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001950 rfield, rflags, ins, &errmsg) != eat)
1951 nasm_error(ERR_NONFATAL, "%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07001952
H. Peter Anvine2c80182005-01-15 22:15:51 +00001953 p = bytes;
1954 *p++ = ea_data.modrm;
1955 if (ea_data.sib_present)
1956 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001957 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001959 /*
1960 * Make sure the address gets the right offset in case
1961 * the line breaks in the .lst file (BR 1197827)
1962 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001963
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001964 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001965 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001966 if (ea_data.disp8) {
1967 out_rawbyte(data, ea_data.disp8);
1968 } else if (ea_data.rip) {
1969 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001970 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001971 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001972
H. Peter Anvina77692b2016-09-20 14:04:33 -07001973 if (overflow_general(opy->offset, asize) ||
1974 signed_bits(opy->offset, ins->addr_size) !=
1975 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001976 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001977
H. Peter Anvina77692b2016-09-20 14:04:33 -07001978 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001979 (asize > ea_data.bytes)
1980 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001981 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001982 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001983 }
1984 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001985
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001986 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001987 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001988 ": instruction code \\%o (0x%02X) given", c, c);
1989 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001990 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001991 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001992}
1993
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001994static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001995{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001996 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001997 nasm_panic("invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001998 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001999}
2000
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00002001static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002002{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002003 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002004 nasm_panic("invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07002005 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002006}
2007
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002008static int op_rexflags(const operand * o, int mask)
2009{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002010 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002011 int val;
2012
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002013 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07002014 nasm_panic("invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002015
H. Peter Anvina4835d42008-05-20 14:21:29 -07002016 flags = nasm_reg_flags[o->basereg];
2017 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002018
2019 return rexflags(val, flags, mask);
2020}
2021
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002022static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002023{
2024 int rex = 0;
2025
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002026 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002027 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002028 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002029 rex |= REX_W;
2030 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2031 rex |= REX_H;
2032 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2033 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002034
2035 return rex & mask;
2036}
2037
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002038static int evexflags(int val, decoflags_t deco,
2039 int mask, uint8_t byte)
2040{
2041 int evex = 0;
2042
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002043 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002044 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002045 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002046 evex |= (EVEX_P0RP | EVEX_P0X);
2047 break;
2048 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002049 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002050 evex |= EVEX_P2VP;
2051 if (deco & Z)
2052 evex |= EVEX_P2Z;
2053 if (deco & OPMASK_MASK)
2054 evex |= deco & EVEX_P2AAA;
2055 break;
2056 }
2057 return evex & mask;
2058}
2059
2060static int op_evexflags(const operand * o, int mask, uint8_t byte)
2061{
2062 int val;
2063
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002064 val = nasm_regvals[o->basereg];
2065
2066 return evexflags(val, o->decoflags, mask, byte);
2067}
2068
H. Peter Anvin23595f52009-07-25 17:44:25 -07002069static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002070 insn *instruction,
2071 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002072{
2073 const struct itemplate *temp;
2074 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002075 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002076 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002077 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002078 int i;
2079
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002080 /* broadcasting uses a different data element size */
2081 for (i = 0; i < instruction->operands; i++)
2082 if (i == broadcast)
2083 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2084 else
2085 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002086
2087 merr = MERR_INVALOP;
2088
2089 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002090 temp->opcode != I_none; temp++) {
2091 m = matches(temp, instruction, bits);
2092 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002093 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002094 m = MOK_GOOD;
2095 else
2096 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002097 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002098 /*
2099 * Missing operand size and a candidate for fuzzy matching...
2100 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002101 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002102 if (i == broadcast)
2103 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2104 else
2105 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002106 opsizemissing = true;
2107 }
2108 if (m > merr)
2109 merr = m;
2110 if (merr == MOK_GOOD)
2111 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002112 }
2113
2114 /* No match, but see if we can get a fuzzy operand size match... */
2115 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002116 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002117
2118 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002119 /*
2120 * We ignore extrinsic operand sizes on registers, so we should
2121 * never try to fuzzy-match on them. This also resolves the case
2122 * when we have e.g. "xmmrm128" in two different positions.
2123 */
2124 if (is_class(REGISTER, instruction->oprs[i].type))
2125 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002126
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002127 /* This tests if xsizeflags[i] has more than one bit set */
2128 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2129 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002130
Jin Kyu Song7903c072013-10-30 03:00:12 -07002131 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002132 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002133 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2134 BITS32 : BITS64);
2135 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002136 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002137 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002138 }
2139
2140 /* Try matching again... */
2141 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002142 temp->opcode != I_none; temp++) {
2143 m = matches(temp, instruction, bits);
2144 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002145 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002146 m = MOK_GOOD;
2147 else
2148 m = MERR_INVALOP;
2149 }
2150 if (m > merr)
2151 merr = m;
2152 if (merr == MOK_GOOD)
2153 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002154 }
2155
H. Peter Anvina81655b2009-07-25 18:15:28 -07002156done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002157 *tempp = temp;
2158 return merr;
2159}
2160
Mark Charneydcaef4b2014-10-09 13:45:17 -04002161static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2162{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002163 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002164 uint8_t brcast_num;
2165
Mark Charneydcaef4b2014-10-09 13:45:17 -04002166 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002167 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002168 "size of broadcasting element is greater than 64 bits");
2169
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002170 /*
2171 * The shift term is to take care of the extra BITS80 inserted
2172 * between BITS64 and BITS128.
2173 */
2174 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2175 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002176
2177 return brcast_num;
2178}
2179
H. Peter Anvin65289e82009-07-25 17:25:11 -07002180static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002181 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002182{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002183 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002184 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002185 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002186
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002187 /*
2188 * Check the opcode
2189 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002190 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002191 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002192
2193 /*
2194 * Count the operands
2195 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002196 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002197 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002198
2199 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002200 * Is it legal?
2201 */
Chang S. Baea5786342018-08-15 23:22:21 +03002202 if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002203 return MERR_INVALOP;
2204
2205 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002206 * {evex} available?
2207 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002208 switch (instruction->prefixes[PPS_VEX]) {
2209 case P_EVEX:
2210 if (!itemp_has(itemp, IF_EVEX))
2211 return MERR_ENCMISMATCH;
2212 break;
2213 case P_VEX3:
2214 case P_VEX2:
2215 if (!itemp_has(itemp, IF_VEX))
2216 return MERR_ENCMISMATCH;
2217 break;
2218 default:
2219 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002220 }
2221
2222 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002223 * Check that no spurious colons or TOs are present
2224 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002225 for (i = 0; i < itemp->operands; i++)
2226 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002227 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002228
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002229 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002230 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002231 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002232 switch (itemp_smask(itemp)) {
2233 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002234 asize = BITS8;
2235 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002236 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002237 asize = BITS16;
2238 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002239 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002240 asize = BITS32;
2241 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002242 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002243 asize = BITS64;
2244 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002245 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002246 asize = BITS128;
2247 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002248 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002249 asize = BITS256;
2250 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002251 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002252 asize = BITS512;
2253 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002254 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002255 switch (bits) {
2256 case 16:
2257 asize = BITS16;
2258 break;
2259 case 32:
2260 asize = BITS32;
2261 break;
2262 case 64:
2263 asize = BITS64;
2264 break;
2265 default:
2266 asize = 0;
2267 break;
2268 }
2269 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002270 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002271 asize = 0;
2272 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002273 }
2274
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002275 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002276 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002277 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002278 memset(size, 0, sizeof size);
2279 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002280 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002281 /* S- flags apply to all operands */
2282 for (i = 0; i < MAX_OPERANDS; i++)
2283 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002284 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002285
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002286 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002287 * Check that the operand flags all match up,
2288 * it's a bit tricky so lets be verbose:
2289 *
2290 * 1) Find out the size of operand. If instruction
2291 * doesn't have one specified -- we're trying to
2292 * guess it either from template (IF_S* flag) or
2293 * from code bits.
2294 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002295 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002296 * template has an operand size specified AND this size differ
2297 * from which instruction has (perhaps we got it from code bits)
2298 * we are:
2299 * a) Check that only size of instruction and operand is differ
2300 * other characteristics do match
2301 * b) Perhaps it's a register specified in instruction so
2302 * for such a case we just mark that operand as "size
2303 * missing" and this will turn on fuzzy operand size
2304 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002305 */
2306 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002307 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002308 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002309 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002310 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002311 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002312 opflags_t template_opsize, insn_opsize;
2313
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002314 if (!(type & SIZE_MASK))
2315 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002316
Jin Kyu Song7903c072013-10-30 03:00:12 -07002317 insn_opsize = type & SIZE_MASK;
2318 if (!is_broadcast) {
2319 template_opsize = itemp->opd[i] & SIZE_MASK;
2320 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002321 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2322
2323 if (~ideco & BRDCAST_MASK)
2324 return MERR_BRNOTHERE;
2325
Jin Kyu Song7903c072013-10-30 03:00:12 -07002326 /*
2327 * when broadcasting, the element size depends on
2328 * the instruction type. decorator flag should match.
2329 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002330 if (deco_brsize) {
2331 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002332 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002333 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002334 } else {
2335 template_opsize = 0;
2336 }
2337 }
2338
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002339 if (~ideco & deco & OPMASK_MASK)
2340 return MERR_MASKNOTHERE;
2341
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002342 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2343 return MERR_DECONOTHERE;
2344
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002345 if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002346 return MERR_INVALOP;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002347
2348 if (~itemp->opd[i] & type & REGSET_MASK)
2349 return (itemp->opd[i] & REGSET_MASK)
2350 ? MERR_REGSETSIZE : MERR_REGSET;
2351
2352 if (template_opsize) {
Jin Kyu Song7903c072013-10-30 03:00:12 -07002353 if (template_opsize != insn_opsize) {
2354 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002355 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002356 } else if (!is_class(REGISTER, type)) {
2357 /*
2358 * Note: we don't honor extrinsic operand sizes for registers,
2359 * so "missing operand size" for a register should be
2360 * considered a wildcard match rather than an error.
2361 */
2362 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002363 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002364 } else if (is_broadcast &&
2365 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002366 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002367 /*
2368 * broadcasting opsize matches but the number of repeated memory
2369 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002370 * if 64b double precision float is broadcasted to ymm (256b),
2371 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002372 */
2373 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002374 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002375 }
2376 }
2377
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002378 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002379 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002380
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002381 /*
2382 * Check operand sizes
2383 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002384 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2385 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002387 asize = itemp->opd[i] & SIZE_MASK;
2388 if (asize) {
2389 for (i = 0; i < oprs; i++)
2390 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391 break;
2392 }
2393 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002394 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002395 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002396 }
2397
Keith Kaniosb7a89542007-04-12 02:40:54 +00002398 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002399 if (!(itemp->opd[i] & SIZE_MASK) &&
2400 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002401 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002402 }
2403
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002404 /*
2405 * Check template is okay at the set cpu level
2406 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002407 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002408 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002409
Keith Kaniosb7a89542007-04-12 02:40:54 +00002410 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002411 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002412 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002413 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002414 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002415
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002416 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002417 * If we have a HLE prefix, look for the NOHLE flag
2418 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002419 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002420 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2421 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2422 return MERR_BADHLE;
2423
2424 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002425 * Check if special handling needed for Jumps
2426 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002427 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002428 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002429
Jin Kyu Song03041092013-10-15 19:38:51 -07002430 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002431 * Check if BND prefix is allowed.
2432 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002433 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002434 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002435 (has_prefix(instruction, PPS_REP, P_BND) ||
2436 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002437 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002438 else if (itemp_has(itemp, IF_BND) &&
2439 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2440 has_prefix(instruction, PPS_REP, P_REPNZ)))
2441 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002442
H. Peter Anvin60926242009-07-26 16:25:38 -07002443 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002444}
2445
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002446/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002447 * Check if ModR/M.mod should/can be 01.
2448 * - EAF_BYTEOFFS is set
2449 * - offset can fit in a byte when EVEX is not used
2450 * - offset can be compressed when EVEX is used
2451 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002452#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2453 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2454 is_disp8n(input, ins, &output->disp8) : \
2455 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2456 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002457
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002458static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002459 int rfield, opflags_t rflags, insn *ins,
2460 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002461{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002462 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002463 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002464 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002465
H. Peter Anvin8f622462017-04-02 19:02:29 -07002466 *errmsg = "invalid effective address"; /* Default error message */
2467
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002468 output->type = EA_SCALAR;
2469 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002470 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002471
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002472 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002473 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002474 /* EVEX.R' flag for the REG operand */
2475 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002476
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002477 if (is_class(REGISTER, input->type)) {
2478 /*
2479 * It's a direct register.
2480 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002481 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002482 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002483
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002484 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002485 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002486
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002487 /* broadcasting is not available with a direct register operand. */
2488 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002489 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002490 goto err;
2491 }
2492
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002493 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002494 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002495 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002496 output->bytes = 0; /* no offset necessary either */
2497 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2498 } else {
2499 /*
2500 * It's a memory reference.
2501 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002502
2503 /* Embedded rounding or SAE is not available with a mem ref operand. */
2504 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002505 *errmsg = "embedded rounding is available only with "
2506 "register-register operations";
2507 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002508 }
2509
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002510 if (input->basereg == -1 &&
2511 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002512 /*
2513 * It's a pure offset.
2514 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002515 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002516 if (input->segment == NO_SEG ||
2517 (input->opflags & OPFLAG_RELATIVE)) {
2518 nasm_error(ERR_WARNING | ERR_PASS2,
2519 "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002520 input->type &= ~IP_REL;
2521 input->type |= MEMORY;
2522 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002523 }
2524
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002525 if (bits == 64 &&
2526 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002527 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002528 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002529 }
2530
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002531 if (eaflags & EAF_BYTEOFFS ||
2532 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002533 input->disp_size != (addrbits != 16 ? 32 : 16))) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002534 nasm_error(ERR_WARNING | ERR_PASS1,
2535 "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002536 }
2537
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002538 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002539 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002540 output->sib = GEN_SIB(0, 4, 5);
2541 output->bytes = 4;
2542 output->modrm = GEN_MODRM(0, rfield, 4);
2543 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002544 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002545 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002546 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002547 output->modrm = GEN_MODRM(0, rfield,
2548 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002549 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002550 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002551 } else {
2552 /*
2553 * It's an indirection.
2554 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002555 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002556 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002557 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002558 int t, it, bt; /* register numbers */
2559 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002560
H. Peter Anvine2c80182005-01-15 22:15:51 +00002561 if (s == 0)
2562 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002563
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002564 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002565 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002566 ix = nasm_reg_flags[i];
2567 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002568 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002569 ix = 0;
2570 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002571
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002572 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002573 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002574 bx = nasm_reg_flags[b];
2575 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002576 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002577 bx = 0;
2578 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002579
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002580 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002581 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002582 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002583 int32_t o = input->offset;
2584 int mod, scale, index, base;
2585
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002586 /*
2587 * For a vector SIB, one has to be a vector and the other,
2588 * if present, a GPR. The vector must be the index operand.
2589 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002590 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002591 if (s == 0)
2592 s = 1;
2593 else if (s != 1)
2594 goto err;
2595
2596 t = bt, bt = it, it = t;
2597 x = bx, bx = ix, ix = x;
2598 }
2599
2600 if (bt != -1) {
2601 if (REG_GPR & ~bx)
2602 goto err;
2603 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2604 sok &= bx;
2605 else
2606 goto err;
2607 }
2608
2609 /*
2610 * While we're here, ensure the user didn't specify
2611 * WORD or QWORD
2612 */
2613 if (input->disp_size == 16 || input->disp_size == 64)
2614 goto err;
2615
2616 if (addrbits == 16 ||
2617 (addrbits == 32 && !(sok & BITS32)) ||
2618 (addrbits == 64 && !(sok & BITS64)))
2619 goto err;
2620
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002621 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2622 : ((ix & YMMREG & ~REG_EA)
2623 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002624
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002625 output->rex |= rexflags(it, ix, REX_X);
2626 output->rex |= rexflags(bt, bx, REX_B);
2627 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002628
2629 index = it & 7; /* it is known to be != -1 */
2630
2631 switch (s) {
2632 case 1:
2633 scale = 0;
2634 break;
2635 case 2:
2636 scale = 1;
2637 break;
2638 case 4:
2639 scale = 2;
2640 break;
2641 case 8:
2642 scale = 3;
2643 break;
2644 default: /* then what the smeg is it? */
2645 goto err; /* panic */
2646 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002647
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002648 if (bt == -1) {
2649 base = 5;
2650 mod = 0;
2651 } else {
2652 base = (bt & 7);
2653 if (base != REG_NUM_EBP && o == 0 &&
2654 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002655 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002656 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002657 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002658 mod = 1;
2659 else
2660 mod = 2;
2661 }
2662
2663 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002664 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2665 output->modrm = GEN_MODRM(mod, rfield, 4);
2666 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002667 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002668 /*
2669 * it must be a 32/64-bit memory reference. Firstly we have
2670 * to check that all registers involved are type E/Rxx.
2671 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002672 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002673 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002674
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002675 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002676 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2677 sok &= ix;
2678 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002679 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002680 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002681
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002682 if (bt != -1) {
2683 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002684 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002685 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002686 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002687 sok &= bx;
2688 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002689
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002690 /*
2691 * While we're here, ensure the user didn't specify
2692 * WORD or QWORD
2693 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002694 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002695 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002696
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002697 if (addrbits == 16 ||
2698 (addrbits == 32 && !(sok & BITS32)) ||
2699 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002700 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002701
Keith Kaniosb7a89542007-04-12 02:40:54 +00002702 /* now reorganize base/index */
2703 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002704 ((hb == b && ht == EAH_NOTBASE) ||
2705 (hb == i && ht == EAH_MAKEBASE))) {
2706 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002707 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002708 x = bx, bx = ix, ix = x;
2709 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002710
Jin Kyu Song164d6072013-10-15 19:10:13 -07002711 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002712 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002713 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002714 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002715 if (eaflags & EAF_MIB) {
2716 /* only for mib operands */
2717 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2718 /*
2719 * make a single reg index [reg*1].
2720 * gas uses this form for an explicit index register.
2721 */
2722 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2723 }
2724 if ((ht == EAH_SUMMED) && bt == -1) {
2725 /* separate once summed index into [base, index] */
2726 bt = it, bx = ix, s--;
2727 }
2728 } else {
2729 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002730 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002731 s == 3 || s == 5 || s == 9) && bt == -1) {
2732 /* convert 3*EAX to EAX+2*EAX */
2733 bt = it, bx = ix, s--;
2734 }
2735 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002736 (eaflags & EAF_TIMESTWO) &&
2737 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002738 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002739 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002740 * to sib format with 0x0 displacement - [EAX*1+0].
2741 */
2742 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2743 }
2744 }
Keith Kanios48af1772007-08-17 07:37:52 +00002745 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002746 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002747 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002748 x = ix, ix = bx, bx = x;
2749 }
2750 if (it == REG_NUM_ESP ||
2751 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002752 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002753
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002754 output->rex |= rexflags(it, ix, REX_X);
2755 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002756
Keith Kanios48af1772007-08-17 07:37:52 +00002757 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002758 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002760
Keith Kaniosb7a89542007-04-12 02:40:54 +00002761 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002764 } else {
2765 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002766 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002767 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002768 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002769 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002770 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002771 mod = 1;
2772 else
2773 mod = 2;
2774 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002775
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002776 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002777 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2778 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002779 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002780 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002781 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002782
Keith Kaniosb7a89542007-04-12 02:40:54 +00002783 if (it == -1)
2784 index = 4, s = 1;
2785 else
2786 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002787
H. Peter Anvine2c80182005-01-15 22:15:51 +00002788 switch (s) {
2789 case 1:
2790 scale = 0;
2791 break;
2792 case 2:
2793 scale = 1;
2794 break;
2795 case 4:
2796 scale = 2;
2797 break;
2798 case 8:
2799 scale = 3;
2800 break;
2801 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002802 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002804
Keith Kaniosb7a89542007-04-12 02:40:54 +00002805 if (bt == -1) {
2806 base = 5;
2807 mod = 0;
2808 } else {
2809 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002810 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002811 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002812 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002813 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002814 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002815 mod = 1;
2816 else
2817 mod = 2;
2818 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002819
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002820 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002821 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2822 output->modrm = GEN_MODRM(mod, rfield, 4);
2823 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002824 }
2825 } else { /* it's 16-bit */
2826 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002827 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002828
Keith Kaniosb7a89542007-04-12 02:40:54 +00002829 /* check for 64-bit long mode */
2830 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002831 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002832
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002834 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2835 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002836 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002837
Keith Kaniosb7a89542007-04-12 02:40:54 +00002838 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002839 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002840 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002841
H. Peter Anvine2c80182005-01-15 22:15:51 +00002842 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002843 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002844 if (b == -1 && i != -1) {
2845 int tmp = b;
2846 b = i;
2847 i = tmp;
2848 } /* swap */
2849 if ((b == R_SI || b == R_DI) && i != -1) {
2850 int tmp = b;
2851 b = i;
2852 i = tmp;
2853 }
2854 /* have BX/BP as base, SI/DI index */
2855 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002856 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 if (i != -1 && b != -1 &&
2858 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002859 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002860 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002861 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002862
H. Peter Anvine2c80182005-01-15 22:15:51 +00002863 rm = -1;
2864 if (i != -1)
2865 switch (i * 256 + b) {
2866 case R_SI * 256 + R_BX:
2867 rm = 0;
2868 break;
2869 case R_DI * 256 + R_BX:
2870 rm = 1;
2871 break;
2872 case R_SI * 256 + R_BP:
2873 rm = 2;
2874 break;
2875 case R_DI * 256 + R_BP:
2876 rm = 3;
2877 break;
2878 } else
2879 switch (b) {
2880 case R_SI:
2881 rm = 4;
2882 break;
2883 case R_DI:
2884 rm = 5;
2885 break;
2886 case R_BP:
2887 rm = 6;
2888 break;
2889 case R_BX:
2890 rm = 7;
2891 break;
2892 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002893 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002894 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002895
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002896 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002897 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002898 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002899 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002900 mod = 1;
2901 else
2902 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002903
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002904 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002905 output->bytes = mod; /* bytes of offset needed */
2906 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002907 }
2908 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002909 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002910
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002911 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002912 return output->type;
2913
2914err:
2915 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002916}
2917
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002918static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002919{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002920 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002921 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002922
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002923 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002924
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002925 switch (ins->prefixes[PPS_ASIZE]) {
2926 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002927 valid &= 16;
2928 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002929 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002930 valid &= 32;
2931 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002932 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002933 valid &= 64;
2934 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002935 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002936 valid &= (addrbits == 32) ? 16 : 32;
2937 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002938 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002939 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002940 }
2941
2942 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002943 if (is_class(MEMORY, ins->oprs[j].type)) {
2944 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002945
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002946 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002947 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002948 i = 0;
2949 else
2950 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002951
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002952 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002953 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002954 b = 0;
2955 else
2956 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002957
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002958 if (ins->oprs[j].scale == 0)
2959 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002960
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002961 if (!i && !b) {
2962 int ds = ins->oprs[j].disp_size;
2963 if ((addrbits != 64 && ds > 8) ||
2964 (addrbits == 64 && ds == 16))
2965 valid &= ds;
2966 } else {
2967 if (!(REG16 & ~b))
2968 valid &= 16;
2969 if (!(REG32 & ~b))
2970 valid &= 32;
2971 if (!(REG64 & ~b))
2972 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002973
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002974 if (!(REG16 & ~i))
2975 valid &= 16;
2976 if (!(REG32 & ~i))
2977 valid &= 32;
2978 if (!(REG64 & ~i))
2979 valid &= 64;
2980 }
2981 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002982 }
2983
2984 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002985 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002986 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002987 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002988 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002989 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002990 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002991 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002992 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002993 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002994 }
2995
2996 defdisp = ins->addr_size == 16 ? 16 : 32;
2997
2998 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002999 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
3000 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
3001 /*
3002 * mem_offs sizes must match the address size; if not,
3003 * strip the MEM_OFFS bit and match only EA instructions
3004 */
3005 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
3006 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00003007 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00003008}