blob: caed64f015abef4ed13f852945c9754de6421c27 [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 Anvinea6e34d2002-04-30 20:51:32 +0000186#include "assemble.h"
187#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700188#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800189#include "disp8.h"
H. Peter Anvin172b8402016-02-18 01:16:18 -0800190#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000191
H. Peter Anvin65289e82009-07-25 17:25:11 -0700192enum match_result {
193 /*
194 * Matching errors. These should be sorted so that more specific
195 * errors come later in the sequence.
196 */
197 MERR_INVALOP,
198 MERR_OPSIZEMISSING,
199 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700200 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700201 MERR_BADCPU,
202 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800203 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700204 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700205 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800206 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700207 /*
208 * Matching success; the conditional ones first
209 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400210 MOK_JUMP, /* Matching OK but needs jmp_match() */
211 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700212};
213
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700215 enum ea_type type; /* what kind of EA is this? */
216 int sib_present; /* is a SIB byte necessary? */
217 int bytes; /* # of bytes of offset needed */
218 int size; /* lazy - this is sib+bytes+1 */
219 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700220 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221} ea;
222
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400223#define GEN_SIB(scale, index, base) \
224 (((scale) << 6) | ((index) << 3) | ((base)))
225
226#define GEN_MODRM(mod, reg, rm) \
227 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
228
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400229static iflag_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000230
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800231static int64_t calcsize(int32_t, int64_t, int, insn *,
232 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700233static int emit_prefix(struct out_data *data, const int bits, insn *ins);
234static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700235static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400236 insn *instruction,
237 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700238static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700239static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000240static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700241static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000242static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700243static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700244static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000245
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700246static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700247
H. Peter Anvin164d2462017-02-20 02:39:56 -0800248static inline bool absolute_op(const struct operand *o)
249{
250 return o->segment == NO_SEG && o->wrt == NO_SEG &&
251 !(o->opflags & OPFLAG_RELATIVE);
252}
253
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400254static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000255{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700256 return ins->prefixes[pos] == prefix;
257}
258
259static void assert_no_prefix(insn * ins, enum prefix_pos pos)
260{
261 if (ins->prefixes[pos])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800262 nasm_error(ERR_NONFATAL, "invalid %s prefix",
263 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700264}
265
266static const char *size_name(int size)
267{
268 switch (size) {
269 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700271 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700273 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700275 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400276 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700277 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400278 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700279 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400280 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700281 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400282 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700283 case 64:
284 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700285 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400286 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000287 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700288}
289
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400290static void warn_overflow(int pass, int size)
291{
H. Peter Anvin215186f2016-02-17 20:27:41 -0800292 nasm_error(ERR_WARNING | pass | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400293 "%s data exceeds bounds", size_name(size));
294}
295
296static void warn_overflow_const(int64_t data, int size)
297{
298 if (overflow_general(data, size))
299 warn_overflow(ERR_PASS1, size);
300}
301
302static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700303{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800304 if (absolute_op(o)) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400305 if (overflow_general(o->offset, size))
306 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700307 }
308}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400309
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800310static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
311{
312 bool err;
313
314 switch (sign) {
315 case OUT_WRAP:
316 err = overflow_general(data, size);
317 break;
318 case OUT_SIGNED:
319 err = overflow_signed(data, size);
320 break;
321 case OUT_UNSIGNED:
322 err = overflow_unsigned(data, size);
323 break;
324 default:
325 panic();
326 break;
327 }
328
329 if (err)
330 warn_overflow(ERR_PASS2, size);
331}
332
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000333/*
334 * This routine wrappers the real output format's output routine,
335 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800336 * generator at the same time, flatten unnecessary relocations,
337 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000338 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700339static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000340{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000341 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700342 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700343 int asize;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800344 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700345 union {
346 uint8_t b[8];
347 uint64_t q;
348 } xdata;
349 uint64_t size = data->size;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800350 int64_t addrval;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000351
H. Peter Anvina77692b2016-09-20 14:04:33 -0700352 if (!data->size)
353 return; /* Nothing to do */
354
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700355 /*
356 * Convert addresses to RAWDATA if possible
357 * XXX: not all backends want this for global symbols!!!!
358 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700359 switch (data->type) {
360 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800361 addrval = data->toffset;
362 goto address;
363
364 case OUT_RELADDR:
365 addrval = data->toffset - data->relbase;
366 goto address;
367
368 address:
H. Peter Anvina77692b2016-09-20 14:04:33 -0700369 asize = data->size;
370 nasm_assert(asize <= 8);
371 if (data->tsegment == NO_SEG && data->twrt == NO_SEG) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700372 uint8_t *q = xdata.b;
373
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800374 warn_overflow_out(addrval, asize, data->sign);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700375
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800376 WRITEADDR(q, addrval, asize);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700377 data->data = xdata.b;
378 data->type = OUT_RAWDATA;
379 asize = 0; /* No longer an address */
380 }
381 break;
382
383 default:
384 asize = 0; /* Not an address */
385 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000386 }
387
H. Peter Anvina77692b2016-09-20 14:04:33 -0700388 lfmt->output(data);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800389
Frank Kotlerabebb082003-09-06 04:45:37 +0000390 /*
391 * this call to src_get determines when we call the
392 * debug-format-specific "linenum" function
393 * it updates lineno and lnfname to the current values
394 * returning 0 if "same as last time", -2 if lnfname
395 * changed, and the amount by which lineno changed,
396 * if it did. thus, these variables must be static
397 */
398
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400399 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700400 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000401
H. Peter Anvinb6412502016-02-11 21:07:40 -0800402 if (asize && asize > amax) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700403 if (data->type != OUT_ADDRESS || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800404 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800405 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800406 asize << 3, ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800407 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800408 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800409 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800410 asize << 3, ofmt->maxbits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700411 data->size = amax;
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700412 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700413 data->insoffs += amax;
414 data->offset += amax;
415 data->size = size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800416 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700417 data->data = zero_buffer;
418 data->type = OUT_RAWDATA;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800419 }
420
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700421 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700422 data->offset += size;
423 data->insoffs += size;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000424}
425
H. Peter Anvina77692b2016-09-20 14:04:33 -0700426static inline void out_rawdata(struct out_data *data, const void *rawdata,
427 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400428{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700429 data->type = OUT_RAWDATA;
430 data->data = rawdata;
431 data->size = size;
432 out(data);
433}
434
435static void out_rawbyte(struct out_data *data, uint8_t byte)
436{
437 data->type = OUT_RAWDATA;
438 data->data = &byte;
439 data->size = 1;
440 out(data);
441}
442
443static inline void out_reserve(struct out_data *data, uint64_t size)
444{
445 data->type = OUT_RESERVE;
446 data->size = size;
447 out(data);
448}
449
H. Peter Anvin164d2462017-02-20 02:39:56 -0800450static inline void out_imm(struct out_data *data, const struct operand *opx,
H. Peter Anvina77692b2016-09-20 14:04:33 -0700451 int size, enum out_sign sign)
452{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800453 data->type =
454 (opx->opflags & OPFLAG_RELATIVE) ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700455 data->sign = sign;
456 data->size = size;
457 data->toffset = opx->offset;
458 data->tsegment = opx->segment;
459 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800460 /*
461 * XXX: improve this if at some point in the future we can
462 * distinguish the subtrahend in expressions like [foo - bar]
463 * where bar is a symbol in the current segment. However, at the
464 * current point, if OPFLAG_RELATIVE is set that subtraction has
465 * already occurred.
466 */
467 data->relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700468 out(data);
469}
470
H. Peter Anvin164d2462017-02-20 02:39:56 -0800471static void out_reladdr(struct out_data *data, const struct operand *opx,
472 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700473{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800474 if (opx->opflags & OPFLAG_RELATIVE)
475 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
476
H. Peter Anvina77692b2016-09-20 14:04:33 -0700477 data->type = OUT_RELADDR;
478 data->sign = OUT_SIGNED;
479 data->size = size;
480 data->toffset = opx->offset;
481 data->tsegment = opx->segment;
482 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800483 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700484 out(data);
485}
486
H. Peter Anvin164d2462017-02-20 02:39:56 -0800487static inline void out_segment(struct out_data *data,
488 const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700489{
490 data->type = OUT_SEGMENT;
491 data->sign = OUT_UNSIGNED;
492 data->size = 2;
493 data->toffset = opx->offset;
494 data->tsegment = ofmt->segbase(opx->segment + 1);
495 data->twrt = opx->wrt;
496 out(data);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400497}
498
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700499static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800500 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000501{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800502 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800503 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000504 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800505 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000506
H. Peter Anvin755f5212012-02-25 11:41:34 -0800507 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700508 return false;
509 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400510 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700511 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400512 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700513
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800514 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100515
Victor van den Elzen154e5922009-02-25 17:32:00 +0100516 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100517 /* Be optimistic in pass 1 */
518 return true;
519
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700521 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000522
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700523 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800524 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
525
526 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
527 /* jmp short (opcode eb) cannot be used with bnd prefix. */
528 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800529 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800530 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800531 }
532
533 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000535
H. Peter Anvin04445362016-09-21 15:56:19 -0700536/* This is totally just a wild guess what is reasonable... */
537#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
538
H. Peter Anvina77692b2016-09-20 14:04:33 -0700539int64_t assemble(int32_t segment, int64_t start, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800540 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000541{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700542 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000543 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700544 enum match_result m;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000545 int32_t itimes;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300546 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000547
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000548 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000549
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800550 nasm_zero(&data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700551 data.offset = start;
552 data.segment = segment;
553 data.itemp = NULL;
554 data.sign = OUT_WRAP;
555 data.bits = bits;
556
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300557 wsize = idata_bytes(instruction->opcode);
558 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000559 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000560
H. Peter Anvineba20a72002-04-30 20:53:55 +0000561 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000563 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564 if (t < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800565 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000566
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400568 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400570 if (wsize > 8) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800571 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400572 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000573 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700574 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700575 data.insoffs = 0;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700576 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700577 data.inslen = data.size = wsize;
578 data.toffset = e->offset;
579 data.tsegment = e->segment;
580 data.twrt = e->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800581 data.relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700582 out(&data);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400583 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700584 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400585 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700586 int align = e->stringlen % wsize;
587 if (align)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000588 align = wsize - align;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700589
590 data.insoffs = 0;
591 data.inslen = e->stringlen + align;
592
593 out_rawdata(&data, e->stringval, e->stringlen);
594 out_rawdata(&data, zero_buffer, align);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000595 }
596 }
597 if (t > 0 && t == instruction->times - 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700598 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800599 lfmt->uplevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 }
601 }
602 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800603 lfmt->downlevel(LIST_TIMES);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700604 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700605 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000606 FILE *fp;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700607 size_t t = instruction->times;
608 off_t base = 0;
609 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700610 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700611 char *buf = NULL;
612 size_t blk = 0; /* Buffered I/O block size */
613 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000614
H. Peter Anvind81a2352016-09-21 14:03:18 -0700615 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400616 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800617 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000618 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700619 goto done;
620 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000621
H. Peter Anvind81a2352016-09-21 14:03:18 -0700622 len = nasm_file_size(fp);
623
624 if (len == (off_t)-1) {
625 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700626 fname);
627 goto close_done;
628 }
629
H. Peter Anvina77692b2016-09-20 14:04:33 -0700630 if (instruction->eops->next) {
631 base = instruction->eops->next->offset;
632 if (base >= len) {
633 len = 0;
634 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000635 len -= base;
636 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700637 len > (off_t)instruction->eops->next->next->offset)
638 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000640 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700641
H. Peter Anvina77692b2016-09-20 14:04:33 -0700642 lfmt->set_offset(data.offset);
643 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000644
H. Peter Anvind81a2352016-09-21 14:03:18 -0700645 if (!len)
646 goto end_incbin;
647
648 /* Try to map file data */
649 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700650 if (!map) {
651 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
652 buf = nasm_malloc(blk);
653 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700654
655 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700656 /*
657 * Consider these irrelevant for INCBIN, since it is fully
658 * possible that these might be (way) bigger than an int
659 * can hold; there is, however, no reason to widen these
660 * types just for INCBIN. data.inslen == 0 signals to the
661 * backend that these fields are meaningless, if at all
662 * needed.
663 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700664 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700665 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700666
H. Peter Anvind81a2352016-09-21 14:03:18 -0700667 if (map) {
668 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700669 } else if ((off_t)m == len) {
670 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700671 } else {
672 off_t l = len;
673
674 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700675 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700676 "`incbin': unable to seek on file `%s'",
677 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700678 goto end_incbin;
679 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700680 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700681 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700682 if (!m || feof(fp)) {
683 /*
684 * This shouldn't happen unless the file
685 * actually changes while we are reading
686 * it.
687 */
688 nasm_error(ERR_NONFATAL,
689 "`incbin': unexpected EOF while"
690 " reading file `%s'", fname);
691 goto end_incbin;
692 }
693 out_rawdata(&data, buf, m);
694 l -= m;
695 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700696 }
697 }
698 end_incbin:
699 lfmt->downlevel(LIST_INCBIN);
700 if (instruction->times > 1) {
701 lfmt->set_offset(data.offset);
702 lfmt->uplevel(LIST_TIMES);
703 lfmt->downlevel(LIST_TIMES);
704 }
705 if (ferror(fp)) {
706 nasm_error(ERR_NONFATAL,
707 "`incbin': error while"
708 " reading file `%s'", fname);
709 }
710 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700711 if (buf)
712 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700713 if (map)
714 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700715 fclose(fp);
716 done:
717 ;
718 } else {
719 /* "Real" instruction */
720
721 /* Check to see if we need an address-size prefix */
722 add_asp(instruction, bits);
723
724 m = find_match(&temp, instruction, data.segment, data.offset, bits);
725
726 if (m == MOK_GOOD) {
727 /* Matches! */
728 int64_t insn_size = calcsize(data.segment, data.offset,
729 bits, instruction, temp);
730 itimes = instruction->times;
731 if (insn_size < 0) /* shouldn't be, on pass two */
732 nasm_panic(0, "errors made it through from pass one");
733
734 data.itemp = temp;
735 data.bits = bits;
736
737 while (itimes--) {
738 data.insoffs = 0;
739 data.inslen = insn_size;
740
741 gencode(&data, instruction);
742 nasm_assert(data.insoffs == insn_size);
743
744 if (itimes > 0 && itimes == instruction->times - 1) {
745 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800746 lfmt->uplevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400747 }
748 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700749 if (instruction->times > 1)
750 lfmt->downlevel(LIST_TIMES);
751 } else {
752 /* No match */
753 switch (m) {
754 case MERR_OPSIZEMISSING:
755 nasm_error(ERR_NONFATAL, "operation size not specified");
756 break;
757 case MERR_OPSIZEMISMATCH:
758 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
759 break;
760 case MERR_BRNUMMISMATCH:
761 nasm_error(ERR_NONFATAL,
762 "mismatch in the number of broadcasting elements");
763 break;
764 case MERR_BADCPU:
765 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
766 break;
767 case MERR_BADMODE:
768 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
769 bits);
770 break;
771 case MERR_ENCMISMATCH:
772 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
773 break;
774 case MERR_BADBND:
775 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
776 break;
777 case MERR_BADREPNE:
778 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
779 (has_prefix(instruction, PPS_REP, P_REPNE) ?
780 "repne" : "repnz"));
781 break;
782 default:
783 nasm_error(ERR_NONFATAL,
784 "invalid combination of opcode and operands");
785 break;
786 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400787 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000788 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700789 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000790}
791
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400792int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800793 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000794{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000795 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700796 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000797
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000798 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000799
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400800 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000802
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700803 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
804 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400805 instruction->opcode == I_DT || instruction->opcode == I_DO ||
806 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000807 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300808 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000809
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300811 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000812
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400813 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000814 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000815
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400817 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400819 warn_overflow_const(e->offset, wsize);
820 } else if (e->type == EOT_DB_STRING ||
821 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000822 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000823
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 align = (-osize) % wsize;
825 if (align < 0)
826 align += wsize;
827 isize += osize + align;
828 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700829 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000830 }
831
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400833 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700834 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000835
H. Peter Anvind81a2352016-09-21 14:03:18 -0700836 len = nasm_file_size_by_path(fname);
837 if (len == (off_t)-1) {
838 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
839 fname);
840 return 0;
841 }
842
843 if (instruction->eops->next) {
844 if (len <= (off_t)instruction->eops->next->offset) {
845 len = 0;
846 } else {
847 len -= instruction->eops->next->offset;
848 if (instruction->eops->next->next &&
849 len > (off_t)instruction->eops->next->next->offset) {
850 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000851 }
852 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000853 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700854
855 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000856 }
857
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700858 /* Check to see if we need an address-size prefix */
859 add_asp(instruction, bits);
860
H. Peter Anvin23595f52009-07-25 17:44:25 -0700861 m = find_match(&temp, instruction, segment, offset, bits);
862 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400863 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700864 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700865 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400866 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000867 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000868}
869
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800870static void bad_hle_warn(const insn * ins, uint8_t hleok)
871{
872 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800873 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800874 static const enum whatwarn warn[2][4] =
875 {
876 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
877 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
878 };
879 unsigned int n;
880
881 n = (unsigned int)rep_pfx - P_XACQUIRE;
882 if (n > 1)
883 return; /* Not XACQUIRE/XRELEASE */
884
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800885 ww = warn[n][hleok];
886 if (!is_class(MEMORY, ins->oprs[0].type))
887 ww = w_inval; /* HLE requires operand 0 to be memory */
888
889 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800890 case w_none:
891 break;
892
893 case w_lock:
894 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800895 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800896 "%s with this instruction requires lock",
897 prefix_name(rep_pfx));
898 }
899 break;
900
901 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800902 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800903 "%s invalid with this instruction",
904 prefix_name(rep_pfx));
905 break;
906 }
907}
908
H. Peter Anvin507ae032008-10-09 15:37:10 -0700909/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400910#define case3(x) case (x): case (x)+1: case (x)+2
911#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700912
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800913static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800914 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000915{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800916 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800917 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000918 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000919 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700920 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700921 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700922 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700923 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800924 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800925 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700926 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000927
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700928 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700929 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700930 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700931
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700932 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400933 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700934
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 (void)segment; /* Don't warn that this parameter is unused */
936 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000937
H. Peter Anvin839eca22007-10-29 23:12:47 -0700938 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400939 c = *codes++;
940 op1 = (c & 3) + ((opex & 1) << 2);
941 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
942 opx = &ins->oprs[op1];
943 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700944
H. Peter Anvin839eca22007-10-29 23:12:47 -0700945 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400946 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000947 codes += c, length += c;
948 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700949
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400950 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400951 opex = c;
952 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700953
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400954 case4(010):
955 ins->rex |=
956 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 codes++, length++;
958 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700959
Jin Kyu Song164d6072013-10-15 19:10:13 -0700960 case4(014):
961 /* this is an index reg of MIB operand */
962 mib_index = opx->basereg;
963 break;
964
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400965 case4(020):
966 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 length++;
968 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700969
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400970 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 length += 2;
972 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700973
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400974 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700975 if (opx->type & (BITS16 | BITS32 | BITS64))
976 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 else
978 length += (bits == 16) ? 2 : 4;
979 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700980
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400981 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 length += 4;
983 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700984
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400985 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700986 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000987 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700988
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400989 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 length++;
991 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700992
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400993 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000994 length += 8; /* MOV reg64/imm */
995 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700996
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400997 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000998 length += 2;
999 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001000
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001001 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001002 if (opx->type & (BITS16 | BITS32 | BITS64))
1003 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 else
1005 length += (bits == 16) ? 2 : 4;
1006 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001007
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001008 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 length += 4;
1010 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001011
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001012 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001013 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001014 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001015
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001016 case 0172:
1017 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001018 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001019 length++;
1020 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001021
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001022 case4(0174):
1023 length++;
1024 break;
1025
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001026 case4(0240):
1027 ins->rex |= REX_EV;
1028 ins->vexreg = regval(opx);
1029 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1030 ins->vex_cm = *codes++;
1031 ins->vex_wlp = *codes++;
1032 ins->evex_tuple = (*codes++ - 0300);
1033 break;
1034
1035 case 0250:
1036 ins->rex |= REX_EV;
1037 ins->vexreg = 0;
1038 ins->vex_cm = *codes++;
1039 ins->vex_wlp = *codes++;
1040 ins->evex_tuple = (*codes++ - 0300);
1041 break;
1042
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001043 case4(0254):
1044 length += 4;
1045 break;
1046
1047 case4(0260):
1048 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001049 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001050 ins->vex_cm = *codes++;
1051 ins->vex_wlp = *codes++;
1052 break;
1053
1054 case 0270:
1055 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001056 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001057 ins->vex_cm = *codes++;
1058 ins->vex_wlp = *codes++;
1059 break;
1060
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001061 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001062 hleok = c & 3;
1063 break;
1064
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001065 case4(0274):
1066 length++;
1067 break;
1068
1069 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001071
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001073 if (bits == 64)
1074 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001075 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001077
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001079 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001081
H. Peter Anvine2c80182005-01-15 22:15:51 +00001082 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001083 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001084
Keith Kaniosb7a89542007-04-12 02:40:54 +00001085 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001086 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1087 has_prefix(ins, PPS_ASIZE, P_A32))
1088 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001090
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001091 case4(0314):
1092 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001093
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001095 {
1096 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1097 if (pfx == P_O16)
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_O16;
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 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001107 {
1108 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1109 if (pfx == P_O32)
1110 break;
1111 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001112 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001113 else
1114 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001116 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001117
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 case 0322:
1119 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001120
Keith Kaniosb7a89542007-04-12 02:40:54 +00001121 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001122 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001123 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001124
Keith Kaniosb7a89542007-04-12 02:40:54 +00001125 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001126 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001127 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001128
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001129 case 0325:
1130 ins->rex |= REX_NH;
1131 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001132
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001133 case 0326:
1134 break;
1135
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 case 0330:
1137 codes++, length++;
1138 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001139
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001142
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001143 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 case 0333:
1145 length++;
1146 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001147
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001148 case 0334:
1149 ins->rex |= REX_L;
1150 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001151
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001152 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001153 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001154
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001155 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001156 if (!ins->prefixes[PPS_REP])
1157 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001158 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001159
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001160 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001161 if (!ins->prefixes[PPS_REP])
1162 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001163 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001164
H. Peter Anvine2c80182005-01-15 22:15:51 +00001165 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001166 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001167 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001169 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1170 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001171 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001173 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001174 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001175
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001176 case 0341:
1177 if (!ins->prefixes[PPS_WAIT])
1178 ins->prefixes[PPS_WAIT] = P_WAIT;
1179 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001180
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001181 case 0360:
1182 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001183
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001184 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001185 length++;
1186 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001187
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001188 case 0364:
1189 case 0365:
1190 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001191
Keith Kanios48af1772007-08-17 07:37:52 +00001192 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001193 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001194 length++;
1195 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001196
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001197 case 0370:
1198 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001200
H. Peter Anvine2c80182005-01-15 22:15:51 +00001201 case 0373:
1202 length++;
1203 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001204
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001205 case 0374:
1206 eat = EA_XMMVSIB;
1207 break;
1208
1209 case 0375:
1210 eat = EA_YMMVSIB;
1211 break;
1212
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001213 case 0376:
1214 eat = EA_ZMMVSIB;
1215 break;
1216
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001217 case4(0100):
1218 case4(0110):
1219 case4(0120):
1220 case4(0130):
1221 case4(0200):
1222 case4(0204):
1223 case4(0210):
1224 case4(0214):
1225 case4(0220):
1226 case4(0224):
1227 case4(0230):
1228 case4(0234):
1229 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001230 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001231 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001232 opflags_t rflags;
1233 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001234 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001235
Keith Kaniosb7a89542007-04-12 02:40:54 +00001236 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001237
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001238 if (c <= 0177) {
1239 /* pick rfield from operand b (opx) */
1240 rflags = regflag(opx);
1241 rfield = nasm_regvals[opx->basereg];
1242 } else {
1243 rflags = 0;
1244 rfield = c & 7;
1245 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001246
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001247 /* EVEX.b1 : evex_brerop contains the operand position */
1248 op_er_sae = (ins->evex_brerop >= 0 ?
1249 &ins->oprs[ins->evex_brerop] : NULL);
1250
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001251 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1252 /* set EVEX.b */
1253 ins->evex_p[2] |= EVEX_P2B;
1254 if (op_er_sae->decoflags & ER) {
1255 /* set EVEX.RC (rounding control) */
1256 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1257 & EVEX_P2RC;
1258 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001259 } else {
1260 /* set EVEX.L'L (vector length) */
1261 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001262 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001263 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001264 /* set EVEX.b */
1265 ins->evex_p[2] |= EVEX_P2B;
1266 }
1267 }
1268
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001269 if (itemp_has(temp, IF_MIB)) {
1270 opy->eaflags |= EAF_MIB;
1271 /*
1272 * if a separate form of MIB (ICC style) is used,
1273 * the index reg info is merged into mem operand
1274 */
1275 if (mib_index != R_none) {
1276 opy->indexreg = mib_index;
1277 opy->scale = 1;
1278 opy->hintbase = mib_index;
1279 opy->hinttype = EAH_NOTBASE;
1280 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001281 }
1282
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001283 if (process_ea(opy, &ea_data, bits,
1284 rfield, rflags, ins) != eat) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001285 nasm_error(ERR_NONFATAL, "invalid effective address");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001286 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001287 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001288 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001290 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001291 }
1292 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001293
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001294 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001295 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001296 ": instruction code \\%o (0x%02X) given", c, c);
1297 break;
1298 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001299 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001300
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001301 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001302
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001303 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001304 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001305 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001306 return -1;
1307 }
1308 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001309 }
1310
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001311 switch (ins->prefixes[PPS_VEX]) {
1312 case P_EVEX:
1313 if (!(ins->rex & REX_EV))
1314 return -1;
1315 break;
1316 case P_VEX3:
1317 case P_VEX2:
1318 if (!(ins->rex & REX_V))
1319 return -1;
1320 break;
1321 default:
1322 break;
1323 }
1324
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001325 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001326 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001327
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001328 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001329 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001330 return -1;
1331 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001332 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001333 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001334 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001335 ins->rex &= ~REX_W;
1336 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001337 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001338 ins->rex |= REX_W;
1339 bad32 &= ~REX_W;
1340 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001341 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001342 /* Follow REX_W */
1343 break;
1344 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001345
H. Peter Anvinfc561202011-07-07 16:58:22 -07001346 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001347 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001348 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001349 } else if (!(ins->rex & REX_EV) &&
1350 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001351 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001352 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001353 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001354 if (ins->rex & REX_EV)
1355 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001356 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1357 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001358 length += 3;
1359 else
1360 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001361 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001362 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001363 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001364 return -1;
1365 } else if (bits == 64) {
1366 length++;
1367 } else if ((ins->rex & REX_L) &&
1368 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001369 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001370 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001371 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001372 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001373 length++;
1374 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001375 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001376 return -1;
1377 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001378 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001379
1380 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001381 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001382 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001383 "instruction is not lockable");
1384 }
1385
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001386 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001387
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001388 /*
1389 * when BND prefix is set by DEFAULT directive,
1390 * BND prefix is added to every appropriate instruction line
1391 * unless it is overridden by NOBND prefix.
1392 */
1393 if (globalbnd &&
1394 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1395 ins->prefixes[PPS_REP] = P_BND;
1396
H. Peter Anvina77692b2016-09-20 14:04:33 -07001397 /*
1398 * Add length of legacy prefixes
1399 */
1400 length += emit_prefix(NULL, bits, ins);
1401
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001402 return length;
1403}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001404
H. Peter Anvina77692b2016-09-20 14:04:33 -07001405static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001406{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001407 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001408 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001409 !(ins->rex & (REX_V | REX_EV)) &&
1410 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001411 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1412 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001413 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001414 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001415 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001416}
1417
H. Peter Anvina77692b2016-09-20 14:04:33 -07001418static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1419{
1420 int bytes = 0;
1421 int j;
1422
1423 for (j = 0; j < MAXPREFIX; j++) {
1424 uint8_t c = 0;
1425 switch (ins->prefixes[j]) {
1426 case P_WAIT:
1427 c = 0x9B;
1428 break;
1429 case P_LOCK:
1430 c = 0xF0;
1431 break;
1432 case P_REPNE:
1433 case P_REPNZ:
1434 case P_XACQUIRE:
1435 case P_BND:
1436 c = 0xF2;
1437 break;
1438 case P_REPE:
1439 case P_REPZ:
1440 case P_REP:
1441 case P_XRELEASE:
1442 c = 0xF3;
1443 break;
1444 case R_CS:
1445 if (bits == 64) {
1446 nasm_error(ERR_WARNING | ERR_PASS2,
1447 "cs segment base generated, but will be ignored in 64-bit mode");
1448 }
1449 c = 0x2E;
1450 break;
1451 case R_DS:
1452 if (bits == 64) {
1453 nasm_error(ERR_WARNING | ERR_PASS2,
1454 "ds segment base generated, but will be ignored in 64-bit mode");
1455 }
1456 c = 0x3E;
1457 break;
1458 case R_ES:
1459 if (bits == 64) {
1460 nasm_error(ERR_WARNING | ERR_PASS2,
1461 "es segment base generated, but will be ignored in 64-bit mode");
1462 }
1463 c = 0x26;
1464 break;
1465 case R_FS:
1466 c = 0x64;
1467 break;
1468 case R_GS:
1469 c = 0x65;
1470 break;
1471 case R_SS:
1472 if (bits == 64) {
1473 nasm_error(ERR_WARNING | ERR_PASS2,
1474 "ss segment base generated, but will be ignored in 64-bit mode");
1475 }
1476 c = 0x36;
1477 break;
1478 case R_SEGR6:
1479 case R_SEGR7:
1480 nasm_error(ERR_NONFATAL,
1481 "segr6 and segr7 cannot be used as prefixes");
1482 break;
1483 case P_A16:
1484 if (bits == 64) {
1485 nasm_error(ERR_NONFATAL,
1486 "16-bit addressing is not supported "
1487 "in 64-bit mode");
1488 } else if (bits != 16)
1489 c = 0x67;
1490 break;
1491 case P_A32:
1492 if (bits != 32)
1493 c = 0x67;
1494 break;
1495 case P_A64:
1496 if (bits != 64) {
1497 nasm_error(ERR_NONFATAL,
1498 "64-bit addressing is only supported "
1499 "in 64-bit mode");
1500 }
1501 break;
1502 case P_ASP:
1503 c = 0x67;
1504 break;
1505 case P_O16:
1506 if (bits != 16)
1507 c = 0x66;
1508 break;
1509 case P_O32:
1510 if (bits == 16)
1511 c = 0x66;
1512 break;
1513 case P_O64:
1514 /* REX.W */
1515 break;
1516 case P_OSP:
1517 c = 0x66;
1518 break;
1519 case P_EVEX:
1520 case P_VEX3:
1521 case P_VEX2:
1522 case P_NOBND:
1523 case P_none:
1524 break;
1525 default:
1526 nasm_panic(0, "invalid instruction prefix");
1527 }
1528 if (c) {
1529 if (data)
1530 out_rawbyte(data, c);
1531 bytes++;
1532 }
1533 }
1534 return bytes;
1535}
1536
1537static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001538{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001539 uint8_t c;
1540 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001541 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001542 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001543 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001544 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001545 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001546 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001547 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001548 const int bits = data->bits;
H. Peter Anvin70653092007-10-19 14:42:29 -07001549
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001550 ins->rex_done = false;
1551
H. Peter Anvina77692b2016-09-20 14:04:33 -07001552 emit_prefix(data, bits, ins);
1553
H. Peter Anvin839eca22007-10-29 23:12:47 -07001554 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001555 c = *codes++;
1556 op1 = (c & 3) + ((opex & 1) << 2);
1557 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1558 opx = &ins->oprs[op1];
1559 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001560
H. Peter Anvina77692b2016-09-20 14:04:33 -07001561
H. Peter Anvin839eca22007-10-29 23:12:47 -07001562 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001563 case 01:
1564 case 02:
1565 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001566 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001567 emit_rex(data, ins);
1568 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001570 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001571
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001572 case 05:
1573 case 06:
1574 case 07:
1575 opex = c;
1576 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001577
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001578 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001579 emit_rex(data, ins);
1580 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001581 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001582
Jin Kyu Song164d6072013-10-15 19:10:13 -07001583 case4(014):
1584 break;
1585
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001586 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001587 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001588 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001589 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001590 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001592
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001594 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001595 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001596 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001597 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001599
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001600 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001601 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001602 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001604
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001605 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001606 if (opx->type & (BITS16 | BITS32))
1607 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 else
1609 size = (bits == 16) ? 2 : 4;
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(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001615 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001616 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001618
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001619 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001620 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001621 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001622 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001624
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001625 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001626 if (opx->segment == data->segment) {
1627 int64_t delta = opx->offset - data->offset
1628 - (data->inslen - data->insoffs);
1629 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001630 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001631 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001632 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001633 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001634
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001635 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001636 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001637 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001638
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001639 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001640 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001641 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001642
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001643 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001644 if (opx->type & (BITS16 | BITS32 | BITS64))
1645 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 else
1647 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001648
1649 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001651
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001653 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001655
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001656 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001657 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001658 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001659 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001660 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001661 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001662
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001663 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001664 {
1665 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1666 const struct operand *opy;
1667
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001668 c = *codes++;
1669 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001670 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001671 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001672 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001673 "non-absolute expression not permitted as argument %d",
1674 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001675 } else if (opy->offset & ~mask) {
1676 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1677 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001678 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001679 c = opy->offset & mask;
1680 goto emit_is4;
1681 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001682
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001683 case 0173:
1684 c = *codes++;
1685 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001686 c &= 15;
1687 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001688
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001689 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001690 c = 0;
1691 emit_is4:
1692 r = nasm_regvals[opx->basereg];
1693 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001694 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001695
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001696 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001697 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001698 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001699 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001700 "signed dword immediate exceeds bounds");
1701 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001702 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001703 break;
1704
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001705 case4(0240):
1706 case 0250:
1707 codes += 3;
1708 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1709 EVEX_P2Z | EVEX_P2AAA, 2);
1710 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1711 bytes[0] = 0x62;
1712 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001713 bytes[1] = ((((ins->rex & 7) << 5) |
1714 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001715 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001716 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1717 ((~ins->vexreg & 15) << 3) |
1718 (1 << 2) | (ins->vex_wlp & 3);
1719 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001720 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001721 break;
1722
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001723 case4(0260):
1724 case 0270:
1725 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001726 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1727 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001728 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1729 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1730 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001731 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001732 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001733 } else {
1734 bytes[0] = 0xc5;
1735 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001736 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001737 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001738 }
1739 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001740
H. Peter Anvine014f352012-02-25 22:35:19 -08001741 case 0271:
1742 case 0272:
1743 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001744 break;
1745
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001746 case4(0274):
1747 {
1748 uint64_t uv, um;
1749 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001750
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001751 if (ins->rex & REX_W)
1752 s = 64;
1753 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1754 s = 16;
1755 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1756 s = 32;
1757 else
1758 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001759
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001760 um = (uint64_t)2 << (s-1);
1761 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001762
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001763 if (uv > 127 && uv < (uint64_t)-128 &&
1764 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001765 /* If this wasn't explicitly byte-sized, warn as though we
1766 * had fallen through to the imm16/32/64 case.
1767 */
H. Peter Anvin215186f2016-02-17 20:27:41 -08001768 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001769 "%s value exceeds bounds",
1770 (opx->type & BITS8) ? "signed byte" :
1771 s == 16 ? "word" :
1772 s == 32 ? "dword" :
1773 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001774 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001775 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001776 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001777 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001778
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001779 case4(0300):
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 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001783 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
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 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001788 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1789 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001791
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 case 0312:
1793 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001794
Keith Kaniosb7a89542007-04-12 02:40:54 +00001795 case 0313:
1796 ins->rex = 0;
1797 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001798
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001799 case4(0314):
1800 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001801
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001805
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001807 case 0323:
1808 break;
1809
Keith Kaniosb7a89542007-04-12 02:40:54 +00001810 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001811 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001813
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001814 case 0325:
1815 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001816
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001817 case 0326:
1818 break;
1819
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001821 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001823
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001826
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001827 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001829 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001831
Keith Kanios48af1772007-08-17 07:37:52 +00001832 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001833 if (ins->rex & REX_R)
1834 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001835 ins->rex &= ~(REX_L|REX_R);
1836 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001837
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001838 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001839 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001840
H. Peter Anvin962e3052008-08-28 17:47:16 -07001841 case 0336:
1842 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001843 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001844
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001847 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001848
1849 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001851
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001852 case 0341:
1853 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001854
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001855 case 0360:
1856 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001857
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001858 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001859 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001860 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001861
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001862 case 0364:
1863 case 0365:
1864 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001865
Keith Kanios48af1772007-08-17 07:37:52 +00001866 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001867 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001868 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001869 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001870
Jin Kyu Song03041092013-10-15 19:38:51 -07001871 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001873
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001875 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001877
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001878 case 0374:
1879 eat = EA_XMMVSIB;
1880 break;
1881
1882 case 0375:
1883 eat = EA_YMMVSIB;
1884 break;
1885
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001886 case 0376:
1887 eat = EA_ZMMVSIB;
1888 break;
1889
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001890 case4(0100):
1891 case4(0110):
1892 case4(0120):
1893 case4(0130):
1894 case4(0200):
1895 case4(0204):
1896 case4(0210):
1897 case4(0214):
1898 case4(0220):
1899 case4(0224):
1900 case4(0230):
1901 case4(0234):
1902 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001903 ea ea_data;
1904 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001905 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001906 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001907 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001908
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001909 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001910 /* pick rfield from operand b (opx) */
1911 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001912 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001913 } else {
1914 /* rfield is constant */
1915 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001916 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001917 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001919 if (process_ea(opy, &ea_data, bits,
1920 rfield, rflags, ins) != eat)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001921 nasm_error(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001922
H. Peter Anvine2c80182005-01-15 22:15:51 +00001923 p = bytes;
1924 *p++ = ea_data.modrm;
1925 if (ea_data.sib_present)
1926 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001927 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001929 /*
1930 * Make sure the address gets the right offset in case
1931 * the line breaks in the .lst file (BR 1197827)
1932 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001933
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001934 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001935 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001936 if (ea_data.disp8) {
1937 out_rawbyte(data, ea_data.disp8);
1938 } else if (ea_data.rip) {
1939 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001940 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001941 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001942
H. Peter Anvina77692b2016-09-20 14:04:33 -07001943 if (overflow_general(opy->offset, asize) ||
1944 signed_bits(opy->offset, ins->addr_size) !=
1945 signed_bits(opy->offset, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001946 warn_overflow(ERR_PASS2, ea_data.bytes);
1947
H. Peter Anvina77692b2016-09-20 14:04:33 -07001948 out_imm(data, opy, ea_data.bytes,
1949 (asize > ea_data.bytes) ? OUT_SIGNED : OUT_UNSIGNED);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001950 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001952 }
1953 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001954
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001955 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001956 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001957 ": instruction code \\%o (0x%02X) given", c, c);
1958 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001960 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001961}
1962
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001963static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001964{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001965 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001966 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001967 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001968}
1969
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001970static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001971{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001972 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001973 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001974 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001975}
1976
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001977static int op_rexflags(const operand * o, int mask)
1978{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001979 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001980 int val;
1981
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001982 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001983 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001984
H. Peter Anvina4835d42008-05-20 14:21:29 -07001985 flags = nasm_reg_flags[o->basereg];
1986 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001987
1988 return rexflags(val, flags, mask);
1989}
1990
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001991static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001992{
1993 int rex = 0;
1994
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001995 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001996 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001997 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001998 rex |= REX_W;
1999 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2000 rex |= REX_H;
2001 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2002 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002003
2004 return rex & mask;
2005}
2006
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002007static int evexflags(int val, decoflags_t deco,
2008 int mask, uint8_t byte)
2009{
2010 int evex = 0;
2011
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002012 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002013 case 0:
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_P0RP | EVEX_P0X);
2016 break;
2017 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002018 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002019 evex |= EVEX_P2VP;
2020 if (deco & Z)
2021 evex |= EVEX_P2Z;
2022 if (deco & OPMASK_MASK)
2023 evex |= deco & EVEX_P2AAA;
2024 break;
2025 }
2026 return evex & mask;
2027}
2028
2029static int op_evexflags(const operand * o, int mask, uint8_t byte)
2030{
2031 int val;
2032
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002033 val = nasm_regvals[o->basereg];
2034
2035 return evexflags(val, o->decoflags, mask, byte);
2036}
2037
H. Peter Anvin23595f52009-07-25 17:44:25 -07002038static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002039 insn *instruction,
2040 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002041{
2042 const struct itemplate *temp;
2043 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002044 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002045 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002046 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002047 int i;
2048
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002049 /* broadcasting uses a different data element size */
2050 for (i = 0; i < instruction->operands; i++)
2051 if (i == broadcast)
2052 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2053 else
2054 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002055
2056 merr = MERR_INVALOP;
2057
2058 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002059 temp->opcode != I_none; temp++) {
2060 m = matches(temp, instruction, bits);
2061 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002062 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002063 m = MOK_GOOD;
2064 else
2065 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002066 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002067 /*
2068 * Missing operand size and a candidate for fuzzy matching...
2069 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002070 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002071 if (i == broadcast)
2072 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2073 else
2074 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002075 opsizemissing = true;
2076 }
2077 if (m > merr)
2078 merr = m;
2079 if (merr == MOK_GOOD)
2080 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002081 }
2082
2083 /* No match, but see if we can get a fuzzy operand size match... */
2084 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002085 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002086
2087 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002088 /*
2089 * We ignore extrinsic operand sizes on registers, so we should
2090 * never try to fuzzy-match on them. This also resolves the case
2091 * when we have e.g. "xmmrm128" in two different positions.
2092 */
2093 if (is_class(REGISTER, instruction->oprs[i].type))
2094 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002095
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002096 /* This tests if xsizeflags[i] has more than one bit set */
2097 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2098 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002099
Jin Kyu Song7903c072013-10-30 03:00:12 -07002100 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002101 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002102 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2103 BITS32 : BITS64);
2104 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002105 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002106 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002107 }
2108
2109 /* Try matching again... */
2110 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002111 temp->opcode != I_none; temp++) {
2112 m = matches(temp, instruction, bits);
2113 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002114 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002115 m = MOK_GOOD;
2116 else
2117 m = MERR_INVALOP;
2118 }
2119 if (m > merr)
2120 merr = m;
2121 if (merr == MOK_GOOD)
2122 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002123 }
2124
H. Peter Anvina81655b2009-07-25 18:15:28 -07002125done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002126 *tempp = temp;
2127 return merr;
2128}
2129
Mark Charneydcaef4b2014-10-09 13:45:17 -04002130static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2131{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002132 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002133 uint8_t brcast_num;
2134
Mark Charneydcaef4b2014-10-09 13:45:17 -04002135 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002136 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002137 "size of broadcasting element is greater than 64 bits");
2138
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002139 /*
2140 * The shift term is to take care of the extra BITS80 inserted
2141 * between BITS64 and BITS128.
2142 */
2143 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2144 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002145
2146 return brcast_num;
2147}
2148
H. Peter Anvin65289e82009-07-25 17:25:11 -07002149static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002150 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002151{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002152 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002153 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002154 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002155
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002156 /*
2157 * Check the opcode
2158 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002159 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002160 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002161
2162 /*
2163 * Count the operands
2164 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002165 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002166 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002167
2168 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002169 * Is it legal?
2170 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002171 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002172 return MERR_INVALOP;
2173
2174 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002175 * {evex} available?
2176 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002177 switch (instruction->prefixes[PPS_VEX]) {
2178 case P_EVEX:
2179 if (!itemp_has(itemp, IF_EVEX))
2180 return MERR_ENCMISMATCH;
2181 break;
2182 case P_VEX3:
2183 case P_VEX2:
2184 if (!itemp_has(itemp, IF_VEX))
2185 return MERR_ENCMISMATCH;
2186 break;
2187 default:
2188 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002189 }
2190
2191 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002192 * Check that no spurious colons or TOs are present
2193 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002194 for (i = 0; i < itemp->operands; i++)
2195 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002196 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002197
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002198 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002199 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002200 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002201 switch (itemp_smask(itemp)) {
2202 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002203 asize = BITS8;
2204 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002205 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002206 asize = BITS16;
2207 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002208 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002209 asize = BITS32;
2210 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002211 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002212 asize = BITS64;
2213 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002214 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002215 asize = BITS128;
2216 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002217 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002218 asize = BITS256;
2219 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002220 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002221 asize = BITS512;
2222 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002223 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002224 switch (bits) {
2225 case 16:
2226 asize = BITS16;
2227 break;
2228 case 32:
2229 asize = BITS32;
2230 break;
2231 case 64:
2232 asize = BITS64;
2233 break;
2234 default:
2235 asize = 0;
2236 break;
2237 }
2238 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002239 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002240 asize = 0;
2241 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002242 }
2243
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002244 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002245 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002246 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002247 memset(size, 0, sizeof size);
2248 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002249 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002250 /* S- flags apply to all operands */
2251 for (i = 0; i < MAX_OPERANDS; i++)
2252 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002253 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002254
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002255 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002256 * Check that the operand flags all match up,
2257 * it's a bit tricky so lets be verbose:
2258 *
2259 * 1) Find out the size of operand. If instruction
2260 * doesn't have one specified -- we're trying to
2261 * guess it either from template (IF_S* flag) or
2262 * from code bits.
2263 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002264 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002265 * template has an operand size specified AND this size differ
2266 * from which instruction has (perhaps we got it from code bits)
2267 * we are:
2268 * a) Check that only size of instruction and operand is differ
2269 * other characteristics do match
2270 * b) Perhaps it's a register specified in instruction so
2271 * for such a case we just mark that operand as "size
2272 * missing" and this will turn on fuzzy operand size
2273 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002274 */
2275 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002276 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002277 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002278 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002279 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002280 opflags_t template_opsize, insn_opsize;
2281
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002282 if (!(type & SIZE_MASK))
2283 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002284
Jin Kyu Song7903c072013-10-30 03:00:12 -07002285 insn_opsize = type & SIZE_MASK;
2286 if (!is_broadcast) {
2287 template_opsize = itemp->opd[i] & SIZE_MASK;
2288 } else {
2289 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2290 /*
2291 * when broadcasting, the element size depends on
2292 * the instruction type. decorator flag should match.
2293 */
2294
2295 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
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002304 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002305 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002306 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002307 } else if (template_opsize) {
2308 if (template_opsize != insn_opsize) {
2309 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002310 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002311 } else if (!is_class(REGISTER, type)) {
2312 /*
2313 * Note: we don't honor extrinsic operand sizes for registers,
2314 * so "missing operand size" for a register should be
2315 * considered a wildcard match rather than an error.
2316 */
2317 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002318 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002319 } else if (is_broadcast &&
2320 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002321 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002322 /*
2323 * broadcasting opsize matches but the number of repeated memory
2324 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002325 * if 64b double precision float is broadcasted to ymm (256b),
2326 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002327 */
2328 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002329 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002330 }
2331 }
2332
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002333 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002334 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002335
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002336 /*
2337 * Check operand sizes
2338 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002339 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2340 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002341 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002342 asize = itemp->opd[i] & SIZE_MASK;
2343 if (asize) {
2344 for (i = 0; i < oprs; i++)
2345 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002346 break;
2347 }
2348 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002349 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002350 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002351 }
2352
Keith Kaniosb7a89542007-04-12 02:40:54 +00002353 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 if (!(itemp->opd[i] & SIZE_MASK) &&
2355 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002356 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002357 }
2358
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002359 /*
2360 * Check template is okay at the set cpu level
2361 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002362 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002363 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002364
Keith Kaniosb7a89542007-04-12 02:40:54 +00002365 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002366 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002367 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002368 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002369 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002371 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002372 * If we have a HLE prefix, look for the NOHLE flag
2373 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002374 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002375 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2376 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2377 return MERR_BADHLE;
2378
2379 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002380 * Check if special handling needed for Jumps
2381 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002382 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002383 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384
Jin Kyu Song03041092013-10-15 19:38:51 -07002385 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002386 * Check if BND prefix is allowed.
2387 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002388 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002389 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002390 (has_prefix(instruction, PPS_REP, P_BND) ||
2391 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002392 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002393 else if (itemp_has(itemp, IF_BND) &&
2394 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2395 has_prefix(instruction, PPS_REP, P_REPNZ)))
2396 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002397
H. Peter Anvin60926242009-07-26 16:25:38 -07002398 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002399}
2400
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002401/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002402 * Check if ModR/M.mod should/can be 01.
2403 * - EAF_BYTEOFFS is set
2404 * - offset can fit in a byte when EVEX is not used
2405 * - offset can be compressed when EVEX is used
2406 */
2407#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2408 (o >= -128 && o <= 127 && \
2409 seg == NO_SEG && !forw_ref && \
2410 !(input->eaflags & EAF_WORDOFFS) && \
2411 !(ins->rex & REX_EV)) || \
2412 (ins->rex & REX_EV && \
2413 is_disp8n(input, ins, &output->disp8)))
2414
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002415static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002416 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002417{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002418 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002419 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002420 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002421
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002422 output->type = EA_SCALAR;
2423 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002424 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002425
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002426 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002427 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002428 /* EVEX.R' flag for the REG operand */
2429 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002430
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002431 if (is_class(REGISTER, input->type)) {
2432 /*
2433 * It's a direct register.
2434 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002435 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002436 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002437
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002438 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002439 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002440
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002441 /* broadcasting is not available with a direct register operand. */
2442 if (input->decoflags & BRDCAST_MASK) {
2443 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2444 goto err;
2445 }
2446
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002447 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002448 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002449 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002450 output->bytes = 0; /* no offset necessary either */
2451 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2452 } else {
2453 /*
2454 * It's a memory reference.
2455 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002456
2457 /* Embedded rounding or SAE is not available with a mem ref operand. */
2458 if (input->decoflags & (ER | SAE)) {
2459 nasm_error(ERR_NONFATAL,
2460 "Embedded rounding is available only with reg-reg op.");
2461 return -1;
2462 }
2463
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002464 if (input->basereg == -1 &&
2465 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002466 /*
2467 * It's a pure offset.
2468 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002469 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
2470 if (input->segment == NO_SEG || (input->opflags & OPFLAG_RELATIVE)) {
2471 nasm_error(ERR_WARNING | ERR_PASS2, "absolute address can not be RIP-relative");
2472 input->type &= ~IP_REL;
2473 input->type |= MEMORY;
2474 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002475 }
2476
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002477 if (bits == 64 &&
2478 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2479 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2480 return -1;
2481 }
2482
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002483 if (eaflags & EAF_BYTEOFFS ||
2484 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002485 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2486 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2487 }
2488
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002489 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002490 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002491 output->sib = GEN_SIB(0, 4, 5);
2492 output->bytes = 4;
2493 output->modrm = GEN_MODRM(0, rfield, 4);
2494 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002495 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002496 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002497 output->bytes = (addrbits != 16 ? 4 : 2);
2498 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2499 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002500 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002501 } else {
2502 /*
2503 * It's an indirection.
2504 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002505 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002506 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002507 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002508 int t, it, bt; /* register numbers */
2509 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002510
H. Peter Anvine2c80182005-01-15 22:15:51 +00002511 if (s == 0)
2512 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002513
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002514 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002515 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002516 ix = nasm_reg_flags[i];
2517 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002518 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002519 ix = 0;
2520 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002521
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002522 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002523 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002524 bx = nasm_reg_flags[b];
2525 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002526 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002527 bx = 0;
2528 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002529
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002530 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002531 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002532 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002533 int32_t o = input->offset;
2534 int mod, scale, index, base;
2535
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002536 /*
2537 * For a vector SIB, one has to be a vector and the other,
2538 * if present, a GPR. The vector must be the index operand.
2539 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002540 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002541 if (s == 0)
2542 s = 1;
2543 else if (s != 1)
2544 goto err;
2545
2546 t = bt, bt = it, it = t;
2547 x = bx, bx = ix, ix = x;
2548 }
2549
2550 if (bt != -1) {
2551 if (REG_GPR & ~bx)
2552 goto err;
2553 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2554 sok &= bx;
2555 else
2556 goto err;
2557 }
2558
2559 /*
2560 * While we're here, ensure the user didn't specify
2561 * WORD or QWORD
2562 */
2563 if (input->disp_size == 16 || input->disp_size == 64)
2564 goto err;
2565
2566 if (addrbits == 16 ||
2567 (addrbits == 32 && !(sok & BITS32)) ||
2568 (addrbits == 64 && !(sok & BITS64)))
2569 goto err;
2570
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002571 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2572 : ((ix & YMMREG & ~REG_EA)
2573 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002574
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002575 output->rex |= rexflags(it, ix, REX_X);
2576 output->rex |= rexflags(bt, bx, REX_B);
2577 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002578
2579 index = it & 7; /* it is known to be != -1 */
2580
2581 switch (s) {
2582 case 1:
2583 scale = 0;
2584 break;
2585 case 2:
2586 scale = 1;
2587 break;
2588 case 4:
2589 scale = 2;
2590 break;
2591 case 8:
2592 scale = 3;
2593 break;
2594 default: /* then what the smeg is it? */
2595 goto err; /* panic */
2596 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002597
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002598 if (bt == -1) {
2599 base = 5;
2600 mod = 0;
2601 } else {
2602 base = (bt & 7);
2603 if (base != REG_NUM_EBP && o == 0 &&
2604 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002605 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002606 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002607 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002608 mod = 1;
2609 else
2610 mod = 2;
2611 }
2612
2613 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002614 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2615 output->modrm = GEN_MODRM(mod, rfield, 4);
2616 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002617 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002618 /*
2619 * it must be a 32/64-bit memory reference. Firstly we have
2620 * to check that all registers involved are type E/Rxx.
2621 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002622 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002623 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002624
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002625 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002626 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2627 sok &= ix;
2628 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002629 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002630 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002631
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002632 if (bt != -1) {
2633 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002634 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002635 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002636 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002637 sok &= bx;
2638 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002639
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002640 /*
2641 * While we're here, ensure the user didn't specify
2642 * WORD or QWORD
2643 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002644 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002645 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002646
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002647 if (addrbits == 16 ||
2648 (addrbits == 32 && !(sok & BITS32)) ||
2649 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002650 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002651
Keith Kaniosb7a89542007-04-12 02:40:54 +00002652 /* now reorganize base/index */
2653 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002654 ((hb == b && ht == EAH_NOTBASE) ||
2655 (hb == i && ht == EAH_MAKEBASE))) {
2656 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002657 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002658 x = bx, bx = ix, ix = x;
2659 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002660
Jin Kyu Song164d6072013-10-15 19:10:13 -07002661 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002662 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002663 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002664 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002665 if (eaflags & EAF_MIB) {
2666 /* only for mib operands */
2667 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2668 /*
2669 * make a single reg index [reg*1].
2670 * gas uses this form for an explicit index register.
2671 */
2672 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2673 }
2674 if ((ht == EAH_SUMMED) && bt == -1) {
2675 /* separate once summed index into [base, index] */
2676 bt = it, bx = ix, s--;
2677 }
2678 } else {
2679 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002680 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002681 s == 3 || s == 5 || s == 9) && bt == -1) {
2682 /* convert 3*EAX to EAX+2*EAX */
2683 bt = it, bx = ix, s--;
2684 }
2685 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002686 (eaflags & EAF_TIMESTWO) &&
2687 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002688 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002689 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002690 * to sib format with 0x0 displacement - [EAX*1+0].
2691 */
2692 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2693 }
2694 }
Keith Kanios48af1772007-08-17 07:37:52 +00002695 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002696 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002697 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002698 x = ix, ix = bx, bx = x;
2699 }
2700 if (it == REG_NUM_ESP ||
2701 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002702 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002703
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002704 output->rex |= rexflags(it, ix, REX_X);
2705 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002706
Keith Kanios48af1772007-08-17 07:37:52 +00002707 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002708 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002709 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002710
Keith Kaniosb7a89542007-04-12 02:40:54 +00002711 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002713 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002714 } else {
2715 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002716 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002717 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002718 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002719 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002720 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002721 mod = 1;
2722 else
2723 mod = 2;
2724 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002725
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002726 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002727 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2728 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002729 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002730 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002732
Keith Kaniosb7a89542007-04-12 02:40:54 +00002733 if (it == -1)
2734 index = 4, s = 1;
2735 else
2736 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002737
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 switch (s) {
2739 case 1:
2740 scale = 0;
2741 break;
2742 case 2:
2743 scale = 1;
2744 break;
2745 case 4:
2746 scale = 2;
2747 break;
2748 case 8:
2749 scale = 3;
2750 break;
2751 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002752 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002754
Keith Kaniosb7a89542007-04-12 02:40:54 +00002755 if (bt == -1) {
2756 base = 5;
2757 mod = 0;
2758 } else {
2759 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002760 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002761 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002762 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002763 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002764 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002765 mod = 1;
2766 else
2767 mod = 2;
2768 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002769
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002770 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002771 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2772 output->modrm = GEN_MODRM(mod, rfield, 4);
2773 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002774 }
2775 } else { /* it's 16-bit */
2776 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002777 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002778
Keith Kaniosb7a89542007-04-12 02:40:54 +00002779 /* check for 64-bit long mode */
2780 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002781 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002782
H. Peter Anvine2c80182005-01-15 22:15:51 +00002783 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002784 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2785 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002786 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002787
Keith Kaniosb7a89542007-04-12 02:40:54 +00002788 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002789 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002790 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002791
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002793 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002794 if (b == -1 && i != -1) {
2795 int tmp = b;
2796 b = i;
2797 i = tmp;
2798 } /* swap */
2799 if ((b == R_SI || b == R_DI) && i != -1) {
2800 int tmp = b;
2801 b = i;
2802 i = tmp;
2803 }
2804 /* have BX/BP as base, SI/DI index */
2805 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002806 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002807 if (i != -1 && b != -1 &&
2808 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002809 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002810 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002811 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002812
H. Peter Anvine2c80182005-01-15 22:15:51 +00002813 rm = -1;
2814 if (i != -1)
2815 switch (i * 256 + b) {
2816 case R_SI * 256 + R_BX:
2817 rm = 0;
2818 break;
2819 case R_DI * 256 + R_BX:
2820 rm = 1;
2821 break;
2822 case R_SI * 256 + R_BP:
2823 rm = 2;
2824 break;
2825 case R_DI * 256 + R_BP:
2826 rm = 3;
2827 break;
2828 } else
2829 switch (b) {
2830 case R_SI:
2831 rm = 4;
2832 break;
2833 case R_DI:
2834 rm = 5;
2835 break;
2836 case R_BP:
2837 rm = 6;
2838 break;
2839 case R_BX:
2840 rm = 7;
2841 break;
2842 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002843 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002844 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002845
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002846 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002847 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002848 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002849 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002850 mod = 1;
2851 else
2852 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002853
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002854 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002855 output->bytes = mod; /* bytes of offset needed */
2856 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 }
2858 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002859 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002860
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002861 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002862 return output->type;
2863
2864err:
2865 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002866}
2867
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002868static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002869{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002870 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002871 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002872
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002873 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002874
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002875 switch (ins->prefixes[PPS_ASIZE]) {
2876 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002877 valid &= 16;
2878 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002879 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002880 valid &= 32;
2881 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002882 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002883 valid &= 64;
2884 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002885 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002886 valid &= (addrbits == 32) ? 16 : 32;
2887 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002888 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002889 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002890 }
2891
2892 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002893 if (is_class(MEMORY, ins->oprs[j].type)) {
2894 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002895
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002896 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002897 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002898 i = 0;
2899 else
2900 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002901
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002902 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002903 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002904 b = 0;
2905 else
2906 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002907
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002908 if (ins->oprs[j].scale == 0)
2909 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002910
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 if (!i && !b) {
2912 int ds = ins->oprs[j].disp_size;
2913 if ((addrbits != 64 && ds > 8) ||
2914 (addrbits == 64 && ds == 16))
2915 valid &= ds;
2916 } else {
2917 if (!(REG16 & ~b))
2918 valid &= 16;
2919 if (!(REG32 & ~b))
2920 valid &= 32;
2921 if (!(REG64 & ~b))
2922 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002923
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002924 if (!(REG16 & ~i))
2925 valid &= 16;
2926 if (!(REG32 & ~i))
2927 valid &= 32;
2928 if (!(REG64 & ~i))
2929 valid &= 64;
2930 }
2931 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002932 }
2933
2934 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002935 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002936 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002937 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002938 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002939 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002940 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002941 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002942 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002943 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002944 }
2945
2946 defdisp = ins->addr_size == 16 ? 16 : 32;
2947
2948 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002949 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2950 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2951 /*
2952 * mem_offs sizes must match the address size; if not,
2953 * strip the MEM_OFFS bit and match only EA instructions
2954 */
2955 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2956 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002957 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002958}