blob: a7a5f4689cf02603e9f30115fc834a2cab5927e3 [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;
H. Peter Anvin217e7142017-05-01 15:10:47 -0700498 data->toffset = opx->offset; /* Is this really needed/wanted? */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700499 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 Anvin5810c592017-05-01 19:51:09 -0700566
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567 if (t < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800568 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000569
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400571 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400573 if (wsize > 8) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800574 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400575 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000576 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700577 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700578 data.insoffs = 0;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700579 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700580 data.inslen = data.size = wsize;
581 data.toffset = e->offset;
582 data.tsegment = e->segment;
583 data.twrt = e->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800584 data.relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700585 out(&data);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400586 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700587 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400588 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700589 int align = e->stringlen % wsize;
590 if (align)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591 align = wsize - align;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700592
593 data.insoffs = 0;
594 data.inslen = e->stringlen + align;
595
596 out_rawdata(&data, e->stringval, e->stringlen);
597 out_rawdata(&data, zero_buffer, align);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 }
599 }
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700600 if (t > 0 && t == instruction->times - 1) {
H. Peter Anvin5810c592017-05-01 19:51:09 -0700601 lfmt->set_offset(start);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700602 lfmt->uplevel(LIST_TIMES);
603 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 }
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700605 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800606 lfmt->downlevel(LIST_TIMES);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700607 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700608 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 FILE *fp;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700610 size_t t = instruction->times;
611 off_t base = 0;
612 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700613 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700614 char *buf = NULL;
615 size_t blk = 0; /* Buffered I/O block size */
616 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000617
H. Peter Anvind81a2352016-09-21 14:03:18 -0700618 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400619 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800620 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700622 goto done;
623 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000624
H. Peter Anvind81a2352016-09-21 14:03:18 -0700625 len = nasm_file_size(fp);
626
627 if (len == (off_t)-1) {
628 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700629 fname);
630 goto close_done;
631 }
632
H. Peter Anvina77692b2016-09-20 14:04:33 -0700633 if (instruction->eops->next) {
634 base = instruction->eops->next->offset;
635 if (base >= len) {
636 len = 0;
637 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 len -= base;
639 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700640 len > (off_t)instruction->eops->next->next->offset)
641 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700644
H. Peter Anvina77692b2016-09-20 14:04:33 -0700645 lfmt->set_offset(data.offset);
646 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000647
H. Peter Anvind81a2352016-09-21 14:03:18 -0700648 if (!len)
649 goto end_incbin;
650
651 /* Try to map file data */
652 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700653 if (!map) {
654 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
655 buf = nasm_malloc(blk);
656 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700657
658 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700659 /*
660 * Consider these irrelevant for INCBIN, since it is fully
661 * possible that these might be (way) bigger than an int
662 * can hold; there is, however, no reason to widen these
663 * types just for INCBIN. data.inslen == 0 signals to the
664 * backend that these fields are meaningless, if at all
665 * needed.
666 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700667 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700668 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700669
H. Peter Anvind81a2352016-09-21 14:03:18 -0700670 if (map) {
671 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700672 } else if ((off_t)m == len) {
673 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700674 } else {
675 off_t l = len;
676
677 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700678 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700679 "`incbin': unable to seek on file `%s'",
680 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700681 goto end_incbin;
682 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700683 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700684 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700685 if (!m || feof(fp)) {
686 /*
687 * This shouldn't happen unless the file
688 * actually changes while we are reading
689 * it.
690 */
691 nasm_error(ERR_NONFATAL,
692 "`incbin': unexpected EOF while"
693 " reading file `%s'", fname);
694 goto end_incbin;
695 }
696 out_rawdata(&data, buf, m);
697 l -= m;
698 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700699 }
700 }
701 end_incbin:
702 lfmt->downlevel(LIST_INCBIN);
703 if (instruction->times > 1) {
H. Peter Anvin5810c592017-05-01 19:51:09 -0700704 lfmt->set_offset(start);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700705 lfmt->uplevel(LIST_TIMES);
706 lfmt->downlevel(LIST_TIMES);
707 }
708 if (ferror(fp)) {
709 nasm_error(ERR_NONFATAL,
710 "`incbin': error while"
711 " reading file `%s'", fname);
712 }
713 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700714 if (buf)
715 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700716 if (map)
717 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700718 fclose(fp);
719 done:
720 ;
721 } else {
722 /* "Real" instruction */
723
724 /* Check to see if we need an address-size prefix */
725 add_asp(instruction, bits);
726
727 m = find_match(&temp, instruction, data.segment, data.offset, bits);
728
729 if (m == MOK_GOOD) {
730 /* Matches! */
731 int64_t insn_size = calcsize(data.segment, data.offset,
732 bits, instruction, temp);
733 itimes = instruction->times;
734 if (insn_size < 0) /* shouldn't be, on pass two */
735 nasm_panic(0, "errors made it through from pass one");
736
737 data.itemp = temp;
738 data.bits = bits;
739
740 while (itimes--) {
741 data.insoffs = 0;
742 data.inslen = insn_size;
743
744 gencode(&data, instruction);
745 nasm_assert(data.insoffs == insn_size);
746
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700747 if (itimes > 0 && itimes == instruction->times - 1) {
H. Peter Anvin5810c592017-05-01 19:51:09 -0700748 lfmt->set_offset(start);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700749 lfmt->uplevel(LIST_TIMES);
750 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400751 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700752 if (instruction->times > 1)
753 lfmt->downlevel(LIST_TIMES);
754 } else {
755 /* No match */
756 switch (m) {
757 case MERR_OPSIZEMISSING:
758 nasm_error(ERR_NONFATAL, "operation size not specified");
759 break;
760 case MERR_OPSIZEMISMATCH:
761 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
762 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700763 case MERR_BRNOTHERE:
764 nasm_error(ERR_NONFATAL,
765 "broadcast not permitted on this operand");
766 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700767 case MERR_BRNUMMISMATCH:
768 nasm_error(ERR_NONFATAL,
769 "mismatch in the number of broadcasting elements");
770 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700771 case MERR_MASKNOTHERE:
772 nasm_error(ERR_NONFATAL,
773 "mask not permitted on this operand");
774 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700775 case MERR_BADCPU:
776 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
777 break;
778 case MERR_BADMODE:
779 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
780 bits);
781 break;
782 case MERR_ENCMISMATCH:
783 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
784 break;
785 case MERR_BADBND:
786 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
787 break;
788 case MERR_BADREPNE:
789 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
790 (has_prefix(instruction, PPS_REP, P_REPNE) ?
791 "repne" : "repnz"));
792 break;
793 default:
794 nasm_error(ERR_NONFATAL,
795 "invalid combination of opcode and operands");
796 break;
797 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400798 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000799 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700800 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000801}
802
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800803int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000804{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000805 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700806 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000807
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400808 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000809 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000810
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700811 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
812 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400813 instruction->opcode == I_DT || instruction->opcode == I_DO ||
814 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300816 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000817
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300819 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000820
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400821 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000822 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000823
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400825 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400827 warn_overflow_const(e->offset, wsize);
828 } else if (e->type == EOT_DB_STRING ||
829 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000831
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 align = (-osize) % wsize;
833 if (align < 0)
834 align += wsize;
835 isize += osize + align;
836 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700837 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000838 }
839
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400841 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700842 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000843
H. Peter Anvind81a2352016-09-21 14:03:18 -0700844 len = nasm_file_size_by_path(fname);
845 if (len == (off_t)-1) {
846 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
847 fname);
848 return 0;
849 }
850
851 if (instruction->eops->next) {
852 if (len <= (off_t)instruction->eops->next->offset) {
853 len = 0;
854 } else {
855 len -= instruction->eops->next->offset;
856 if (instruction->eops->next->next &&
857 len > (off_t)instruction->eops->next->next->offset) {
858 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000859 }
860 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700862
863 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000864 }
865
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700866 /* Check to see if we need an address-size prefix */
867 add_asp(instruction, bits);
868
H. Peter Anvin23595f52009-07-25 17:44:25 -0700869 m = find_match(&temp, instruction, segment, offset, bits);
870 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400871 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700872 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700873 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400874 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000875 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000876}
877
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800878static void bad_hle_warn(const insn * ins, uint8_t hleok)
879{
880 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800881 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800882 static const enum whatwarn warn[2][4] =
883 {
884 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
885 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
886 };
887 unsigned int n;
888
889 n = (unsigned int)rep_pfx - P_XACQUIRE;
890 if (n > 1)
891 return; /* Not XACQUIRE/XRELEASE */
892
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800893 ww = warn[n][hleok];
894 if (!is_class(MEMORY, ins->oprs[0].type))
895 ww = w_inval; /* HLE requires operand 0 to be memory */
896
897 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800898 case w_none:
899 break;
900
901 case w_lock:
902 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800903 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800904 "%s with this instruction requires lock",
905 prefix_name(rep_pfx));
906 }
907 break;
908
909 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800910 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800911 "%s invalid with this instruction",
912 prefix_name(rep_pfx));
913 break;
914 }
915}
916
H. Peter Anvin507ae032008-10-09 15:37:10 -0700917/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400918#define case3(x) case (x): case (x)+1: case (x)+2
919#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700920
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800921static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800922 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000923{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800924 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800925 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000926 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000927 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700928 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700929 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700930 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700931 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800932 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800933 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700934 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -0700935 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000936
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700937 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700938 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700939 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700940
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700941 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400942 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700943
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 (void)segment; /* Don't warn that this parameter is unused */
945 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000946
H. Peter Anvin839eca22007-10-29 23:12:47 -0700947 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400948 c = *codes++;
949 op1 = (c & 3) + ((opex & 1) << 2);
950 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
951 opx = &ins->oprs[op1];
952 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700953
H. Peter Anvin839eca22007-10-29 23:12:47 -0700954 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400955 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 codes += c, length += c;
957 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700958
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400959 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400960 opex = c;
961 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700962
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400963 case4(010):
964 ins->rex |=
965 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 codes++, length++;
967 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700968
Jin Kyu Song164d6072013-10-15 19:10:13 -0700969 case4(014):
970 /* this is an index reg of MIB operand */
971 mib_index = opx->basereg;
972 break;
973
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400974 case4(020):
975 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 length++;
977 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700978
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400979 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 length += 2;
981 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700982
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400983 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700984 if (opx->type & (BITS16 | BITS32 | BITS64))
985 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 else
987 length += (bits == 16) ? 2 : 4;
988 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700989
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400990 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 length += 4;
992 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700993
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400994 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700995 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700997
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400998 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 length++;
1000 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001001
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001002 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001003 length += 8; /* MOV reg64/imm */
1004 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001005
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001006 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 length += 2;
1008 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001009
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001010 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001011 if (opx->type & (BITS16 | BITS32 | BITS64))
1012 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001013 else
1014 length += (bits == 16) ? 2 : 4;
1015 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001016
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001017 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 length += 4;
1019 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001020
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001021 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001022 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001024
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001025 case 0172:
1026 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001027 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001028 length++;
1029 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001030
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001031 case4(0174):
1032 length++;
1033 break;
1034
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001035 case4(0240):
1036 ins->rex |= REX_EV;
1037 ins->vexreg = regval(opx);
1038 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1039 ins->vex_cm = *codes++;
1040 ins->vex_wlp = *codes++;
1041 ins->evex_tuple = (*codes++ - 0300);
1042 break;
1043
1044 case 0250:
1045 ins->rex |= REX_EV;
1046 ins->vexreg = 0;
1047 ins->vex_cm = *codes++;
1048 ins->vex_wlp = *codes++;
1049 ins->evex_tuple = (*codes++ - 0300);
1050 break;
1051
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001052 case4(0254):
1053 length += 4;
1054 break;
1055
1056 case4(0260):
1057 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001058 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001059 ins->vex_cm = *codes++;
1060 ins->vex_wlp = *codes++;
1061 break;
1062
1063 case 0270:
1064 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001065 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001066 ins->vex_cm = *codes++;
1067 ins->vex_wlp = *codes++;
1068 break;
1069
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001070 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001071 hleok = c & 3;
1072 break;
1073
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001074 case4(0274):
1075 length++;
1076 break;
1077
1078 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001080
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001082 if (bits == 64)
1083 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001084 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001086
H. Peter Anvine2c80182005-01-15 22:15:51 +00001087 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001088 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001090
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001092 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001093
Keith Kaniosb7a89542007-04-12 02:40:54 +00001094 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001095 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1096 has_prefix(ins, PPS_ASIZE, P_A32))
1097 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001099
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001100 case4(0314):
1101 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001102
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001104 {
1105 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1106 if (pfx == P_O16)
1107 break;
1108 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001109 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001110 else
1111 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001113 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001114
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001116 {
1117 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1118 if (pfx == P_O32)
1119 break;
1120 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001121 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001122 else
1123 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001125 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001126
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 case 0322:
1128 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001129
Keith Kaniosb7a89542007-04-12 02:40:54 +00001130 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001131 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001132 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001133
Keith Kaniosb7a89542007-04-12 02:40:54 +00001134 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001135 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001136 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001137
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001138 case 0325:
1139 ins->rex |= REX_NH;
1140 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001141
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001142 case 0326:
1143 break;
1144
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 case 0330:
1146 codes++, length++;
1147 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001148
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001151
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001152 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 case 0333:
1154 length++;
1155 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001156
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001157 case 0334:
1158 ins->rex |= REX_L;
1159 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001160
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001161 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001162 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001163
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001164 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001165 if (!ins->prefixes[PPS_REP])
1166 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001167 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001168
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001169 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001170 if (!ins->prefixes[PPS_REP])
1171 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001172 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001173
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001175 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001176 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001178 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1179 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001180 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001182 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001184
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001185 case 0341:
1186 if (!ins->prefixes[PPS_WAIT])
1187 ins->prefixes[PPS_WAIT] = P_WAIT;
1188 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001189
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001190 case 0360:
1191 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001192
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001193 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001194 length++;
1195 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001196
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001197 case 0364:
1198 case 0365:
1199 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001200
Keith Kanios48af1772007-08-17 07:37:52 +00001201 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001202 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001203 length++;
1204 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001205
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001206 case 0370:
1207 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001208 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001209
H. Peter Anvine2c80182005-01-15 22:15:51 +00001210 case 0373:
1211 length++;
1212 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001213
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001214 case 0374:
1215 eat = EA_XMMVSIB;
1216 break;
1217
1218 case 0375:
1219 eat = EA_YMMVSIB;
1220 break;
1221
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001222 case 0376:
1223 eat = EA_ZMMVSIB;
1224 break;
1225
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001226 case4(0100):
1227 case4(0110):
1228 case4(0120):
1229 case4(0130):
1230 case4(0200):
1231 case4(0204):
1232 case4(0210):
1233 case4(0214):
1234 case4(0220):
1235 case4(0224):
1236 case4(0230):
1237 case4(0234):
1238 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001240 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001241 opflags_t rflags;
1242 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001243 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001244
Keith Kaniosb7a89542007-04-12 02:40:54 +00001245 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001246
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001247 if (c <= 0177) {
1248 /* pick rfield from operand b (opx) */
1249 rflags = regflag(opx);
1250 rfield = nasm_regvals[opx->basereg];
1251 } else {
1252 rflags = 0;
1253 rfield = c & 7;
1254 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001255
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001256 /* EVEX.b1 : evex_brerop contains the operand position */
1257 op_er_sae = (ins->evex_brerop >= 0 ?
1258 &ins->oprs[ins->evex_brerop] : NULL);
1259
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001260 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1261 /* set EVEX.b */
1262 ins->evex_p[2] |= EVEX_P2B;
1263 if (op_er_sae->decoflags & ER) {
1264 /* set EVEX.RC (rounding control) */
1265 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1266 & EVEX_P2RC;
1267 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001268 } else {
1269 /* set EVEX.L'L (vector length) */
1270 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001271 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001272 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001273 /* set EVEX.b */
1274 ins->evex_p[2] |= EVEX_P2B;
1275 }
1276 }
1277
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001278 if (itemp_has(temp, IF_MIB)) {
1279 opy->eaflags |= EAF_MIB;
1280 /*
1281 * if a separate form of MIB (ICC style) is used,
1282 * the index reg info is merged into mem operand
1283 */
1284 if (mib_index != R_none) {
1285 opy->indexreg = mib_index;
1286 opy->scale = 1;
1287 opy->hintbase = mib_index;
1288 opy->hinttype = EAH_NOTBASE;
1289 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001290 }
1291
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001292 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001293 rfield, rflags, ins, &errmsg) != eat) {
1294 nasm_error(ERR_NONFATAL, "%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001295 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001296 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001297 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001298 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001299 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001300 }
1301 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001302
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001303 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001304 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001305 ": instruction code \\%o (0x%02X) given", c, c);
1306 break;
1307 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001308 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001309
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001310 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001311
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001312 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001313 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001314 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001315 return -1;
1316 }
1317 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001318 }
1319
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001320 switch (ins->prefixes[PPS_VEX]) {
1321 case P_EVEX:
1322 if (!(ins->rex & REX_EV))
1323 return -1;
1324 break;
1325 case P_VEX3:
1326 case P_VEX2:
1327 if (!(ins->rex & REX_V))
1328 return -1;
1329 break;
1330 default:
1331 break;
1332 }
1333
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001334 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001335 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001336
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001337 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001338 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001339 return -1;
1340 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001341 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001342 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001343 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001344 ins->rex &= ~REX_W;
1345 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001346 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001347 ins->rex |= REX_W;
1348 bad32 &= ~REX_W;
1349 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001350 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001351 /* Follow REX_W */
1352 break;
1353 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001354
H. Peter Anvinfc561202011-07-07 16:58:22 -07001355 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001356 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001357 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001358 } else if (!(ins->rex & REX_EV) &&
1359 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001360 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001361 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001362 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001363 if (ins->rex & REX_EV)
1364 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001365 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1366 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001367 length += 3;
1368 else
1369 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001370 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001371 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001372 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001373 return -1;
1374 } else if (bits == 64) {
1375 length++;
1376 } else if ((ins->rex & REX_L) &&
1377 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001378 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001379 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001380 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001381 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001382 length++;
1383 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001384 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001385 return -1;
1386 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001387 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001388
1389 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001390 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001391 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001392 "instruction is not lockable");
1393 }
1394
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001395 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001396
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001397 /*
1398 * when BND prefix is set by DEFAULT directive,
1399 * BND prefix is added to every appropriate instruction line
1400 * unless it is overridden by NOBND prefix.
1401 */
1402 if (globalbnd &&
1403 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1404 ins->prefixes[PPS_REP] = P_BND;
1405
H. Peter Anvina77692b2016-09-20 14:04:33 -07001406 /*
1407 * Add length of legacy prefixes
1408 */
1409 length += emit_prefix(NULL, bits, ins);
1410
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001411 return length;
1412}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001413
H. Peter Anvina77692b2016-09-20 14:04:33 -07001414static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001415{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001416 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001417 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001418 !(ins->rex & (REX_V | REX_EV)) &&
1419 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001420 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1421 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001422 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001423 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001424 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001425}
1426
H. Peter Anvina77692b2016-09-20 14:04:33 -07001427static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1428{
1429 int bytes = 0;
1430 int j;
1431
1432 for (j = 0; j < MAXPREFIX; j++) {
1433 uint8_t c = 0;
1434 switch (ins->prefixes[j]) {
1435 case P_WAIT:
1436 c = 0x9B;
1437 break;
1438 case P_LOCK:
1439 c = 0xF0;
1440 break;
1441 case P_REPNE:
1442 case P_REPNZ:
1443 case P_XACQUIRE:
1444 case P_BND:
1445 c = 0xF2;
1446 break;
1447 case P_REPE:
1448 case P_REPZ:
1449 case P_REP:
1450 case P_XRELEASE:
1451 c = 0xF3;
1452 break;
1453 case R_CS:
1454 if (bits == 64) {
1455 nasm_error(ERR_WARNING | ERR_PASS2,
1456 "cs segment base generated, but will be ignored in 64-bit mode");
1457 }
1458 c = 0x2E;
1459 break;
1460 case R_DS:
1461 if (bits == 64) {
1462 nasm_error(ERR_WARNING | ERR_PASS2,
1463 "ds segment base generated, but will be ignored in 64-bit mode");
1464 }
1465 c = 0x3E;
1466 break;
1467 case R_ES:
1468 if (bits == 64) {
1469 nasm_error(ERR_WARNING | ERR_PASS2,
1470 "es segment base generated, but will be ignored in 64-bit mode");
1471 }
1472 c = 0x26;
1473 break;
1474 case R_FS:
1475 c = 0x64;
1476 break;
1477 case R_GS:
1478 c = 0x65;
1479 break;
1480 case R_SS:
1481 if (bits == 64) {
1482 nasm_error(ERR_WARNING | ERR_PASS2,
1483 "ss segment base generated, but will be ignored in 64-bit mode");
1484 }
1485 c = 0x36;
1486 break;
1487 case R_SEGR6:
1488 case R_SEGR7:
1489 nasm_error(ERR_NONFATAL,
1490 "segr6 and segr7 cannot be used as prefixes");
1491 break;
1492 case P_A16:
1493 if (bits == 64) {
1494 nasm_error(ERR_NONFATAL,
1495 "16-bit addressing is not supported "
1496 "in 64-bit mode");
1497 } else if (bits != 16)
1498 c = 0x67;
1499 break;
1500 case P_A32:
1501 if (bits != 32)
1502 c = 0x67;
1503 break;
1504 case P_A64:
1505 if (bits != 64) {
1506 nasm_error(ERR_NONFATAL,
1507 "64-bit addressing is only supported "
1508 "in 64-bit mode");
1509 }
1510 break;
1511 case P_ASP:
1512 c = 0x67;
1513 break;
1514 case P_O16:
1515 if (bits != 16)
1516 c = 0x66;
1517 break;
1518 case P_O32:
1519 if (bits == 16)
1520 c = 0x66;
1521 break;
1522 case P_O64:
1523 /* REX.W */
1524 break;
1525 case P_OSP:
1526 c = 0x66;
1527 break;
1528 case P_EVEX:
1529 case P_VEX3:
1530 case P_VEX2:
1531 case P_NOBND:
1532 case P_none:
1533 break;
1534 default:
1535 nasm_panic(0, "invalid instruction prefix");
1536 }
1537 if (c) {
1538 if (data)
1539 out_rawbyte(data, c);
1540 bytes++;
1541 }
1542 }
1543 return bytes;
1544}
1545
1546static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001547{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001548 uint8_t c;
1549 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001550 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001551 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001552 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001553 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001554 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001555 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001556 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001557 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001558 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001559
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001560 ins->rex_done = false;
1561
H. Peter Anvina77692b2016-09-20 14:04:33 -07001562 emit_prefix(data, bits, ins);
1563
H. Peter Anvin839eca22007-10-29 23:12:47 -07001564 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001565 c = *codes++;
1566 op1 = (c & 3) + ((opex & 1) << 2);
1567 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1568 opx = &ins->oprs[op1];
1569 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001570
H. Peter Anvina77692b2016-09-20 14:04:33 -07001571
H. Peter Anvin839eca22007-10-29 23:12:47 -07001572 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 case 01:
1574 case 02:
1575 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001576 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001577 emit_rex(data, ins);
1578 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001581
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001582 case 05:
1583 case 06:
1584 case 07:
1585 opex = c;
1586 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001587
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001588 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001589 emit_rex(data, ins);
1590 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001592
Jin Kyu Song164d6072013-10-15 19:10:13 -07001593 case4(014):
1594 break;
1595
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001596 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001597 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001598 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001599 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001600 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001602
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001603 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001604 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001605 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001606 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001607 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001609
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001610 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001611 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001612 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001614
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001615 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001616 if (opx->type & (BITS16 | BITS32))
1617 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 else
1619 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001620 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001621 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001623
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001624 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001625 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001626 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001628
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001629 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001630 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001631 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001632 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001634
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001635 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001636 if (opx->segment == data->segment) {
1637 int64_t delta = opx->offset - data->offset
1638 - (data->inslen - data->insoffs);
1639 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001640 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001641 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001642 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001644
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001645 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001646 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001647 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001648
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001649 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001650 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001652
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001653 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001654 if (opx->type & (BITS16 | BITS32 | BITS64))
1655 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001656 else
1657 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001658
1659 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001660 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001661
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001662 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001663 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001665
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001666 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001667 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001668 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001669 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001670 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001671 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001672
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001673 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001674 {
1675 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1676 const struct operand *opy;
1677
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001678 c = *codes++;
1679 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001680 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001681 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001682 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001683 "non-absolute expression not permitted as argument %d",
1684 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001685 } else if (opy->offset & ~mask) {
1686 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1687 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001688 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001689 c = opy->offset & mask;
1690 goto emit_is4;
1691 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001692
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001693 case 0173:
1694 c = *codes++;
1695 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001696 c &= 15;
1697 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001698
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001699 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001700 c = 0;
1701 emit_is4:
1702 r = nasm_regvals[opx->basereg];
1703 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001704 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001705
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001706 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001707 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001708 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001709 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001710 "signed dword immediate exceeds bounds");
1711 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001712 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001713 break;
1714
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001715 case4(0240):
1716 case 0250:
1717 codes += 3;
1718 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1719 EVEX_P2Z | EVEX_P2AAA, 2);
1720 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1721 bytes[0] = 0x62;
1722 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001723 bytes[1] = ((((ins->rex & 7) << 5) |
1724 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001725 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001726 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1727 ((~ins->vexreg & 15) << 3) |
1728 (1 << 2) | (ins->vex_wlp & 3);
1729 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001730 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001731 break;
1732
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001733 case4(0260):
1734 case 0270:
1735 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001736 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1737 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001738 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1739 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1740 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001741 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001742 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001743 } else {
1744 bytes[0] = 0xc5;
1745 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001746 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001747 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001748 }
1749 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001750
H. Peter Anvine014f352012-02-25 22:35:19 -08001751 case 0271:
1752 case 0272:
1753 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001754 break;
1755
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001756 case4(0274):
1757 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001758 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001759 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001760
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001761 if (absolute_op(opx)) {
1762 if (ins->rex & REX_W)
1763 s = 64;
1764 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1765 s = 16;
1766 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1767 s = 32;
1768 else
1769 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001770
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001771 um = (uint64_t)2 << (s-1);
1772 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001773
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001774 if (uv > 127 && uv < (uint64_t)-128 &&
1775 (uv < um-128 || uv > um-1)) {
1776 /* If this wasn't explicitly byte-sized, warn as though we
1777 * had fallen through to the imm16/32/64 case.
1778 */
1779 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1780 "%s value exceeds bounds",
1781 (opx->type & BITS8) ? "signed byte" :
1782 s == 16 ? "word" :
1783 s == 32 ? "dword" :
1784 "signed dword");
1785 }
1786
1787 /* Output as a raw byte to avoid byte overflow check */
1788 out_rawbyte(data, (uint8_t)uv);
1789 } else {
1790 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001791 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001792 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001793 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001794
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001795 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001797
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001799 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1800 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001802
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001804 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1805 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001807
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 case 0312:
1809 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001810
Keith Kaniosb7a89542007-04-12 02:40:54 +00001811 case 0313:
1812 ins->rex = 0;
1813 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001814
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001815 case4(0314):
1816 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001817
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001821
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001823 case 0323:
1824 break;
1825
Keith Kaniosb7a89542007-04-12 02:40:54 +00001826 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001827 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001829
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001830 case 0325:
1831 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001832
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001833 case 0326:
1834 break;
1835
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001837 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001839
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001842
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001843 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001845 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001847
Keith Kanios48af1772007-08-17 07:37:52 +00001848 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001849 if (ins->rex & REX_R)
1850 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001851 ins->rex &= ~(REX_L|REX_R);
1852 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001853
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001854 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001855 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001856
H. Peter Anvin962e3052008-08-28 17:47:16 -07001857 case 0336:
1858 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001859 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001860
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001862 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001863 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001864
1865 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001867
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001868 case 0341:
1869 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001870
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001871 case 0360:
1872 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001873
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001874 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001875 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001876 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001877
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001878 case 0364:
1879 case 0365:
1880 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001881
Keith Kanios48af1772007-08-17 07:37:52 +00001882 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001883 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001884 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001885 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001886
Jin Kyu Song03041092013-10-15 19:38:51 -07001887 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001888 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001889
H. Peter Anvine2c80182005-01-15 22:15:51 +00001890 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001891 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001893
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001894 case 0374:
1895 eat = EA_XMMVSIB;
1896 break;
1897
1898 case 0375:
1899 eat = EA_YMMVSIB;
1900 break;
1901
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001902 case 0376:
1903 eat = EA_ZMMVSIB;
1904 break;
1905
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001906 case4(0100):
1907 case4(0110):
1908 case4(0120):
1909 case4(0130):
1910 case4(0200):
1911 case4(0204):
1912 case4(0210):
1913 case4(0214):
1914 case4(0220):
1915 case4(0224):
1916 case4(0230):
1917 case4(0234):
1918 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001919 ea ea_data;
1920 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001921 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001922 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001923 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001924
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001925 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001926 /* pick rfield from operand b (opx) */
1927 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001928 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001929 } else {
1930 /* rfield is constant */
1931 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001932 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001933 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001934
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001935 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001936 rfield, rflags, ins, &errmsg) != eat)
1937 nasm_error(ERR_NONFATAL, "%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07001938
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939 p = bytes;
1940 *p++ = ea_data.modrm;
1941 if (ea_data.sib_present)
1942 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001943 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001945 /*
1946 * Make sure the address gets the right offset in case
1947 * the line breaks in the .lst file (BR 1197827)
1948 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001949
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001950 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001951 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001952 if (ea_data.disp8) {
1953 out_rawbyte(data, ea_data.disp8);
1954 } else if (ea_data.rip) {
1955 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001956 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001957 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001958
H. Peter Anvina77692b2016-09-20 14:04:33 -07001959 if (overflow_general(opy->offset, asize) ||
1960 signed_bits(opy->offset, ins->addr_size) !=
1961 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001962 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001963
H. Peter Anvina77692b2016-09-20 14:04:33 -07001964 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001965 (asize > ea_data.bytes)
1966 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001967 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001969 }
1970 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001971
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001972 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001973 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001974 ": instruction code \\%o (0x%02X) given", c, c);
1975 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001976 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001977 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001978}
1979
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001980static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001981{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001982 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001983 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001984 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001985}
1986
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001987static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001988{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001989 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001990 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001991 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001992}
1993
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001994static int op_rexflags(const operand * o, int mask)
1995{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001996 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001997 int val;
1998
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001999 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08002000 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002001
H. Peter Anvina4835d42008-05-20 14:21:29 -07002002 flags = nasm_reg_flags[o->basereg];
2003 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002004
2005 return rexflags(val, flags, mask);
2006}
2007
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002008static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002009{
2010 int rex = 0;
2011
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002012 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002013 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002014 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002015 rex |= REX_W;
2016 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2017 rex |= REX_H;
2018 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2019 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002020
2021 return rex & mask;
2022}
2023
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002024static int evexflags(int val, decoflags_t deco,
2025 int mask, uint8_t byte)
2026{
2027 int evex = 0;
2028
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002029 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002030 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002031 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002032 evex |= (EVEX_P0RP | EVEX_P0X);
2033 break;
2034 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002035 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002036 evex |= EVEX_P2VP;
2037 if (deco & Z)
2038 evex |= EVEX_P2Z;
2039 if (deco & OPMASK_MASK)
2040 evex |= deco & EVEX_P2AAA;
2041 break;
2042 }
2043 return evex & mask;
2044}
2045
2046static int op_evexflags(const operand * o, int mask, uint8_t byte)
2047{
2048 int val;
2049
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002050 val = nasm_regvals[o->basereg];
2051
2052 return evexflags(val, o->decoflags, mask, byte);
2053}
2054
H. Peter Anvin23595f52009-07-25 17:44:25 -07002055static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002056 insn *instruction,
2057 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002058{
2059 const struct itemplate *temp;
2060 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002061 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002062 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002063 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002064 int i;
2065
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002066 /* broadcasting uses a different data element size */
2067 for (i = 0; i < instruction->operands; i++)
2068 if (i == broadcast)
2069 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2070 else
2071 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002072
2073 merr = MERR_INVALOP;
2074
2075 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002076 temp->opcode != I_none; temp++) {
2077 m = matches(temp, instruction, bits);
2078 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002079 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002080 m = MOK_GOOD;
2081 else
2082 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002083 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002084 /*
2085 * Missing operand size and a candidate for fuzzy matching...
2086 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002087 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002088 if (i == broadcast)
2089 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2090 else
2091 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002092 opsizemissing = true;
2093 }
2094 if (m > merr)
2095 merr = m;
2096 if (merr == MOK_GOOD)
2097 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002098 }
2099
2100 /* No match, but see if we can get a fuzzy operand size match... */
2101 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002102 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002103
2104 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002105 /*
2106 * We ignore extrinsic operand sizes on registers, so we should
2107 * never try to fuzzy-match on them. This also resolves the case
2108 * when we have e.g. "xmmrm128" in two different positions.
2109 */
2110 if (is_class(REGISTER, instruction->oprs[i].type))
2111 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002112
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002113 /* This tests if xsizeflags[i] has more than one bit set */
2114 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2115 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002116
Jin Kyu Song7903c072013-10-30 03:00:12 -07002117 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002118 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002119 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2120 BITS32 : BITS64);
2121 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002122 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002123 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002124 }
2125
2126 /* Try matching again... */
2127 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002128 temp->opcode != I_none; temp++) {
2129 m = matches(temp, instruction, bits);
2130 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002131 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002132 m = MOK_GOOD;
2133 else
2134 m = MERR_INVALOP;
2135 }
2136 if (m > merr)
2137 merr = m;
2138 if (merr == MOK_GOOD)
2139 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002140 }
2141
H. Peter Anvina81655b2009-07-25 18:15:28 -07002142done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002143 *tempp = temp;
2144 return merr;
2145}
2146
Mark Charneydcaef4b2014-10-09 13:45:17 -04002147static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2148{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002149 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002150 uint8_t brcast_num;
2151
Mark Charneydcaef4b2014-10-09 13:45:17 -04002152 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002153 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002154 "size of broadcasting element is greater than 64 bits");
2155
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002156 /*
2157 * The shift term is to take care of the extra BITS80 inserted
2158 * between BITS64 and BITS128.
2159 */
2160 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2161 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002162
2163 return brcast_num;
2164}
2165
H. Peter Anvin65289e82009-07-25 17:25:11 -07002166static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002167 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002168{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002169 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002170 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002171 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002172
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002173 /*
2174 * Check the opcode
2175 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002176 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002177 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002178
2179 /*
2180 * Count the operands
2181 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002182 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002183 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002184
2185 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002186 * Is it legal?
2187 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002188 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002189 return MERR_INVALOP;
2190
2191 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002192 * {evex} available?
2193 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002194 switch (instruction->prefixes[PPS_VEX]) {
2195 case P_EVEX:
2196 if (!itemp_has(itemp, IF_EVEX))
2197 return MERR_ENCMISMATCH;
2198 break;
2199 case P_VEX3:
2200 case P_VEX2:
2201 if (!itemp_has(itemp, IF_VEX))
2202 return MERR_ENCMISMATCH;
2203 break;
2204 default:
2205 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002206 }
2207
2208 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002209 * Check that no spurious colons or TOs are present
2210 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002211 for (i = 0; i < itemp->operands; i++)
2212 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002213 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002214
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002215 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002216 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002217 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002218 switch (itemp_smask(itemp)) {
2219 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002220 asize = BITS8;
2221 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002222 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002223 asize = BITS16;
2224 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002225 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002226 asize = BITS32;
2227 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002228 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002229 asize = BITS64;
2230 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002231 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002232 asize = BITS128;
2233 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002234 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002235 asize = BITS256;
2236 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002237 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002238 asize = BITS512;
2239 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002240 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002241 switch (bits) {
2242 case 16:
2243 asize = BITS16;
2244 break;
2245 case 32:
2246 asize = BITS32;
2247 break;
2248 case 64:
2249 asize = BITS64;
2250 break;
2251 default:
2252 asize = 0;
2253 break;
2254 }
2255 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002256 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002257 asize = 0;
2258 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002259 }
2260
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002261 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002262 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002263 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002264 memset(size, 0, sizeof size);
2265 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002266 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002267 /* S- flags apply to all operands */
2268 for (i = 0; i < MAX_OPERANDS; i++)
2269 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002270 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002271
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002272 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002273 * Check that the operand flags all match up,
2274 * it's a bit tricky so lets be verbose:
2275 *
2276 * 1) Find out the size of operand. If instruction
2277 * doesn't have one specified -- we're trying to
2278 * guess it either from template (IF_S* flag) or
2279 * from code bits.
2280 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002281 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002282 * template has an operand size specified AND this size differ
2283 * from which instruction has (perhaps we got it from code bits)
2284 * we are:
2285 * a) Check that only size of instruction and operand is differ
2286 * other characteristics do match
2287 * b) Perhaps it's a register specified in instruction so
2288 * for such a case we just mark that operand as "size
2289 * missing" and this will turn on fuzzy operand size
2290 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002291 */
2292 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002293 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002294 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002295 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002296 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002297 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002298 opflags_t template_opsize, insn_opsize;
2299
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002300 if (!(type & SIZE_MASK))
2301 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002302
Jin Kyu Song7903c072013-10-30 03:00:12 -07002303 insn_opsize = type & SIZE_MASK;
2304 if (!is_broadcast) {
2305 template_opsize = itemp->opd[i] & SIZE_MASK;
2306 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002307 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2308
2309 if (~ideco & BRDCAST_MASK)
2310 return MERR_BRNOTHERE;
2311
Jin Kyu Song7903c072013-10-30 03:00:12 -07002312 /*
2313 * when broadcasting, the element size depends on
2314 * the instruction type. decorator flag should match.
2315 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002316 if (deco_brsize) {
2317 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002318 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002319 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002320 } else {
2321 template_opsize = 0;
2322 }
2323 }
2324
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002325 if (~ideco & deco & OPMASK_MASK)
2326 return MERR_MASKNOTHERE;
2327
2328 if (itemp->opd[i] & ~type & ~SIZE_MASK) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002329 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002330 } else if (template_opsize) {
2331 if (template_opsize != insn_opsize) {
2332 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002333 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002334 } else if (!is_class(REGISTER, type)) {
2335 /*
2336 * Note: we don't honor extrinsic operand sizes for registers,
2337 * so "missing operand size" for a register should be
2338 * considered a wildcard match rather than an error.
2339 */
2340 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002341 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002342 } else if (is_broadcast &&
2343 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002344 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002345 /*
2346 * broadcasting opsize matches but the number of repeated memory
2347 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002348 * if 64b double precision float is broadcasted to ymm (256b),
2349 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002350 */
2351 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002352 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002353 }
2354 }
2355
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002356 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002357 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002358
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002359 /*
2360 * Check operand sizes
2361 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002362 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2363 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002364 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002365 asize = itemp->opd[i] & SIZE_MASK;
2366 if (asize) {
2367 for (i = 0; i < oprs; i++)
2368 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002369 break;
2370 }
2371 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002372 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002374 }
2375
Keith Kaniosb7a89542007-04-12 02:40:54 +00002376 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002377 if (!(itemp->opd[i] & SIZE_MASK) &&
2378 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002379 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002380 }
2381
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002382 /*
2383 * Check template is okay at the set cpu level
2384 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002385 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002386 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002387
Keith Kaniosb7a89542007-04-12 02:40:54 +00002388 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002389 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002390 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002391 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002392 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002394 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002395 * If we have a HLE prefix, look for the NOHLE flag
2396 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002397 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002398 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2399 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2400 return MERR_BADHLE;
2401
2402 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002403 * Check if special handling needed for Jumps
2404 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002405 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002406 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002407
Jin Kyu Song03041092013-10-15 19:38:51 -07002408 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002409 * Check if BND prefix is allowed.
2410 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002411 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002412 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002413 (has_prefix(instruction, PPS_REP, P_BND) ||
2414 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002415 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002416 else if (itemp_has(itemp, IF_BND) &&
2417 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2418 has_prefix(instruction, PPS_REP, P_REPNZ)))
2419 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002420
H. Peter Anvin60926242009-07-26 16:25:38 -07002421 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002422}
2423
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002424/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002425 * Check if ModR/M.mod should/can be 01.
2426 * - EAF_BYTEOFFS is set
2427 * - offset can fit in a byte when EVEX is not used
2428 * - offset can be compressed when EVEX is used
2429 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002430#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2431 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2432 is_disp8n(input, ins, &output->disp8) : \
2433 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2434 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002435
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002436static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002437 int rfield, opflags_t rflags, insn *ins,
2438 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002439{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002440 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002441 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002442 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002443
H. Peter Anvin8f622462017-04-02 19:02:29 -07002444 *errmsg = "invalid effective address"; /* Default error message */
2445
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002446 output->type = EA_SCALAR;
2447 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002448 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002449
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002450 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002451 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002452 /* EVEX.R' flag for the REG operand */
2453 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002454
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002455 if (is_class(REGISTER, input->type)) {
2456 /*
2457 * It's a direct register.
2458 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002459 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002460 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002461
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002462 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002463 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002464
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002465 /* broadcasting is not available with a direct register operand. */
2466 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002467 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002468 goto err;
2469 }
2470
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002471 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002472 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002473 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002474 output->bytes = 0; /* no offset necessary either */
2475 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2476 } else {
2477 /*
2478 * It's a memory reference.
2479 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002480
2481 /* Embedded rounding or SAE is not available with a mem ref operand. */
2482 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002483 *errmsg = "embedded rounding is available only with "
2484 "register-register operations";
2485 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002486 }
2487
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002488 if (input->basereg == -1 &&
2489 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002490 /*
2491 * It's a pure offset.
2492 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002493 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002494 if (input->segment == NO_SEG ||
2495 (input->opflags & OPFLAG_RELATIVE)) {
2496 nasm_error(ERR_WARNING | ERR_PASS2,
2497 "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002498 input->type &= ~IP_REL;
2499 input->type |= MEMORY;
2500 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002501 }
2502
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002503 if (bits == 64 &&
2504 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002505 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002506 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002507 }
2508
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002509 if (eaflags & EAF_BYTEOFFS ||
2510 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002511 input->disp_size != (addrbits != 16 ? 32 : 16))) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002512 nasm_error(ERR_WARNING | ERR_PASS1,
2513 "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002514 }
2515
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002516 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002517 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002518 output->sib = GEN_SIB(0, 4, 5);
2519 output->bytes = 4;
2520 output->modrm = GEN_MODRM(0, rfield, 4);
2521 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002522 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002523 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002524 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002525 output->modrm = GEN_MODRM(0, rfield,
2526 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002527 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002528 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002529 } else {
2530 /*
2531 * It's an indirection.
2532 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002534 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002535 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002536 int t, it, bt; /* register numbers */
2537 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002538
H. Peter Anvine2c80182005-01-15 22:15:51 +00002539 if (s == 0)
2540 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002541
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002542 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002543 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002544 ix = nasm_reg_flags[i];
2545 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002546 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002547 ix = 0;
2548 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002549
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002550 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002551 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002552 bx = nasm_reg_flags[b];
2553 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002554 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002555 bx = 0;
2556 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002557
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002558 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002559 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002560 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002561 int32_t o = input->offset;
2562 int mod, scale, index, base;
2563
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002564 /*
2565 * For a vector SIB, one has to be a vector and the other,
2566 * if present, a GPR. The vector must be the index operand.
2567 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002568 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002569 if (s == 0)
2570 s = 1;
2571 else if (s != 1)
2572 goto err;
2573
2574 t = bt, bt = it, it = t;
2575 x = bx, bx = ix, ix = x;
2576 }
2577
2578 if (bt != -1) {
2579 if (REG_GPR & ~bx)
2580 goto err;
2581 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2582 sok &= bx;
2583 else
2584 goto err;
2585 }
2586
2587 /*
2588 * While we're here, ensure the user didn't specify
2589 * WORD or QWORD
2590 */
2591 if (input->disp_size == 16 || input->disp_size == 64)
2592 goto err;
2593
2594 if (addrbits == 16 ||
2595 (addrbits == 32 && !(sok & BITS32)) ||
2596 (addrbits == 64 && !(sok & BITS64)))
2597 goto err;
2598
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002599 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2600 : ((ix & YMMREG & ~REG_EA)
2601 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002602
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002603 output->rex |= rexflags(it, ix, REX_X);
2604 output->rex |= rexflags(bt, bx, REX_B);
2605 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002606
2607 index = it & 7; /* it is known to be != -1 */
2608
2609 switch (s) {
2610 case 1:
2611 scale = 0;
2612 break;
2613 case 2:
2614 scale = 1;
2615 break;
2616 case 4:
2617 scale = 2;
2618 break;
2619 case 8:
2620 scale = 3;
2621 break;
2622 default: /* then what the smeg is it? */
2623 goto err; /* panic */
2624 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002625
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002626 if (bt == -1) {
2627 base = 5;
2628 mod = 0;
2629 } else {
2630 base = (bt & 7);
2631 if (base != REG_NUM_EBP && o == 0 &&
2632 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002633 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002634 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002635 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002636 mod = 1;
2637 else
2638 mod = 2;
2639 }
2640
2641 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002642 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2643 output->modrm = GEN_MODRM(mod, rfield, 4);
2644 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002645 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002646 /*
2647 * it must be a 32/64-bit memory reference. Firstly we have
2648 * to check that all registers involved are type E/Rxx.
2649 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002650 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002651 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002652
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002653 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002654 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2655 sok &= ix;
2656 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002657 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002658 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002659
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002660 if (bt != -1) {
2661 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002662 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002663 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002664 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002665 sok &= bx;
2666 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002667
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002668 /*
2669 * While we're here, ensure the user didn't specify
2670 * WORD or QWORD
2671 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002672 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002673 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002674
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002675 if (addrbits == 16 ||
2676 (addrbits == 32 && !(sok & BITS32)) ||
2677 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002678 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002679
Keith Kaniosb7a89542007-04-12 02:40:54 +00002680 /* now reorganize base/index */
2681 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002682 ((hb == b && ht == EAH_NOTBASE) ||
2683 (hb == i && ht == EAH_MAKEBASE))) {
2684 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002685 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002686 x = bx, bx = ix, ix = x;
2687 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002688
Jin Kyu Song164d6072013-10-15 19:10:13 -07002689 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002690 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002691 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002692 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002693 if (eaflags & EAF_MIB) {
2694 /* only for mib operands */
2695 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2696 /*
2697 * make a single reg index [reg*1].
2698 * gas uses this form for an explicit index register.
2699 */
2700 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2701 }
2702 if ((ht == EAH_SUMMED) && bt == -1) {
2703 /* separate once summed index into [base, index] */
2704 bt = it, bx = ix, s--;
2705 }
2706 } else {
2707 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002708 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002709 s == 3 || s == 5 || s == 9) && bt == -1) {
2710 /* convert 3*EAX to EAX+2*EAX */
2711 bt = it, bx = ix, s--;
2712 }
2713 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002714 (eaflags & EAF_TIMESTWO) &&
2715 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002716 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002717 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002718 * to sib format with 0x0 displacement - [EAX*1+0].
2719 */
2720 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2721 }
2722 }
Keith Kanios48af1772007-08-17 07:37:52 +00002723 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002724 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002725 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002726 x = ix, ix = bx, bx = x;
2727 }
2728 if (it == REG_NUM_ESP ||
2729 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002730 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002731
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002732 output->rex |= rexflags(it, ix, REX_X);
2733 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002734
Keith Kanios48af1772007-08-17 07:37:52 +00002735 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002736 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002738
Keith Kaniosb7a89542007-04-12 02:40:54 +00002739 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002742 } else {
2743 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002744 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002745 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002746 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002747 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002748 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002749 mod = 1;
2750 else
2751 mod = 2;
2752 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002753
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002754 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002755 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2756 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002757 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002758 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002759 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002760
Keith Kaniosb7a89542007-04-12 02:40:54 +00002761 if (it == -1)
2762 index = 4, s = 1;
2763 else
2764 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002765
H. Peter Anvine2c80182005-01-15 22:15:51 +00002766 switch (s) {
2767 case 1:
2768 scale = 0;
2769 break;
2770 case 2:
2771 scale = 1;
2772 break;
2773 case 4:
2774 scale = 2;
2775 break;
2776 case 8:
2777 scale = 3;
2778 break;
2779 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002780 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002781 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002782
Keith Kaniosb7a89542007-04-12 02:40:54 +00002783 if (bt == -1) {
2784 base = 5;
2785 mod = 0;
2786 } else {
2787 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002788 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002789 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002790 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002791 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002792 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002793 mod = 1;
2794 else
2795 mod = 2;
2796 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002797
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002798 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002799 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2800 output->modrm = GEN_MODRM(mod, rfield, 4);
2801 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002802 }
2803 } else { /* it's 16-bit */
2804 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002805 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002806
Keith Kaniosb7a89542007-04-12 02:40:54 +00002807 /* check for 64-bit long mode */
2808 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002809 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002810
H. Peter Anvine2c80182005-01-15 22:15:51 +00002811 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002812 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2813 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002814 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002815
Keith Kaniosb7a89542007-04-12 02:40:54 +00002816 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002817 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002818 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002819
H. Peter Anvine2c80182005-01-15 22:15:51 +00002820 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002821 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002822 if (b == -1 && i != -1) {
2823 int tmp = b;
2824 b = i;
2825 i = tmp;
2826 } /* swap */
2827 if ((b == R_SI || b == R_DI) && i != -1) {
2828 int tmp = b;
2829 b = i;
2830 i = tmp;
2831 }
2832 /* have BX/BP as base, SI/DI index */
2833 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002834 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002835 if (i != -1 && b != -1 &&
2836 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002837 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002838 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002839 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002840
H. Peter Anvine2c80182005-01-15 22:15:51 +00002841 rm = -1;
2842 if (i != -1)
2843 switch (i * 256 + b) {
2844 case R_SI * 256 + R_BX:
2845 rm = 0;
2846 break;
2847 case R_DI * 256 + R_BX:
2848 rm = 1;
2849 break;
2850 case R_SI * 256 + R_BP:
2851 rm = 2;
2852 break;
2853 case R_DI * 256 + R_BP:
2854 rm = 3;
2855 break;
2856 } else
2857 switch (b) {
2858 case R_SI:
2859 rm = 4;
2860 break;
2861 case R_DI:
2862 rm = 5;
2863 break;
2864 case R_BP:
2865 rm = 6;
2866 break;
2867 case R_BX:
2868 rm = 7;
2869 break;
2870 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002871 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002872 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002873
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002874 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002875 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002877 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002878 mod = 1;
2879 else
2880 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002881
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002882 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002883 output->bytes = mod; /* bytes of offset needed */
2884 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002885 }
2886 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002887 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002888
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002889 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002890 return output->type;
2891
2892err:
2893 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002894}
2895
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002896static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002897{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002898 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002899 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002900
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002901 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002902
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002903 switch (ins->prefixes[PPS_ASIZE]) {
2904 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 valid &= 16;
2906 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002907 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002908 valid &= 32;
2909 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002910 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 valid &= 64;
2912 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002913 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002914 valid &= (addrbits == 32) ? 16 : 32;
2915 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002916 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002917 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002918 }
2919
2920 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002921 if (is_class(MEMORY, ins->oprs[j].type)) {
2922 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002923
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002924 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002925 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002926 i = 0;
2927 else
2928 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002929
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002930 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002931 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002932 b = 0;
2933 else
2934 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002935
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002936 if (ins->oprs[j].scale == 0)
2937 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002938
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002939 if (!i && !b) {
2940 int ds = ins->oprs[j].disp_size;
2941 if ((addrbits != 64 && ds > 8) ||
2942 (addrbits == 64 && ds == 16))
2943 valid &= ds;
2944 } else {
2945 if (!(REG16 & ~b))
2946 valid &= 16;
2947 if (!(REG32 & ~b))
2948 valid &= 32;
2949 if (!(REG64 & ~b))
2950 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002951
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002952 if (!(REG16 & ~i))
2953 valid &= 16;
2954 if (!(REG32 & ~i))
2955 valid &= 32;
2956 if (!(REG64 & ~i))
2957 valid &= 64;
2958 }
2959 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002960 }
2961
2962 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002963 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002964 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002965 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002966 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002967 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002968 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002969 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002970 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002971 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002972 }
2973
2974 defdisp = ins->addr_size == 16 ? 16 : 32;
2975
2976 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002977 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2978 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2979 /*
2980 * mem_offs sizes must match the address size; if not,
2981 * strip the MEM_OFFS bit and match only EA instructions
2982 */
2983 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2984 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002985 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002986}