blob: 6fd80274c0e55e1657fd759f995ec5cc95a6cb06 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvin164d2462017-02-20 02:39:56 -08003 * Copyright 1996-2017 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 Anvin65289e82009-07-25 17:25:11 -0700204 MERR_BADCPU,
205 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800206 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700207 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700208 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800209 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700210 /*
211 * Matching success; the conditional ones first
212 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400213 MOK_JUMP, /* Matching OK but needs jmp_match() */
214 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700215};
216
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000217typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700218 enum ea_type type; /* what kind of EA is this? */
219 int sib_present; /* is a SIB byte necessary? */
220 int bytes; /* # of bytes of offset needed */
221 int size; /* lazy - this is sib+bytes+1 */
222 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700223 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000224} ea;
225
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400226#define GEN_SIB(scale, index, base) \
227 (((scale) << 6) | ((index) << 3) | ((base)))
228
229#define GEN_MODRM(mod, reg, rm) \
230 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
231
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800232static int64_t calcsize(int32_t, int64_t, int, insn *,
233 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700234static int emit_prefix(struct out_data *data, const int bits, insn *ins);
235static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700236static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400237 insn *instruction,
238 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700239static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700240static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000241static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700242static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000243static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700244static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700245static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000246
H. Peter Anvin8f622462017-04-02 19:02:29 -0700247static enum ea_type process_ea(operand *, ea *, int, int,
248 opflags_t, insn *, const char **);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700249
H. Peter Anvin164d2462017-02-20 02:39:56 -0800250static inline bool absolute_op(const struct operand *o)
251{
252 return o->segment == NO_SEG && o->wrt == NO_SEG &&
253 !(o->opflags & OPFLAG_RELATIVE);
254}
255
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400256static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000257{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700258 return ins->prefixes[pos] == prefix;
259}
260
261static void assert_no_prefix(insn * ins, enum prefix_pos pos)
262{
263 if (ins->prefixes[pos])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800264 nasm_error(ERR_NONFATAL, "invalid %s prefix",
265 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700266}
267
268static const char *size_name(int size)
269{
270 switch (size) {
271 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700273 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700275 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400276 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700277 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400278 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700279 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400280 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700281 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400282 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700283 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400284 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700285 case 64:
286 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700287 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400288 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000289 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700290}
291
H. Peter Anvin285222f2017-03-01 13:27:33 -0800292static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400293{
H. Peter Anvin285222f2017-03-01 13:27:33 -0800294 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400295 "%s data exceeds bounds", size_name(size));
296}
297
298static void warn_overflow_const(int64_t data, int size)
299{
300 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800301 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400302}
303
304static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700305{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800306 if (absolute_op(o)) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400307 if (overflow_general(o->offset, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800308 warn_overflow(size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700309 }
310}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400311
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800312static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
313{
314 bool err;
315
316 switch (sign) {
317 case OUT_WRAP:
318 err = overflow_general(data, size);
319 break;
320 case OUT_SIGNED:
321 err = overflow_signed(data, size);
322 break;
323 case OUT_UNSIGNED:
324 err = overflow_unsigned(data, size);
325 break;
326 default:
327 panic();
328 break;
329 }
330
331 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800332 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800333}
334
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000335/*
336 * This routine wrappers the real output format's output routine,
337 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800338 * generator at the same time, flatten unnecessary relocations,
339 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000340 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700341static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000342{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000343 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700344 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700345 union {
346 uint8_t b[8];
347 uint64_t q;
348 } xdata;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700349 size_t asize, amax;
350 uint64_t zeropad = 0;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800351 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800352 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000353
H. Peter Anvina77692b2016-09-20 14:04:33 -0700354 if (!data->size)
355 return; /* Nothing to do */
356
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700357 /*
358 * Convert addresses to RAWDATA if possible
359 * XXX: not all backends want this for global symbols!!!!
360 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700361 switch (data->type) {
362 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800363 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800364 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800365 goto address;
366
367 case OUT_RELADDR:
368 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800369 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800370 goto address;
371
372 address:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700373 nasm_assert(data->size <= 8);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700374 asize = data->size;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700375 amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800376 if (data->tsegment == fixseg && data->twrt == NO_SEG) {
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800377 warn_overflow_out(addrval, asize, data->sign);
Martin Storsjö869087d2017-05-22 13:54:20 +0300378 xdata.q = cpu_to_le64(addrval);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700379 data->data = xdata.b;
380 data->type = OUT_RAWDATA;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700381 asize = amax = 0; /* No longer an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700382 }
383 break;
384
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700385 case OUT_SEGMENT:
386 nasm_assert(data->size <= 8);
387 asize = data->size;
388 amax = 2;
389 break;
390
H. Peter Anvina77692b2016-09-20 14:04:33 -0700391 default:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700392 asize = amax = 0; /* Not an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700393 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000394 }
395
Frank Kotlerabebb082003-09-06 04:45:37 +0000396 /*
397 * this call to src_get determines when we call the
398 * debug-format-specific "linenum" function
399 * it updates lineno and lnfname to the current values
400 * returning 0 if "same as last time", -2 if lnfname
401 * changed, and the amount by which lineno changed,
402 * if it did. thus, these variables must be static
403 */
404
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400405 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700406 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000407
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700408 if (asize > amax) {
409 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800410 nasm_error(ERR_NONFATAL,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700411 "%u-bit signed relocation unsupported by output format %s",
412 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800413 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800414 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700415 "%u-bit %s relocation zero-extended from %u bits",
416 (unsigned int)(asize << 3),
417 data->type == OUT_SEGMENT ? "segment" : "unsigned",
418 (unsigned int)(amax << 3));
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800419 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700420 zeropad = data->size - amax;
421 data->size = amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800422 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700423 lfmt->output(data);
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700424 ofmt->output(data);
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700425 data->offset += data->size;
426 data->insoffs += data->size;
427
428 if (zeropad) {
429 data->type = OUT_ZERODATA;
430 data->size = zeropad;
431 lfmt->output(data);
432 ofmt->output(data);
433 data->offset += zeropad;
434 data->insoffs += zeropad;
435 data->size += zeropad; /* Restore original size value */
436 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000437}
438
H. Peter Anvina77692b2016-09-20 14:04:33 -0700439static inline void out_rawdata(struct out_data *data, const void *rawdata,
440 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400441{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700442 data->type = OUT_RAWDATA;
443 data->data = rawdata;
444 data->size = size;
445 out(data);
446}
447
448static void out_rawbyte(struct out_data *data, uint8_t byte)
449{
450 data->type = OUT_RAWDATA;
451 data->data = &byte;
452 data->size = 1;
453 out(data);
454}
455
456static inline void out_reserve(struct out_data *data, uint64_t size)
457{
458 data->type = OUT_RESERVE;
459 data->size = size;
460 out(data);
461}
462
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700463static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700464{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700465 if (opx->opflags & OPFLAG_RELATIVE)
466 nasm_error(ERR_NONFATAL, "segment references cannot be relative");
467
468 data->type = OUT_SEGMENT;
469 data->sign = OUT_UNSIGNED;
470 data->size = 2;
471 data->toffset = opx->offset;
472 data->tsegment = ofmt->segbase(opx->segment | 1);
473 data->twrt = opx->wrt;
474 out(data);
475}
476
477static void out_imm(struct out_data *data, const struct operand *opx,
478 int size, enum out_sign sign)
479{
480 if (opx->segment != NO_SEG && (opx->segment & 1)) {
481 /*
482 * This is actually a segment reference, but eval() has
483 * already called ofmt->segbase() for us. Sigh.
484 */
485 if (size < 2)
486 nasm_error(ERR_NONFATAL, "segment reference must be 16 bits");
487
488 data->type = OUT_SEGMENT;
489 } else {
490 data->type = (opx->opflags & OPFLAG_RELATIVE)
491 ? OUT_RELADDR : OUT_ADDRESS;
492 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700493 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700494 data->toffset = opx->offset;
495 data->tsegment = opx->segment;
496 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800497 /*
498 * XXX: improve this if at some point in the future we can
499 * distinguish the subtrahend in expressions like [foo - bar]
500 * where bar is a symbol in the current segment. However, at the
501 * current point, if OPFLAG_RELATIVE is set that subtraction has
502 * already occurred.
503 */
504 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700505 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700506 out(data);
507}
508
H. Peter Anvin164d2462017-02-20 02:39:56 -0800509static void out_reladdr(struct out_data *data, const struct operand *opx,
510 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700511{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800512 if (opx->opflags & OPFLAG_RELATIVE)
513 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
514
H. Peter Anvina77692b2016-09-20 14:04:33 -0700515 data->type = OUT_RELADDR;
516 data->sign = OUT_SIGNED;
517 data->size = size;
518 data->toffset = opx->offset;
519 data->tsegment = opx->segment;
520 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800521 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700522 out(data);
523}
524
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700525static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800526 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000527{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800528 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800529 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000530 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800531 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000532
H. Peter Anvin755f5212012-02-25 11:41:34 -0800533 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700534 return false;
535 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400536 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700537 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400538 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700539
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800540 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100541
Victor van den Elzen154e5922009-02-25 17:32:00 +0100542 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100543 /* Be optimistic in pass 1 */
544 return true;
545
H. Peter Anvine2c80182005-01-15 22:15:51 +0000546 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700547 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000548
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700549 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800550 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
551
552 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
553 /* jmp short (opcode eb) cannot be used with bnd prefix. */
554 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800555 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800556 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800557 }
558
559 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000561
H. Peter Anvin04445362016-09-21 15:56:19 -0700562/* This is totally just a wild guess what is reasonable... */
563#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
564
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800565int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000566{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700567 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000568 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700569 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300570 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000571
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700572 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700573 data.offset = start;
574 data.segment = segment;
575 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700576 data.bits = bits;
577
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700578 wsize = db_bytes(instruction->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300579 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000581
H. Peter Anvineba20a72002-04-30 20:53:55 +0000582 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700584
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700585 list_for_each(e, instruction->eops) {
586 if (e->type == EOT_DB_NUMBER) {
587 if (wsize > 8) {
588 nasm_error(ERR_NONFATAL,
589 "integer supplied to a DT, DO, DY or DZ"
590 " instruction");
591 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700592 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700593 data.inslen = data.size = wsize;
594 data.toffset = e->offset;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700595 data.twrt = e->wrt;
596 data.relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700597 if (e->segment != NO_SEG && (e->segment & 1)) {
598 data.tsegment = e->segment;
599 data.type = OUT_SEGMENT;
600 data.sign = OUT_UNSIGNED;
601 } else {
602 data.tsegment = e->segment;
603 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
604 data.sign = OUT_WRAP;
605 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700606 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000607 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700608 } else if (e->type == EOT_DB_STRING ||
609 e->type == EOT_DB_STRING_FREE) {
610 int align = e->stringlen % wsize;
611 if (align)
612 align = wsize - align;
613
614 data.insoffs = 0;
615 data.inslen = e->stringlen + align;
616
617 out_rawdata(&data, e->stringval, e->stringlen);
618 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700619 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700621 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700622 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000623 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700624 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700625 off_t base = 0;
626 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700627 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700628 char *buf = NULL;
629 size_t blk = 0; /* Buffered I/O block size */
630 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000631
H. Peter Anvind81a2352016-09-21 14:03:18 -0700632 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400633 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800634 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700636 goto done;
637 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000638
H. Peter Anvind81a2352016-09-21 14:03:18 -0700639 len = nasm_file_size(fp);
640
641 if (len == (off_t)-1) {
642 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700643 fname);
644 goto close_done;
645 }
646
H. Peter Anvina77692b2016-09-20 14:04:33 -0700647 if (instruction->eops->next) {
648 base = instruction->eops->next->offset;
649 if (base >= len) {
650 len = 0;
651 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 len -= base;
653 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700654 len > (off_t)instruction->eops->next->next->offset)
655 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000657 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700658
H. Peter Anvina77692b2016-09-20 14:04:33 -0700659 lfmt->set_offset(data.offset);
660 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000661
H. Peter Anvind81a2352016-09-21 14:03:18 -0700662 if (!len)
663 goto end_incbin;
664
665 /* Try to map file data */
666 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700667 if (!map) {
668 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
669 buf = nasm_malloc(blk);
670 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700671
672 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700673 /*
674 * Consider these irrelevant for INCBIN, since it is fully
675 * possible that these might be (way) bigger than an int
676 * can hold; there is, however, no reason to widen these
677 * types just for INCBIN. data.inslen == 0 signals to the
678 * backend that these fields are meaningless, if at all
679 * needed.
680 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700681 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700682 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700683
H. Peter Anvind81a2352016-09-21 14:03:18 -0700684 if (map) {
685 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700686 } else if ((off_t)m == len) {
687 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700688 } else {
689 off_t l = len;
690
691 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700692 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700693 "`incbin': unable to seek on file `%s'",
694 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700695 goto end_incbin;
696 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700697 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700698 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700699 if (!m || feof(fp)) {
700 /*
701 * This shouldn't happen unless the file
702 * actually changes while we are reading
703 * it.
704 */
705 nasm_error(ERR_NONFATAL,
706 "`incbin': unexpected EOF while"
707 " reading file `%s'", fname);
708 goto end_incbin;
709 }
710 out_rawdata(&data, buf, m);
711 l -= m;
712 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700713 }
714 }
715 end_incbin:
716 lfmt->downlevel(LIST_INCBIN);
717 if (instruction->times > 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700718 lfmt->uplevel(LIST_TIMES);
719 lfmt->downlevel(LIST_TIMES);
720 }
721 if (ferror(fp)) {
722 nasm_error(ERR_NONFATAL,
723 "`incbin': error while"
724 " reading file `%s'", fname);
725 }
726 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700727 if (buf)
728 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700729 if (map)
730 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700731 fclose(fp);
732 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700733 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700734 ;
735 } else {
736 /* "Real" instruction */
737
738 /* Check to see if we need an address-size prefix */
739 add_asp(instruction, bits);
740
741 m = find_match(&temp, instruction, data.segment, data.offset, bits);
742
743 if (m == MOK_GOOD) {
744 /* Matches! */
745 int64_t insn_size = calcsize(data.segment, data.offset,
746 bits, instruction, temp);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700747 nasm_assert(insn_size >= 0);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700748
749 data.itemp = temp;
750 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700751 data.insoffs = 0;
752 data.inslen = insn_size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700753
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700754 gencode(&data, instruction);
755 nasm_assert(data.insoffs == insn_size);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700756 } else {
757 /* No match */
758 switch (m) {
759 case MERR_OPSIZEMISSING:
760 nasm_error(ERR_NONFATAL, "operation size not specified");
761 break;
762 case MERR_OPSIZEMISMATCH:
763 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
764 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700765 case MERR_BRNOTHERE:
766 nasm_error(ERR_NONFATAL,
767 "broadcast not permitted on this operand");
768 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700769 case MERR_BRNUMMISMATCH:
770 nasm_error(ERR_NONFATAL,
771 "mismatch in the number of broadcasting elements");
772 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700773 case MERR_MASKNOTHERE:
774 nasm_error(ERR_NONFATAL,
775 "mask not permitted on this operand");
776 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700777 case MERR_BADCPU:
778 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
779 break;
780 case MERR_BADMODE:
781 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
782 bits);
783 break;
784 case MERR_ENCMISMATCH:
785 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
786 break;
787 case MERR_BADBND:
788 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
789 break;
790 case MERR_BADREPNE:
791 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
792 (has_prefix(instruction, PPS_REP, P_REPNE) ?
793 "repne" : "repnz"));
794 break;
795 default:
796 nasm_error(ERR_NONFATAL,
797 "invalid combination of opcode and operands");
798 break;
799 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700800
801 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400802 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000803 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700804 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000805}
806
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800807int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000808{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000809 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700810 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000811
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400812 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000814
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700815 if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300817 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000818
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 isize = 0;
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700820 wsize = db_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700821 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000822
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400823 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000824 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000825
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400827 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400829 warn_overflow_const(e->offset, wsize);
830 } else if (e->type == EOT_DB_STRING ||
831 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000833
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 align = (-osize) % wsize;
835 if (align < 0)
836 align += wsize;
837 isize += osize + align;
838 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700839 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000840 }
841
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400843 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700844 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000845
H. Peter Anvind81a2352016-09-21 14:03:18 -0700846 len = nasm_file_size_by_path(fname);
847 if (len == (off_t)-1) {
848 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
849 fname);
850 return 0;
851 }
852
853 if (instruction->eops->next) {
854 if (len <= (off_t)instruction->eops->next->offset) {
855 len = 0;
856 } else {
857 len -= instruction->eops->next->offset;
858 if (instruction->eops->next->next &&
859 len > (off_t)instruction->eops->next->next->offset) {
860 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 }
862 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700864
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700865 len *= instruction->times;
866 instruction->times = 1; /* Tell the upper layer to not iterate */
867
H. Peter Anvind81a2352016-09-21 14:03:18 -0700868 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000869 }
870
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700871 /* Check to see if we need an address-size prefix */
872 add_asp(instruction, bits);
873
H. Peter Anvin23595f52009-07-25 17:44:25 -0700874 m = find_match(&temp, instruction, segment, offset, bits);
875 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400876 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700877 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700878 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400879 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000880 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000881}
882
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800883static void bad_hle_warn(const insn * ins, uint8_t hleok)
884{
885 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800886 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800887 static const enum whatwarn warn[2][4] =
888 {
889 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
890 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
891 };
892 unsigned int n;
893
894 n = (unsigned int)rep_pfx - P_XACQUIRE;
895 if (n > 1)
896 return; /* Not XACQUIRE/XRELEASE */
897
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800898 ww = warn[n][hleok];
899 if (!is_class(MEMORY, ins->oprs[0].type))
900 ww = w_inval; /* HLE requires operand 0 to be memory */
901
902 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800903 case w_none:
904 break;
905
906 case w_lock:
907 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800908 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800909 "%s with this instruction requires lock",
910 prefix_name(rep_pfx));
911 }
912 break;
913
914 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800915 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800916 "%s invalid with this instruction",
917 prefix_name(rep_pfx));
918 break;
919 }
920}
921
H. Peter Anvin507ae032008-10-09 15:37:10 -0700922/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400923#define case3(x) case (x): case (x)+1: case (x)+2
924#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700925
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800926static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800927 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000928{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800929 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800930 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000931 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000932 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700933 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700934 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700935 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700936 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800937 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800938 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700939 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -0700940 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000941
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700942 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700943 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700944 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700945
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700946 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400947 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700948
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 (void)segment; /* Don't warn that this parameter is unused */
950 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000951
H. Peter Anvin839eca22007-10-29 23:12:47 -0700952 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400953 c = *codes++;
954 op1 = (c & 3) + ((opex & 1) << 2);
955 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
956 opx = &ins->oprs[op1];
957 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700958
H. Peter Anvin839eca22007-10-29 23:12:47 -0700959 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400960 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 codes += c, length += c;
962 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700963
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400964 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400965 opex = c;
966 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700967
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400968 case4(010):
969 ins->rex |=
970 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 codes++, length++;
972 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700973
Jin Kyu Song164d6072013-10-15 19:10:13 -0700974 case4(014):
975 /* this is an index reg of MIB operand */
976 mib_index = opx->basereg;
977 break;
978
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400979 case4(020):
980 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 length++;
982 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700983
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400984 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 length += 2;
986 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700987
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400988 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700989 if (opx->type & (BITS16 | BITS32 | BITS64))
990 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 else
992 length += (bits == 16) ? 2 : 4;
993 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700994
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400995 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 length += 4;
997 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700998
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400999 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001000 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001002
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001003 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 length++;
1005 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001006
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001007 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001008 length += 8; /* MOV reg64/imm */
1009 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001010
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001011 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 length += 2;
1013 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001014
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001015 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001016 if (opx->type & (BITS16 | BITS32 | BITS64))
1017 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 else
1019 length += (bits == 16) ? 2 : 4;
1020 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001021
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001022 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 length += 4;
1024 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001025
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001026 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001027 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001029
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001030 case 0172:
1031 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001032 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001033 length++;
1034 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001035
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001036 case4(0174):
1037 length++;
1038 break;
1039
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001040 case4(0240):
1041 ins->rex |= REX_EV;
1042 ins->vexreg = regval(opx);
1043 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1044 ins->vex_cm = *codes++;
1045 ins->vex_wlp = *codes++;
1046 ins->evex_tuple = (*codes++ - 0300);
1047 break;
1048
1049 case 0250:
1050 ins->rex |= REX_EV;
1051 ins->vexreg = 0;
1052 ins->vex_cm = *codes++;
1053 ins->vex_wlp = *codes++;
1054 ins->evex_tuple = (*codes++ - 0300);
1055 break;
1056
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001057 case4(0254):
1058 length += 4;
1059 break;
1060
1061 case4(0260):
1062 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001063 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001064 ins->vex_cm = *codes++;
1065 ins->vex_wlp = *codes++;
1066 break;
1067
1068 case 0270:
1069 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001070 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001071 ins->vex_cm = *codes++;
1072 ins->vex_wlp = *codes++;
1073 break;
1074
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001075 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001076 hleok = c & 3;
1077 break;
1078
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001079 case4(0274):
1080 length++;
1081 break;
1082
1083 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001085
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001087 if (bits == 64)
1088 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001089 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001091
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001093 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001095
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001097 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001098
Keith Kaniosb7a89542007-04-12 02:40:54 +00001099 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001100 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1101 has_prefix(ins, PPS_ASIZE, P_A32))
1102 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001104
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001105 case4(0314):
1106 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001107
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001109 {
1110 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1111 if (pfx == P_O16)
1112 break;
1113 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001114 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001115 else
1116 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001118 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001119
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001121 {
1122 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1123 if (pfx == P_O32)
1124 break;
1125 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001126 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001127 else
1128 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001130 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001131
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 case 0322:
1133 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001134
Keith Kaniosb7a89542007-04-12 02:40:54 +00001135 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001136 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001137 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001138
Keith Kaniosb7a89542007-04-12 02:40:54 +00001139 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001140 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001141 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001142
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001143 case 0325:
1144 ins->rex |= REX_NH;
1145 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001146
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001147 case 0326:
1148 break;
1149
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 case 0330:
1151 codes++, length++;
1152 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001153
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001156
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001157 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 case 0333:
1159 length++;
1160 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001161
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001162 case 0334:
1163 ins->rex |= REX_L;
1164 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001165
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001166 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001167 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001168
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001169 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001170 if (!ins->prefixes[PPS_REP])
1171 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001172 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001173
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001174 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001175 if (!ins->prefixes[PPS_REP])
1176 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001177 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001178
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001180 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001181 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001183 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1184 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001185 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001187 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001189
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001190 case 0341:
1191 if (!ins->prefixes[PPS_WAIT])
1192 ins->prefixes[PPS_WAIT] = P_WAIT;
1193 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001194
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001195 case 0360:
1196 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001197
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001198 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001199 length++;
1200 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001201
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001202 case 0364:
1203 case 0365:
1204 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001205
Keith Kanios48af1772007-08-17 07:37:52 +00001206 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001207 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001208 length++;
1209 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001210
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001211 case 0370:
1212 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001214
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215 case 0373:
1216 length++;
1217 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001218
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001219 case 0374:
1220 eat = EA_XMMVSIB;
1221 break;
1222
1223 case 0375:
1224 eat = EA_YMMVSIB;
1225 break;
1226
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001227 case 0376:
1228 eat = EA_ZMMVSIB;
1229 break;
1230
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001231 case4(0100):
1232 case4(0110):
1233 case4(0120):
1234 case4(0130):
1235 case4(0200):
1236 case4(0204):
1237 case4(0210):
1238 case4(0214):
1239 case4(0220):
1240 case4(0224):
1241 case4(0230):
1242 case4(0234):
1243 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001245 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001246 opflags_t rflags;
1247 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001248 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001249
Keith Kaniosb7a89542007-04-12 02:40:54 +00001250 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001251
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001252 if (c <= 0177) {
1253 /* pick rfield from operand b (opx) */
1254 rflags = regflag(opx);
1255 rfield = nasm_regvals[opx->basereg];
1256 } else {
1257 rflags = 0;
1258 rfield = c & 7;
1259 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001260
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001261 /* EVEX.b1 : evex_brerop contains the operand position */
1262 op_er_sae = (ins->evex_brerop >= 0 ?
1263 &ins->oprs[ins->evex_brerop] : NULL);
1264
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001265 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1266 /* set EVEX.b */
1267 ins->evex_p[2] |= EVEX_P2B;
1268 if (op_er_sae->decoflags & ER) {
1269 /* set EVEX.RC (rounding control) */
1270 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1271 & EVEX_P2RC;
1272 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001273 } else {
1274 /* set EVEX.L'L (vector length) */
1275 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001276 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001277 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001278 /* set EVEX.b */
1279 ins->evex_p[2] |= EVEX_P2B;
1280 }
1281 }
1282
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001283 if (itemp_has(temp, IF_MIB)) {
1284 opy->eaflags |= EAF_MIB;
1285 /*
1286 * if a separate form of MIB (ICC style) is used,
1287 * the index reg info is merged into mem operand
1288 */
1289 if (mib_index != R_none) {
1290 opy->indexreg = mib_index;
1291 opy->scale = 1;
1292 opy->hintbase = mib_index;
1293 opy->hinttype = EAH_NOTBASE;
1294 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001295 }
1296
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001297 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001298 rfield, rflags, ins, &errmsg) != eat) {
1299 nasm_error(ERR_NONFATAL, "%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001301 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001302 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001304 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001305 }
1306 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001307
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001308 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001309 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001310 ": instruction code \\%o (0x%02X) given", c, c);
1311 break;
1312 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001313 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001314
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001315 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001316
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001317 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001318 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001319 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001320 return -1;
1321 }
1322 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001323 }
1324
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001325 switch (ins->prefixes[PPS_VEX]) {
1326 case P_EVEX:
1327 if (!(ins->rex & REX_EV))
1328 return -1;
1329 break;
1330 case P_VEX3:
1331 case P_VEX2:
1332 if (!(ins->rex & REX_V))
1333 return -1;
1334 break;
1335 default:
1336 break;
1337 }
1338
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001339 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001340 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001341
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001342 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001343 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001344 return -1;
1345 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001346 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001347 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001348 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001349 ins->rex &= ~REX_W;
1350 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001351 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001352 ins->rex |= REX_W;
1353 bad32 &= ~REX_W;
1354 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001355 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001356 /* Follow REX_W */
1357 break;
1358 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001359
H. Peter Anvinfc561202011-07-07 16:58:22 -07001360 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001361 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001362 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001363 } else if (!(ins->rex & REX_EV) &&
1364 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001365 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001366 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001367 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001368 if (ins->rex & REX_EV)
1369 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001370 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1371 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001372 length += 3;
1373 else
1374 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001375 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001376 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001377 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001378 return -1;
1379 } else if (bits == 64) {
1380 length++;
1381 } else if ((ins->rex & REX_L) &&
1382 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001383 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001384 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001385 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001386 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001387 length++;
1388 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001389 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001390 return -1;
1391 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001392 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001393
1394 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001395 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001396 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001397 "instruction is not lockable");
1398 }
1399
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001400 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001401
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001402 /*
1403 * when BND prefix is set by DEFAULT directive,
1404 * BND prefix is added to every appropriate instruction line
1405 * unless it is overridden by NOBND prefix.
1406 */
1407 if (globalbnd &&
1408 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1409 ins->prefixes[PPS_REP] = P_BND;
1410
H. Peter Anvina77692b2016-09-20 14:04:33 -07001411 /*
1412 * Add length of legacy prefixes
1413 */
1414 length += emit_prefix(NULL, bits, ins);
1415
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001416 return length;
1417}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001418
H. Peter Anvina77692b2016-09-20 14:04:33 -07001419static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001420{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001421 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001422 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001423 !(ins->rex & (REX_V | REX_EV)) &&
1424 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001425 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1426 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001427 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001428 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001429 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001430}
1431
H. Peter Anvina77692b2016-09-20 14:04:33 -07001432static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1433{
1434 int bytes = 0;
1435 int j;
1436
1437 for (j = 0; j < MAXPREFIX; j++) {
1438 uint8_t c = 0;
1439 switch (ins->prefixes[j]) {
1440 case P_WAIT:
1441 c = 0x9B;
1442 break;
1443 case P_LOCK:
1444 c = 0xF0;
1445 break;
1446 case P_REPNE:
1447 case P_REPNZ:
1448 case P_XACQUIRE:
1449 case P_BND:
1450 c = 0xF2;
1451 break;
1452 case P_REPE:
1453 case P_REPZ:
1454 case P_REP:
1455 case P_XRELEASE:
1456 c = 0xF3;
1457 break;
1458 case R_CS:
1459 if (bits == 64) {
1460 nasm_error(ERR_WARNING | ERR_PASS2,
1461 "cs segment base generated, but will be ignored in 64-bit mode");
1462 }
1463 c = 0x2E;
1464 break;
1465 case R_DS:
1466 if (bits == 64) {
1467 nasm_error(ERR_WARNING | ERR_PASS2,
1468 "ds segment base generated, but will be ignored in 64-bit mode");
1469 }
1470 c = 0x3E;
1471 break;
1472 case R_ES:
1473 if (bits == 64) {
1474 nasm_error(ERR_WARNING | ERR_PASS2,
1475 "es segment base generated, but will be ignored in 64-bit mode");
1476 }
1477 c = 0x26;
1478 break;
1479 case R_FS:
1480 c = 0x64;
1481 break;
1482 case R_GS:
1483 c = 0x65;
1484 break;
1485 case R_SS:
1486 if (bits == 64) {
1487 nasm_error(ERR_WARNING | ERR_PASS2,
1488 "ss segment base generated, but will be ignored in 64-bit mode");
1489 }
1490 c = 0x36;
1491 break;
1492 case R_SEGR6:
1493 case R_SEGR7:
1494 nasm_error(ERR_NONFATAL,
1495 "segr6 and segr7 cannot be used as prefixes");
1496 break;
1497 case P_A16:
1498 if (bits == 64) {
1499 nasm_error(ERR_NONFATAL,
1500 "16-bit addressing is not supported "
1501 "in 64-bit mode");
1502 } else if (bits != 16)
1503 c = 0x67;
1504 break;
1505 case P_A32:
1506 if (bits != 32)
1507 c = 0x67;
1508 break;
1509 case P_A64:
1510 if (bits != 64) {
1511 nasm_error(ERR_NONFATAL,
1512 "64-bit addressing is only supported "
1513 "in 64-bit mode");
1514 }
1515 break;
1516 case P_ASP:
1517 c = 0x67;
1518 break;
1519 case P_O16:
1520 if (bits != 16)
1521 c = 0x66;
1522 break;
1523 case P_O32:
1524 if (bits == 16)
1525 c = 0x66;
1526 break;
1527 case P_O64:
1528 /* REX.W */
1529 break;
1530 case P_OSP:
1531 c = 0x66;
1532 break;
1533 case P_EVEX:
1534 case P_VEX3:
1535 case P_VEX2:
1536 case P_NOBND:
1537 case P_none:
1538 break;
1539 default:
1540 nasm_panic(0, "invalid instruction prefix");
1541 }
1542 if (c) {
1543 if (data)
1544 out_rawbyte(data, c);
1545 bytes++;
1546 }
1547 }
1548 return bytes;
1549}
1550
1551static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001552{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001553 uint8_t c;
1554 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001555 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001556 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001557 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001558 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001559 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001560 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001561 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001562 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001563 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001564
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001565 ins->rex_done = false;
1566
H. Peter Anvina77692b2016-09-20 14:04:33 -07001567 emit_prefix(data, bits, ins);
1568
H. Peter Anvin839eca22007-10-29 23:12:47 -07001569 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001570 c = *codes++;
1571 op1 = (c & 3) + ((opex & 1) << 2);
1572 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1573 opx = &ins->oprs[op1];
1574 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001575
H. Peter Anvina77692b2016-09-20 14:04:33 -07001576
H. Peter Anvin839eca22007-10-29 23:12:47 -07001577 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001578 case 01:
1579 case 02:
1580 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001581 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001582 emit_rex(data, ins);
1583 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001586
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001587 case 05:
1588 case 06:
1589 case 07:
1590 opex = c;
1591 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001592
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001594 emit_rex(data, ins);
1595 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001597
Jin Kyu Song164d6072013-10-15 19:10:13 -07001598 case4(014):
1599 break;
1600
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001601 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001602 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001603 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001604 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001605 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001607
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001608 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001609 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001610 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001611 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001612 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001614
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001615 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001616 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001617 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001619
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001620 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001621 if (opx->type & (BITS16 | BITS32))
1622 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 else
1624 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001625 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001626 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001628
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001629 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001630 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001631 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001633
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001634 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001635 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001636 warn_overflow_opd(opx, size);
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(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001641 if (opx->segment == data->segment) {
1642 int64_t delta = opx->offset - data->offset
1643 - (data->inslen - data->insoffs);
1644 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001645 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001646 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001647 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001649
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001650 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001651 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001652 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001653
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001654 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001655 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001657
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001658 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001659 if (opx->type & (BITS16 | BITS32 | BITS64))
1660 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 else
1662 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001663
1664 out_reladdr(data, opx, size);
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(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001668 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001669 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001670
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001671 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001672 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001673 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001674 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001675 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001676 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001677
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001678 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001679 {
1680 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1681 const struct operand *opy;
1682
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001683 c = *codes++;
1684 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001685 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001686 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001687 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001688 "non-absolute expression not permitted as argument %d",
1689 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001690 } else if (opy->offset & ~mask) {
1691 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1692 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001693 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001694 c = opy->offset & mask;
1695 goto emit_is4;
1696 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001697
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001698 case 0173:
1699 c = *codes++;
1700 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001701 c &= 15;
1702 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001703
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001704 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001705 c = 0;
1706 emit_is4:
1707 r = nasm_regvals[opx->basereg];
1708 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001709 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001710
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001711 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001712 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001713 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001714 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001715 "signed dword immediate exceeds bounds");
1716 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001717 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001718 break;
1719
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001720 case4(0240):
1721 case 0250:
1722 codes += 3;
1723 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1724 EVEX_P2Z | EVEX_P2AAA, 2);
1725 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1726 bytes[0] = 0x62;
1727 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001728 bytes[1] = ((((ins->rex & 7) << 5) |
1729 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001730 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001731 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1732 ((~ins->vexreg & 15) << 3) |
1733 (1 << 2) | (ins->vex_wlp & 3);
1734 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001735 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001736 break;
1737
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001738 case4(0260):
1739 case 0270:
1740 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001741 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1742 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001743 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1744 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1745 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001746 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001747 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001748 } else {
1749 bytes[0] = 0xc5;
1750 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001751 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001752 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001753 }
1754 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001755
H. Peter Anvine014f352012-02-25 22:35:19 -08001756 case 0271:
1757 case 0272:
1758 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001759 break;
1760
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001761 case4(0274):
1762 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001763 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001764 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001765
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001766 if (absolute_op(opx)) {
1767 if (ins->rex & REX_W)
1768 s = 64;
1769 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1770 s = 16;
1771 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1772 s = 32;
1773 else
1774 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001775
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001776 um = (uint64_t)2 << (s-1);
1777 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001778
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001779 if (uv > 127 && uv < (uint64_t)-128 &&
1780 (uv < um-128 || uv > um-1)) {
1781 /* If this wasn't explicitly byte-sized, warn as though we
1782 * had fallen through to the imm16/32/64 case.
1783 */
1784 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1785 "%s value exceeds bounds",
1786 (opx->type & BITS8) ? "signed byte" :
1787 s == 16 ? "word" :
1788 s == 32 ? "dword" :
1789 "signed dword");
1790 }
1791
1792 /* Output as a raw byte to avoid byte overflow check */
1793 out_rawbyte(data, (uint8_t)uv);
1794 } else {
1795 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001796 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001797 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001798 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001799
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001800 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001802
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001804 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1805 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001807
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001809 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1810 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001812
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 case 0312:
1814 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001815
Keith Kaniosb7a89542007-04-12 02:40:54 +00001816 case 0313:
1817 ins->rex = 0;
1818 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001819
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001820 case4(0314):
1821 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001822
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001826
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001828 case 0323:
1829 break;
1830
Keith Kaniosb7a89542007-04-12 02:40:54 +00001831 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001832 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001833 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001834
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001835 case 0325:
1836 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001837
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001838 case 0326:
1839 break;
1840
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001842 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001844
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001847
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001848 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001850 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001852
Keith Kanios48af1772007-08-17 07:37:52 +00001853 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001854 if (ins->rex & REX_R)
1855 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001856 ins->rex &= ~(REX_L|REX_R);
1857 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001858
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001859 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001860 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001861
H. Peter Anvin962e3052008-08-28 17:47:16 -07001862 case 0336:
1863 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001864 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001865
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001868 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001869
1870 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001872
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001873 case 0341:
1874 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001875
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001876 case 0360:
1877 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001878
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001879 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001880 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001881 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001882
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001883 case 0364:
1884 case 0365:
1885 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001886
Keith Kanios48af1772007-08-17 07:37:52 +00001887 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001888 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001889 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001890 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001891
Jin Kyu Song03041092013-10-15 19:38:51 -07001892 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001893 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001894
H. Peter Anvine2c80182005-01-15 22:15:51 +00001895 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001896 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001898
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001899 case 0374:
1900 eat = EA_XMMVSIB;
1901 break;
1902
1903 case 0375:
1904 eat = EA_YMMVSIB;
1905 break;
1906
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001907 case 0376:
1908 eat = EA_ZMMVSIB;
1909 break;
1910
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001911 case4(0100):
1912 case4(0110):
1913 case4(0120):
1914 case4(0130):
1915 case4(0200):
1916 case4(0204):
1917 case4(0210):
1918 case4(0214):
1919 case4(0220):
1920 case4(0224):
1921 case4(0230):
1922 case4(0234):
1923 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924 ea ea_data;
1925 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001926 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001927 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001928 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001929
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001930 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001931 /* pick rfield from operand b (opx) */
1932 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001933 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001934 } else {
1935 /* rfield is constant */
1936 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001938 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001940 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001941 rfield, rflags, ins, &errmsg) != eat)
1942 nasm_error(ERR_NONFATAL, "%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07001943
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 p = bytes;
1945 *p++ = ea_data.modrm;
1946 if (ea_data.sib_present)
1947 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001948 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001950 /*
1951 * Make sure the address gets the right offset in case
1952 * the line breaks in the .lst file (BR 1197827)
1953 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001954
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001955 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001956 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001957 if (ea_data.disp8) {
1958 out_rawbyte(data, ea_data.disp8);
1959 } else if (ea_data.rip) {
1960 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001961 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001962 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001963
H. Peter Anvina77692b2016-09-20 14:04:33 -07001964 if (overflow_general(opy->offset, asize) ||
1965 signed_bits(opy->offset, ins->addr_size) !=
1966 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001967 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001968
H. Peter Anvina77692b2016-09-20 14:04:33 -07001969 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001970 (asize > ea_data.bytes)
1971 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001972 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001973 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001974 }
1975 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001976
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001977 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001978 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001979 ": instruction code \\%o (0x%02X) given", c, c);
1980 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001981 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001982 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001983}
1984
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001985static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001986{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001987 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001988 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001989 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001990}
1991
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001992static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001993{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001994 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001995 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001996 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001997}
1998
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001999static int op_rexflags(const operand * o, int mask)
2000{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002001 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002002 int val;
2003
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002004 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08002005 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002006
H. Peter Anvina4835d42008-05-20 14:21:29 -07002007 flags = nasm_reg_flags[o->basereg];
2008 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002009
2010 return rexflags(val, flags, mask);
2011}
2012
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002013static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002014{
2015 int rex = 0;
2016
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002017 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002018 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002019 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002020 rex |= REX_W;
2021 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2022 rex |= REX_H;
2023 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2024 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002025
2026 return rex & mask;
2027}
2028
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002029static int evexflags(int val, decoflags_t deco,
2030 int mask, uint8_t byte)
2031{
2032 int evex = 0;
2033
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002034 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002035 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002036 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002037 evex |= (EVEX_P0RP | EVEX_P0X);
2038 break;
2039 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002040 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002041 evex |= EVEX_P2VP;
2042 if (deco & Z)
2043 evex |= EVEX_P2Z;
2044 if (deco & OPMASK_MASK)
2045 evex |= deco & EVEX_P2AAA;
2046 break;
2047 }
2048 return evex & mask;
2049}
2050
2051static int op_evexflags(const operand * o, int mask, uint8_t byte)
2052{
2053 int val;
2054
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002055 val = nasm_regvals[o->basereg];
2056
2057 return evexflags(val, o->decoflags, mask, byte);
2058}
2059
H. Peter Anvin23595f52009-07-25 17:44:25 -07002060static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002061 insn *instruction,
2062 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002063{
2064 const struct itemplate *temp;
2065 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002066 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002067 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002068 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002069 int i;
2070
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002071 /* broadcasting uses a different data element size */
2072 for (i = 0; i < instruction->operands; i++)
2073 if (i == broadcast)
2074 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2075 else
2076 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002077
2078 merr = MERR_INVALOP;
2079
2080 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002081 temp->opcode != I_none; temp++) {
2082 m = matches(temp, instruction, bits);
2083 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002084 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002085 m = MOK_GOOD;
2086 else
2087 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002088 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002089 /*
2090 * Missing operand size and a candidate for fuzzy matching...
2091 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002092 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002093 if (i == broadcast)
2094 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2095 else
2096 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002097 opsizemissing = true;
2098 }
2099 if (m > merr)
2100 merr = m;
2101 if (merr == MOK_GOOD)
2102 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002103 }
2104
2105 /* No match, but see if we can get a fuzzy operand size match... */
2106 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002107 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002108
2109 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002110 /*
2111 * We ignore extrinsic operand sizes on registers, so we should
2112 * never try to fuzzy-match on them. This also resolves the case
2113 * when we have e.g. "xmmrm128" in two different positions.
2114 */
2115 if (is_class(REGISTER, instruction->oprs[i].type))
2116 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002117
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002118 /* This tests if xsizeflags[i] has more than one bit set */
2119 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2120 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002121
Jin Kyu Song7903c072013-10-30 03:00:12 -07002122 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002123 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002124 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2125 BITS32 : BITS64);
2126 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002127 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002128 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002129 }
2130
2131 /* Try matching again... */
2132 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002133 temp->opcode != I_none; temp++) {
2134 m = matches(temp, instruction, bits);
2135 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002136 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002137 m = MOK_GOOD;
2138 else
2139 m = MERR_INVALOP;
2140 }
2141 if (m > merr)
2142 merr = m;
2143 if (merr == MOK_GOOD)
2144 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002145 }
2146
H. Peter Anvina81655b2009-07-25 18:15:28 -07002147done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002148 *tempp = temp;
2149 return merr;
2150}
2151
Mark Charneydcaef4b2014-10-09 13:45:17 -04002152static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2153{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002154 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002155 uint8_t brcast_num;
2156
Mark Charneydcaef4b2014-10-09 13:45:17 -04002157 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002158 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002159 "size of broadcasting element is greater than 64 bits");
2160
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002161 /*
2162 * The shift term is to take care of the extra BITS80 inserted
2163 * between BITS64 and BITS128.
2164 */
2165 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2166 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002167
2168 return brcast_num;
2169}
2170
H. Peter Anvin65289e82009-07-25 17:25:11 -07002171static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002172 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002173{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002174 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002175 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002176 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002177
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002178 /*
2179 * Check the opcode
2180 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002181 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002182 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002183
2184 /*
2185 * Count the operands
2186 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002187 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002188 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002189
2190 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002191 * Is it legal?
2192 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002193 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002194 return MERR_INVALOP;
2195
2196 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002197 * {evex} available?
2198 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002199 switch (instruction->prefixes[PPS_VEX]) {
2200 case P_EVEX:
2201 if (!itemp_has(itemp, IF_EVEX))
2202 return MERR_ENCMISMATCH;
2203 break;
2204 case P_VEX3:
2205 case P_VEX2:
2206 if (!itemp_has(itemp, IF_VEX))
2207 return MERR_ENCMISMATCH;
2208 break;
2209 default:
2210 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002211 }
2212
2213 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002214 * Check that no spurious colons or TOs are present
2215 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002216 for (i = 0; i < itemp->operands; i++)
2217 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002218 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002219
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002220 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002221 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002222 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002223 switch (itemp_smask(itemp)) {
2224 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002225 asize = BITS8;
2226 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002227 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002228 asize = BITS16;
2229 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002230 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002231 asize = BITS32;
2232 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002233 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002234 asize = BITS64;
2235 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002236 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002237 asize = BITS128;
2238 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002239 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002240 asize = BITS256;
2241 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002242 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002243 asize = BITS512;
2244 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002245 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002246 switch (bits) {
2247 case 16:
2248 asize = BITS16;
2249 break;
2250 case 32:
2251 asize = BITS32;
2252 break;
2253 case 64:
2254 asize = BITS64;
2255 break;
2256 default:
2257 asize = 0;
2258 break;
2259 }
2260 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002261 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002262 asize = 0;
2263 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002264 }
2265
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002266 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002267 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002268 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002269 memset(size, 0, sizeof size);
2270 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002271 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002272 /* S- flags apply to all operands */
2273 for (i = 0; i < MAX_OPERANDS; i++)
2274 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002275 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002276
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002277 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002278 * Check that the operand flags all match up,
2279 * it's a bit tricky so lets be verbose:
2280 *
2281 * 1) Find out the size of operand. If instruction
2282 * doesn't have one specified -- we're trying to
2283 * guess it either from template (IF_S* flag) or
2284 * from code bits.
2285 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002286 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002287 * template has an operand size specified AND this size differ
2288 * from which instruction has (perhaps we got it from code bits)
2289 * we are:
2290 * a) Check that only size of instruction and operand is differ
2291 * other characteristics do match
2292 * b) Perhaps it's a register specified in instruction so
2293 * for such a case we just mark that operand as "size
2294 * missing" and this will turn on fuzzy operand size
2295 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002296 */
2297 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002298 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002299 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002300 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002301 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002302 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002303 opflags_t template_opsize, insn_opsize;
2304
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002305 if (!(type & SIZE_MASK))
2306 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002307
Jin Kyu Song7903c072013-10-30 03:00:12 -07002308 insn_opsize = type & SIZE_MASK;
2309 if (!is_broadcast) {
2310 template_opsize = itemp->opd[i] & SIZE_MASK;
2311 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002312 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2313
2314 if (~ideco & BRDCAST_MASK)
2315 return MERR_BRNOTHERE;
2316
Jin Kyu Song7903c072013-10-30 03:00:12 -07002317 /*
2318 * when broadcasting, the element size depends on
2319 * the instruction type. decorator flag should match.
2320 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002321 if (deco_brsize) {
2322 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002323 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002324 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002325 } else {
2326 template_opsize = 0;
2327 }
2328 }
2329
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002330 if (~ideco & deco & OPMASK_MASK)
2331 return MERR_MASKNOTHERE;
2332
2333 if (itemp->opd[i] & ~type & ~SIZE_MASK) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002334 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002335 } else if (template_opsize) {
2336 if (template_opsize != insn_opsize) {
2337 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002338 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002339 } else if (!is_class(REGISTER, type)) {
2340 /*
2341 * Note: we don't honor extrinsic operand sizes for registers,
2342 * so "missing operand size" for a register should be
2343 * considered a wildcard match rather than an error.
2344 */
2345 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002346 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002347 } else if (is_broadcast &&
2348 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002349 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002350 /*
2351 * broadcasting opsize matches but the number of repeated memory
2352 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002353 * if 64b double precision float is broadcasted to ymm (256b),
2354 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002355 */
2356 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002357 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002358 }
2359 }
2360
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002361 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002362 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002363
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002364 /*
2365 * Check operand sizes
2366 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002367 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2368 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002370 asize = itemp->opd[i] & SIZE_MASK;
2371 if (asize) {
2372 for (i = 0; i < oprs; i++)
2373 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 break;
2375 }
2376 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002377 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002379 }
2380
Keith Kaniosb7a89542007-04-12 02:40:54 +00002381 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002382 if (!(itemp->opd[i] & SIZE_MASK) &&
2383 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002384 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002385 }
2386
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002387 /*
2388 * Check template is okay at the set cpu level
2389 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002390 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002391 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002392
Keith Kaniosb7a89542007-04-12 02:40:54 +00002393 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002394 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002395 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002396 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002397 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002398
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002399 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002400 * If we have a HLE prefix, look for the NOHLE flag
2401 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002402 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002403 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2404 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2405 return MERR_BADHLE;
2406
2407 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002408 * Check if special handling needed for Jumps
2409 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002410 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002411 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002412
Jin Kyu Song03041092013-10-15 19:38:51 -07002413 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002414 * Check if BND prefix is allowed.
2415 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002416 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002417 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002418 (has_prefix(instruction, PPS_REP, P_BND) ||
2419 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002420 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002421 else if (itemp_has(itemp, IF_BND) &&
2422 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2423 has_prefix(instruction, PPS_REP, P_REPNZ)))
2424 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002425
H. Peter Anvin60926242009-07-26 16:25:38 -07002426 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002427}
2428
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002429/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002430 * Check if ModR/M.mod should/can be 01.
2431 * - EAF_BYTEOFFS is set
2432 * - offset can fit in a byte when EVEX is not used
2433 * - offset can be compressed when EVEX is used
2434 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002435#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2436 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2437 is_disp8n(input, ins, &output->disp8) : \
2438 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2439 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002440
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002441static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002442 int rfield, opflags_t rflags, insn *ins,
2443 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002444{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002445 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002446 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002447 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002448
H. Peter Anvin8f622462017-04-02 19:02:29 -07002449 *errmsg = "invalid effective address"; /* Default error message */
2450
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002451 output->type = EA_SCALAR;
2452 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002453 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002454
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002455 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002456 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002457 /* EVEX.R' flag for the REG operand */
2458 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002459
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002460 if (is_class(REGISTER, input->type)) {
2461 /*
2462 * It's a direct register.
2463 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002464 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002465 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002466
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002467 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002468 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002469
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002470 /* broadcasting is not available with a direct register operand. */
2471 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002472 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002473 goto err;
2474 }
2475
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002476 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002477 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002478 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002479 output->bytes = 0; /* no offset necessary either */
2480 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2481 } else {
2482 /*
2483 * It's a memory reference.
2484 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002485
2486 /* Embedded rounding or SAE is not available with a mem ref operand. */
2487 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002488 *errmsg = "embedded rounding is available only with "
2489 "register-register operations";
2490 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002491 }
2492
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002493 if (input->basereg == -1 &&
2494 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002495 /*
2496 * It's a pure offset.
2497 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002498 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002499 if (input->segment == NO_SEG ||
2500 (input->opflags & OPFLAG_RELATIVE)) {
2501 nasm_error(ERR_WARNING | ERR_PASS2,
2502 "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002503 input->type &= ~IP_REL;
2504 input->type |= MEMORY;
2505 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002506 }
2507
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002508 if (bits == 64 &&
2509 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002510 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002511 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002512 }
2513
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002514 if (eaflags & EAF_BYTEOFFS ||
2515 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002516 input->disp_size != (addrbits != 16 ? 32 : 16))) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002517 nasm_error(ERR_WARNING | ERR_PASS1,
2518 "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002519 }
2520
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002521 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002522 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002523 output->sib = GEN_SIB(0, 4, 5);
2524 output->bytes = 4;
2525 output->modrm = GEN_MODRM(0, rfield, 4);
2526 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002527 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002528 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002529 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002530 output->modrm = GEN_MODRM(0, rfield,
2531 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002532 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002533 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002534 } else {
2535 /*
2536 * It's an indirection.
2537 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002538 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002539 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002541 int t, it, bt; /* register numbers */
2542 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002543
H. Peter Anvine2c80182005-01-15 22:15:51 +00002544 if (s == 0)
2545 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002546
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002547 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002548 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002549 ix = nasm_reg_flags[i];
2550 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002551 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002552 ix = 0;
2553 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002554
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002555 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002556 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002557 bx = nasm_reg_flags[b];
2558 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002559 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002560 bx = 0;
2561 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002562
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002563 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002564 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002565 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002566 int32_t o = input->offset;
2567 int mod, scale, index, base;
2568
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002569 /*
2570 * For a vector SIB, one has to be a vector and the other,
2571 * if present, a GPR. The vector must be the index operand.
2572 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002573 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002574 if (s == 0)
2575 s = 1;
2576 else if (s != 1)
2577 goto err;
2578
2579 t = bt, bt = it, it = t;
2580 x = bx, bx = ix, ix = x;
2581 }
2582
2583 if (bt != -1) {
2584 if (REG_GPR & ~bx)
2585 goto err;
2586 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2587 sok &= bx;
2588 else
2589 goto err;
2590 }
2591
2592 /*
2593 * While we're here, ensure the user didn't specify
2594 * WORD or QWORD
2595 */
2596 if (input->disp_size == 16 || input->disp_size == 64)
2597 goto err;
2598
2599 if (addrbits == 16 ||
2600 (addrbits == 32 && !(sok & BITS32)) ||
2601 (addrbits == 64 && !(sok & BITS64)))
2602 goto err;
2603
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002604 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2605 : ((ix & YMMREG & ~REG_EA)
2606 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002607
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002608 output->rex |= rexflags(it, ix, REX_X);
2609 output->rex |= rexflags(bt, bx, REX_B);
2610 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002611
2612 index = it & 7; /* it is known to be != -1 */
2613
2614 switch (s) {
2615 case 1:
2616 scale = 0;
2617 break;
2618 case 2:
2619 scale = 1;
2620 break;
2621 case 4:
2622 scale = 2;
2623 break;
2624 case 8:
2625 scale = 3;
2626 break;
2627 default: /* then what the smeg is it? */
2628 goto err; /* panic */
2629 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002630
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002631 if (bt == -1) {
2632 base = 5;
2633 mod = 0;
2634 } else {
2635 base = (bt & 7);
2636 if (base != REG_NUM_EBP && o == 0 &&
2637 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002638 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002639 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002640 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002641 mod = 1;
2642 else
2643 mod = 2;
2644 }
2645
2646 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002647 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2648 output->modrm = GEN_MODRM(mod, rfield, 4);
2649 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002650 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002651 /*
2652 * it must be a 32/64-bit memory reference. Firstly we have
2653 * to check that all registers involved are type E/Rxx.
2654 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002655 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002656 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002657
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002658 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002659 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2660 sok &= ix;
2661 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002662 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002663 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002664
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002665 if (bt != -1) {
2666 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002667 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002668 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002669 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002670 sok &= bx;
2671 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002672
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002673 /*
2674 * While we're here, ensure the user didn't specify
2675 * WORD or QWORD
2676 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002677 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002678 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002679
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002680 if (addrbits == 16 ||
2681 (addrbits == 32 && !(sok & BITS32)) ||
2682 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002683 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002684
Keith Kaniosb7a89542007-04-12 02:40:54 +00002685 /* now reorganize base/index */
2686 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002687 ((hb == b && ht == EAH_NOTBASE) ||
2688 (hb == i && ht == EAH_MAKEBASE))) {
2689 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002690 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002691 x = bx, bx = ix, ix = x;
2692 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002693
Jin Kyu Song164d6072013-10-15 19:10:13 -07002694 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002695 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002696 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002697 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002698 if (eaflags & EAF_MIB) {
2699 /* only for mib operands */
2700 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2701 /*
2702 * make a single reg index [reg*1].
2703 * gas uses this form for an explicit index register.
2704 */
2705 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2706 }
2707 if ((ht == EAH_SUMMED) && bt == -1) {
2708 /* separate once summed index into [base, index] */
2709 bt = it, bx = ix, s--;
2710 }
2711 } else {
2712 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002713 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002714 s == 3 || s == 5 || s == 9) && bt == -1) {
2715 /* convert 3*EAX to EAX+2*EAX */
2716 bt = it, bx = ix, s--;
2717 }
2718 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002719 (eaflags & EAF_TIMESTWO) &&
2720 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002721 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002722 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002723 * to sib format with 0x0 displacement - [EAX*1+0].
2724 */
2725 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2726 }
2727 }
Keith Kanios48af1772007-08-17 07:37:52 +00002728 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002729 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002730 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002731 x = ix, ix = bx, bx = x;
2732 }
2733 if (it == REG_NUM_ESP ||
2734 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002735 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002736
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002737 output->rex |= rexflags(it, ix, REX_X);
2738 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002739
Keith Kanios48af1772007-08-17 07:37:52 +00002740 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002741 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002743
Keith Kaniosb7a89542007-04-12 02:40:54 +00002744 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002745 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002746 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002747 } else {
2748 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002749 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002750 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002751 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002752 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002753 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002754 mod = 1;
2755 else
2756 mod = 2;
2757 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002758
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002759 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002760 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2761 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002762 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002763 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002765
Keith Kaniosb7a89542007-04-12 02:40:54 +00002766 if (it == -1)
2767 index = 4, s = 1;
2768 else
2769 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002770
H. Peter Anvine2c80182005-01-15 22:15:51 +00002771 switch (s) {
2772 case 1:
2773 scale = 0;
2774 break;
2775 case 2:
2776 scale = 1;
2777 break;
2778 case 4:
2779 scale = 2;
2780 break;
2781 case 8:
2782 scale = 3;
2783 break;
2784 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002785 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002787
Keith Kaniosb7a89542007-04-12 02:40:54 +00002788 if (bt == -1) {
2789 base = 5;
2790 mod = 0;
2791 } else {
2792 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002793 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002794 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002795 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002796 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002797 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002798 mod = 1;
2799 else
2800 mod = 2;
2801 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002802
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002803 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002804 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2805 output->modrm = GEN_MODRM(mod, rfield, 4);
2806 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 }
2808 } else { /* it's 16-bit */
2809 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002810 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002811
Keith Kaniosb7a89542007-04-12 02:40:54 +00002812 /* check for 64-bit long mode */
2813 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002814 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002815
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002817 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2818 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002819 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002820
Keith Kaniosb7a89542007-04-12 02:40:54 +00002821 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002822 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002823 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002824
H. Peter Anvine2c80182005-01-15 22:15:51 +00002825 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002826 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 if (b == -1 && i != -1) {
2828 int tmp = b;
2829 b = i;
2830 i = tmp;
2831 } /* swap */
2832 if ((b == R_SI || b == R_DI) && i != -1) {
2833 int tmp = b;
2834 b = i;
2835 i = tmp;
2836 }
2837 /* have BX/BP as base, SI/DI index */
2838 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002839 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002840 if (i != -1 && b != -1 &&
2841 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002842 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002843 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002844 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002845
H. Peter Anvine2c80182005-01-15 22:15:51 +00002846 rm = -1;
2847 if (i != -1)
2848 switch (i * 256 + b) {
2849 case R_SI * 256 + R_BX:
2850 rm = 0;
2851 break;
2852 case R_DI * 256 + R_BX:
2853 rm = 1;
2854 break;
2855 case R_SI * 256 + R_BP:
2856 rm = 2;
2857 break;
2858 case R_DI * 256 + R_BP:
2859 rm = 3;
2860 break;
2861 } else
2862 switch (b) {
2863 case R_SI:
2864 rm = 4;
2865 break;
2866 case R_DI:
2867 rm = 5;
2868 break;
2869 case R_BP:
2870 rm = 6;
2871 break;
2872 case R_BX:
2873 rm = 7;
2874 break;
2875 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002876 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002877 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002878
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002879 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002880 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002881 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002882 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 mod = 1;
2884 else
2885 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002886
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002887 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002888 output->bytes = mod; /* bytes of offset needed */
2889 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002890 }
2891 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002892 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002893
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002894 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002895 return output->type;
2896
2897err:
2898 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002899}
2900
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002901static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002902{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002903 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002904 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002905
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002906 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002907
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002908 switch (ins->prefixes[PPS_ASIZE]) {
2909 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002910 valid &= 16;
2911 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002912 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002913 valid &= 32;
2914 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002915 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002916 valid &= 64;
2917 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002918 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002919 valid &= (addrbits == 32) ? 16 : 32;
2920 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002921 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002922 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002923 }
2924
2925 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002926 if (is_class(MEMORY, ins->oprs[j].type)) {
2927 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002928
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002929 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002930 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002931 i = 0;
2932 else
2933 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002934
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002935 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002936 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002937 b = 0;
2938 else
2939 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002940
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002941 if (ins->oprs[j].scale == 0)
2942 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002943
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002944 if (!i && !b) {
2945 int ds = ins->oprs[j].disp_size;
2946 if ((addrbits != 64 && ds > 8) ||
2947 (addrbits == 64 && ds == 16))
2948 valid &= ds;
2949 } else {
2950 if (!(REG16 & ~b))
2951 valid &= 16;
2952 if (!(REG32 & ~b))
2953 valid &= 32;
2954 if (!(REG64 & ~b))
2955 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002956
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002957 if (!(REG16 & ~i))
2958 valid &= 16;
2959 if (!(REG32 & ~i))
2960 valid &= 32;
2961 if (!(REG64 & ~i))
2962 valid &= 64;
2963 }
2964 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002965 }
2966
2967 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002968 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002969 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002970 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002971 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002972 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002973 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002974 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002975 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002976 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002977 }
2978
2979 defdisp = ins->addr_size == 16 ? 16 : 32;
2980
2981 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002982 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2983 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2984 /*
2985 * mem_offs sizes must match the address size; if not,
2986 * strip the MEM_OFFS bit and match only EA instructions
2987 */
2988 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2989 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002990 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002991}