blob: abdefd615b0964e2ac1e7ad53be35f3a95349554 [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 int asize;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800346 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700347 union {
348 uint8_t b[8];
349 uint64_t q;
350 } xdata;
351 uint64_t size = data->size;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800352 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800353 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000354
H. Peter Anvina77692b2016-09-20 14:04:33 -0700355 if (!data->size)
356 return; /* Nothing to do */
357
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700358 /*
359 * Convert addresses to RAWDATA if possible
360 * XXX: not all backends want this for global symbols!!!!
361 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700362 switch (data->type) {
363 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800364 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800365 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800366 goto address;
367
368 case OUT_RELADDR:
369 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800370 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800371 goto address;
372
373 address:
H. Peter Anvina77692b2016-09-20 14:04:33 -0700374 asize = data->size;
375 nasm_assert(asize <= 8);
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800376 if (data->tsegment == fixseg && data->twrt == NO_SEG) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700377 uint8_t *q = xdata.b;
378
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800379 warn_overflow_out(addrval, asize, data->sign);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700380
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800381 WRITEADDR(q, addrval, asize);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700382 data->data = xdata.b;
383 data->type = OUT_RAWDATA;
384 asize = 0; /* No longer an address */
385 }
386 break;
387
388 default:
389 asize = 0; /* Not an address */
390 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000391 }
392
H. Peter Anvina77692b2016-09-20 14:04:33 -0700393 lfmt->output(data);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800394
Frank Kotlerabebb082003-09-06 04:45:37 +0000395 /*
396 * this call to src_get determines when we call the
397 * debug-format-specific "linenum" function
398 * it updates lineno and lnfname to the current values
399 * returning 0 if "same as last time", -2 if lnfname
400 * changed, and the amount by which lineno changed,
401 * if it did. thus, these variables must be static
402 */
403
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400404 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700405 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000406
H. Peter Anvinb6412502016-02-11 21:07:40 -0800407 if (asize && asize > amax) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700408 if (data->type != OUT_ADDRESS || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800409 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800410 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800411 asize << 3, ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800412 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800413 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800414 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800415 asize << 3, ofmt->maxbits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700416 data->size = amax;
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700417 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700418 data->insoffs += amax;
419 data->offset += amax;
420 data->size = size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800421 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700422 data->data = zero_buffer;
423 data->type = OUT_RAWDATA;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800424 }
425
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700426 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700427 data->offset += size;
428 data->insoffs += size;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000429}
430
H. Peter Anvina77692b2016-09-20 14:04:33 -0700431static inline void out_rawdata(struct out_data *data, const void *rawdata,
432 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400433{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700434 data->type = OUT_RAWDATA;
435 data->data = rawdata;
436 data->size = size;
437 out(data);
438}
439
440static void out_rawbyte(struct out_data *data, uint8_t byte)
441{
442 data->type = OUT_RAWDATA;
443 data->data = &byte;
444 data->size = 1;
445 out(data);
446}
447
448static inline void out_reserve(struct out_data *data, uint64_t size)
449{
450 data->type = OUT_RESERVE;
451 data->size = size;
452 out(data);
453}
454
H. Peter Anvin164d2462017-02-20 02:39:56 -0800455static inline void out_imm(struct out_data *data, const struct operand *opx,
H. Peter Anvina77692b2016-09-20 14:04:33 -0700456 int size, enum out_sign sign)
457{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800458 data->type =
459 (opx->opflags & OPFLAG_RELATIVE) ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700460 data->sign = sign;
461 data->size = size;
462 data->toffset = opx->offset;
463 data->tsegment = opx->segment;
464 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800465 /*
466 * XXX: improve this if at some point in the future we can
467 * distinguish the subtrahend in expressions like [foo - bar]
468 * where bar is a symbol in the current segment. However, at the
469 * current point, if OPFLAG_RELATIVE is set that subtraction has
470 * already occurred.
471 */
472 data->relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700473 out(data);
474}
475
H. Peter Anvin164d2462017-02-20 02:39:56 -0800476static void out_reladdr(struct out_data *data, const struct operand *opx,
477 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700478{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800479 if (opx->opflags & OPFLAG_RELATIVE)
480 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
481
H. Peter Anvina77692b2016-09-20 14:04:33 -0700482 data->type = OUT_RELADDR;
483 data->sign = OUT_SIGNED;
484 data->size = size;
485 data->toffset = opx->offset;
486 data->tsegment = opx->segment;
487 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800488 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700489 out(data);
490}
491
H. Peter Anvin164d2462017-02-20 02:39:56 -0800492static inline void out_segment(struct out_data *data,
493 const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700494{
495 data->type = OUT_SEGMENT;
496 data->sign = OUT_UNSIGNED;
497 data->size = 2;
498 data->toffset = opx->offset;
499 data->tsegment = ofmt->segbase(opx->segment + 1);
500 data->twrt = opx->wrt;
501 out(data);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400502}
503
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700504static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800505 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000506{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800507 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800508 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000509 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800510 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000511
H. Peter Anvin755f5212012-02-25 11:41:34 -0800512 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700513 return false;
514 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400515 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700516 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400517 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700518
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800519 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100520
Victor van den Elzen154e5922009-02-25 17:32:00 +0100521 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100522 /* Be optimistic in pass 1 */
523 return true;
524
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700526 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000527
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700528 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800529 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
530
531 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
532 /* jmp short (opcode eb) cannot be used with bnd prefix. */
533 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800534 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800535 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800536 }
537
538 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000540
H. Peter Anvin04445362016-09-21 15:56:19 -0700541/* This is totally just a wild guess what is reasonable... */
542#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
543
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800544int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000545{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700546 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000547 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700548 enum match_result m;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000549 int32_t itimes;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300550 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000551
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700552 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700553 data.offset = start;
554 data.segment = segment;
555 data.itemp = NULL;
556 data.sign = OUT_WRAP;
557 data.bits = bits;
558
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300559 wsize = idata_bytes(instruction->opcode);
560 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000562
H. Peter Anvineba20a72002-04-30 20:53:55 +0000563 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000565 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566 if (t < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800567 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000568
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400570 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000571 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400572 if (wsize > 8) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800573 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400574 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000575 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700576 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700577 data.insoffs = 0;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700578 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700579 data.inslen = data.size = wsize;
580 data.toffset = e->offset;
581 data.tsegment = e->segment;
582 data.twrt = e->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800583 data.relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700584 out(&data);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400585 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700586 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400587 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700588 int align = e->stringlen % wsize;
589 if (align)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000590 align = wsize - align;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700591
592 data.insoffs = 0;
593 data.inslen = e->stringlen + align;
594
595 out_rawdata(&data, e->stringval, e->stringlen);
596 out_rawdata(&data, zero_buffer, align);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 }
598 }
599 if (t > 0 && t == instruction->times - 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700600 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800601 lfmt->uplevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 }
603 }
604 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800605 lfmt->downlevel(LIST_TIMES);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700606 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700607 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608 FILE *fp;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700609 size_t t = instruction->times;
610 off_t base = 0;
611 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700612 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700613 char *buf = NULL;
614 size_t blk = 0; /* Buffered I/O block size */
615 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000616
H. Peter Anvind81a2352016-09-21 14:03:18 -0700617 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400618 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800619 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000620 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700621 goto done;
622 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000623
H. Peter Anvind81a2352016-09-21 14:03:18 -0700624 len = nasm_file_size(fp);
625
626 if (len == (off_t)-1) {
627 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700628 fname);
629 goto close_done;
630 }
631
H. Peter Anvina77692b2016-09-20 14:04:33 -0700632 if (instruction->eops->next) {
633 base = instruction->eops->next->offset;
634 if (base >= len) {
635 len = 0;
636 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000637 len -= base;
638 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700639 len > (off_t)instruction->eops->next->next->offset)
640 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000641 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700643
H. Peter Anvina77692b2016-09-20 14:04:33 -0700644 lfmt->set_offset(data.offset);
645 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000646
H. Peter Anvind81a2352016-09-21 14:03:18 -0700647 if (!len)
648 goto end_incbin;
649
650 /* Try to map file data */
651 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700652 if (!map) {
653 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
654 buf = nasm_malloc(blk);
655 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700656
657 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700658 /*
659 * Consider these irrelevant for INCBIN, since it is fully
660 * possible that these might be (way) bigger than an int
661 * can hold; there is, however, no reason to widen these
662 * types just for INCBIN. data.inslen == 0 signals to the
663 * backend that these fields are meaningless, if at all
664 * needed.
665 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700666 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700667 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700668
H. Peter Anvind81a2352016-09-21 14:03:18 -0700669 if (map) {
670 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700671 } else if ((off_t)m == len) {
672 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700673 } else {
674 off_t l = len;
675
676 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700677 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700678 "`incbin': unable to seek on file `%s'",
679 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700680 goto end_incbin;
681 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700682 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700683 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700684 if (!m || feof(fp)) {
685 /*
686 * This shouldn't happen unless the file
687 * actually changes while we are reading
688 * it.
689 */
690 nasm_error(ERR_NONFATAL,
691 "`incbin': unexpected EOF while"
692 " reading file `%s'", fname);
693 goto end_incbin;
694 }
695 out_rawdata(&data, buf, m);
696 l -= m;
697 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700698 }
699 }
700 end_incbin:
701 lfmt->downlevel(LIST_INCBIN);
702 if (instruction->times > 1) {
703 lfmt->set_offset(data.offset);
704 lfmt->uplevel(LIST_TIMES);
705 lfmt->downlevel(LIST_TIMES);
706 }
707 if (ferror(fp)) {
708 nasm_error(ERR_NONFATAL,
709 "`incbin': error while"
710 " reading file `%s'", fname);
711 }
712 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700713 if (buf)
714 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700715 if (map)
716 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700717 fclose(fp);
718 done:
719 ;
720 } else {
721 /* "Real" instruction */
722
723 /* Check to see if we need an address-size prefix */
724 add_asp(instruction, bits);
725
726 m = find_match(&temp, instruction, data.segment, data.offset, bits);
727
728 if (m == MOK_GOOD) {
729 /* Matches! */
730 int64_t insn_size = calcsize(data.segment, data.offset,
731 bits, instruction, temp);
732 itimes = instruction->times;
733 if (insn_size < 0) /* shouldn't be, on pass two */
734 nasm_panic(0, "errors made it through from pass one");
735
736 data.itemp = temp;
737 data.bits = bits;
738
739 while (itimes--) {
740 data.insoffs = 0;
741 data.inslen = insn_size;
742
743 gencode(&data, instruction);
744 nasm_assert(data.insoffs == insn_size);
745
746 if (itimes > 0 && itimes == instruction->times - 1) {
747 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800748 lfmt->uplevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400749 }
750 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700751 if (instruction->times > 1)
752 lfmt->downlevel(LIST_TIMES);
753 } else {
754 /* No match */
755 switch (m) {
756 case MERR_OPSIZEMISSING:
757 nasm_error(ERR_NONFATAL, "operation size not specified");
758 break;
759 case MERR_OPSIZEMISMATCH:
760 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
761 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700762 case MERR_BRNOTHERE:
763 nasm_error(ERR_NONFATAL,
764 "broadcast not permitted on this operand");
765 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700766 case MERR_BRNUMMISMATCH:
767 nasm_error(ERR_NONFATAL,
768 "mismatch in the number of broadcasting elements");
769 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700770 case MERR_MASKNOTHERE:
771 nasm_error(ERR_NONFATAL,
772 "mask not permitted on this operand");
773 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700774 case MERR_BADCPU:
775 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
776 break;
777 case MERR_BADMODE:
778 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
779 bits);
780 break;
781 case MERR_ENCMISMATCH:
782 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
783 break;
784 case MERR_BADBND:
785 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
786 break;
787 case MERR_BADREPNE:
788 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
789 (has_prefix(instruction, PPS_REP, P_REPNE) ?
790 "repne" : "repnz"));
791 break;
792 default:
793 nasm_error(ERR_NONFATAL,
794 "invalid combination of opcode and operands");
795 break;
796 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400797 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000798 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700799 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000800}
801
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800802int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000803{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000804 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700805 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000806
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400807 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000809
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700810 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
811 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400812 instruction->opcode == I_DT || instruction->opcode == I_DO ||
813 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300815 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000816
H. Peter Anvine2c80182005-01-15 22:15:51 +0000817 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300818 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000819
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400820 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000821 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000822
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400824 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400826 warn_overflow_const(e->offset, wsize);
827 } else if (e->type == EOT_DB_STRING ||
828 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000829 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000830
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 align = (-osize) % wsize;
832 if (align < 0)
833 align += wsize;
834 isize += osize + align;
835 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700836 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000837 }
838
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400840 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700841 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000842
H. Peter Anvind81a2352016-09-21 14:03:18 -0700843 len = nasm_file_size_by_path(fname);
844 if (len == (off_t)-1) {
845 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
846 fname);
847 return 0;
848 }
849
850 if (instruction->eops->next) {
851 if (len <= (off_t)instruction->eops->next->offset) {
852 len = 0;
853 } else {
854 len -= instruction->eops->next->offset;
855 if (instruction->eops->next->next &&
856 len > (off_t)instruction->eops->next->next->offset) {
857 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000858 }
859 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000860 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700861
862 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000863 }
864
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700865 /* Check to see if we need an address-size prefix */
866 add_asp(instruction, bits);
867
H. Peter Anvin23595f52009-07-25 17:44:25 -0700868 m = find_match(&temp, instruction, segment, offset, bits);
869 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400870 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700871 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700872 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400873 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000874 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000875}
876
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800877static void bad_hle_warn(const insn * ins, uint8_t hleok)
878{
879 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800880 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800881 static const enum whatwarn warn[2][4] =
882 {
883 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
884 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
885 };
886 unsigned int n;
887
888 n = (unsigned int)rep_pfx - P_XACQUIRE;
889 if (n > 1)
890 return; /* Not XACQUIRE/XRELEASE */
891
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800892 ww = warn[n][hleok];
893 if (!is_class(MEMORY, ins->oprs[0].type))
894 ww = w_inval; /* HLE requires operand 0 to be memory */
895
896 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800897 case w_none:
898 break;
899
900 case w_lock:
901 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800902 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800903 "%s with this instruction requires lock",
904 prefix_name(rep_pfx));
905 }
906 break;
907
908 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800909 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800910 "%s invalid with this instruction",
911 prefix_name(rep_pfx));
912 break;
913 }
914}
915
H. Peter Anvin507ae032008-10-09 15:37:10 -0700916/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400917#define case3(x) case (x): case (x)+1: case (x)+2
918#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700919
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800920static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800921 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000922{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800923 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800924 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000925 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000926 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700927 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700928 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700929 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700930 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800931 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800932 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700933 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -0700934 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000935
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700936 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700937 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700938 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700939
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700940 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400941 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700942
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 (void)segment; /* Don't warn that this parameter is unused */
944 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000945
H. Peter Anvin839eca22007-10-29 23:12:47 -0700946 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400947 c = *codes++;
948 op1 = (c & 3) + ((opex & 1) << 2);
949 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
950 opx = &ins->oprs[op1];
951 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700952
H. Peter Anvin839eca22007-10-29 23:12:47 -0700953 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400954 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 codes += c, length += c;
956 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700957
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400958 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400959 opex = c;
960 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700961
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400962 case4(010):
963 ins->rex |=
964 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 codes++, length++;
966 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700967
Jin Kyu Song164d6072013-10-15 19:10:13 -0700968 case4(014):
969 /* this is an index reg of MIB operand */
970 mib_index = opx->basereg;
971 break;
972
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400973 case4(020):
974 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 length++;
976 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700977
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400978 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 length += 2;
980 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700981
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400982 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700983 if (opx->type & (BITS16 | BITS32 | BITS64))
984 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 else
986 length += (bits == 16) ? 2 : 4;
987 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700988
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400989 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 length += 4;
991 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700992
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400993 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700994 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700996
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400997 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 length++;
999 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001000
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001001 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001002 length += 8; /* MOV reg64/imm */
1003 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001004
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001005 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 length += 2;
1007 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001008
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001009 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001010 if (opx->type & (BITS16 | BITS32 | BITS64))
1011 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 else
1013 length += (bits == 16) ? 2 : 4;
1014 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001015
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001016 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 length += 4;
1018 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001019
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001020 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001021 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001023
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001024 case 0172:
1025 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001026 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001027 length++;
1028 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001029
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001030 case4(0174):
1031 length++;
1032 break;
1033
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001034 case4(0240):
1035 ins->rex |= REX_EV;
1036 ins->vexreg = regval(opx);
1037 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1038 ins->vex_cm = *codes++;
1039 ins->vex_wlp = *codes++;
1040 ins->evex_tuple = (*codes++ - 0300);
1041 break;
1042
1043 case 0250:
1044 ins->rex |= REX_EV;
1045 ins->vexreg = 0;
1046 ins->vex_cm = *codes++;
1047 ins->vex_wlp = *codes++;
1048 ins->evex_tuple = (*codes++ - 0300);
1049 break;
1050
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001051 case4(0254):
1052 length += 4;
1053 break;
1054
1055 case4(0260):
1056 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001057 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001058 ins->vex_cm = *codes++;
1059 ins->vex_wlp = *codes++;
1060 break;
1061
1062 case 0270:
1063 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001064 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001065 ins->vex_cm = *codes++;
1066 ins->vex_wlp = *codes++;
1067 break;
1068
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001069 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001070 hleok = c & 3;
1071 break;
1072
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001073 case4(0274):
1074 length++;
1075 break;
1076
1077 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001079
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001081 if (bits == 64)
1082 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001083 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
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 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001087 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001089
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001091 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001092
Keith Kaniosb7a89542007-04-12 02:40:54 +00001093 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001094 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1095 has_prefix(ins, PPS_ASIZE, P_A32))
1096 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001098
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001099 case4(0314):
1100 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001101
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001103 {
1104 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1105 if (pfx == P_O16)
1106 break;
1107 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001108 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001109 else
1110 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001112 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001113
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001115 {
1116 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1117 if (pfx == P_O32)
1118 break;
1119 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001120 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001121 else
1122 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001124 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001125
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 case 0322:
1127 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001128
Keith Kaniosb7a89542007-04-12 02:40:54 +00001129 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001130 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001131 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001132
Keith Kaniosb7a89542007-04-12 02:40:54 +00001133 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001134 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001135 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001136
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001137 case 0325:
1138 ins->rex |= REX_NH;
1139 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001140
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001141 case 0326:
1142 break;
1143
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 case 0330:
1145 codes++, length++;
1146 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001147
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001150
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001151 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152 case 0333:
1153 length++;
1154 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001155
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001156 case 0334:
1157 ins->rex |= REX_L;
1158 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001159
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001160 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001161 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001162
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001163 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001164 if (!ins->prefixes[PPS_REP])
1165 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001166 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001167
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001168 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001169 if (!ins->prefixes[PPS_REP])
1170 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001171 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001172
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001174 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001175 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001176 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001177 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1178 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001179 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001181 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001183
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001184 case 0341:
1185 if (!ins->prefixes[PPS_WAIT])
1186 ins->prefixes[PPS_WAIT] = P_WAIT;
1187 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001188
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001189 case 0360:
1190 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001191
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001192 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001193 length++;
1194 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001195
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001196 case 0364:
1197 case 0365:
1198 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001199
Keith Kanios48af1772007-08-17 07:37:52 +00001200 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001201 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001202 length++;
1203 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001204
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001205 case 0370:
1206 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001207 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001208
H. Peter Anvine2c80182005-01-15 22:15:51 +00001209 case 0373:
1210 length++;
1211 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001212
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001213 case 0374:
1214 eat = EA_XMMVSIB;
1215 break;
1216
1217 case 0375:
1218 eat = EA_YMMVSIB;
1219 break;
1220
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001221 case 0376:
1222 eat = EA_ZMMVSIB;
1223 break;
1224
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001225 case4(0100):
1226 case4(0110):
1227 case4(0120):
1228 case4(0130):
1229 case4(0200):
1230 case4(0204):
1231 case4(0210):
1232 case4(0214):
1233 case4(0220):
1234 case4(0224):
1235 case4(0230):
1236 case4(0234):
1237 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001238 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001239 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001240 opflags_t rflags;
1241 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001242 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001243
Keith Kaniosb7a89542007-04-12 02:40:54 +00001244 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001245
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001246 if (c <= 0177) {
1247 /* pick rfield from operand b (opx) */
1248 rflags = regflag(opx);
1249 rfield = nasm_regvals[opx->basereg];
1250 } else {
1251 rflags = 0;
1252 rfield = c & 7;
1253 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001254
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001255 /* EVEX.b1 : evex_brerop contains the operand position */
1256 op_er_sae = (ins->evex_brerop >= 0 ?
1257 &ins->oprs[ins->evex_brerop] : NULL);
1258
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001259 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1260 /* set EVEX.b */
1261 ins->evex_p[2] |= EVEX_P2B;
1262 if (op_er_sae->decoflags & ER) {
1263 /* set EVEX.RC (rounding control) */
1264 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1265 & EVEX_P2RC;
1266 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001267 } else {
1268 /* set EVEX.L'L (vector length) */
1269 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001270 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001271 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001272 /* set EVEX.b */
1273 ins->evex_p[2] |= EVEX_P2B;
1274 }
1275 }
1276
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001277 if (itemp_has(temp, IF_MIB)) {
1278 opy->eaflags |= EAF_MIB;
1279 /*
1280 * if a separate form of MIB (ICC style) is used,
1281 * the index reg info is merged into mem operand
1282 */
1283 if (mib_index != R_none) {
1284 opy->indexreg = mib_index;
1285 opy->scale = 1;
1286 opy->hintbase = mib_index;
1287 opy->hinttype = EAH_NOTBASE;
1288 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001289 }
1290
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001291 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001292 rfield, rflags, ins, &errmsg) != eat) {
1293 nasm_error(ERR_NONFATAL, "%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001294 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001295 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001296 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001297 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001298 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001299 }
1300 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001301
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001302 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001303 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001304 ": instruction code \\%o (0x%02X) given", c, c);
1305 break;
1306 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001307 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001308
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001309 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001310
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001311 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001312 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001313 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001314 return -1;
1315 }
1316 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001317 }
1318
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001319 switch (ins->prefixes[PPS_VEX]) {
1320 case P_EVEX:
1321 if (!(ins->rex & REX_EV))
1322 return -1;
1323 break;
1324 case P_VEX3:
1325 case P_VEX2:
1326 if (!(ins->rex & REX_V))
1327 return -1;
1328 break;
1329 default:
1330 break;
1331 }
1332
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001333 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001334 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001335
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001336 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001337 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001338 return -1;
1339 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001340 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001341 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001342 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001343 ins->rex &= ~REX_W;
1344 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001345 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001346 ins->rex |= REX_W;
1347 bad32 &= ~REX_W;
1348 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001349 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001350 /* Follow REX_W */
1351 break;
1352 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001353
H. Peter Anvinfc561202011-07-07 16:58:22 -07001354 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001355 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001356 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001357 } else if (!(ins->rex & REX_EV) &&
1358 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001359 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001360 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001361 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001362 if (ins->rex & REX_EV)
1363 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001364 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1365 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001366 length += 3;
1367 else
1368 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001369 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001370 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001371 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001372 return -1;
1373 } else if (bits == 64) {
1374 length++;
1375 } else if ((ins->rex & REX_L) &&
1376 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001377 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001378 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001379 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001380 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001381 length++;
1382 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001383 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001384 return -1;
1385 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001386 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001387
1388 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001389 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001390 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001391 "instruction is not lockable");
1392 }
1393
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001394 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001395
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001396 /*
1397 * when BND prefix is set by DEFAULT directive,
1398 * BND prefix is added to every appropriate instruction line
1399 * unless it is overridden by NOBND prefix.
1400 */
1401 if (globalbnd &&
1402 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1403 ins->prefixes[PPS_REP] = P_BND;
1404
H. Peter Anvina77692b2016-09-20 14:04:33 -07001405 /*
1406 * Add length of legacy prefixes
1407 */
1408 length += emit_prefix(NULL, bits, ins);
1409
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001410 return length;
1411}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001412
H. Peter Anvina77692b2016-09-20 14:04:33 -07001413static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001414{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001415 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001416 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001417 !(ins->rex & (REX_V | REX_EV)) &&
1418 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001419 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1420 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001421 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001422 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001423 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001424}
1425
H. Peter Anvina77692b2016-09-20 14:04:33 -07001426static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1427{
1428 int bytes = 0;
1429 int j;
1430
1431 for (j = 0; j < MAXPREFIX; j++) {
1432 uint8_t c = 0;
1433 switch (ins->prefixes[j]) {
1434 case P_WAIT:
1435 c = 0x9B;
1436 break;
1437 case P_LOCK:
1438 c = 0xF0;
1439 break;
1440 case P_REPNE:
1441 case P_REPNZ:
1442 case P_XACQUIRE:
1443 case P_BND:
1444 c = 0xF2;
1445 break;
1446 case P_REPE:
1447 case P_REPZ:
1448 case P_REP:
1449 case P_XRELEASE:
1450 c = 0xF3;
1451 break;
1452 case R_CS:
1453 if (bits == 64) {
1454 nasm_error(ERR_WARNING | ERR_PASS2,
1455 "cs segment base generated, but will be ignored in 64-bit mode");
1456 }
1457 c = 0x2E;
1458 break;
1459 case R_DS:
1460 if (bits == 64) {
1461 nasm_error(ERR_WARNING | ERR_PASS2,
1462 "ds segment base generated, but will be ignored in 64-bit mode");
1463 }
1464 c = 0x3E;
1465 break;
1466 case R_ES:
1467 if (bits == 64) {
1468 nasm_error(ERR_WARNING | ERR_PASS2,
1469 "es segment base generated, but will be ignored in 64-bit mode");
1470 }
1471 c = 0x26;
1472 break;
1473 case R_FS:
1474 c = 0x64;
1475 break;
1476 case R_GS:
1477 c = 0x65;
1478 break;
1479 case R_SS:
1480 if (bits == 64) {
1481 nasm_error(ERR_WARNING | ERR_PASS2,
1482 "ss segment base generated, but will be ignored in 64-bit mode");
1483 }
1484 c = 0x36;
1485 break;
1486 case R_SEGR6:
1487 case R_SEGR7:
1488 nasm_error(ERR_NONFATAL,
1489 "segr6 and segr7 cannot be used as prefixes");
1490 break;
1491 case P_A16:
1492 if (bits == 64) {
1493 nasm_error(ERR_NONFATAL,
1494 "16-bit addressing is not supported "
1495 "in 64-bit mode");
1496 } else if (bits != 16)
1497 c = 0x67;
1498 break;
1499 case P_A32:
1500 if (bits != 32)
1501 c = 0x67;
1502 break;
1503 case P_A64:
1504 if (bits != 64) {
1505 nasm_error(ERR_NONFATAL,
1506 "64-bit addressing is only supported "
1507 "in 64-bit mode");
1508 }
1509 break;
1510 case P_ASP:
1511 c = 0x67;
1512 break;
1513 case P_O16:
1514 if (bits != 16)
1515 c = 0x66;
1516 break;
1517 case P_O32:
1518 if (bits == 16)
1519 c = 0x66;
1520 break;
1521 case P_O64:
1522 /* REX.W */
1523 break;
1524 case P_OSP:
1525 c = 0x66;
1526 break;
1527 case P_EVEX:
1528 case P_VEX3:
1529 case P_VEX2:
1530 case P_NOBND:
1531 case P_none:
1532 break;
1533 default:
1534 nasm_panic(0, "invalid instruction prefix");
1535 }
1536 if (c) {
1537 if (data)
1538 out_rawbyte(data, c);
1539 bytes++;
1540 }
1541 }
1542 return bytes;
1543}
1544
1545static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001546{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001547 uint8_t c;
1548 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001549 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001550 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001551 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001552 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001553 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001554 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001555 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001556 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001557 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001558
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001559 ins->rex_done = false;
1560
H. Peter Anvina77692b2016-09-20 14:04:33 -07001561 emit_prefix(data, bits, ins);
1562
H. Peter Anvin839eca22007-10-29 23:12:47 -07001563 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001564 c = *codes++;
1565 op1 = (c & 3) + ((opex & 1) << 2);
1566 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1567 opx = &ins->oprs[op1];
1568 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001569
H. Peter Anvina77692b2016-09-20 14:04:33 -07001570
H. Peter Anvin839eca22007-10-29 23:12:47 -07001571 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 case 01:
1573 case 02:
1574 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001575 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001576 emit_rex(data, ins);
1577 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001578 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001580
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001581 case 05:
1582 case 06:
1583 case 07:
1584 opex = c;
1585 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001586
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001587 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001588 emit_rex(data, ins);
1589 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001590 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001591
Jin Kyu Song164d6072013-10-15 19:10:13 -07001592 case4(014):
1593 break;
1594
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001595 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001596 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001597 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001598 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001599 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001601
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001602 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001603 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001604 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001605 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001606 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001608
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001609 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001610 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001611 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001613
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001614 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001615 if (opx->type & (BITS16 | BITS32))
1616 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 else
1618 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001619 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001620 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001622
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001623 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001624 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001625 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001627
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001628 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001629 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001630 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001631 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001633
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001634 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001635 if (opx->segment == data->segment) {
1636 int64_t delta = opx->offset - data->offset
1637 - (data->inslen - data->insoffs);
1638 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001639 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001640 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001641 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001643
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001644 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001645 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001646 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001647
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001648 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001649 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001651
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001653 if (opx->type & (BITS16 | BITS32 | BITS64))
1654 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 else
1656 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001657
1658 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001660
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001661 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001662 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001664
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001665 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001666 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001667 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001668 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001669 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001670 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001671
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001672 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001673 {
1674 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1675 const struct operand *opy;
1676
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001677 c = *codes++;
1678 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001679 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001680 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001681 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001682 "non-absolute expression not permitted as argument %d",
1683 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001684 } else if (opy->offset & ~mask) {
1685 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1686 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001687 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001688 c = opy->offset & mask;
1689 goto emit_is4;
1690 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001691
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001692 case 0173:
1693 c = *codes++;
1694 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001695 c &= 15;
1696 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001697
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001698 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001699 c = 0;
1700 emit_is4:
1701 r = nasm_regvals[opx->basereg];
1702 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001703 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001704
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001705 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001706 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001707 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001708 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001709 "signed dword immediate exceeds bounds");
1710 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001711 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001712 break;
1713
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001714 case4(0240):
1715 case 0250:
1716 codes += 3;
1717 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1718 EVEX_P2Z | EVEX_P2AAA, 2);
1719 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1720 bytes[0] = 0x62;
1721 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001722 bytes[1] = ((((ins->rex & 7) << 5) |
1723 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001724 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001725 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1726 ((~ins->vexreg & 15) << 3) |
1727 (1 << 2) | (ins->vex_wlp & 3);
1728 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001729 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001730 break;
1731
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001732 case4(0260):
1733 case 0270:
1734 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001735 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1736 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001737 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1738 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1739 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001740 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001741 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001742 } else {
1743 bytes[0] = 0xc5;
1744 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001745 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001746 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001747 }
1748 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001749
H. Peter Anvine014f352012-02-25 22:35:19 -08001750 case 0271:
1751 case 0272:
1752 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001753 break;
1754
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001755 case4(0274):
1756 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001757 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001758 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001759
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001760 if (absolute_op(opx)) {
1761 if (ins->rex & REX_W)
1762 s = 64;
1763 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1764 s = 16;
1765 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1766 s = 32;
1767 else
1768 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001769
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001770 um = (uint64_t)2 << (s-1);
1771 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001772
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001773 if (uv > 127 && uv < (uint64_t)-128 &&
1774 (uv < um-128 || uv > um-1)) {
1775 /* If this wasn't explicitly byte-sized, warn as though we
1776 * had fallen through to the imm16/32/64 case.
1777 */
1778 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1779 "%s value exceeds bounds",
1780 (opx->type & BITS8) ? "signed byte" :
1781 s == 16 ? "word" :
1782 s == 32 ? "dword" :
1783 "signed dword");
1784 }
1785
1786 /* Output as a raw byte to avoid byte overflow check */
1787 out_rawbyte(data, (uint8_t)uv);
1788 } else {
1789 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001790 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001791 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001792 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001793
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001794 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001796
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001798 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1799 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001801
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001803 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1804 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001806
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 case 0312:
1808 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001809
Keith Kaniosb7a89542007-04-12 02:40:54 +00001810 case 0313:
1811 ins->rex = 0;
1812 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001813
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001814 case4(0314):
1815 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001816
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001820
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001822 case 0323:
1823 break;
1824
Keith Kaniosb7a89542007-04-12 02:40:54 +00001825 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001826 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001828
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001829 case 0325:
1830 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001831
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001832 case 0326:
1833 break;
1834
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001836 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001838
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001841
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001842 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001844 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001846
Keith Kanios48af1772007-08-17 07:37:52 +00001847 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001848 if (ins->rex & REX_R)
1849 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001850 ins->rex &= ~(REX_L|REX_R);
1851 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001852
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001853 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001854 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001855
H. Peter Anvin962e3052008-08-28 17:47:16 -07001856 case 0336:
1857 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001858 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001859
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001862 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001863
1864 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001866
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001867 case 0341:
1868 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001869
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001870 case 0360:
1871 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001872
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001873 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001874 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001875 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001876
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001877 case 0364:
1878 case 0365:
1879 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001880
Keith Kanios48af1772007-08-17 07:37:52 +00001881 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001882 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001883 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001884 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001885
Jin Kyu Song03041092013-10-15 19:38:51 -07001886 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001887 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001888
H. Peter Anvine2c80182005-01-15 22:15:51 +00001889 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001890 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001892
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001893 case 0374:
1894 eat = EA_XMMVSIB;
1895 break;
1896
1897 case 0375:
1898 eat = EA_YMMVSIB;
1899 break;
1900
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001901 case 0376:
1902 eat = EA_ZMMVSIB;
1903 break;
1904
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001905 case4(0100):
1906 case4(0110):
1907 case4(0120):
1908 case4(0130):
1909 case4(0200):
1910 case4(0204):
1911 case4(0210):
1912 case4(0214):
1913 case4(0220):
1914 case4(0224):
1915 case4(0230):
1916 case4(0234):
1917 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918 ea ea_data;
1919 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001920 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001921 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001922 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001923
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001924 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001925 /* pick rfield from operand b (opx) */
1926 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001927 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001928 } else {
1929 /* rfield is constant */
1930 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001931 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001932 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001934 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001935 rfield, rflags, ins, &errmsg) != eat)
1936 nasm_error(ERR_NONFATAL, "%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07001937
H. Peter Anvine2c80182005-01-15 22:15:51 +00001938 p = bytes;
1939 *p++ = ea_data.modrm;
1940 if (ea_data.sib_present)
1941 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001942 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001943
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001944 /*
1945 * Make sure the address gets the right offset in case
1946 * the line breaks in the .lst file (BR 1197827)
1947 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001948
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001949 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001950 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001951 if (ea_data.disp8) {
1952 out_rawbyte(data, ea_data.disp8);
1953 } else if (ea_data.rip) {
1954 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001955 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001956 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001957
H. Peter Anvina77692b2016-09-20 14:04:33 -07001958 if (overflow_general(opy->offset, asize) ||
1959 signed_bits(opy->offset, ins->addr_size) !=
1960 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001961 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001962
H. Peter Anvina77692b2016-09-20 14:04:33 -07001963 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001964 (asize > ea_data.bytes)
1965 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001966 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001967 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001968 }
1969 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001970
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001971 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001972 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001973 ": instruction code \\%o (0x%02X) given", c, c);
1974 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001975 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001976 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001977}
1978
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001979static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001980{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001981 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001982 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001983 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001984}
1985
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001986static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001987{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001988 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001989 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001990 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001991}
1992
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001993static int op_rexflags(const operand * o, int mask)
1994{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001995 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001996 int val;
1997
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001998 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001999 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002000
H. Peter Anvina4835d42008-05-20 14:21:29 -07002001 flags = nasm_reg_flags[o->basereg];
2002 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002003
2004 return rexflags(val, flags, mask);
2005}
2006
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002007static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002008{
2009 int rex = 0;
2010
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002011 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002012 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002013 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002014 rex |= REX_W;
2015 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2016 rex |= REX_H;
2017 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2018 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002019
2020 return rex & mask;
2021}
2022
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002023static int evexflags(int val, decoflags_t deco,
2024 int mask, uint8_t byte)
2025{
2026 int evex = 0;
2027
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002028 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002029 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002030 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002031 evex |= (EVEX_P0RP | EVEX_P0X);
2032 break;
2033 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002034 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002035 evex |= EVEX_P2VP;
2036 if (deco & Z)
2037 evex |= EVEX_P2Z;
2038 if (deco & OPMASK_MASK)
2039 evex |= deco & EVEX_P2AAA;
2040 break;
2041 }
2042 return evex & mask;
2043}
2044
2045static int op_evexflags(const operand * o, int mask, uint8_t byte)
2046{
2047 int val;
2048
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002049 val = nasm_regvals[o->basereg];
2050
2051 return evexflags(val, o->decoflags, mask, byte);
2052}
2053
H. Peter Anvin23595f52009-07-25 17:44:25 -07002054static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002055 insn *instruction,
2056 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002057{
2058 const struct itemplate *temp;
2059 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002060 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002061 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002062 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002063 int i;
2064
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002065 /* broadcasting uses a different data element size */
2066 for (i = 0; i < instruction->operands; i++)
2067 if (i == broadcast)
2068 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2069 else
2070 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002071
2072 merr = MERR_INVALOP;
2073
2074 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002075 temp->opcode != I_none; temp++) {
2076 m = matches(temp, instruction, bits);
2077 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002078 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002079 m = MOK_GOOD;
2080 else
2081 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002082 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002083 /*
2084 * Missing operand size and a candidate for fuzzy matching...
2085 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002086 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002087 if (i == broadcast)
2088 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2089 else
2090 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002091 opsizemissing = true;
2092 }
2093 if (m > merr)
2094 merr = m;
2095 if (merr == MOK_GOOD)
2096 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002097 }
2098
2099 /* No match, but see if we can get a fuzzy operand size match... */
2100 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002101 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002102
2103 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002104 /*
2105 * We ignore extrinsic operand sizes on registers, so we should
2106 * never try to fuzzy-match on them. This also resolves the case
2107 * when we have e.g. "xmmrm128" in two different positions.
2108 */
2109 if (is_class(REGISTER, instruction->oprs[i].type))
2110 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002111
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002112 /* This tests if xsizeflags[i] has more than one bit set */
2113 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2114 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002115
Jin Kyu Song7903c072013-10-30 03:00:12 -07002116 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002117 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002118 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2119 BITS32 : BITS64);
2120 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002121 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002122 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002123 }
2124
2125 /* Try matching again... */
2126 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002127 temp->opcode != I_none; temp++) {
2128 m = matches(temp, instruction, bits);
2129 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002130 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002131 m = MOK_GOOD;
2132 else
2133 m = MERR_INVALOP;
2134 }
2135 if (m > merr)
2136 merr = m;
2137 if (merr == MOK_GOOD)
2138 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002139 }
2140
H. Peter Anvina81655b2009-07-25 18:15:28 -07002141done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002142 *tempp = temp;
2143 return merr;
2144}
2145
Mark Charneydcaef4b2014-10-09 13:45:17 -04002146static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2147{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002148 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002149 uint8_t brcast_num;
2150
Mark Charneydcaef4b2014-10-09 13:45:17 -04002151 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002152 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002153 "size of broadcasting element is greater than 64 bits");
2154
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002155 /*
2156 * The shift term is to take care of the extra BITS80 inserted
2157 * between BITS64 and BITS128.
2158 */
2159 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2160 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002161
2162 return brcast_num;
2163}
2164
H. Peter Anvin65289e82009-07-25 17:25:11 -07002165static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002166 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002167{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002168 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002169 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002170 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002171
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002172 /*
2173 * Check the opcode
2174 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002175 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002176 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002177
2178 /*
2179 * Count the operands
2180 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002181 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002182 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002183
2184 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002185 * Is it legal?
2186 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002187 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002188 return MERR_INVALOP;
2189
2190 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002191 * {evex} available?
2192 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002193 switch (instruction->prefixes[PPS_VEX]) {
2194 case P_EVEX:
2195 if (!itemp_has(itemp, IF_EVEX))
2196 return MERR_ENCMISMATCH;
2197 break;
2198 case P_VEX3:
2199 case P_VEX2:
2200 if (!itemp_has(itemp, IF_VEX))
2201 return MERR_ENCMISMATCH;
2202 break;
2203 default:
2204 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002205 }
2206
2207 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002208 * Check that no spurious colons or TOs are present
2209 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002210 for (i = 0; i < itemp->operands; i++)
2211 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002212 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002213
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002214 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002215 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002216 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002217 switch (itemp_smask(itemp)) {
2218 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002219 asize = BITS8;
2220 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002221 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002222 asize = BITS16;
2223 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002224 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002225 asize = BITS32;
2226 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002227 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002228 asize = BITS64;
2229 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002230 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002231 asize = BITS128;
2232 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002233 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002234 asize = BITS256;
2235 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002236 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002237 asize = BITS512;
2238 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002239 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002240 switch (bits) {
2241 case 16:
2242 asize = BITS16;
2243 break;
2244 case 32:
2245 asize = BITS32;
2246 break;
2247 case 64:
2248 asize = BITS64;
2249 break;
2250 default:
2251 asize = 0;
2252 break;
2253 }
2254 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002255 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002256 asize = 0;
2257 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002258 }
2259
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002260 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002261 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002262 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002263 memset(size, 0, sizeof size);
2264 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002265 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002266 /* S- flags apply to all operands */
2267 for (i = 0; i < MAX_OPERANDS; i++)
2268 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002269 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002270
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002271 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002272 * Check that the operand flags all match up,
2273 * it's a bit tricky so lets be verbose:
2274 *
2275 * 1) Find out the size of operand. If instruction
2276 * doesn't have one specified -- we're trying to
2277 * guess it either from template (IF_S* flag) or
2278 * from code bits.
2279 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002280 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002281 * template has an operand size specified AND this size differ
2282 * from which instruction has (perhaps we got it from code bits)
2283 * we are:
2284 * a) Check that only size of instruction and operand is differ
2285 * other characteristics do match
2286 * b) Perhaps it's a register specified in instruction so
2287 * for such a case we just mark that operand as "size
2288 * missing" and this will turn on fuzzy operand size
2289 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002290 */
2291 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002292 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002293 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002294 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002295 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002296 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002297 opflags_t template_opsize, insn_opsize;
2298
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002299 if (!(type & SIZE_MASK))
2300 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002301
Jin Kyu Song7903c072013-10-30 03:00:12 -07002302 insn_opsize = type & SIZE_MASK;
2303 if (!is_broadcast) {
2304 template_opsize = itemp->opd[i] & SIZE_MASK;
2305 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002306 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2307
2308 if (~ideco & BRDCAST_MASK)
2309 return MERR_BRNOTHERE;
2310
Jin Kyu Song7903c072013-10-30 03:00:12 -07002311 /*
2312 * when broadcasting, the element size depends on
2313 * the instruction type. decorator flag should match.
2314 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002315 if (deco_brsize) {
2316 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002317 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002318 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002319 } else {
2320 template_opsize = 0;
2321 }
2322 }
2323
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002324 if (~ideco & deco & OPMASK_MASK)
2325 return MERR_MASKNOTHERE;
2326
2327 if (itemp->opd[i] & ~type & ~SIZE_MASK) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002328 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002329 } else if (template_opsize) {
2330 if (template_opsize != insn_opsize) {
2331 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002332 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002333 } else if (!is_class(REGISTER, type)) {
2334 /*
2335 * Note: we don't honor extrinsic operand sizes for registers,
2336 * so "missing operand size" for a register should be
2337 * considered a wildcard match rather than an error.
2338 */
2339 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002340 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002341 } else if (is_broadcast &&
2342 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002343 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002344 /*
2345 * broadcasting opsize matches but the number of repeated memory
2346 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002347 * if 64b double precision float is broadcasted to ymm (256b),
2348 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002349 */
2350 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002351 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002352 }
2353 }
2354
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002355 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002356 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002357
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002358 /*
2359 * Check operand sizes
2360 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002361 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2362 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002364 asize = itemp->opd[i] & SIZE_MASK;
2365 if (asize) {
2366 for (i = 0; i < oprs; i++)
2367 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002368 break;
2369 }
2370 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002371 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002373 }
2374
Keith Kaniosb7a89542007-04-12 02:40:54 +00002375 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 if (!(itemp->opd[i] & SIZE_MASK) &&
2377 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002378 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002379 }
2380
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002381 /*
2382 * Check template is okay at the set cpu level
2383 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002384 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002385 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002386
Keith Kaniosb7a89542007-04-12 02:40:54 +00002387 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002388 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002389 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002390 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002391 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002392
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002393 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002394 * If we have a HLE prefix, look for the NOHLE flag
2395 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002396 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002397 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2398 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2399 return MERR_BADHLE;
2400
2401 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002402 * Check if special handling needed for Jumps
2403 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002404 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002405 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002406
Jin Kyu Song03041092013-10-15 19:38:51 -07002407 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002408 * Check if BND prefix is allowed.
2409 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002410 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002411 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002412 (has_prefix(instruction, PPS_REP, P_BND) ||
2413 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002414 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002415 else if (itemp_has(itemp, IF_BND) &&
2416 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2417 has_prefix(instruction, PPS_REP, P_REPNZ)))
2418 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002419
H. Peter Anvin60926242009-07-26 16:25:38 -07002420 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002421}
2422
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002423/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002424 * Check if ModR/M.mod should/can be 01.
2425 * - EAF_BYTEOFFS is set
2426 * - offset can fit in a byte when EVEX is not used
2427 * - offset can be compressed when EVEX is used
2428 */
2429#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2430 (o >= -128 && o <= 127 && \
2431 seg == NO_SEG && !forw_ref && \
2432 !(input->eaflags & EAF_WORDOFFS) && \
2433 !(ins->rex & REX_EV)) || \
2434 (ins->rex & REX_EV && \
2435 is_disp8n(input, ins, &output->disp8)))
2436
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002437static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002438 int rfield, opflags_t rflags, insn *ins,
2439 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002440{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002441 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002442 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002443 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002444
H. Peter Anvin8f622462017-04-02 19:02:29 -07002445 *errmsg = "invalid effective address"; /* Default error message */
2446
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002447 output->type = EA_SCALAR;
2448 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002449 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002450
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002451 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002452 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002453 /* EVEX.R' flag for the REG operand */
2454 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002455
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002456 if (is_class(REGISTER, input->type)) {
2457 /*
2458 * It's a direct register.
2459 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002460 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002461 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002462
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002463 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002464 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002465
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002466 /* broadcasting is not available with a direct register operand. */
2467 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002468 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002469 goto err;
2470 }
2471
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002472 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002473 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002474 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002475 output->bytes = 0; /* no offset necessary either */
2476 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2477 } else {
2478 /*
2479 * It's a memory reference.
2480 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002481
2482 /* Embedded rounding or SAE is not available with a mem ref operand. */
2483 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002484 *errmsg = "embedded rounding is available only with "
2485 "register-register operations";
2486 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002487 }
2488
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002489 if (input->basereg == -1 &&
2490 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002491 /*
2492 * It's a pure offset.
2493 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002494 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002495 if (input->segment == NO_SEG ||
2496 (input->opflags & OPFLAG_RELATIVE)) {
2497 nasm_error(ERR_WARNING | ERR_PASS2,
2498 "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002499 input->type &= ~IP_REL;
2500 input->type |= MEMORY;
2501 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002502 }
2503
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002504 if (bits == 64 &&
2505 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002506 *errmsg = "RIP-relative addressing is prohibited for MIB.";
2507 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002508 }
2509
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002510 if (eaflags & EAF_BYTEOFFS ||
2511 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002512 input->disp_size != (addrbits != 16 ? 32 : 16))) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002513 nasm_error(ERR_WARNING | ERR_PASS1,
2514 "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002515 }
2516
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002517 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002518 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002519 output->sib = GEN_SIB(0, 4, 5);
2520 output->bytes = 4;
2521 output->modrm = GEN_MODRM(0, rfield, 4);
2522 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002523 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002524 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002525 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002526 output->modrm = GEN_MODRM(0, rfield,
2527 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002528 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002529 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002530 } else {
2531 /*
2532 * It's an indirection.
2533 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002534 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002535 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002536 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002537 int t, it, bt; /* register numbers */
2538 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002539
H. Peter Anvine2c80182005-01-15 22:15:51 +00002540 if (s == 0)
2541 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002542
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002543 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002544 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002545 ix = nasm_reg_flags[i];
2546 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002547 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002548 ix = 0;
2549 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002550
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002551 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002552 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002553 bx = nasm_reg_flags[b];
2554 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002555 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002556 bx = 0;
2557 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002558
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002559 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002560 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002561 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002562 int32_t o = input->offset;
2563 int mod, scale, index, base;
2564
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002565 /*
2566 * For a vector SIB, one has to be a vector and the other,
2567 * if present, a GPR. The vector must be the index operand.
2568 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002569 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002570 if (s == 0)
2571 s = 1;
2572 else if (s != 1)
2573 goto err;
2574
2575 t = bt, bt = it, it = t;
2576 x = bx, bx = ix, ix = x;
2577 }
2578
2579 if (bt != -1) {
2580 if (REG_GPR & ~bx)
2581 goto err;
2582 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2583 sok &= bx;
2584 else
2585 goto err;
2586 }
2587
2588 /*
2589 * While we're here, ensure the user didn't specify
2590 * WORD or QWORD
2591 */
2592 if (input->disp_size == 16 || input->disp_size == 64)
2593 goto err;
2594
2595 if (addrbits == 16 ||
2596 (addrbits == 32 && !(sok & BITS32)) ||
2597 (addrbits == 64 && !(sok & BITS64)))
2598 goto err;
2599
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002600 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2601 : ((ix & YMMREG & ~REG_EA)
2602 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002603
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002604 output->rex |= rexflags(it, ix, REX_X);
2605 output->rex |= rexflags(bt, bx, REX_B);
2606 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002607
2608 index = it & 7; /* it is known to be != -1 */
2609
2610 switch (s) {
2611 case 1:
2612 scale = 0;
2613 break;
2614 case 2:
2615 scale = 1;
2616 break;
2617 case 4:
2618 scale = 2;
2619 break;
2620 case 8:
2621 scale = 3;
2622 break;
2623 default: /* then what the smeg is it? */
2624 goto err; /* panic */
2625 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002626
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002627 if (bt == -1) {
2628 base = 5;
2629 mod = 0;
2630 } else {
2631 base = (bt & 7);
2632 if (base != REG_NUM_EBP && o == 0 &&
2633 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002634 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002635 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002636 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002637 mod = 1;
2638 else
2639 mod = 2;
2640 }
2641
2642 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002643 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2644 output->modrm = GEN_MODRM(mod, rfield, 4);
2645 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002646 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002647 /*
2648 * it must be a 32/64-bit memory reference. Firstly we have
2649 * to check that all registers involved are type E/Rxx.
2650 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002651 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002652 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002653
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002654 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002655 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2656 sok &= ix;
2657 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002658 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002659 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002660
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002661 if (bt != -1) {
2662 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002663 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002664 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002665 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002666 sok &= bx;
2667 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002668
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002669 /*
2670 * While we're here, ensure the user didn't specify
2671 * WORD or QWORD
2672 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002673 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002674 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002675
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002676 if (addrbits == 16 ||
2677 (addrbits == 32 && !(sok & BITS32)) ||
2678 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002679 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002680
Keith Kaniosb7a89542007-04-12 02:40:54 +00002681 /* now reorganize base/index */
2682 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002683 ((hb == b && ht == EAH_NOTBASE) ||
2684 (hb == i && ht == EAH_MAKEBASE))) {
2685 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002686 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002687 x = bx, bx = ix, ix = x;
2688 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002689
Jin Kyu Song164d6072013-10-15 19:10:13 -07002690 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002691 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002692 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002693 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002694 if (eaflags & EAF_MIB) {
2695 /* only for mib operands */
2696 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2697 /*
2698 * make a single reg index [reg*1].
2699 * gas uses this form for an explicit index register.
2700 */
2701 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2702 }
2703 if ((ht == EAH_SUMMED) && bt == -1) {
2704 /* separate once summed index into [base, index] */
2705 bt = it, bx = ix, s--;
2706 }
2707 } else {
2708 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002709 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002710 s == 3 || s == 5 || s == 9) && bt == -1) {
2711 /* convert 3*EAX to EAX+2*EAX */
2712 bt = it, bx = ix, s--;
2713 }
2714 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002715 (eaflags & EAF_TIMESTWO) &&
2716 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002717 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002718 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002719 * to sib format with 0x0 displacement - [EAX*1+0].
2720 */
2721 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2722 }
2723 }
Keith Kanios48af1772007-08-17 07:37:52 +00002724 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002725 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002726 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002727 x = ix, ix = bx, bx = x;
2728 }
2729 if (it == REG_NUM_ESP ||
2730 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002731 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002732
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002733 output->rex |= rexflags(it, ix, REX_X);
2734 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002735
Keith Kanios48af1772007-08-17 07:37:52 +00002736 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002737 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002739
Keith Kaniosb7a89542007-04-12 02:40:54 +00002740 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002742 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002743 } else {
2744 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002745 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002746 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002747 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002748 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002749 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002750 mod = 1;
2751 else
2752 mod = 2;
2753 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002754
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002755 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002756 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2757 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002758 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002759 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002761
Keith Kaniosb7a89542007-04-12 02:40:54 +00002762 if (it == -1)
2763 index = 4, s = 1;
2764 else
2765 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002766
H. Peter Anvine2c80182005-01-15 22:15:51 +00002767 switch (s) {
2768 case 1:
2769 scale = 0;
2770 break;
2771 case 2:
2772 scale = 1;
2773 break;
2774 case 4:
2775 scale = 2;
2776 break;
2777 case 8:
2778 scale = 3;
2779 break;
2780 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002781 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002783
Keith Kaniosb7a89542007-04-12 02:40:54 +00002784 if (bt == -1) {
2785 base = 5;
2786 mod = 0;
2787 } else {
2788 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002789 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002790 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002791 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002792 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002793 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002794 mod = 1;
2795 else
2796 mod = 2;
2797 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002798
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002799 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002800 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2801 output->modrm = GEN_MODRM(mod, rfield, 4);
2802 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 }
2804 } else { /* it's 16-bit */
2805 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002806 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002807
Keith Kaniosb7a89542007-04-12 02:40:54 +00002808 /* check for 64-bit long mode */
2809 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002810 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002811
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002813 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2814 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002815 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002816
Keith Kaniosb7a89542007-04-12 02:40:54 +00002817 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002818 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002819 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002820
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002822 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002823 if (b == -1 && i != -1) {
2824 int tmp = b;
2825 b = i;
2826 i = tmp;
2827 } /* swap */
2828 if ((b == R_SI || b == R_DI) && i != -1) {
2829 int tmp = b;
2830 b = i;
2831 i = tmp;
2832 }
2833 /* have BX/BP as base, SI/DI index */
2834 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002835 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002836 if (i != -1 && b != -1 &&
2837 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002838 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002839 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002840 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002841
H. Peter Anvine2c80182005-01-15 22:15:51 +00002842 rm = -1;
2843 if (i != -1)
2844 switch (i * 256 + b) {
2845 case R_SI * 256 + R_BX:
2846 rm = 0;
2847 break;
2848 case R_DI * 256 + R_BX:
2849 rm = 1;
2850 break;
2851 case R_SI * 256 + R_BP:
2852 rm = 2;
2853 break;
2854 case R_DI * 256 + R_BP:
2855 rm = 3;
2856 break;
2857 } else
2858 switch (b) {
2859 case R_SI:
2860 rm = 4;
2861 break;
2862 case R_DI:
2863 rm = 5;
2864 break;
2865 case R_BP:
2866 rm = 6;
2867 break;
2868 case R_BX:
2869 rm = 7;
2870 break;
2871 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002872 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002873 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002874
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002875 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002876 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002877 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002878 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002879 mod = 1;
2880 else
2881 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002882
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002883 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002884 output->bytes = mod; /* bytes of offset needed */
2885 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002886 }
2887 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002888 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002889
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002890 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002891 return output->type;
2892
2893err:
2894 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002895}
2896
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002897static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002898{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002899 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002900 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002901
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002902 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002903
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002904 switch (ins->prefixes[PPS_ASIZE]) {
2905 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002906 valid &= 16;
2907 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002908 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002909 valid &= 32;
2910 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002911 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002912 valid &= 64;
2913 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002914 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002915 valid &= (addrbits == 32) ? 16 : 32;
2916 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002917 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002918 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002919 }
2920
2921 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002922 if (is_class(MEMORY, ins->oprs[j].type)) {
2923 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002924
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002925 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002926 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002927 i = 0;
2928 else
2929 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002930
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002931 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002932 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002933 b = 0;
2934 else
2935 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002936
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002937 if (ins->oprs[j].scale == 0)
2938 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002939
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002940 if (!i && !b) {
2941 int ds = ins->oprs[j].disp_size;
2942 if ((addrbits != 64 && ds > 8) ||
2943 (addrbits == 64 && ds == 16))
2944 valid &= ds;
2945 } else {
2946 if (!(REG16 & ~b))
2947 valid &= 16;
2948 if (!(REG32 & ~b))
2949 valid &= 32;
2950 if (!(REG64 & ~b))
2951 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002952
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002953 if (!(REG16 & ~i))
2954 valid &= 16;
2955 if (!(REG32 & ~i))
2956 valid &= 32;
2957 if (!(REG64 & ~i))
2958 valid &= 64;
2959 }
2960 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002961 }
2962
2963 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002964 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002965 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002966 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002967 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002968 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002969 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002970 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002971 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002972 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002973 }
2974
2975 defdisp = ins->addr_size == 16 ? 16 : 32;
2976
2977 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002978 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2979 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2980 /*
2981 * mem_offs sizes must match the address size; if not,
2982 * strip the MEM_OFFS bit and match only EA instructions
2983 */
2984 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2985 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002986 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002987}