blob: 1748a0ecfb5254687e6f8c46da4d9041d7140b5e [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 Anvin6768eb72002-04-30 20:52:26 +0000310/*
311 * This routine wrappers the real output format's output routine,
312 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800313 * generator at the same time, flatten unnecessary relocations,
314 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000315 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700316static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000317{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000318 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700319 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700320 int asize;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800321 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700322 union {
323 uint8_t b[8];
324 uint64_t q;
325 } xdata;
326 uint64_t size = data->size;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000327
H. Peter Anvina77692b2016-09-20 14:04:33 -0700328 if (!data->size)
329 return; /* Nothing to do */
330
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700331 /*
332 * Convert addresses to RAWDATA if possible
333 * XXX: not all backends want this for global symbols!!!!
334 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700335 switch (data->type) {
336 case OUT_ADDRESS:
337 asize = data->size;
338 nasm_assert(asize <= 8);
339 if (data->tsegment == NO_SEG && data->twrt == NO_SEG) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700340 /* XXX: check for overflow */
341 uint8_t *q = xdata.b;
342
343 WRITEADDR(q, data->toffset, asize);
344 data->data = xdata.b;
345 data->type = OUT_RAWDATA;
346 asize = 0; /* No longer an address */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400347 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700348 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700349
H. Peter Anvina77692b2016-09-20 14:04:33 -0700350 case OUT_RELADDR:
351 asize = data->size;
352 nasm_assert(asize <= 8);
353 if (data->tsegment == data->segment && data->twrt == NO_SEG) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700354 uint8_t *q = xdata.b;
355 int64_t delta = data->toffset - data->offset
356 - (data->inslen - data->insoffs);
357
358 if (overflow_signed(delta, asize))
359 warn_overflow(ERR_PASS2, asize);
360
361 WRITEADDR(q, delta, asize);
362 data->data = xdata.b;
363 data->type = OUT_RAWDATA;
364 asize = 0; /* No longer an address */
365 }
366 break;
367
368 default:
369 asize = 0; /* Not an address */
370 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000371 }
372
H. Peter Anvina77692b2016-09-20 14:04:33 -0700373 lfmt->output(data);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800374
Frank Kotlerabebb082003-09-06 04:45:37 +0000375 /*
376 * this call to src_get determines when we call the
377 * debug-format-specific "linenum" function
378 * it updates lineno and lnfname to the current values
379 * returning 0 if "same as last time", -2 if lnfname
380 * changed, and the amount by which lineno changed,
381 * if it did. thus, these variables must be static
382 */
383
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400384 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700385 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000386
H. Peter Anvinb6412502016-02-11 21:07:40 -0800387 if (asize && asize > amax) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700388 if (data->type != OUT_ADDRESS || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800389 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800390 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800391 asize << 3, ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800392 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800393 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800394 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800395 asize << 3, ofmt->maxbits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700396 data->size = amax;
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700397 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700398 data->insoffs += amax;
399 data->offset += amax;
400 data->size = size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800401 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700402 data->data = zero_buffer;
403 data->type = OUT_RAWDATA;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800404 }
405
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700406 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700407 data->offset += size;
408 data->insoffs += size;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000409}
410
H. Peter Anvina77692b2016-09-20 14:04:33 -0700411static inline void out_rawdata(struct out_data *data, const void *rawdata,
412 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400413{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700414 data->type = OUT_RAWDATA;
415 data->data = rawdata;
416 data->size = size;
417 out(data);
418}
419
420static void out_rawbyte(struct out_data *data, uint8_t byte)
421{
422 data->type = OUT_RAWDATA;
423 data->data = &byte;
424 data->size = 1;
425 out(data);
426}
427
428static inline void out_reserve(struct out_data *data, uint64_t size)
429{
430 data->type = OUT_RESERVE;
431 data->size = size;
432 out(data);
433}
434
H. Peter Anvin164d2462017-02-20 02:39:56 -0800435static inline void out_imm(struct out_data *data, const struct operand *opx,
H. Peter Anvina77692b2016-09-20 14:04:33 -0700436 int size, enum out_sign sign)
437{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800438 data->type =
439 (opx->opflags & OPFLAG_RELATIVE) ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700440 data->sign = sign;
441 data->size = size;
442 data->toffset = opx->offset;
443 data->tsegment = opx->segment;
444 data->twrt = opx->wrt;
445 out(data);
446}
447
H. Peter Anvin164d2462017-02-20 02:39:56 -0800448static void out_reladdr(struct out_data *data, const struct operand *opx,
449 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700450{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800451 if (opx->opflags & OPFLAG_RELATIVE)
452 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
453
H. Peter Anvina77692b2016-09-20 14:04:33 -0700454 data->type = OUT_RELADDR;
455 data->sign = OUT_SIGNED;
456 data->size = size;
457 data->toffset = opx->offset;
458 data->tsegment = opx->segment;
459 data->twrt = opx->wrt;
460 out(data);
461}
462
H. Peter Anvin164d2462017-02-20 02:39:56 -0800463static inline void out_segment(struct out_data *data,
464 const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700465{
466 data->type = OUT_SEGMENT;
467 data->sign = OUT_UNSIGNED;
468 data->size = 2;
469 data->toffset = opx->offset;
470 data->tsegment = ofmt->segbase(opx->segment + 1);
471 data->twrt = opx->wrt;
472 out(data);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400473}
474
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700475static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800476 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000477{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800478 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800479 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000480 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800481 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000482
H. Peter Anvin755f5212012-02-25 11:41:34 -0800483 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700484 return false;
485 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400486 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700487 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400488 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700489
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800490 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100491
Victor van den Elzen154e5922009-02-25 17:32:00 +0100492 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100493 /* Be optimistic in pass 1 */
494 return true;
495
H. Peter Anvine2c80182005-01-15 22:15:51 +0000496 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700497 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000498
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700499 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800500 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
501
502 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
503 /* jmp short (opcode eb) cannot be used with bnd prefix. */
504 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800505 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800506 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800507 }
508
509 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000510}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000511
H. Peter Anvin04445362016-09-21 15:56:19 -0700512/* This is totally just a wild guess what is reasonable... */
513#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
514
H. Peter Anvina77692b2016-09-20 14:04:33 -0700515int64_t assemble(int32_t segment, int64_t start, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800516 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000517{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700518 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000519 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700520 enum match_result m;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000521 int32_t itimes;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300522 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000523
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000524 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000525
H. Peter Anvina77692b2016-09-20 14:04:33 -0700526 data.offset = start;
527 data.segment = segment;
528 data.itemp = NULL;
529 data.sign = OUT_WRAP;
530 data.bits = bits;
531
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300532 wsize = idata_bytes(instruction->opcode);
533 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000535
H. Peter Anvineba20a72002-04-30 20:53:55 +0000536 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000538 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 if (t < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800540 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000541
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400543 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000544 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400545 if (wsize > 8) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800546 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400547 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000548 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700549 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700550 data.insoffs = 0;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700551 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700552 data.inslen = data.size = wsize;
553 data.toffset = e->offset;
554 data.tsegment = e->segment;
555 data.twrt = e->wrt;
556 out(&data);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400557 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700558 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400559 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700560 int align = e->stringlen % wsize;
561 if (align)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 align = wsize - align;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700563
564 data.insoffs = 0;
565 data.inslen = e->stringlen + align;
566
567 out_rawdata(&data, e->stringval, e->stringlen);
568 out_rawdata(&data, zero_buffer, align);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 }
570 }
571 if (t > 0 && t == instruction->times - 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700572 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800573 lfmt->uplevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 }
575 }
576 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800577 lfmt->downlevel(LIST_TIMES);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700578 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700579 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 FILE *fp;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700581 size_t t = instruction->times;
582 off_t base = 0;
583 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700584 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700585 char *buf = NULL;
586 size_t blk = 0; /* Buffered I/O block size */
587 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000588
H. Peter Anvind81a2352016-09-21 14:03:18 -0700589 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400590 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800591 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000592 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700593 goto done;
594 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000595
H. Peter Anvind81a2352016-09-21 14:03:18 -0700596 len = nasm_file_size(fp);
597
598 if (len == (off_t)-1) {
599 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700600 fname);
601 goto close_done;
602 }
603
H. Peter Anvina77692b2016-09-20 14:04:33 -0700604 if (instruction->eops->next) {
605 base = instruction->eops->next->offset;
606 if (base >= len) {
607 len = 0;
608 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 len -= base;
610 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700611 len > (off_t)instruction->eops->next->next->offset)
612 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700615
H. Peter Anvina77692b2016-09-20 14:04:33 -0700616 lfmt->set_offset(data.offset);
617 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000618
H. Peter Anvind81a2352016-09-21 14:03:18 -0700619 if (!len)
620 goto end_incbin;
621
622 /* Try to map file data */
623 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700624 if (!map) {
625 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
626 buf = nasm_malloc(blk);
627 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700628
629 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700630 /*
631 * Consider these irrelevant for INCBIN, since it is fully
632 * possible that these might be (way) bigger than an int
633 * can hold; there is, however, no reason to widen these
634 * types just for INCBIN. data.inslen == 0 signals to the
635 * backend that these fields are meaningless, if at all
636 * needed.
637 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700638 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700639 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700640
H. Peter Anvind81a2352016-09-21 14:03:18 -0700641 if (map) {
642 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700643 } else if ((off_t)m == len) {
644 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700645 } else {
646 off_t l = len;
647
648 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700649 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700650 "`incbin': unable to seek on file `%s'",
651 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700652 goto end_incbin;
653 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700654 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700655 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700656 if (!m || feof(fp)) {
657 /*
658 * This shouldn't happen unless the file
659 * actually changes while we are reading
660 * it.
661 */
662 nasm_error(ERR_NONFATAL,
663 "`incbin': unexpected EOF while"
664 " reading file `%s'", fname);
665 goto end_incbin;
666 }
667 out_rawdata(&data, buf, m);
668 l -= m;
669 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700670 }
671 }
672 end_incbin:
673 lfmt->downlevel(LIST_INCBIN);
674 if (instruction->times > 1) {
675 lfmt->set_offset(data.offset);
676 lfmt->uplevel(LIST_TIMES);
677 lfmt->downlevel(LIST_TIMES);
678 }
679 if (ferror(fp)) {
680 nasm_error(ERR_NONFATAL,
681 "`incbin': error while"
682 " reading file `%s'", fname);
683 }
684 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700685 if (buf)
686 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700687 if (map)
688 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700689 fclose(fp);
690 done:
691 ;
692 } else {
693 /* "Real" instruction */
694
695 /* Check to see if we need an address-size prefix */
696 add_asp(instruction, bits);
697
698 m = find_match(&temp, instruction, data.segment, data.offset, bits);
699
700 if (m == MOK_GOOD) {
701 /* Matches! */
702 int64_t insn_size = calcsize(data.segment, data.offset,
703 bits, instruction, temp);
704 itimes = instruction->times;
705 if (insn_size < 0) /* shouldn't be, on pass two */
706 nasm_panic(0, "errors made it through from pass one");
707
708 data.itemp = temp;
709 data.bits = bits;
710
711 while (itimes--) {
712 data.insoffs = 0;
713 data.inslen = insn_size;
714
715 gencode(&data, instruction);
716 nasm_assert(data.insoffs == insn_size);
717
718 if (itimes > 0 && itimes == instruction->times - 1) {
719 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800720 lfmt->uplevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400721 }
722 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700723 if (instruction->times > 1)
724 lfmt->downlevel(LIST_TIMES);
725 } else {
726 /* No match */
727 switch (m) {
728 case MERR_OPSIZEMISSING:
729 nasm_error(ERR_NONFATAL, "operation size not specified");
730 break;
731 case MERR_OPSIZEMISMATCH:
732 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
733 break;
734 case MERR_BRNUMMISMATCH:
735 nasm_error(ERR_NONFATAL,
736 "mismatch in the number of broadcasting elements");
737 break;
738 case MERR_BADCPU:
739 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
740 break;
741 case MERR_BADMODE:
742 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
743 bits);
744 break;
745 case MERR_ENCMISMATCH:
746 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
747 break;
748 case MERR_BADBND:
749 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
750 break;
751 case MERR_BADREPNE:
752 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
753 (has_prefix(instruction, PPS_REP, P_REPNE) ?
754 "repne" : "repnz"));
755 break;
756 default:
757 nasm_error(ERR_NONFATAL,
758 "invalid combination of opcode and operands");
759 break;
760 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400761 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000762 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700763 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000764}
765
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400766int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800767 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000768{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000769 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700770 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000771
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000772 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000773
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400774 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000775 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000776
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700777 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
778 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400779 instruction->opcode == I_DT || instruction->opcode == I_DO ||
780 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300782 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000783
H. Peter Anvine2c80182005-01-15 22:15:51 +0000784 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300785 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000786
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400787 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000788 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000789
H. Peter Anvine2c80182005-01-15 22:15:51 +0000790 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400791 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400793 warn_overflow_const(e->offset, wsize);
794 } else if (e->type == EOT_DB_STRING ||
795 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000797
H. Peter Anvine2c80182005-01-15 22:15:51 +0000798 align = (-osize) % wsize;
799 if (align < 0)
800 align += wsize;
801 isize += osize + align;
802 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700803 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000804 }
805
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400807 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700808 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000809
H. Peter Anvind81a2352016-09-21 14:03:18 -0700810 len = nasm_file_size_by_path(fname);
811 if (len == (off_t)-1) {
812 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
813 fname);
814 return 0;
815 }
816
817 if (instruction->eops->next) {
818 if (len <= (off_t)instruction->eops->next->offset) {
819 len = 0;
820 } else {
821 len -= instruction->eops->next->offset;
822 if (instruction->eops->next->next &&
823 len > (off_t)instruction->eops->next->next->offset) {
824 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 }
826 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700828
829 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000830 }
831
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700832 /* Check to see if we need an address-size prefix */
833 add_asp(instruction, bits);
834
H. Peter Anvin23595f52009-07-25 17:44:25 -0700835 m = find_match(&temp, instruction, segment, offset, bits);
836 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400837 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700838 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700839 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400840 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000841 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000842}
843
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800844static void bad_hle_warn(const insn * ins, uint8_t hleok)
845{
846 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800847 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800848 static const enum whatwarn warn[2][4] =
849 {
850 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
851 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
852 };
853 unsigned int n;
854
855 n = (unsigned int)rep_pfx - P_XACQUIRE;
856 if (n > 1)
857 return; /* Not XACQUIRE/XRELEASE */
858
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800859 ww = warn[n][hleok];
860 if (!is_class(MEMORY, ins->oprs[0].type))
861 ww = w_inval; /* HLE requires operand 0 to be memory */
862
863 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800864 case w_none:
865 break;
866
867 case w_lock:
868 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800869 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800870 "%s with this instruction requires lock",
871 prefix_name(rep_pfx));
872 }
873 break;
874
875 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800876 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800877 "%s invalid with this instruction",
878 prefix_name(rep_pfx));
879 break;
880 }
881}
882
H. Peter Anvin507ae032008-10-09 15:37:10 -0700883/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400884#define case3(x) case (x): case (x)+1: case (x)+2
885#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700886
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800887static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800888 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000889{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800890 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800891 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000892 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000893 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700894 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700895 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700896 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700897 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800898 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800899 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700900 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000901
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700902 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700903 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700904 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700905
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700906 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400907 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700908
H. Peter Anvine2c80182005-01-15 22:15:51 +0000909 (void)segment; /* Don't warn that this parameter is unused */
910 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000911
H. Peter Anvin839eca22007-10-29 23:12:47 -0700912 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400913 c = *codes++;
914 op1 = (c & 3) + ((opex & 1) << 2);
915 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
916 opx = &ins->oprs[op1];
917 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700918
H. Peter Anvin839eca22007-10-29 23:12:47 -0700919 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400920 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000921 codes += c, length += c;
922 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700923
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400924 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400925 opex = c;
926 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700927
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400928 case4(010):
929 ins->rex |=
930 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 codes++, length++;
932 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700933
Jin Kyu Song164d6072013-10-15 19:10:13 -0700934 case4(014):
935 /* this is an index reg of MIB operand */
936 mib_index = opx->basereg;
937 break;
938
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400939 case4(020):
940 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000941 length++;
942 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700943
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400944 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 length += 2;
946 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700947
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400948 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700949 if (opx->type & (BITS16 | BITS32 | BITS64))
950 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 else
952 length += (bits == 16) ? 2 : 4;
953 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700954
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400955 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000956 length += 4;
957 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700958
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400959 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700960 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700962
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400963 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 length++;
965 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700966
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400967 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000968 length += 8; /* MOV reg64/imm */
969 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700970
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400971 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 length += 2;
973 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700974
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400975 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700976 if (opx->type & (BITS16 | BITS32 | BITS64))
977 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 else
979 length += (bits == 16) ? 2 : 4;
980 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700981
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400982 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 length += 4;
984 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700985
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400986 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700987 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000988 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700989
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400990 case 0172:
991 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400992 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700993 length++;
994 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700995
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700996 case4(0174):
997 length++;
998 break;
999
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001000 case4(0240):
1001 ins->rex |= REX_EV;
1002 ins->vexreg = regval(opx);
1003 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1004 ins->vex_cm = *codes++;
1005 ins->vex_wlp = *codes++;
1006 ins->evex_tuple = (*codes++ - 0300);
1007 break;
1008
1009 case 0250:
1010 ins->rex |= REX_EV;
1011 ins->vexreg = 0;
1012 ins->vex_cm = *codes++;
1013 ins->vex_wlp = *codes++;
1014 ins->evex_tuple = (*codes++ - 0300);
1015 break;
1016
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001017 case4(0254):
1018 length += 4;
1019 break;
1020
1021 case4(0260):
1022 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001023 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001024 ins->vex_cm = *codes++;
1025 ins->vex_wlp = *codes++;
1026 break;
1027
1028 case 0270:
1029 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001030 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001031 ins->vex_cm = *codes++;
1032 ins->vex_wlp = *codes++;
1033 break;
1034
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001035 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001036 hleok = c & 3;
1037 break;
1038
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001039 case4(0274):
1040 length++;
1041 break;
1042
1043 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001044 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001045
H. Peter Anvine2c80182005-01-15 22:15:51 +00001046 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001047 if (bits == 64)
1048 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001049 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001051
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001053 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001054 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001055
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001057 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001058
Keith Kaniosb7a89542007-04-12 02:40:54 +00001059 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001060 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1061 has_prefix(ins, PPS_ASIZE, P_A32))
1062 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001064
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001065 case4(0314):
1066 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001067
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001069 {
1070 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1071 if (pfx == P_O16)
1072 break;
1073 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001074 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001075 else
1076 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001078 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001079
H. Peter Anvine2c80182005-01-15 22:15:51 +00001080 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001081 {
1082 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1083 if (pfx == P_O32)
1084 break;
1085 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001086 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001087 else
1088 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001090 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001091
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 case 0322:
1093 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001094
Keith Kaniosb7a89542007-04-12 02:40:54 +00001095 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001096 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001097 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001098
Keith Kaniosb7a89542007-04-12 02:40:54 +00001099 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001100 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001101 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001102
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001103 case 0325:
1104 ins->rex |= REX_NH;
1105 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001106
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001107 case 0326:
1108 break;
1109
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 case 0330:
1111 codes++, length++;
1112 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001113
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001115 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001116
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001117 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 case 0333:
1119 length++;
1120 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001121
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001122 case 0334:
1123 ins->rex |= REX_L;
1124 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001125
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001126 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001127 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001128
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001129 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001130 if (!ins->prefixes[PPS_REP])
1131 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001132 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001133
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001134 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001135 if (!ins->prefixes[PPS_REP])
1136 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001137 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001138
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001140 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001141 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001143 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1144 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001145 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001146 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001147 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001148 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001149
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001150 case 0341:
1151 if (!ins->prefixes[PPS_WAIT])
1152 ins->prefixes[PPS_WAIT] = P_WAIT;
1153 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001154
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001155 case 0360:
1156 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001157
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001158 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001159 length++;
1160 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001161
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001162 case 0364:
1163 case 0365:
1164 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001165
Keith Kanios48af1772007-08-17 07:37:52 +00001166 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001167 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001168 length++;
1169 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001170
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001171 case 0370:
1172 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001173 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001174
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175 case 0373:
1176 length++;
1177 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001178
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001179 case 0374:
1180 eat = EA_XMMVSIB;
1181 break;
1182
1183 case 0375:
1184 eat = EA_YMMVSIB;
1185 break;
1186
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001187 case 0376:
1188 eat = EA_ZMMVSIB;
1189 break;
1190
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001191 case4(0100):
1192 case4(0110):
1193 case4(0120):
1194 case4(0130):
1195 case4(0200):
1196 case4(0204):
1197 case4(0210):
1198 case4(0214):
1199 case4(0220):
1200 case4(0224):
1201 case4(0230):
1202 case4(0234):
1203 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001205 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001206 opflags_t rflags;
1207 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001208 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001209
Keith Kaniosb7a89542007-04-12 02:40:54 +00001210 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001211
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001212 if (c <= 0177) {
1213 /* pick rfield from operand b (opx) */
1214 rflags = regflag(opx);
1215 rfield = nasm_regvals[opx->basereg];
1216 } else {
1217 rflags = 0;
1218 rfield = c & 7;
1219 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001220
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001221 /* EVEX.b1 : evex_brerop contains the operand position */
1222 op_er_sae = (ins->evex_brerop >= 0 ?
1223 &ins->oprs[ins->evex_brerop] : NULL);
1224
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001225 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1226 /* set EVEX.b */
1227 ins->evex_p[2] |= EVEX_P2B;
1228 if (op_er_sae->decoflags & ER) {
1229 /* set EVEX.RC (rounding control) */
1230 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1231 & EVEX_P2RC;
1232 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001233 } else {
1234 /* set EVEX.L'L (vector length) */
1235 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001236 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001237 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001238 /* set EVEX.b */
1239 ins->evex_p[2] |= EVEX_P2B;
1240 }
1241 }
1242
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001243 if (itemp_has(temp, IF_MIB)) {
1244 opy->eaflags |= EAF_MIB;
1245 /*
1246 * if a separate form of MIB (ICC style) is used,
1247 * the index reg info is merged into mem operand
1248 */
1249 if (mib_index != R_none) {
1250 opy->indexreg = mib_index;
1251 opy->scale = 1;
1252 opy->hintbase = mib_index;
1253 opy->hinttype = EAH_NOTBASE;
1254 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001255 }
1256
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001257 if (process_ea(opy, &ea_data, bits,
1258 rfield, rflags, ins) != eat) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001259 nasm_error(ERR_NONFATAL, "invalid effective address");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001260 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001261 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001262 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001263 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001264 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001265 }
1266 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001267
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001268 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001269 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001270 ": instruction code \\%o (0x%02X) given", c, c);
1271 break;
1272 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001273 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001274
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001275 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001276
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001277 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001278 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001279 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001280 return -1;
1281 }
1282 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001283 }
1284
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001285 switch (ins->prefixes[PPS_VEX]) {
1286 case P_EVEX:
1287 if (!(ins->rex & REX_EV))
1288 return -1;
1289 break;
1290 case P_VEX3:
1291 case P_VEX2:
1292 if (!(ins->rex & REX_V))
1293 return -1;
1294 break;
1295 default:
1296 break;
1297 }
1298
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001299 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001300 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001301
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001302 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001303 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001304 return -1;
1305 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001306 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001307 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001308 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001309 ins->rex &= ~REX_W;
1310 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001311 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001312 ins->rex |= REX_W;
1313 bad32 &= ~REX_W;
1314 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001315 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001316 /* Follow REX_W */
1317 break;
1318 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001319
H. Peter Anvinfc561202011-07-07 16:58:22 -07001320 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001321 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001322 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001323 } else if (!(ins->rex & REX_EV) &&
1324 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001325 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001326 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001327 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001328 if (ins->rex & REX_EV)
1329 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001330 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1331 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001332 length += 3;
1333 else
1334 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001335 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001336 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001337 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001338 return -1;
1339 } else if (bits == 64) {
1340 length++;
1341 } else if ((ins->rex & REX_L) &&
1342 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001343 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001344 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001345 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001346 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001347 length++;
1348 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001349 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001350 return -1;
1351 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001352 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001353
1354 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001355 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001356 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001357 "instruction is not lockable");
1358 }
1359
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001360 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001361
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001362 /*
1363 * when BND prefix is set by DEFAULT directive,
1364 * BND prefix is added to every appropriate instruction line
1365 * unless it is overridden by NOBND prefix.
1366 */
1367 if (globalbnd &&
1368 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1369 ins->prefixes[PPS_REP] = P_BND;
1370
H. Peter Anvina77692b2016-09-20 14:04:33 -07001371 /*
1372 * Add length of legacy prefixes
1373 */
1374 length += emit_prefix(NULL, bits, ins);
1375
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001376 return length;
1377}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001378
H. Peter Anvina77692b2016-09-20 14:04:33 -07001379static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001380{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001381 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001382 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001383 !(ins->rex & (REX_V | REX_EV)) &&
1384 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001385 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1386 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001387 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001388 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001389 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001390}
1391
H. Peter Anvina77692b2016-09-20 14:04:33 -07001392static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1393{
1394 int bytes = 0;
1395 int j;
1396
1397 for (j = 0; j < MAXPREFIX; j++) {
1398 uint8_t c = 0;
1399 switch (ins->prefixes[j]) {
1400 case P_WAIT:
1401 c = 0x9B;
1402 break;
1403 case P_LOCK:
1404 c = 0xF0;
1405 break;
1406 case P_REPNE:
1407 case P_REPNZ:
1408 case P_XACQUIRE:
1409 case P_BND:
1410 c = 0xF2;
1411 break;
1412 case P_REPE:
1413 case P_REPZ:
1414 case P_REP:
1415 case P_XRELEASE:
1416 c = 0xF3;
1417 break;
1418 case R_CS:
1419 if (bits == 64) {
1420 nasm_error(ERR_WARNING | ERR_PASS2,
1421 "cs segment base generated, but will be ignored in 64-bit mode");
1422 }
1423 c = 0x2E;
1424 break;
1425 case R_DS:
1426 if (bits == 64) {
1427 nasm_error(ERR_WARNING | ERR_PASS2,
1428 "ds segment base generated, but will be ignored in 64-bit mode");
1429 }
1430 c = 0x3E;
1431 break;
1432 case R_ES:
1433 if (bits == 64) {
1434 nasm_error(ERR_WARNING | ERR_PASS2,
1435 "es segment base generated, but will be ignored in 64-bit mode");
1436 }
1437 c = 0x26;
1438 break;
1439 case R_FS:
1440 c = 0x64;
1441 break;
1442 case R_GS:
1443 c = 0x65;
1444 break;
1445 case R_SS:
1446 if (bits == 64) {
1447 nasm_error(ERR_WARNING | ERR_PASS2,
1448 "ss segment base generated, but will be ignored in 64-bit mode");
1449 }
1450 c = 0x36;
1451 break;
1452 case R_SEGR6:
1453 case R_SEGR7:
1454 nasm_error(ERR_NONFATAL,
1455 "segr6 and segr7 cannot be used as prefixes");
1456 break;
1457 case P_A16:
1458 if (bits == 64) {
1459 nasm_error(ERR_NONFATAL,
1460 "16-bit addressing is not supported "
1461 "in 64-bit mode");
1462 } else if (bits != 16)
1463 c = 0x67;
1464 break;
1465 case P_A32:
1466 if (bits != 32)
1467 c = 0x67;
1468 break;
1469 case P_A64:
1470 if (bits != 64) {
1471 nasm_error(ERR_NONFATAL,
1472 "64-bit addressing is only supported "
1473 "in 64-bit mode");
1474 }
1475 break;
1476 case P_ASP:
1477 c = 0x67;
1478 break;
1479 case P_O16:
1480 if (bits != 16)
1481 c = 0x66;
1482 break;
1483 case P_O32:
1484 if (bits == 16)
1485 c = 0x66;
1486 break;
1487 case P_O64:
1488 /* REX.W */
1489 break;
1490 case P_OSP:
1491 c = 0x66;
1492 break;
1493 case P_EVEX:
1494 case P_VEX3:
1495 case P_VEX2:
1496 case P_NOBND:
1497 case P_none:
1498 break;
1499 default:
1500 nasm_panic(0, "invalid instruction prefix");
1501 }
1502 if (c) {
1503 if (data)
1504 out_rawbyte(data, c);
1505 bytes++;
1506 }
1507 }
1508 return bytes;
1509}
1510
1511static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001512{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001513 uint8_t c;
1514 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001515 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001516 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001517 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001518 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001519 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001520 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001521 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001522 const int bits = data->bits;
H. Peter Anvin70653092007-10-19 14:42:29 -07001523
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001524 ins->rex_done = false;
1525
H. Peter Anvina77692b2016-09-20 14:04:33 -07001526 emit_prefix(data, bits, ins);
1527
H. Peter Anvin839eca22007-10-29 23:12:47 -07001528 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001529 c = *codes++;
1530 op1 = (c & 3) + ((opex & 1) << 2);
1531 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1532 opx = &ins->oprs[op1];
1533 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001534
H. Peter Anvina77692b2016-09-20 14:04:33 -07001535
H. Peter Anvin839eca22007-10-29 23:12:47 -07001536 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001537 case 01:
1538 case 02:
1539 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001540 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001541 emit_rex(data, ins);
1542 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001543 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001545
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001546 case 05:
1547 case 06:
1548 case 07:
1549 opex = c;
1550 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001551
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001552 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001553 emit_rex(data, ins);
1554 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001555 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001556
Jin Kyu Song164d6072013-10-15 19:10:13 -07001557 case4(014):
1558 break;
1559
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001560 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001561 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001562 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001563 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001564 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001566
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001567 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001568 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001569 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001570 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001571 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001573
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001574 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001575 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001576 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001578
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001579 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001580 if (opx->type & (BITS16 | BITS32))
1581 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582 else
1583 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001584 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001585 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001587
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001588 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001589 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001590 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001592
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001594 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001595 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001596 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001597 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001598
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001599 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001600 if (opx->segment == data->segment) {
1601 int64_t delta = opx->offset - data->offset
1602 - (data->inslen - data->insoffs);
1603 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001604 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001605 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001606 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001608
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001609 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001610 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001611 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001612
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001613 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001614 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001616
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001617 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001618 if (opx->type & (BITS16 | BITS32 | BITS64))
1619 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 else
1621 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001622
1623 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001625
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001626 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001627 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001629
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001630 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001631 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001632 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001633 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001634 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001635 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001636
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001637 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001638 {
1639 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1640 const struct operand *opy;
1641
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001642 c = *codes++;
1643 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001644 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001645 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001646 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001647 "non-absolute expression not permitted as argument %d",
1648 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001649 } else if (opy->offset & ~mask) {
1650 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1651 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001653 c = opy->offset & mask;
1654 goto emit_is4;
1655 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001656
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001657 case 0173:
1658 c = *codes++;
1659 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001660 c &= 15;
1661 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001662
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001663 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001664 c = 0;
1665 emit_is4:
1666 r = nasm_regvals[opx->basereg];
1667 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001668 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001669
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001670 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001671 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001672 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001673 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001674 "signed dword immediate exceeds bounds");
1675 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001676 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001677 break;
1678
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001679 case4(0240):
1680 case 0250:
1681 codes += 3;
1682 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1683 EVEX_P2Z | EVEX_P2AAA, 2);
1684 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1685 bytes[0] = 0x62;
1686 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001687 bytes[1] = ((((ins->rex & 7) << 5) |
1688 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001689 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001690 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1691 ((~ins->vexreg & 15) << 3) |
1692 (1 << 2) | (ins->vex_wlp & 3);
1693 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001694 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001695 break;
1696
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001697 case4(0260):
1698 case 0270:
1699 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001700 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1701 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001702 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1703 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1704 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001705 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001706 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001707 } else {
1708 bytes[0] = 0xc5;
1709 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001710 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001711 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001712 }
1713 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001714
H. Peter Anvine014f352012-02-25 22:35:19 -08001715 case 0271:
1716 case 0272:
1717 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001718 break;
1719
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001720 case4(0274):
1721 {
1722 uint64_t uv, um;
1723 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001724
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001725 if (ins->rex & REX_W)
1726 s = 64;
1727 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1728 s = 16;
1729 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1730 s = 32;
1731 else
1732 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001733
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001734 um = (uint64_t)2 << (s-1);
1735 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001736
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001737 if (uv > 127 && uv < (uint64_t)-128 &&
1738 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001739 /* If this wasn't explicitly byte-sized, warn as though we
1740 * had fallen through to the imm16/32/64 case.
1741 */
H. Peter Anvin215186f2016-02-17 20:27:41 -08001742 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001743 "%s value exceeds bounds",
1744 (opx->type & BITS8) ? "signed byte" :
1745 s == 16 ? "word" :
1746 s == 32 ? "dword" :
1747 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001748 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001749 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001750 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001751 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001752
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001753 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001755
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001757 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1758 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001759 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001760
H. Peter Anvine2c80182005-01-15 22:15:51 +00001761 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001762 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1763 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001765
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 case 0312:
1767 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001768
Keith Kaniosb7a89542007-04-12 02:40:54 +00001769 case 0313:
1770 ins->rex = 0;
1771 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001772
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001773 case4(0314):
1774 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001775
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001777 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001779
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001781 case 0323:
1782 break;
1783
Keith Kaniosb7a89542007-04-12 02:40:54 +00001784 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001785 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001787
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001788 case 0325:
1789 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001790
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001791 case 0326:
1792 break;
1793
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001795 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001797
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001800
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001801 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001803 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001805
Keith Kanios48af1772007-08-17 07:37:52 +00001806 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001807 if (ins->rex & REX_R)
1808 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001809 ins->rex &= ~(REX_L|REX_R);
1810 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001811
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001812 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001813 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001814
H. Peter Anvin962e3052008-08-28 17:47:16 -07001815 case 0336:
1816 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001817 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001818
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001821 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001822
1823 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001825
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001826 case 0341:
1827 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001828
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001829 case 0360:
1830 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001831
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001832 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001833 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001834 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001835
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001836 case 0364:
1837 case 0365:
1838 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001839
Keith Kanios48af1772007-08-17 07:37:52 +00001840 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001841 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001842 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001843 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001844
Jin Kyu Song03041092013-10-15 19:38:51 -07001845 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001846 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001847
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001849 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001850 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001851
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001852 case 0374:
1853 eat = EA_XMMVSIB;
1854 break;
1855
1856 case 0375:
1857 eat = EA_YMMVSIB;
1858 break;
1859
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001860 case 0376:
1861 eat = EA_ZMMVSIB;
1862 break;
1863
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001864 case4(0100):
1865 case4(0110):
1866 case4(0120):
1867 case4(0130):
1868 case4(0200):
1869 case4(0204):
1870 case4(0210):
1871 case4(0214):
1872 case4(0220):
1873 case4(0224):
1874 case4(0230):
1875 case4(0234):
1876 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 ea ea_data;
1878 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001879 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001880 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001881 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001882
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001883 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001884 /* pick rfield from operand b (opx) */
1885 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001886 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001887 } else {
1888 /* rfield is constant */
1889 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001890 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001891 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001892
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001893 if (process_ea(opy, &ea_data, bits,
1894 rfield, rflags, ins) != eat)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001895 nasm_error(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001896
H. Peter Anvine2c80182005-01-15 22:15:51 +00001897 p = bytes;
1898 *p++ = ea_data.modrm;
1899 if (ea_data.sib_present)
1900 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001901 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001902
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001903 /*
1904 * Make sure the address gets the right offset in case
1905 * the line breaks in the .lst file (BR 1197827)
1906 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001907
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001908 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001909 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001910 if (ea_data.disp8) {
1911 out_rawbyte(data, ea_data.disp8);
1912 } else if (ea_data.rip) {
1913 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001914 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001915 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001916
H. Peter Anvina77692b2016-09-20 14:04:33 -07001917 if (overflow_general(opy->offset, asize) ||
1918 signed_bits(opy->offset, ins->addr_size) !=
1919 signed_bits(opy->offset, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001920 warn_overflow(ERR_PASS2, ea_data.bytes);
1921
H. Peter Anvina77692b2016-09-20 14:04:33 -07001922 out_imm(data, opy, ea_data.bytes,
1923 (asize > ea_data.bytes) ? OUT_SIGNED : OUT_UNSIGNED);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001924 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001926 }
1927 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001928
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001929 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001930 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001931 ": instruction code \\%o (0x%02X) given", c, c);
1932 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001934 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001935}
1936
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001937static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001938{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001939 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001940 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001941 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001942}
1943
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001944static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001945{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001946 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001947 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001948 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001949}
1950
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001951static int op_rexflags(const operand * o, int mask)
1952{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001953 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001954 int val;
1955
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001956 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001957 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001958
H. Peter Anvina4835d42008-05-20 14:21:29 -07001959 flags = nasm_reg_flags[o->basereg];
1960 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001961
1962 return rexflags(val, flags, mask);
1963}
1964
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001965static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001966{
1967 int rex = 0;
1968
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001969 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001970 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001971 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001972 rex |= REX_W;
1973 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1974 rex |= REX_H;
1975 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1976 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001977
1978 return rex & mask;
1979}
1980
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001981static int evexflags(int val, decoflags_t deco,
1982 int mask, uint8_t byte)
1983{
1984 int evex = 0;
1985
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001986 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001987 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001988 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001989 evex |= (EVEX_P0RP | EVEX_P0X);
1990 break;
1991 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001992 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001993 evex |= EVEX_P2VP;
1994 if (deco & Z)
1995 evex |= EVEX_P2Z;
1996 if (deco & OPMASK_MASK)
1997 evex |= deco & EVEX_P2AAA;
1998 break;
1999 }
2000 return evex & mask;
2001}
2002
2003static int op_evexflags(const operand * o, int mask, uint8_t byte)
2004{
2005 int val;
2006
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002007 val = nasm_regvals[o->basereg];
2008
2009 return evexflags(val, o->decoflags, mask, byte);
2010}
2011
H. Peter Anvin23595f52009-07-25 17:44:25 -07002012static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002013 insn *instruction,
2014 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002015{
2016 const struct itemplate *temp;
2017 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002018 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002019 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002020 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002021 int i;
2022
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002023 /* broadcasting uses a different data element size */
2024 for (i = 0; i < instruction->operands; i++)
2025 if (i == broadcast)
2026 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2027 else
2028 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002029
2030 merr = MERR_INVALOP;
2031
2032 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002033 temp->opcode != I_none; temp++) {
2034 m = matches(temp, instruction, bits);
2035 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002036 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002037 m = MOK_GOOD;
2038 else
2039 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002040 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002041 /*
2042 * Missing operand size and a candidate for fuzzy matching...
2043 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002044 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002045 if (i == broadcast)
2046 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2047 else
2048 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002049 opsizemissing = true;
2050 }
2051 if (m > merr)
2052 merr = m;
2053 if (merr == MOK_GOOD)
2054 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002055 }
2056
2057 /* No match, but see if we can get a fuzzy operand size match... */
2058 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002059 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002060
2061 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002062 /*
2063 * We ignore extrinsic operand sizes on registers, so we should
2064 * never try to fuzzy-match on them. This also resolves the case
2065 * when we have e.g. "xmmrm128" in two different positions.
2066 */
2067 if (is_class(REGISTER, instruction->oprs[i].type))
2068 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002069
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002070 /* This tests if xsizeflags[i] has more than one bit set */
2071 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2072 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002073
Jin Kyu Song7903c072013-10-30 03:00:12 -07002074 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002075 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002076 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2077 BITS32 : BITS64);
2078 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002079 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002080 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002081 }
2082
2083 /* Try matching again... */
2084 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002085 temp->opcode != I_none; temp++) {
2086 m = matches(temp, instruction, bits);
2087 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002088 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002089 m = MOK_GOOD;
2090 else
2091 m = MERR_INVALOP;
2092 }
2093 if (m > merr)
2094 merr = m;
2095 if (merr == MOK_GOOD)
2096 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002097 }
2098
H. Peter Anvina81655b2009-07-25 18:15:28 -07002099done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002100 *tempp = temp;
2101 return merr;
2102}
2103
Mark Charneydcaef4b2014-10-09 13:45:17 -04002104static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2105{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002106 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002107 uint8_t brcast_num;
2108
Mark Charneydcaef4b2014-10-09 13:45:17 -04002109 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002110 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002111 "size of broadcasting element is greater than 64 bits");
2112
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002113 /*
2114 * The shift term is to take care of the extra BITS80 inserted
2115 * between BITS64 and BITS128.
2116 */
2117 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2118 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002119
2120 return brcast_num;
2121}
2122
H. Peter Anvin65289e82009-07-25 17:25:11 -07002123static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002124 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002125{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002126 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002127 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002128 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002129
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002130 /*
2131 * Check the opcode
2132 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002133 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002134 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002135
2136 /*
2137 * Count the operands
2138 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002139 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002140 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002141
2142 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002143 * Is it legal?
2144 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002145 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002146 return MERR_INVALOP;
2147
2148 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002149 * {evex} available?
2150 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002151 switch (instruction->prefixes[PPS_VEX]) {
2152 case P_EVEX:
2153 if (!itemp_has(itemp, IF_EVEX))
2154 return MERR_ENCMISMATCH;
2155 break;
2156 case P_VEX3:
2157 case P_VEX2:
2158 if (!itemp_has(itemp, IF_VEX))
2159 return MERR_ENCMISMATCH;
2160 break;
2161 default:
2162 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002163 }
2164
2165 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002166 * Check that no spurious colons or TOs are present
2167 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 for (i = 0; i < itemp->operands; i++)
2169 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002170 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002171
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002172 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002173 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002174 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002175 switch (itemp_smask(itemp)) {
2176 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002177 asize = BITS8;
2178 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002179 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002180 asize = BITS16;
2181 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002182 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002183 asize = BITS32;
2184 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002185 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002186 asize = BITS64;
2187 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002188 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002189 asize = BITS128;
2190 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002191 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002192 asize = BITS256;
2193 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002194 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002195 asize = BITS512;
2196 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002197 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002198 switch (bits) {
2199 case 16:
2200 asize = BITS16;
2201 break;
2202 case 32:
2203 asize = BITS32;
2204 break;
2205 case 64:
2206 asize = BITS64;
2207 break;
2208 default:
2209 asize = 0;
2210 break;
2211 }
2212 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002213 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002214 asize = 0;
2215 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002216 }
2217
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002218 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002219 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002220 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002221 memset(size, 0, sizeof size);
2222 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002223 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002224 /* S- flags apply to all operands */
2225 for (i = 0; i < MAX_OPERANDS; i++)
2226 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002227 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002228
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002229 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002230 * Check that the operand flags all match up,
2231 * it's a bit tricky so lets be verbose:
2232 *
2233 * 1) Find out the size of operand. If instruction
2234 * doesn't have one specified -- we're trying to
2235 * guess it either from template (IF_S* flag) or
2236 * from code bits.
2237 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002238 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002239 * template has an operand size specified AND this size differ
2240 * from which instruction has (perhaps we got it from code bits)
2241 * we are:
2242 * a) Check that only size of instruction and operand is differ
2243 * other characteristics do match
2244 * b) Perhaps it's a register specified in instruction so
2245 * for such a case we just mark that operand as "size
2246 * missing" and this will turn on fuzzy operand size
2247 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002248 */
2249 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002250 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002251 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002252 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002253 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002254 opflags_t template_opsize, insn_opsize;
2255
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002256 if (!(type & SIZE_MASK))
2257 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002258
Jin Kyu Song7903c072013-10-30 03:00:12 -07002259 insn_opsize = type & SIZE_MASK;
2260 if (!is_broadcast) {
2261 template_opsize = itemp->opd[i] & SIZE_MASK;
2262 } else {
2263 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2264 /*
2265 * when broadcasting, the element size depends on
2266 * the instruction type. decorator flag should match.
2267 */
2268
2269 if (deco_brsize) {
2270 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002271 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002272 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002273 } else {
2274 template_opsize = 0;
2275 }
2276 }
2277
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002278 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002279 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002280 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002281 } else if (template_opsize) {
2282 if (template_opsize != insn_opsize) {
2283 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002284 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002285 } else if (!is_class(REGISTER, type)) {
2286 /*
2287 * Note: we don't honor extrinsic operand sizes for registers,
2288 * so "missing operand size" for a register should be
2289 * considered a wildcard match rather than an error.
2290 */
2291 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002292 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002293 } else if (is_broadcast &&
2294 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002295 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002296 /*
2297 * broadcasting opsize matches but the number of repeated memory
2298 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002299 * if 64b double precision float is broadcasted to ymm (256b),
2300 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002301 */
2302 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002303 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002304 }
2305 }
2306
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002307 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002308 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002309
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002310 /*
2311 * Check operand sizes
2312 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002313 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2314 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002315 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002316 asize = itemp->opd[i] & SIZE_MASK;
2317 if (asize) {
2318 for (i = 0; i < oprs; i++)
2319 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002320 break;
2321 }
2322 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002323 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002324 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002325 }
2326
Keith Kaniosb7a89542007-04-12 02:40:54 +00002327 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002328 if (!(itemp->opd[i] & SIZE_MASK) &&
2329 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002330 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002331 }
2332
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002333 /*
2334 * Check template is okay at the set cpu level
2335 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002336 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002337 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002338
Keith Kaniosb7a89542007-04-12 02:40:54 +00002339 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002340 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002341 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002342 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002343 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002345 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002346 * If we have a HLE prefix, look for the NOHLE flag
2347 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002348 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002349 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2350 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2351 return MERR_BADHLE;
2352
2353 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002354 * Check if special handling needed for Jumps
2355 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002356 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002357 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358
Jin Kyu Song03041092013-10-15 19:38:51 -07002359 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002360 * Check if BND prefix is allowed.
2361 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002362 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002363 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002364 (has_prefix(instruction, PPS_REP, P_BND) ||
2365 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002366 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002367 else if (itemp_has(itemp, IF_BND) &&
2368 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2369 has_prefix(instruction, PPS_REP, P_REPNZ)))
2370 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002371
H. Peter Anvin60926242009-07-26 16:25:38 -07002372 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002373}
2374
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002375/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002376 * Check if ModR/M.mod should/can be 01.
2377 * - EAF_BYTEOFFS is set
2378 * - offset can fit in a byte when EVEX is not used
2379 * - offset can be compressed when EVEX is used
2380 */
2381#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2382 (o >= -128 && o <= 127 && \
2383 seg == NO_SEG && !forw_ref && \
2384 !(input->eaflags & EAF_WORDOFFS) && \
2385 !(ins->rex & REX_EV)) || \
2386 (ins->rex & REX_EV && \
2387 is_disp8n(input, ins, &output->disp8)))
2388
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002389static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002390 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002391{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002392 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002393 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002394 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002395
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002396 output->type = EA_SCALAR;
2397 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002398 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002399
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002400 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002401 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002402 /* EVEX.R' flag for the REG operand */
2403 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002404
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002405 if (is_class(REGISTER, input->type)) {
2406 /*
2407 * It's a direct register.
2408 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002409 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002410 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002411
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002412 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002413 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002414
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002415 /* broadcasting is not available with a direct register operand. */
2416 if (input->decoflags & BRDCAST_MASK) {
2417 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2418 goto err;
2419 }
2420
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002421 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002422 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002423 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002424 output->bytes = 0; /* no offset necessary either */
2425 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2426 } else {
2427 /*
2428 * It's a memory reference.
2429 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002430
2431 /* Embedded rounding or SAE is not available with a mem ref operand. */
2432 if (input->decoflags & (ER | SAE)) {
2433 nasm_error(ERR_NONFATAL,
2434 "Embedded rounding is available only with reg-reg op.");
2435 return -1;
2436 }
2437
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002438 if (input->basereg == -1 &&
2439 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002440 /*
2441 * It's a pure offset.
2442 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002443 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
2444 if (input->segment == NO_SEG || (input->opflags & OPFLAG_RELATIVE)) {
2445 nasm_error(ERR_WARNING | ERR_PASS2, "absolute address can not be RIP-relative");
2446 input->type &= ~IP_REL;
2447 input->type |= MEMORY;
2448 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002449 }
2450
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002451 if (bits == 64 &&
2452 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2453 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2454 return -1;
2455 }
2456
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002457 if (eaflags & EAF_BYTEOFFS ||
2458 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002459 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2460 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2461 }
2462
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002463 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002464 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002465 output->sib = GEN_SIB(0, 4, 5);
2466 output->bytes = 4;
2467 output->modrm = GEN_MODRM(0, rfield, 4);
2468 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002469 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002470 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002471 output->bytes = (addrbits != 16 ? 4 : 2);
2472 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2473 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002474 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002475 } else {
2476 /*
2477 * It's an indirection.
2478 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002479 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002480 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002481 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002482 int t, it, bt; /* register numbers */
2483 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002484
H. Peter Anvine2c80182005-01-15 22:15:51 +00002485 if (s == 0)
2486 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002487
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002488 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002489 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002490 ix = nasm_reg_flags[i];
2491 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002492 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002493 ix = 0;
2494 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002495
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002496 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002497 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002498 bx = nasm_reg_flags[b];
2499 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002500 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002501 bx = 0;
2502 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002503
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002504 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002505 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002506 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002507 int32_t o = input->offset;
2508 int mod, scale, index, base;
2509
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002510 /*
2511 * For a vector SIB, one has to be a vector and the other,
2512 * if present, a GPR. The vector must be the index operand.
2513 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002514 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002515 if (s == 0)
2516 s = 1;
2517 else if (s != 1)
2518 goto err;
2519
2520 t = bt, bt = it, it = t;
2521 x = bx, bx = ix, ix = x;
2522 }
2523
2524 if (bt != -1) {
2525 if (REG_GPR & ~bx)
2526 goto err;
2527 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2528 sok &= bx;
2529 else
2530 goto err;
2531 }
2532
2533 /*
2534 * While we're here, ensure the user didn't specify
2535 * WORD or QWORD
2536 */
2537 if (input->disp_size == 16 || input->disp_size == 64)
2538 goto err;
2539
2540 if (addrbits == 16 ||
2541 (addrbits == 32 && !(sok & BITS32)) ||
2542 (addrbits == 64 && !(sok & BITS64)))
2543 goto err;
2544
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002545 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2546 : ((ix & YMMREG & ~REG_EA)
2547 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002548
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002549 output->rex |= rexflags(it, ix, REX_X);
2550 output->rex |= rexflags(bt, bx, REX_B);
2551 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002552
2553 index = it & 7; /* it is known to be != -1 */
2554
2555 switch (s) {
2556 case 1:
2557 scale = 0;
2558 break;
2559 case 2:
2560 scale = 1;
2561 break;
2562 case 4:
2563 scale = 2;
2564 break;
2565 case 8:
2566 scale = 3;
2567 break;
2568 default: /* then what the smeg is it? */
2569 goto err; /* panic */
2570 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002571
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002572 if (bt == -1) {
2573 base = 5;
2574 mod = 0;
2575 } else {
2576 base = (bt & 7);
2577 if (base != REG_NUM_EBP && o == 0 &&
2578 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002579 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002580 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002581 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002582 mod = 1;
2583 else
2584 mod = 2;
2585 }
2586
2587 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002588 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2589 output->modrm = GEN_MODRM(mod, rfield, 4);
2590 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002591 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002592 /*
2593 * it must be a 32/64-bit memory reference. Firstly we have
2594 * to check that all registers involved are type E/Rxx.
2595 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002596 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002597 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002598
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002599 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002600 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2601 sok &= ix;
2602 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002603 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002604 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002605
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002606 if (bt != -1) {
2607 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002608 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002609 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002610 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002611 sok &= bx;
2612 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002613
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002614 /*
2615 * While we're here, ensure the user didn't specify
2616 * WORD or QWORD
2617 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002618 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002619 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002620
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002621 if (addrbits == 16 ||
2622 (addrbits == 32 && !(sok & BITS32)) ||
2623 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002624 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002625
Keith Kaniosb7a89542007-04-12 02:40:54 +00002626 /* now reorganize base/index */
2627 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002628 ((hb == b && ht == EAH_NOTBASE) ||
2629 (hb == i && ht == EAH_MAKEBASE))) {
2630 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002631 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002632 x = bx, bx = ix, ix = x;
2633 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002634
Jin Kyu Song164d6072013-10-15 19:10:13 -07002635 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002636 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002637 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002638 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002639 if (eaflags & EAF_MIB) {
2640 /* only for mib operands */
2641 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2642 /*
2643 * make a single reg index [reg*1].
2644 * gas uses this form for an explicit index register.
2645 */
2646 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2647 }
2648 if ((ht == EAH_SUMMED) && bt == -1) {
2649 /* separate once summed index into [base, index] */
2650 bt = it, bx = ix, s--;
2651 }
2652 } else {
2653 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002654 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002655 s == 3 || s == 5 || s == 9) && bt == -1) {
2656 /* convert 3*EAX to EAX+2*EAX */
2657 bt = it, bx = ix, s--;
2658 }
2659 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002660 (eaflags & EAF_TIMESTWO) &&
2661 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002662 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002663 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002664 * to sib format with 0x0 displacement - [EAX*1+0].
2665 */
2666 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2667 }
2668 }
Keith Kanios48af1772007-08-17 07:37:52 +00002669 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002670 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002671 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002672 x = ix, ix = bx, bx = x;
2673 }
2674 if (it == REG_NUM_ESP ||
2675 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002676 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002677
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002678 output->rex |= rexflags(it, ix, REX_X);
2679 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002680
Keith Kanios48af1772007-08-17 07:37:52 +00002681 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002682 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002683 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002684
Keith Kaniosb7a89542007-04-12 02:40:54 +00002685 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002686 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002687 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002688 } else {
2689 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002690 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002691 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002692 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002693 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002694 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002695 mod = 1;
2696 else
2697 mod = 2;
2698 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002699
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002700 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002701 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2702 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002703 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002704 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002705 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002706
Keith Kaniosb7a89542007-04-12 02:40:54 +00002707 if (it == -1)
2708 index = 4, s = 1;
2709 else
2710 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002711
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 switch (s) {
2713 case 1:
2714 scale = 0;
2715 break;
2716 case 2:
2717 scale = 1;
2718 break;
2719 case 4:
2720 scale = 2;
2721 break;
2722 case 8:
2723 scale = 3;
2724 break;
2725 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002726 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002728
Keith Kaniosb7a89542007-04-12 02:40:54 +00002729 if (bt == -1) {
2730 base = 5;
2731 mod = 0;
2732 } else {
2733 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002734 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002735 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002736 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002737 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002738 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002739 mod = 1;
2740 else
2741 mod = 2;
2742 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002743
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002744 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002745 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2746 output->modrm = GEN_MODRM(mod, rfield, 4);
2747 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002748 }
2749 } else { /* it's 16-bit */
2750 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002751 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002752
Keith Kaniosb7a89542007-04-12 02:40:54 +00002753 /* check for 64-bit long mode */
2754 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002755 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002756
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002758 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2759 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002760 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002761
Keith Kaniosb7a89542007-04-12 02:40:54 +00002762 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002763 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002764 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002765
H. Peter Anvine2c80182005-01-15 22:15:51 +00002766 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002767 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 if (b == -1 && i != -1) {
2769 int tmp = b;
2770 b = i;
2771 i = tmp;
2772 } /* swap */
2773 if ((b == R_SI || b == R_DI) && i != -1) {
2774 int tmp = b;
2775 b = i;
2776 i = tmp;
2777 }
2778 /* have BX/BP as base, SI/DI index */
2779 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002780 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002781 if (i != -1 && b != -1 &&
2782 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002783 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002784 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002785 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002786
H. Peter Anvine2c80182005-01-15 22:15:51 +00002787 rm = -1;
2788 if (i != -1)
2789 switch (i * 256 + b) {
2790 case R_SI * 256 + R_BX:
2791 rm = 0;
2792 break;
2793 case R_DI * 256 + R_BX:
2794 rm = 1;
2795 break;
2796 case R_SI * 256 + R_BP:
2797 rm = 2;
2798 break;
2799 case R_DI * 256 + R_BP:
2800 rm = 3;
2801 break;
2802 } else
2803 switch (b) {
2804 case R_SI:
2805 rm = 4;
2806 break;
2807 case R_DI:
2808 rm = 5;
2809 break;
2810 case R_BP:
2811 rm = 6;
2812 break;
2813 case R_BX:
2814 rm = 7;
2815 break;
2816 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002817 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002818 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002819
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002820 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002821 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002822 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002823 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002824 mod = 1;
2825 else
2826 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002827
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002828 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002829 output->bytes = mod; /* bytes of offset needed */
2830 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002831 }
2832 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002833 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002834
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002835 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002836 return output->type;
2837
2838err:
2839 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002840}
2841
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002842static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002843{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002844 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002845 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002846
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002847 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002848
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002849 switch (ins->prefixes[PPS_ASIZE]) {
2850 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002851 valid &= 16;
2852 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002853 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002854 valid &= 32;
2855 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002856 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002857 valid &= 64;
2858 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002859 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002860 valid &= (addrbits == 32) ? 16 : 32;
2861 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002862 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002863 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002864 }
2865
2866 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002867 if (is_class(MEMORY, ins->oprs[j].type)) {
2868 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002869
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002870 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002871 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002872 i = 0;
2873 else
2874 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002875
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002876 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002877 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002878 b = 0;
2879 else
2880 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002881
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002882 if (ins->oprs[j].scale == 0)
2883 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002884
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002885 if (!i && !b) {
2886 int ds = ins->oprs[j].disp_size;
2887 if ((addrbits != 64 && ds > 8) ||
2888 (addrbits == 64 && ds == 16))
2889 valid &= ds;
2890 } else {
2891 if (!(REG16 & ~b))
2892 valid &= 16;
2893 if (!(REG32 & ~b))
2894 valid &= 32;
2895 if (!(REG64 & ~b))
2896 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002897
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002898 if (!(REG16 & ~i))
2899 valid &= 16;
2900 if (!(REG32 & ~i))
2901 valid &= 32;
2902 if (!(REG64 & ~i))
2903 valid &= 64;
2904 }
2905 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002906 }
2907
2908 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002909 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002910 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002912 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002913 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002914 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002915 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002916 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002917 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002918 }
2919
2920 defdisp = ins->addr_size == 16 ? 16 : 32;
2921
2922 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002923 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2924 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2925 /*
2926 * mem_offs sizes must match the address size; if not,
2927 * strip the MEM_OFFS bit and match only EA instructions
2928 */
2929 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2930 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002931 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002932}