blob: 9b720772f94b78240bf58e45d9b3065c9b298503 [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;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300549 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000550
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700551 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700552 data.offset = start;
553 data.segment = segment;
554 data.itemp = NULL;
555 data.sign = OUT_WRAP;
556 data.bits = bits;
557
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300558 wsize = idata_bytes(instruction->opcode);
559 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000560 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000561
H. Peter Anvineba20a72002-04-30 20:53:55 +0000562 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700564
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700565 list_for_each(e, instruction->eops) {
566 if (e->type == EOT_DB_NUMBER) {
567 if (wsize > 8) {
568 nasm_error(ERR_NONFATAL,
569 "integer supplied to a DT, DO, DY or DZ"
570 " instruction");
571 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700572 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700573 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
574 data.inslen = data.size = wsize;
575 data.toffset = e->offset;
576 data.tsegment = e->segment;
577 data.twrt = e->wrt;
578 data.relbase = 0;
579 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700581 } else if (e->type == EOT_DB_STRING ||
582 e->type == EOT_DB_STRING_FREE) {
583 int align = e->stringlen % wsize;
584 if (align)
585 align = wsize - align;
586
587 data.insoffs = 0;
588 data.inslen = e->stringlen + align;
589
590 out_rawdata(&data, e->stringval, e->stringlen);
591 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700592 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000593 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700594 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700595 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000596 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700597 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700598 off_t base = 0;
599 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700600 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700601 char *buf = NULL;
602 size_t blk = 0; /* Buffered I/O block size */
603 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000604
H. Peter Anvind81a2352016-09-21 14:03:18 -0700605 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400606 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800607 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700609 goto done;
610 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000611
H. Peter Anvind81a2352016-09-21 14:03:18 -0700612 len = nasm_file_size(fp);
613
614 if (len == (off_t)-1) {
615 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700616 fname);
617 goto close_done;
618 }
619
H. Peter Anvina77692b2016-09-20 14:04:33 -0700620 if (instruction->eops->next) {
621 base = instruction->eops->next->offset;
622 if (base >= len) {
623 len = 0;
624 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 len -= base;
626 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700627 len > (off_t)instruction->eops->next->next->offset)
628 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000629 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700631
H. Peter Anvina77692b2016-09-20 14:04:33 -0700632 lfmt->set_offset(data.offset);
633 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000634
H. Peter Anvind81a2352016-09-21 14:03:18 -0700635 if (!len)
636 goto end_incbin;
637
638 /* Try to map file data */
639 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700640 if (!map) {
641 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
642 buf = nasm_malloc(blk);
643 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700644
645 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700646 /*
647 * Consider these irrelevant for INCBIN, since it is fully
648 * possible that these might be (way) bigger than an int
649 * can hold; there is, however, no reason to widen these
650 * types just for INCBIN. data.inslen == 0 signals to the
651 * backend that these fields are meaningless, if at all
652 * needed.
653 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700654 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700655 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700656
H. Peter Anvind81a2352016-09-21 14:03:18 -0700657 if (map) {
658 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700659 } else if ((off_t)m == len) {
660 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700661 } else {
662 off_t l = len;
663
664 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700665 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700666 "`incbin': unable to seek on file `%s'",
667 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700668 goto end_incbin;
669 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700670 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700671 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700672 if (!m || feof(fp)) {
673 /*
674 * This shouldn't happen unless the file
675 * actually changes while we are reading
676 * it.
677 */
678 nasm_error(ERR_NONFATAL,
679 "`incbin': unexpected EOF while"
680 " reading file `%s'", fname);
681 goto end_incbin;
682 }
683 out_rawdata(&data, buf, m);
684 l -= m;
685 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700686 }
687 }
688 end_incbin:
689 lfmt->downlevel(LIST_INCBIN);
690 if (instruction->times > 1) {
H. Peter Anvin5810c592017-05-01 19:51:09 -0700691 lfmt->set_offset(start);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700692 lfmt->uplevel(LIST_TIMES);
693 lfmt->downlevel(LIST_TIMES);
694 }
695 if (ferror(fp)) {
696 nasm_error(ERR_NONFATAL,
697 "`incbin': error while"
698 " reading file `%s'", fname);
699 }
700 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700701 if (buf)
702 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700703 if (map)
704 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700705 fclose(fp);
706 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700707 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700708 ;
709 } else {
710 /* "Real" instruction */
711
712 /* Check to see if we need an address-size prefix */
713 add_asp(instruction, bits);
714
715 m = find_match(&temp, instruction, data.segment, data.offset, bits);
716
717 if (m == MOK_GOOD) {
718 /* Matches! */
719 int64_t insn_size = calcsize(data.segment, data.offset,
720 bits, instruction, temp);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700721 nasm_assert(insn_size >= 0);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700722
723 data.itemp = temp;
724 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700725 data.insoffs = 0;
726 data.inslen = insn_size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700727
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700728 gencode(&data, instruction);
729 nasm_assert(data.insoffs == insn_size);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700730 } else {
731 /* No match */
732 switch (m) {
733 case MERR_OPSIZEMISSING:
734 nasm_error(ERR_NONFATAL, "operation size not specified");
735 break;
736 case MERR_OPSIZEMISMATCH:
737 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
738 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700739 case MERR_BRNOTHERE:
740 nasm_error(ERR_NONFATAL,
741 "broadcast not permitted on this operand");
742 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700743 case MERR_BRNUMMISMATCH:
744 nasm_error(ERR_NONFATAL,
745 "mismatch in the number of broadcasting elements");
746 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700747 case MERR_MASKNOTHERE:
748 nasm_error(ERR_NONFATAL,
749 "mask not permitted on this operand");
750 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700751 case MERR_BADCPU:
752 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
753 break;
754 case MERR_BADMODE:
755 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
756 bits);
757 break;
758 case MERR_ENCMISMATCH:
759 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
760 break;
761 case MERR_BADBND:
762 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
763 break;
764 case MERR_BADREPNE:
765 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
766 (has_prefix(instruction, PPS_REP, P_REPNE) ?
767 "repne" : "repnz"));
768 break;
769 default:
770 nasm_error(ERR_NONFATAL,
771 "invalid combination of opcode and operands");
772 break;
773 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700774
775 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400776 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000777 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700778 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000779}
780
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800781int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000782{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000783 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700784 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000785
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400786 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000788
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700789 if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300791 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000792
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300794 wsize = idata_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700795 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000796
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400797 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000798 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000799
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400801 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400803 warn_overflow_const(e->offset, wsize);
804 } else if (e->type == EOT_DB_STRING ||
805 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000807
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 align = (-osize) % wsize;
809 if (align < 0)
810 align += wsize;
811 isize += osize + align;
812 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700813 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000814 }
815
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400817 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700818 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000819
H. Peter Anvind81a2352016-09-21 14:03:18 -0700820 len = nasm_file_size_by_path(fname);
821 if (len == (off_t)-1) {
822 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
823 fname);
824 return 0;
825 }
826
827 if (instruction->eops->next) {
828 if (len <= (off_t)instruction->eops->next->offset) {
829 len = 0;
830 } else {
831 len -= instruction->eops->next->offset;
832 if (instruction->eops->next->next &&
833 len > (off_t)instruction->eops->next->next->offset) {
834 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 }
836 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700838
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700839 len *= instruction->times;
840 instruction->times = 1; /* Tell the upper layer to not iterate */
841
H. Peter Anvind81a2352016-09-21 14:03:18 -0700842 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000843 }
844
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700845 /* Check to see if we need an address-size prefix */
846 add_asp(instruction, bits);
847
H. Peter Anvin23595f52009-07-25 17:44:25 -0700848 m = find_match(&temp, instruction, segment, offset, bits);
849 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400850 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700851 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700852 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400853 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000854 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000855}
856
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800857static void bad_hle_warn(const insn * ins, uint8_t hleok)
858{
859 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800860 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800861 static const enum whatwarn warn[2][4] =
862 {
863 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
864 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
865 };
866 unsigned int n;
867
868 n = (unsigned int)rep_pfx - P_XACQUIRE;
869 if (n > 1)
870 return; /* Not XACQUIRE/XRELEASE */
871
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800872 ww = warn[n][hleok];
873 if (!is_class(MEMORY, ins->oprs[0].type))
874 ww = w_inval; /* HLE requires operand 0 to be memory */
875
876 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800877 case w_none:
878 break;
879
880 case w_lock:
881 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800882 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800883 "%s with this instruction requires lock",
884 prefix_name(rep_pfx));
885 }
886 break;
887
888 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800889 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800890 "%s invalid with this instruction",
891 prefix_name(rep_pfx));
892 break;
893 }
894}
895
H. Peter Anvin507ae032008-10-09 15:37:10 -0700896/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400897#define case3(x) case (x): case (x)+1: case (x)+2
898#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700899
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800900static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800901 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000902{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800903 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800904 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000905 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000906 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700907 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700908 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700909 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700910 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800911 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800912 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700913 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -0700914 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000915
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700916 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700917 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700918 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700919
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700920 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400921 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700922
H. Peter Anvine2c80182005-01-15 22:15:51 +0000923 (void)segment; /* Don't warn that this parameter is unused */
924 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000925
H. Peter Anvin839eca22007-10-29 23:12:47 -0700926 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400927 c = *codes++;
928 op1 = (c & 3) + ((opex & 1) << 2);
929 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
930 opx = &ins->oprs[op1];
931 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700932
H. Peter Anvin839eca22007-10-29 23:12:47 -0700933 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400934 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 codes += c, length += c;
936 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700937
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400938 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400939 opex = c;
940 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700941
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400942 case4(010):
943 ins->rex |=
944 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 codes++, length++;
946 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700947
Jin Kyu Song164d6072013-10-15 19:10:13 -0700948 case4(014):
949 /* this is an index reg of MIB operand */
950 mib_index = opx->basereg;
951 break;
952
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400953 case4(020):
954 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000955 length++;
956 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700957
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400958 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 length += 2;
960 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700961
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400962 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700963 if (opx->type & (BITS16 | BITS32 | BITS64))
964 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 else
966 length += (bits == 16) ? 2 : 4;
967 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700968
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400969 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 length += 4;
971 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700972
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400973 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700974 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700976
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400977 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 length++;
979 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700980
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400981 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000982 length += 8; /* MOV reg64/imm */
983 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700984
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400985 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 length += 2;
987 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700988
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400989 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700990 if (opx->type & (BITS16 | BITS32 | BITS64))
991 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000992 else
993 length += (bits == 16) ? 2 : 4;
994 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700995
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400996 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000997 length += 4;
998 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700999
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001000 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001001 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001002 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001003
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001004 case 0172:
1005 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001006 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001007 length++;
1008 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001009
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001010 case4(0174):
1011 length++;
1012 break;
1013
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001014 case4(0240):
1015 ins->rex |= REX_EV;
1016 ins->vexreg = regval(opx);
1017 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1018 ins->vex_cm = *codes++;
1019 ins->vex_wlp = *codes++;
1020 ins->evex_tuple = (*codes++ - 0300);
1021 break;
1022
1023 case 0250:
1024 ins->rex |= REX_EV;
1025 ins->vexreg = 0;
1026 ins->vex_cm = *codes++;
1027 ins->vex_wlp = *codes++;
1028 ins->evex_tuple = (*codes++ - 0300);
1029 break;
1030
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001031 case4(0254):
1032 length += 4;
1033 break;
1034
1035 case4(0260):
1036 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001037 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001038 ins->vex_cm = *codes++;
1039 ins->vex_wlp = *codes++;
1040 break;
1041
1042 case 0270:
1043 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001044 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001045 ins->vex_cm = *codes++;
1046 ins->vex_wlp = *codes++;
1047 break;
1048
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001049 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001050 hleok = c & 3;
1051 break;
1052
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001053 case4(0274):
1054 length++;
1055 break;
1056
1057 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001058 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001059
H. Peter Anvine2c80182005-01-15 22:15:51 +00001060 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001061 if (bits == 64)
1062 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001063 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001065
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001067 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001069
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001071 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001072
Keith Kaniosb7a89542007-04-12 02:40:54 +00001073 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001074 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1075 has_prefix(ins, PPS_ASIZE, P_A32))
1076 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001078
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001079 case4(0314):
1080 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001081
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001083 {
1084 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1085 if (pfx == P_O16)
1086 break;
1087 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001088 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001089 else
1090 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001092 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001093
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001095 {
1096 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1097 if (pfx == P_O32)
1098 break;
1099 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001100 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001101 else
1102 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001104 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001105
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 case 0322:
1107 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001108
Keith Kaniosb7a89542007-04-12 02:40:54 +00001109 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001110 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001111 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001112
Keith Kaniosb7a89542007-04-12 02:40:54 +00001113 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001114 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001115 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001116
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001117 case 0325:
1118 ins->rex |= REX_NH;
1119 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001120
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001121 case 0326:
1122 break;
1123
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 case 0330:
1125 codes++, length++;
1126 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001127
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001130
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001131 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 case 0333:
1133 length++;
1134 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001135
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001136 case 0334:
1137 ins->rex |= REX_L;
1138 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001139
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001140 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001141 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001142
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001143 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001144 if (!ins->prefixes[PPS_REP])
1145 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001146 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001147
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001148 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001149 if (!ins->prefixes[PPS_REP])
1150 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001151 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001152
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001154 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001155 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001156 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001157 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1158 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001159 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001161 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001163
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001164 case 0341:
1165 if (!ins->prefixes[PPS_WAIT])
1166 ins->prefixes[PPS_WAIT] = P_WAIT;
1167 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001168
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001169 case 0360:
1170 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001171
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001172 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001173 length++;
1174 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001175
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001176 case 0364:
1177 case 0365:
1178 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001179
Keith Kanios48af1772007-08-17 07:37:52 +00001180 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001181 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001182 length++;
1183 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001184
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001185 case 0370:
1186 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001188
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 case 0373:
1190 length++;
1191 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001192
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001193 case 0374:
1194 eat = EA_XMMVSIB;
1195 break;
1196
1197 case 0375:
1198 eat = EA_YMMVSIB;
1199 break;
1200
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001201 case 0376:
1202 eat = EA_ZMMVSIB;
1203 break;
1204
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001205 case4(0100):
1206 case4(0110):
1207 case4(0120):
1208 case4(0130):
1209 case4(0200):
1210 case4(0204):
1211 case4(0210):
1212 case4(0214):
1213 case4(0220):
1214 case4(0224):
1215 case4(0230):
1216 case4(0234):
1217 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001219 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001220 opflags_t rflags;
1221 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001222 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001223
Keith Kaniosb7a89542007-04-12 02:40:54 +00001224 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001225
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001226 if (c <= 0177) {
1227 /* pick rfield from operand b (opx) */
1228 rflags = regflag(opx);
1229 rfield = nasm_regvals[opx->basereg];
1230 } else {
1231 rflags = 0;
1232 rfield = c & 7;
1233 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001234
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001235 /* EVEX.b1 : evex_brerop contains the operand position */
1236 op_er_sae = (ins->evex_brerop >= 0 ?
1237 &ins->oprs[ins->evex_brerop] : NULL);
1238
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001239 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1240 /* set EVEX.b */
1241 ins->evex_p[2] |= EVEX_P2B;
1242 if (op_er_sae->decoflags & ER) {
1243 /* set EVEX.RC (rounding control) */
1244 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1245 & EVEX_P2RC;
1246 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001247 } else {
1248 /* set EVEX.L'L (vector length) */
1249 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001250 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001251 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001252 /* set EVEX.b */
1253 ins->evex_p[2] |= EVEX_P2B;
1254 }
1255 }
1256
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001257 if (itemp_has(temp, IF_MIB)) {
1258 opy->eaflags |= EAF_MIB;
1259 /*
1260 * if a separate form of MIB (ICC style) is used,
1261 * the index reg info is merged into mem operand
1262 */
1263 if (mib_index != R_none) {
1264 opy->indexreg = mib_index;
1265 opy->scale = 1;
1266 opy->hintbase = mib_index;
1267 opy->hinttype = EAH_NOTBASE;
1268 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001269 }
1270
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001271 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001272 rfield, rflags, ins, &errmsg) != eat) {
1273 nasm_error(ERR_NONFATAL, "%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001274 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001275 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001276 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001277 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001278 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001279 }
1280 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001281
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001282 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001283 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001284 ": instruction code \\%o (0x%02X) given", c, c);
1285 break;
1286 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001287 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001288
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001289 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001290
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001291 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001292 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001293 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001294 return -1;
1295 }
1296 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001297 }
1298
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001299 switch (ins->prefixes[PPS_VEX]) {
1300 case P_EVEX:
1301 if (!(ins->rex & REX_EV))
1302 return -1;
1303 break;
1304 case P_VEX3:
1305 case P_VEX2:
1306 if (!(ins->rex & REX_V))
1307 return -1;
1308 break;
1309 default:
1310 break;
1311 }
1312
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001313 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001314 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001315
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001316 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001317 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001318 return -1;
1319 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001320 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001321 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001322 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001323 ins->rex &= ~REX_W;
1324 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001325 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001326 ins->rex |= REX_W;
1327 bad32 &= ~REX_W;
1328 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001329 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001330 /* Follow REX_W */
1331 break;
1332 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001333
H. Peter Anvinfc561202011-07-07 16:58:22 -07001334 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001335 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001336 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001337 } else if (!(ins->rex & REX_EV) &&
1338 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001339 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001340 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001341 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001342 if (ins->rex & REX_EV)
1343 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001344 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1345 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001346 length += 3;
1347 else
1348 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001349 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001350 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001351 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001352 return -1;
1353 } else if (bits == 64) {
1354 length++;
1355 } else if ((ins->rex & REX_L) &&
1356 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001357 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001358 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001359 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001360 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001361 length++;
1362 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001363 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001364 return -1;
1365 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001366 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001367
1368 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001369 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001370 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001371 "instruction is not lockable");
1372 }
1373
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001374 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001375
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001376 /*
1377 * when BND prefix is set by DEFAULT directive,
1378 * BND prefix is added to every appropriate instruction line
1379 * unless it is overridden by NOBND prefix.
1380 */
1381 if (globalbnd &&
1382 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1383 ins->prefixes[PPS_REP] = P_BND;
1384
H. Peter Anvina77692b2016-09-20 14:04:33 -07001385 /*
1386 * Add length of legacy prefixes
1387 */
1388 length += emit_prefix(NULL, bits, ins);
1389
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001390 return length;
1391}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001392
H. Peter Anvina77692b2016-09-20 14:04:33 -07001393static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001394{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001395 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001396 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001397 !(ins->rex & (REX_V | REX_EV)) &&
1398 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001399 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1400 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001401 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001402 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001403 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001404}
1405
H. Peter Anvina77692b2016-09-20 14:04:33 -07001406static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1407{
1408 int bytes = 0;
1409 int j;
1410
1411 for (j = 0; j < MAXPREFIX; j++) {
1412 uint8_t c = 0;
1413 switch (ins->prefixes[j]) {
1414 case P_WAIT:
1415 c = 0x9B;
1416 break;
1417 case P_LOCK:
1418 c = 0xF0;
1419 break;
1420 case P_REPNE:
1421 case P_REPNZ:
1422 case P_XACQUIRE:
1423 case P_BND:
1424 c = 0xF2;
1425 break;
1426 case P_REPE:
1427 case P_REPZ:
1428 case P_REP:
1429 case P_XRELEASE:
1430 c = 0xF3;
1431 break;
1432 case R_CS:
1433 if (bits == 64) {
1434 nasm_error(ERR_WARNING | ERR_PASS2,
1435 "cs segment base generated, but will be ignored in 64-bit mode");
1436 }
1437 c = 0x2E;
1438 break;
1439 case R_DS:
1440 if (bits == 64) {
1441 nasm_error(ERR_WARNING | ERR_PASS2,
1442 "ds segment base generated, but will be ignored in 64-bit mode");
1443 }
1444 c = 0x3E;
1445 break;
1446 case R_ES:
1447 if (bits == 64) {
1448 nasm_error(ERR_WARNING | ERR_PASS2,
1449 "es segment base generated, but will be ignored in 64-bit mode");
1450 }
1451 c = 0x26;
1452 break;
1453 case R_FS:
1454 c = 0x64;
1455 break;
1456 case R_GS:
1457 c = 0x65;
1458 break;
1459 case R_SS:
1460 if (bits == 64) {
1461 nasm_error(ERR_WARNING | ERR_PASS2,
1462 "ss segment base generated, but will be ignored in 64-bit mode");
1463 }
1464 c = 0x36;
1465 break;
1466 case R_SEGR6:
1467 case R_SEGR7:
1468 nasm_error(ERR_NONFATAL,
1469 "segr6 and segr7 cannot be used as prefixes");
1470 break;
1471 case P_A16:
1472 if (bits == 64) {
1473 nasm_error(ERR_NONFATAL,
1474 "16-bit addressing is not supported "
1475 "in 64-bit mode");
1476 } else if (bits != 16)
1477 c = 0x67;
1478 break;
1479 case P_A32:
1480 if (bits != 32)
1481 c = 0x67;
1482 break;
1483 case P_A64:
1484 if (bits != 64) {
1485 nasm_error(ERR_NONFATAL,
1486 "64-bit addressing is only supported "
1487 "in 64-bit mode");
1488 }
1489 break;
1490 case P_ASP:
1491 c = 0x67;
1492 break;
1493 case P_O16:
1494 if (bits != 16)
1495 c = 0x66;
1496 break;
1497 case P_O32:
1498 if (bits == 16)
1499 c = 0x66;
1500 break;
1501 case P_O64:
1502 /* REX.W */
1503 break;
1504 case P_OSP:
1505 c = 0x66;
1506 break;
1507 case P_EVEX:
1508 case P_VEX3:
1509 case P_VEX2:
1510 case P_NOBND:
1511 case P_none:
1512 break;
1513 default:
1514 nasm_panic(0, "invalid instruction prefix");
1515 }
1516 if (c) {
1517 if (data)
1518 out_rawbyte(data, c);
1519 bytes++;
1520 }
1521 }
1522 return bytes;
1523}
1524
1525static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001526{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001527 uint8_t c;
1528 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001529 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001530 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001531 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001532 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001533 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001534 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001535 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001536 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001537 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001538
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001539 ins->rex_done = false;
1540
H. Peter Anvina77692b2016-09-20 14:04:33 -07001541 emit_prefix(data, bits, ins);
1542
H. Peter Anvin839eca22007-10-29 23:12:47 -07001543 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001544 c = *codes++;
1545 op1 = (c & 3) + ((opex & 1) << 2);
1546 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1547 opx = &ins->oprs[op1];
1548 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001549
H. Peter Anvina77692b2016-09-20 14:04:33 -07001550
H. Peter Anvin839eca22007-10-29 23:12:47 -07001551 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 case 01:
1553 case 02:
1554 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001555 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001556 emit_rex(data, ins);
1557 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001560
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001561 case 05:
1562 case 06:
1563 case 07:
1564 opex = c;
1565 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001566
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001567 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001568 emit_rex(data, ins);
1569 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001571
Jin Kyu Song164d6072013-10-15 19:10:13 -07001572 case4(014):
1573 break;
1574
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001575 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001576 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001577 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001578 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001579 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001581
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001582 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001583 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001584 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001585 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001586 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001588
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001589 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001590 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001591 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001592 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001593
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001594 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001595 if (opx->type & (BITS16 | BITS32))
1596 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 else
1598 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001599 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001600 out_imm(data, opx, size, 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(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001604 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001605 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001607
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001608 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001609 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001610 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001611 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001612 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001613
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001614 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001615 if (opx->segment == data->segment) {
1616 int64_t delta = opx->offset - data->offset
1617 - (data->inslen - data->insoffs);
1618 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001619 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001620 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001621 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001623
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001624 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001625 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001626 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001627
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001628 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001629 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001630 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001631
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001632 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001633 if (opx->type & (BITS16 | BITS32 | BITS64))
1634 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 else
1636 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001637
1638 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001640
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001641 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001642 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001644
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001645 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001646 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001647 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001648 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001649 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001651
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001653 {
1654 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1655 const struct operand *opy;
1656
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001657 c = *codes++;
1658 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001659 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001660 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001661 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001662 "non-absolute expression not permitted as argument %d",
1663 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001664 } else if (opy->offset & ~mask) {
1665 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1666 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001667 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001668 c = opy->offset & mask;
1669 goto emit_is4;
1670 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001671
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001672 case 0173:
1673 c = *codes++;
1674 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001675 c &= 15;
1676 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001677
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001678 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001679 c = 0;
1680 emit_is4:
1681 r = nasm_regvals[opx->basereg];
1682 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001683 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001684
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001685 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001686 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001687 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001688 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001689 "signed dword immediate exceeds bounds");
1690 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001691 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001692 break;
1693
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001694 case4(0240):
1695 case 0250:
1696 codes += 3;
1697 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1698 EVEX_P2Z | EVEX_P2AAA, 2);
1699 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1700 bytes[0] = 0x62;
1701 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001702 bytes[1] = ((((ins->rex & 7) << 5) |
1703 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001704 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001705 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1706 ((~ins->vexreg & 15) << 3) |
1707 (1 << 2) | (ins->vex_wlp & 3);
1708 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001709 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001710 break;
1711
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001712 case4(0260):
1713 case 0270:
1714 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001715 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1716 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001717 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1718 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1719 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001720 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001721 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001722 } else {
1723 bytes[0] = 0xc5;
1724 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001725 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001726 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001727 }
1728 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001729
H. Peter Anvine014f352012-02-25 22:35:19 -08001730 case 0271:
1731 case 0272:
1732 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001733 break;
1734
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001735 case4(0274):
1736 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001737 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001738 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001739
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001740 if (absolute_op(opx)) {
1741 if (ins->rex & REX_W)
1742 s = 64;
1743 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1744 s = 16;
1745 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1746 s = 32;
1747 else
1748 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001749
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001750 um = (uint64_t)2 << (s-1);
1751 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001752
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001753 if (uv > 127 && uv < (uint64_t)-128 &&
1754 (uv < um-128 || uv > um-1)) {
1755 /* If this wasn't explicitly byte-sized, warn as though we
1756 * had fallen through to the imm16/32/64 case.
1757 */
1758 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1759 "%s value exceeds bounds",
1760 (opx->type & BITS8) ? "signed byte" :
1761 s == 16 ? "word" :
1762 s == 32 ? "dword" :
1763 "signed dword");
1764 }
1765
1766 /* Output as a raw byte to avoid byte overflow check */
1767 out_rawbyte(data, (uint8_t)uv);
1768 } else {
1769 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001770 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001771 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001772 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001773
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001774 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001776
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001778 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1779 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001781
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001783 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1784 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001786
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 case 0312:
1788 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001789
Keith Kaniosb7a89542007-04-12 02:40:54 +00001790 case 0313:
1791 ins->rex = 0;
1792 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001793
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001794 case4(0314):
1795 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001796
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001800
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001802 case 0323:
1803 break;
1804
Keith Kaniosb7a89542007-04-12 02:40:54 +00001805 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001806 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001808
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001809 case 0325:
1810 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001811
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001812 case 0326:
1813 break;
1814
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001816 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001818
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001821
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001822 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001824 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001826
Keith Kanios48af1772007-08-17 07:37:52 +00001827 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001828 if (ins->rex & REX_R)
1829 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001830 ins->rex &= ~(REX_L|REX_R);
1831 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001832
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001833 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001834 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001835
H. Peter Anvin962e3052008-08-28 17:47:16 -07001836 case 0336:
1837 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001838 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001839
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001842 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001843
1844 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001846
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001847 case 0341:
1848 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001849
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001850 case 0360:
1851 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001852
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001853 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001854 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001855 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001856
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001857 case 0364:
1858 case 0365:
1859 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001860
Keith Kanios48af1772007-08-17 07:37:52 +00001861 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001862 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001863 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001864 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001865
Jin Kyu Song03041092013-10-15 19:38:51 -07001866 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001868
H. Peter Anvine2c80182005-01-15 22:15:51 +00001869 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001870 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001872
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001873 case 0374:
1874 eat = EA_XMMVSIB;
1875 break;
1876
1877 case 0375:
1878 eat = EA_YMMVSIB;
1879 break;
1880
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001881 case 0376:
1882 eat = EA_ZMMVSIB;
1883 break;
1884
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001885 case4(0100):
1886 case4(0110):
1887 case4(0120):
1888 case4(0130):
1889 case4(0200):
1890 case4(0204):
1891 case4(0210):
1892 case4(0214):
1893 case4(0220):
1894 case4(0224):
1895 case4(0230):
1896 case4(0234):
1897 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001898 ea ea_data;
1899 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001900 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001901 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001902 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001903
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001904 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001905 /* pick rfield from operand b (opx) */
1906 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001907 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001908 } else {
1909 /* rfield is constant */
1910 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001911 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001912 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001914 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001915 rfield, rflags, ins, &errmsg) != eat)
1916 nasm_error(ERR_NONFATAL, "%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07001917
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918 p = bytes;
1919 *p++ = ea_data.modrm;
1920 if (ea_data.sib_present)
1921 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001922 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001924 /*
1925 * Make sure the address gets the right offset in case
1926 * the line breaks in the .lst file (BR 1197827)
1927 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001928
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001929 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001930 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001931 if (ea_data.disp8) {
1932 out_rawbyte(data, ea_data.disp8);
1933 } else if (ea_data.rip) {
1934 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001935 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001936 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001937
H. Peter Anvina77692b2016-09-20 14:04:33 -07001938 if (overflow_general(opy->offset, asize) ||
1939 signed_bits(opy->offset, ins->addr_size) !=
1940 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001941 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001942
H. Peter Anvina77692b2016-09-20 14:04:33 -07001943 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001944 (asize > ea_data.bytes)
1945 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001946 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001947 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001948 }
1949 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001950
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001951 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001952 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001953 ": instruction code \\%o (0x%02X) given", c, c);
1954 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001955 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001956 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001957}
1958
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001959static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001960{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001961 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001962 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001963 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001964}
1965
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001966static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001967{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001968 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001969 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001970 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001971}
1972
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001973static int op_rexflags(const operand * o, int mask)
1974{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001975 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001976 int val;
1977
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001978 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001979 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001980
H. Peter Anvina4835d42008-05-20 14:21:29 -07001981 flags = nasm_reg_flags[o->basereg];
1982 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001983
1984 return rexflags(val, flags, mask);
1985}
1986
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001987static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001988{
1989 int rex = 0;
1990
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001991 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001992 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001993 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001994 rex |= REX_W;
1995 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1996 rex |= REX_H;
1997 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1998 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001999
2000 return rex & mask;
2001}
2002
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002003static int evexflags(int val, decoflags_t deco,
2004 int mask, uint8_t byte)
2005{
2006 int evex = 0;
2007
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002008 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002009 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002010 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002011 evex |= (EVEX_P0RP | EVEX_P0X);
2012 break;
2013 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002014 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002015 evex |= EVEX_P2VP;
2016 if (deco & Z)
2017 evex |= EVEX_P2Z;
2018 if (deco & OPMASK_MASK)
2019 evex |= deco & EVEX_P2AAA;
2020 break;
2021 }
2022 return evex & mask;
2023}
2024
2025static int op_evexflags(const operand * o, int mask, uint8_t byte)
2026{
2027 int val;
2028
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002029 val = nasm_regvals[o->basereg];
2030
2031 return evexflags(val, o->decoflags, mask, byte);
2032}
2033
H. Peter Anvin23595f52009-07-25 17:44:25 -07002034static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002035 insn *instruction,
2036 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002037{
2038 const struct itemplate *temp;
2039 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002040 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002041 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002042 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002043 int i;
2044
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002045 /* broadcasting uses a different data element size */
2046 for (i = 0; i < instruction->operands; i++)
2047 if (i == broadcast)
2048 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2049 else
2050 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002051
2052 merr = MERR_INVALOP;
2053
2054 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002055 temp->opcode != I_none; temp++) {
2056 m = matches(temp, instruction, bits);
2057 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002058 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002059 m = MOK_GOOD;
2060 else
2061 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002062 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002063 /*
2064 * Missing operand size and a candidate for fuzzy matching...
2065 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002066 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002067 if (i == broadcast)
2068 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2069 else
2070 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002071 opsizemissing = true;
2072 }
2073 if (m > merr)
2074 merr = m;
2075 if (merr == MOK_GOOD)
2076 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002077 }
2078
2079 /* No match, but see if we can get a fuzzy operand size match... */
2080 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002081 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002082
2083 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002084 /*
2085 * We ignore extrinsic operand sizes on registers, so we should
2086 * never try to fuzzy-match on them. This also resolves the case
2087 * when we have e.g. "xmmrm128" in two different positions.
2088 */
2089 if (is_class(REGISTER, instruction->oprs[i].type))
2090 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002091
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002092 /* This tests if xsizeflags[i] has more than one bit set */
2093 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2094 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002095
Jin Kyu Song7903c072013-10-30 03:00:12 -07002096 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002097 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002098 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2099 BITS32 : BITS64);
2100 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002101 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002102 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002103 }
2104
2105 /* Try matching again... */
2106 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002107 temp->opcode != I_none; temp++) {
2108 m = matches(temp, instruction, bits);
2109 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002110 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002111 m = MOK_GOOD;
2112 else
2113 m = MERR_INVALOP;
2114 }
2115 if (m > merr)
2116 merr = m;
2117 if (merr == MOK_GOOD)
2118 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002119 }
2120
H. Peter Anvina81655b2009-07-25 18:15:28 -07002121done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002122 *tempp = temp;
2123 return merr;
2124}
2125
Mark Charneydcaef4b2014-10-09 13:45:17 -04002126static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2127{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002128 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002129 uint8_t brcast_num;
2130
Mark Charneydcaef4b2014-10-09 13:45:17 -04002131 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002132 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002133 "size of broadcasting element is greater than 64 bits");
2134
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002135 /*
2136 * The shift term is to take care of the extra BITS80 inserted
2137 * between BITS64 and BITS128.
2138 */
2139 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2140 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002141
2142 return brcast_num;
2143}
2144
H. Peter Anvin65289e82009-07-25 17:25:11 -07002145static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002146 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002147{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002148 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002149 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002150 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002151
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002152 /*
2153 * Check the opcode
2154 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002155 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002156 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002157
2158 /*
2159 * Count the operands
2160 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002161 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002162 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002163
2164 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002165 * Is it legal?
2166 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002167 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002168 return MERR_INVALOP;
2169
2170 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002171 * {evex} available?
2172 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002173 switch (instruction->prefixes[PPS_VEX]) {
2174 case P_EVEX:
2175 if (!itemp_has(itemp, IF_EVEX))
2176 return MERR_ENCMISMATCH;
2177 break;
2178 case P_VEX3:
2179 case P_VEX2:
2180 if (!itemp_has(itemp, IF_VEX))
2181 return MERR_ENCMISMATCH;
2182 break;
2183 default:
2184 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002185 }
2186
2187 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002188 * Check that no spurious colons or TOs are present
2189 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002190 for (i = 0; i < itemp->operands; i++)
2191 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002192 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002193
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002194 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002195 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002196 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002197 switch (itemp_smask(itemp)) {
2198 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002199 asize = BITS8;
2200 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002201 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002202 asize = BITS16;
2203 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002204 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002205 asize = BITS32;
2206 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002207 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002208 asize = BITS64;
2209 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002210 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002211 asize = BITS128;
2212 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002213 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002214 asize = BITS256;
2215 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002216 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002217 asize = BITS512;
2218 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002219 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002220 switch (bits) {
2221 case 16:
2222 asize = BITS16;
2223 break;
2224 case 32:
2225 asize = BITS32;
2226 break;
2227 case 64:
2228 asize = BITS64;
2229 break;
2230 default:
2231 asize = 0;
2232 break;
2233 }
2234 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002235 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002236 asize = 0;
2237 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002238 }
2239
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002240 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002241 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002242 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002243 memset(size, 0, sizeof size);
2244 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002245 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002246 /* S- flags apply to all operands */
2247 for (i = 0; i < MAX_OPERANDS; i++)
2248 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002249 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002250
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002251 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002252 * Check that the operand flags all match up,
2253 * it's a bit tricky so lets be verbose:
2254 *
2255 * 1) Find out the size of operand. If instruction
2256 * doesn't have one specified -- we're trying to
2257 * guess it either from template (IF_S* flag) or
2258 * from code bits.
2259 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002260 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002261 * template has an operand size specified AND this size differ
2262 * from which instruction has (perhaps we got it from code bits)
2263 * we are:
2264 * a) Check that only size of instruction and operand is differ
2265 * other characteristics do match
2266 * b) Perhaps it's a register specified in instruction so
2267 * for such a case we just mark that operand as "size
2268 * missing" and this will turn on fuzzy operand size
2269 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002270 */
2271 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002272 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002273 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002274 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002275 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002276 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002277 opflags_t template_opsize, insn_opsize;
2278
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002279 if (!(type & SIZE_MASK))
2280 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002281
Jin Kyu Song7903c072013-10-30 03:00:12 -07002282 insn_opsize = type & SIZE_MASK;
2283 if (!is_broadcast) {
2284 template_opsize = itemp->opd[i] & SIZE_MASK;
2285 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002286 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2287
2288 if (~ideco & BRDCAST_MASK)
2289 return MERR_BRNOTHERE;
2290
Jin Kyu Song7903c072013-10-30 03:00:12 -07002291 /*
2292 * when broadcasting, the element size depends on
2293 * the instruction type. decorator flag should match.
2294 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002295 if (deco_brsize) {
2296 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002297 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002298 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002299 } else {
2300 template_opsize = 0;
2301 }
2302 }
2303
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002304 if (~ideco & deco & OPMASK_MASK)
2305 return MERR_MASKNOTHERE;
2306
2307 if (itemp->opd[i] & ~type & ~SIZE_MASK) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002308 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002309 } else if (template_opsize) {
2310 if (template_opsize != insn_opsize) {
2311 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002312 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002313 } else if (!is_class(REGISTER, type)) {
2314 /*
2315 * Note: we don't honor extrinsic operand sizes for registers,
2316 * so "missing operand size" for a register should be
2317 * considered a wildcard match rather than an error.
2318 */
2319 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002320 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002321 } else if (is_broadcast &&
2322 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002323 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002324 /*
2325 * broadcasting opsize matches but the number of repeated memory
2326 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002327 * if 64b double precision float is broadcasted to ymm (256b),
2328 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002329 */
2330 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002331 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002332 }
2333 }
2334
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002335 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002336 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002337
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002338 /*
2339 * Check operand sizes
2340 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002341 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2342 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002343 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002344 asize = itemp->opd[i] & SIZE_MASK;
2345 if (asize) {
2346 for (i = 0; i < oprs; i++)
2347 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002348 break;
2349 }
2350 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002351 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002352 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002353 }
2354
Keith Kaniosb7a89542007-04-12 02:40:54 +00002355 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002356 if (!(itemp->opd[i] & SIZE_MASK) &&
2357 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002358 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002359 }
2360
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002361 /*
2362 * Check template is okay at the set cpu level
2363 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002364 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002365 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002366
Keith Kaniosb7a89542007-04-12 02:40:54 +00002367 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002368 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002369 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002370 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002371 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002373 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002374 * If we have a HLE prefix, look for the NOHLE flag
2375 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002376 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002377 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2378 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2379 return MERR_BADHLE;
2380
2381 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002382 * Check if special handling needed for Jumps
2383 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002384 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002385 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002386
Jin Kyu Song03041092013-10-15 19:38:51 -07002387 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002388 * Check if BND prefix is allowed.
2389 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002390 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002391 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002392 (has_prefix(instruction, PPS_REP, P_BND) ||
2393 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002394 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002395 else if (itemp_has(itemp, IF_BND) &&
2396 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2397 has_prefix(instruction, PPS_REP, P_REPNZ)))
2398 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002399
H. Peter Anvin60926242009-07-26 16:25:38 -07002400 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002401}
2402
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002403/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002404 * Check if ModR/M.mod should/can be 01.
2405 * - EAF_BYTEOFFS is set
2406 * - offset can fit in a byte when EVEX is not used
2407 * - offset can be compressed when EVEX is used
2408 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002409#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2410 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2411 is_disp8n(input, ins, &output->disp8) : \
2412 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2413 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002414
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002415static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002416 int rfield, opflags_t rflags, insn *ins,
2417 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002418{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002419 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002420 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002421 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002422
H. Peter Anvin8f622462017-04-02 19:02:29 -07002423 *errmsg = "invalid effective address"; /* Default error message */
2424
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002425 output->type = EA_SCALAR;
2426 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002427 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002428
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002429 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002430 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002431 /* EVEX.R' flag for the REG operand */
2432 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002433
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002434 if (is_class(REGISTER, input->type)) {
2435 /*
2436 * It's a direct register.
2437 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002438 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002439 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002440
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002441 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002442 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002443
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002444 /* broadcasting is not available with a direct register operand. */
2445 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002446 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002447 goto err;
2448 }
2449
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002450 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002451 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002452 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002453 output->bytes = 0; /* no offset necessary either */
2454 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2455 } else {
2456 /*
2457 * It's a memory reference.
2458 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002459
2460 /* Embedded rounding or SAE is not available with a mem ref operand. */
2461 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002462 *errmsg = "embedded rounding is available only with "
2463 "register-register operations";
2464 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002465 }
2466
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002467 if (input->basereg == -1 &&
2468 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002469 /*
2470 * It's a pure offset.
2471 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002472 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002473 if (input->segment == NO_SEG ||
2474 (input->opflags & OPFLAG_RELATIVE)) {
2475 nasm_error(ERR_WARNING | ERR_PASS2,
2476 "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002477 input->type &= ~IP_REL;
2478 input->type |= MEMORY;
2479 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002480 }
2481
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002482 if (bits == 64 &&
2483 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002484 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002485 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002486 }
2487
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002488 if (eaflags & EAF_BYTEOFFS ||
2489 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002490 input->disp_size != (addrbits != 16 ? 32 : 16))) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002491 nasm_error(ERR_WARNING | ERR_PASS1,
2492 "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002493 }
2494
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002495 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002496 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002497 output->sib = GEN_SIB(0, 4, 5);
2498 output->bytes = 4;
2499 output->modrm = GEN_MODRM(0, rfield, 4);
2500 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002501 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002502 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002503 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002504 output->modrm = GEN_MODRM(0, rfield,
2505 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002506 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002507 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002508 } else {
2509 /*
2510 * It's an indirection.
2511 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002512 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002513 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002515 int t, it, bt; /* register numbers */
2516 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002517
H. Peter Anvine2c80182005-01-15 22:15:51 +00002518 if (s == 0)
2519 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002520
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002521 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002522 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002523 ix = nasm_reg_flags[i];
2524 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002525 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002526 ix = 0;
2527 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002528
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002529 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002530 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002531 bx = nasm_reg_flags[b];
2532 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002533 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002534 bx = 0;
2535 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002536
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002537 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002538 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002539 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002540 int32_t o = input->offset;
2541 int mod, scale, index, base;
2542
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002543 /*
2544 * For a vector SIB, one has to be a vector and the other,
2545 * if present, a GPR. The vector must be the index operand.
2546 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002547 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002548 if (s == 0)
2549 s = 1;
2550 else if (s != 1)
2551 goto err;
2552
2553 t = bt, bt = it, it = t;
2554 x = bx, bx = ix, ix = x;
2555 }
2556
2557 if (bt != -1) {
2558 if (REG_GPR & ~bx)
2559 goto err;
2560 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2561 sok &= bx;
2562 else
2563 goto err;
2564 }
2565
2566 /*
2567 * While we're here, ensure the user didn't specify
2568 * WORD or QWORD
2569 */
2570 if (input->disp_size == 16 || input->disp_size == 64)
2571 goto err;
2572
2573 if (addrbits == 16 ||
2574 (addrbits == 32 && !(sok & BITS32)) ||
2575 (addrbits == 64 && !(sok & BITS64)))
2576 goto err;
2577
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002578 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2579 : ((ix & YMMREG & ~REG_EA)
2580 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002581
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002582 output->rex |= rexflags(it, ix, REX_X);
2583 output->rex |= rexflags(bt, bx, REX_B);
2584 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002585
2586 index = it & 7; /* it is known to be != -1 */
2587
2588 switch (s) {
2589 case 1:
2590 scale = 0;
2591 break;
2592 case 2:
2593 scale = 1;
2594 break;
2595 case 4:
2596 scale = 2;
2597 break;
2598 case 8:
2599 scale = 3;
2600 break;
2601 default: /* then what the smeg is it? */
2602 goto err; /* panic */
2603 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002604
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002605 if (bt == -1) {
2606 base = 5;
2607 mod = 0;
2608 } else {
2609 base = (bt & 7);
2610 if (base != REG_NUM_EBP && o == 0 &&
2611 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002612 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002613 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002614 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002615 mod = 1;
2616 else
2617 mod = 2;
2618 }
2619
2620 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002621 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2622 output->modrm = GEN_MODRM(mod, rfield, 4);
2623 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002624 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002625 /*
2626 * it must be a 32/64-bit memory reference. Firstly we have
2627 * to check that all registers involved are type E/Rxx.
2628 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002629 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002630 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002631
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002632 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002633 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2634 sok &= ix;
2635 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002636 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002637 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002638
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002639 if (bt != -1) {
2640 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002641 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002642 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002643 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002644 sok &= bx;
2645 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002646
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002647 /*
2648 * While we're here, ensure the user didn't specify
2649 * WORD or QWORD
2650 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002651 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002652 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002653
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002654 if (addrbits == 16 ||
2655 (addrbits == 32 && !(sok & BITS32)) ||
2656 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002657 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002658
Keith Kaniosb7a89542007-04-12 02:40:54 +00002659 /* now reorganize base/index */
2660 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002661 ((hb == b && ht == EAH_NOTBASE) ||
2662 (hb == i && ht == EAH_MAKEBASE))) {
2663 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002664 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002665 x = bx, bx = ix, ix = x;
2666 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002667
Jin Kyu Song164d6072013-10-15 19:10:13 -07002668 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002669 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002670 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002671 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002672 if (eaflags & EAF_MIB) {
2673 /* only for mib operands */
2674 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2675 /*
2676 * make a single reg index [reg*1].
2677 * gas uses this form for an explicit index register.
2678 */
2679 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2680 }
2681 if ((ht == EAH_SUMMED) && bt == -1) {
2682 /* separate once summed index into [base, index] */
2683 bt = it, bx = ix, s--;
2684 }
2685 } else {
2686 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002687 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002688 s == 3 || s == 5 || s == 9) && bt == -1) {
2689 /* convert 3*EAX to EAX+2*EAX */
2690 bt = it, bx = ix, s--;
2691 }
2692 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002693 (eaflags & EAF_TIMESTWO) &&
2694 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002695 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002696 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002697 * to sib format with 0x0 displacement - [EAX*1+0].
2698 */
2699 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2700 }
2701 }
Keith Kanios48af1772007-08-17 07:37:52 +00002702 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002703 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002704 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002705 x = ix, ix = bx, bx = x;
2706 }
2707 if (it == REG_NUM_ESP ||
2708 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002709 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002710
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002711 output->rex |= rexflags(it, ix, REX_X);
2712 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002713
Keith Kanios48af1772007-08-17 07:37:52 +00002714 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002715 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002717
Keith Kaniosb7a89542007-04-12 02:40:54 +00002718 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002720 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002721 } else {
2722 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002723 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002724 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002725 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002726 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002727 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002728 mod = 1;
2729 else
2730 mod = 2;
2731 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002732
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002733 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002734 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2735 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002736 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002737 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002739
Keith Kaniosb7a89542007-04-12 02:40:54 +00002740 if (it == -1)
2741 index = 4, s = 1;
2742 else
2743 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002744
H. Peter Anvine2c80182005-01-15 22:15:51 +00002745 switch (s) {
2746 case 1:
2747 scale = 0;
2748 break;
2749 case 2:
2750 scale = 1;
2751 break;
2752 case 4:
2753 scale = 2;
2754 break;
2755 case 8:
2756 scale = 3;
2757 break;
2758 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002759 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002761
Keith Kaniosb7a89542007-04-12 02:40:54 +00002762 if (bt == -1) {
2763 base = 5;
2764 mod = 0;
2765 } else {
2766 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002767 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002768 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002769 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002770 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002771 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002772 mod = 1;
2773 else
2774 mod = 2;
2775 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002776
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002777 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002778 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2779 output->modrm = GEN_MODRM(mod, rfield, 4);
2780 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002781 }
2782 } else { /* it's 16-bit */
2783 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002784 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002785
Keith Kaniosb7a89542007-04-12 02:40:54 +00002786 /* check for 64-bit long mode */
2787 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002788 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002789
H. Peter Anvine2c80182005-01-15 22:15:51 +00002790 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002791 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2792 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002793 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002794
Keith Kaniosb7a89542007-04-12 02:40:54 +00002795 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002796 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002797 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002798
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002800 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 if (b == -1 && i != -1) {
2802 int tmp = b;
2803 b = i;
2804 i = tmp;
2805 } /* swap */
2806 if ((b == R_SI || b == R_DI) && i != -1) {
2807 int tmp = b;
2808 b = i;
2809 i = tmp;
2810 }
2811 /* have BX/BP as base, SI/DI index */
2812 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002813 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 if (i != -1 && b != -1 &&
2815 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002816 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002817 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002818 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002819
H. Peter Anvine2c80182005-01-15 22:15:51 +00002820 rm = -1;
2821 if (i != -1)
2822 switch (i * 256 + b) {
2823 case R_SI * 256 + R_BX:
2824 rm = 0;
2825 break;
2826 case R_DI * 256 + R_BX:
2827 rm = 1;
2828 break;
2829 case R_SI * 256 + R_BP:
2830 rm = 2;
2831 break;
2832 case R_DI * 256 + R_BP:
2833 rm = 3;
2834 break;
2835 } else
2836 switch (b) {
2837 case R_SI:
2838 rm = 4;
2839 break;
2840 case R_DI:
2841 rm = 5;
2842 break;
2843 case R_BP:
2844 rm = 6;
2845 break;
2846 case R_BX:
2847 rm = 7;
2848 break;
2849 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002850 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002851 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002852
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002853 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002854 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002855 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002856 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 mod = 1;
2858 else
2859 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002860
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002861 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002862 output->bytes = mod; /* bytes of offset needed */
2863 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002864 }
2865 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002866 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002867
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002868 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002869 return output->type;
2870
2871err:
2872 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002873}
2874
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002875static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002876{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002877 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002878 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002879
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002880 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002881
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002882 switch (ins->prefixes[PPS_ASIZE]) {
2883 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002884 valid &= 16;
2885 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002886 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002887 valid &= 32;
2888 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002889 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002890 valid &= 64;
2891 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002892 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002893 valid &= (addrbits == 32) ? 16 : 32;
2894 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002895 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002896 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002897 }
2898
2899 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002900 if (is_class(MEMORY, ins->oprs[j].type)) {
2901 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002902
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002903 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002904 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 i = 0;
2906 else
2907 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002908
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002909 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002910 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 b = 0;
2912 else
2913 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002914
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002915 if (ins->oprs[j].scale == 0)
2916 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002917
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002918 if (!i && !b) {
2919 int ds = ins->oprs[j].disp_size;
2920 if ((addrbits != 64 && ds > 8) ||
2921 (addrbits == 64 && ds == 16))
2922 valid &= ds;
2923 } else {
2924 if (!(REG16 & ~b))
2925 valid &= 16;
2926 if (!(REG32 & ~b))
2927 valid &= 32;
2928 if (!(REG64 & ~b))
2929 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002930
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002931 if (!(REG16 & ~i))
2932 valid &= 16;
2933 if (!(REG32 & ~i))
2934 valid &= 32;
2935 if (!(REG64 & ~i))
2936 valid &= 64;
2937 }
2938 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002939 }
2940
2941 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002942 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002943 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002944 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002945 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002946 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002947 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002948 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002949 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002950 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002951 }
2952
2953 defdisp = ins->addr_size == 16 ? 16 : 32;
2954
2955 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002956 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2957 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2958 /*
2959 * mem_offs sizes must match the address size; if not,
2960 * strip the MEM_OFFS bit and match only EA instructions
2961 */
2962 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2963 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002964 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002965}