blob: b09f7cbb9b816bdc008851e0485a051cfac70c09 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvind24dd5f2016-02-08 10:32:13 -08003 * Copyright 1996-2016 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
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400248static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000249{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700250 return ins->prefixes[pos] == prefix;
251}
252
253static void assert_no_prefix(insn * ins, enum prefix_pos pos)
254{
255 if (ins->prefixes[pos])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800256 nasm_error(ERR_NONFATAL, "invalid %s prefix",
257 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700258}
259
260static const char *size_name(int size)
261{
262 switch (size) {
263 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400264 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400266 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700267 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700269 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700271 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700273 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700275 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400276 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700277 case 64:
278 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700279 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400280 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000281 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700282}
283
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400284static void warn_overflow(int pass, int size)
285{
H. Peter Anvin215186f2016-02-17 20:27:41 -0800286 nasm_error(ERR_WARNING | pass | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400287 "%s data exceeds bounds", size_name(size));
288}
289
290static void warn_overflow_const(int64_t data, int size)
291{
292 if (overflow_general(data, size))
293 warn_overflow(ERR_PASS1, size);
294}
295
296static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700297{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100298 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400299 if (overflow_general(o->offset, size))
300 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700301 }
302}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400303
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000304/*
305 * This routine wrappers the real output format's output routine,
306 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800307 * generator at the same time, flatten unnecessary relocations,
308 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000309 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700310static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000311{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000312 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700313 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700314 int asize;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800315 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700316 union {
317 uint8_t b[8];
318 uint64_t q;
319 } xdata;
320 uint64_t size = data->size;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000321
H. Peter Anvina77692b2016-09-20 14:04:33 -0700322 if (!data->size)
323 return; /* Nothing to do */
324
325 switch (data->type) {
326 case OUT_ADDRESS:
327 asize = data->size;
328 nasm_assert(asize <= 8);
329 if (data->tsegment == NO_SEG && data->twrt == NO_SEG) {
330 /* Convert to RAWDATA */
331 /* XXX: check for overflow */
332 uint8_t *q = xdata.b;
333
334 WRITEADDR(q, data->toffset, asize);
335 data->data = xdata.b;
336 data->type = OUT_RAWDATA;
337 asize = 0; /* No longer an address */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400338 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700339 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700340
H. Peter Anvina77692b2016-09-20 14:04:33 -0700341 case OUT_RELADDR:
342 asize = data->size;
343 nasm_assert(asize <= 8);
344 if (data->tsegment == data->segment && data->twrt == NO_SEG) {
345 /* Convert to RAWDATA */
346 uint8_t *q = xdata.b;
347 int64_t delta = data->toffset - data->offset
348 - (data->inslen - data->insoffs);
349
350 if (overflow_signed(delta, asize))
351 warn_overflow(ERR_PASS2, asize);
352
353 WRITEADDR(q, delta, asize);
354 data->data = xdata.b;
355 data->type = OUT_RAWDATA;
356 asize = 0; /* No longer an address */
357 }
358 break;
359
360 default:
361 asize = 0; /* Not an address */
362 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000363 }
364
H. Peter Anvina77692b2016-09-20 14:04:33 -0700365 lfmt->output(data);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800366
Frank Kotlerabebb082003-09-06 04:45:37 +0000367 /*
368 * this call to src_get determines when we call the
369 * debug-format-specific "linenum" function
370 * it updates lineno and lnfname to the current values
371 * returning 0 if "same as last time", -2 if lnfname
372 * changed, and the amount by which lineno changed,
373 * if it did. thus, these variables must be static
374 */
375
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400376 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700377 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000378
H. Peter Anvinb6412502016-02-11 21:07:40 -0800379 if (asize && asize > amax) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700380 if (data->type != OUT_ADDRESS || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800381 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800382 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800383 asize << 3, ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800384 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800385 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800386 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800387 asize << 3, ofmt->maxbits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700388 data->size = amax;
389 ofmt->output(data->segment, data->data, data->type,
390 data->size, data->tsegment, data->twrt);
391 data->insoffs += amax;
392 data->offset += amax;
393 data->size = size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800394 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700395 data->data = zero_buffer;
396 data->type = OUT_RAWDATA;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800397 }
398
H. Peter Anvina77692b2016-09-20 14:04:33 -0700399 /* Hack until backend change */
400 switch (data->type) {
401 case OUT_RELADDR:
402 switch (data->size) {
403 case 1:
404 data->type = OUT_REL1ADR;
405 break;
406 case 2:
407 data->type = OUT_REL2ADR;
408 break;
409 case 4:
410 data->type = OUT_REL4ADR;
411 break;
412 case 8:
413 data->type = OUT_REL8ADR;
414 break;
415 default:
416 panic();
417 break;
418 }
419
420 xdata.q = data->toffset;
421 data->data = xdata.b;
422 data->size = data->inslen - data->insoffs;
423 break;
424
425 case OUT_SEGMENT:
426 data->type = OUT_ADDRESS;
427 /* fall through */
428
429 case OUT_ADDRESS:
430 xdata.q = data->toffset;
431 data->data = xdata.b;
432 data->size = (data->sign == OUT_SIGNED) ? -data->size : data->size;
433 break;
434
435 case OUT_RAWDATA:
436 case OUT_RESERVE:
437 data->tsegment = data->twrt = NO_SEG;
438 break;
439
440 default:
441 panic();
442 break;
443 }
444
445 ofmt->output(data->segment, data->data, data->type,
446 data->size, data->tsegment, data->twrt);
447 data->offset += size;
448 data->insoffs += size;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000449}
450
H. Peter Anvina77692b2016-09-20 14:04:33 -0700451static inline void out_rawdata(struct out_data *data, const void *rawdata,
452 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400453{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700454 data->type = OUT_RAWDATA;
455 data->data = rawdata;
456 data->size = size;
457 out(data);
458}
459
460static void out_rawbyte(struct out_data *data, uint8_t byte)
461{
462 data->type = OUT_RAWDATA;
463 data->data = &byte;
464 data->size = 1;
465 out(data);
466}
467
468static inline void out_reserve(struct out_data *data, uint64_t size)
469{
470 data->type = OUT_RESERVE;
471 data->size = size;
472 out(data);
473}
474
475static inline void out_imm(struct out_data *data, struct operand *opx,
476 int size, enum out_sign sign)
477{
478 data->type = OUT_ADDRESS;
479 data->sign = sign;
480 data->size = size;
481 data->toffset = opx->offset;
482 data->tsegment = opx->segment;
483 data->twrt = opx->wrt;
484 out(data);
485}
486
487static inline void out_reladdr(struct out_data *data, struct operand *opx,
488 int size)
489{
490 data->type = OUT_RELADDR;
491 data->sign = OUT_SIGNED;
492 data->size = size;
493 data->toffset = opx->offset;
494 data->tsegment = opx->segment;
495 data->twrt = opx->wrt;
496 out(data);
497}
498
499static inline void out_segment(struct out_data *data, struct operand *opx)
500{
501 data->type = OUT_SEGMENT;
502 data->sign = OUT_UNSIGNED;
503 data->size = 2;
504 data->toffset = opx->offset;
505 data->tsegment = ofmt->segbase(opx->segment + 1);
506 data->twrt = opx->wrt;
507 out(data);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400508}
509
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700510static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800511 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000512{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800513 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800514 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000515 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800516 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000517
H. Peter Anvin755f5212012-02-25 11:41:34 -0800518 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700519 return false;
520 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400521 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700522 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400523 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700524
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800525 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100526
Victor van den Elzen154e5922009-02-25 17:32:00 +0100527 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100528 /* Be optimistic in pass 1 */
529 return true;
530
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700532 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000533
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700534 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800535 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
536
537 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
538 /* jmp short (opcode eb) cannot be used with bnd prefix. */
539 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800540 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800541 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800542 }
543
544 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000545}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000546
H. Peter Anvin04445362016-09-21 15:56:19 -0700547/* This is totally just a wild guess what is reasonable... */
548#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
549
H. Peter Anvina77692b2016-09-20 14:04:33 -0700550int64_t assemble(int32_t segment, int64_t start, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800551 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000552{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700553 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000554 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700555 enum match_result m;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000556 int32_t itimes;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300557 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000558
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000559 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000560
H. Peter Anvina77692b2016-09-20 14:04:33 -0700561 data.offset = start;
562 data.segment = segment;
563 data.itemp = NULL;
564 data.sign = OUT_WRAP;
565 data.bits = bits;
566
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300567 wsize = idata_bytes(instruction->opcode);
568 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000570
H. Peter Anvineba20a72002-04-30 20:53:55 +0000571 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000573 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 if (t < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800575 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000576
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400578 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400580 if (wsize > 8) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800581 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400582 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000583 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700584 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700585 data.insoffs = 0;
586 data.type = OUT_ADDRESS;
587 data.inslen = data.size = wsize;
588 data.toffset = e->offset;
589 data.tsegment = e->segment;
590 data.twrt = e->wrt;
591 out(&data);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400592 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700593 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400594 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700595 int align = e->stringlen % wsize;
596 if (align)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 align = wsize - align;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700598
599 data.insoffs = 0;
600 data.inslen = e->stringlen + align;
601
602 out_rawdata(&data, e->stringval, e->stringlen);
603 out_rawdata(&data, zero_buffer, align);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 }
605 }
606 if (t > 0 && t == instruction->times - 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700607 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800608 lfmt->uplevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 }
610 }
611 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800612 lfmt->downlevel(LIST_TIMES);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700613 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700614 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000615 FILE *fp;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700616 size_t t = instruction->times;
617 off_t base = 0;
618 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700619 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700620 char *buf = NULL;
621 size_t blk = 0; /* Buffered I/O block size */
622 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000623
H. Peter Anvind81a2352016-09-21 14:03:18 -0700624 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400625 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800626 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000627 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700628 goto done;
629 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000630
H. Peter Anvind81a2352016-09-21 14:03:18 -0700631 len = nasm_file_size(fp);
632
633 if (len == (off_t)-1) {
634 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700635 fname);
636 goto close_done;
637 }
638
H. Peter Anvina77692b2016-09-20 14:04:33 -0700639 if (instruction->eops->next) {
640 base = instruction->eops->next->offset;
641 if (base >= len) {
642 len = 0;
643 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000644 len -= base;
645 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700646 len > (off_t)instruction->eops->next->next->offset)
647 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700650
H. Peter Anvina77692b2016-09-20 14:04:33 -0700651 lfmt->set_offset(data.offset);
652 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000653
H. Peter Anvind81a2352016-09-21 14:03:18 -0700654 if (!len)
655 goto end_incbin;
656
657 /* Try to map file data */
658 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700659 if (!map) {
660 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
661 buf = nasm_malloc(blk);
662 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700663
664 while (t--) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700665 data.insoffs = 0;
666 data.inslen = len;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700667
H. Peter Anvind81a2352016-09-21 14:03:18 -0700668 if (map) {
669 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700670 } else if ((off_t)m == len) {
671 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700672 } else {
673 off_t l = len;
674
675 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700676 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700677 "`incbin': unable to seek on file `%s'",
678 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700679 goto end_incbin;
680 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700681 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700682 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700683 if (!m || feof(fp)) {
684 /*
685 * This shouldn't happen unless the file
686 * actually changes while we are reading
687 * it.
688 */
689 nasm_error(ERR_NONFATAL,
690 "`incbin': unexpected EOF while"
691 " reading file `%s'", fname);
692 goto end_incbin;
693 }
694 out_rawdata(&data, buf, m);
695 l -= m;
696 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700697 }
698 }
699 end_incbin:
700 lfmt->downlevel(LIST_INCBIN);
701 if (instruction->times > 1) {
702 lfmt->set_offset(data.offset);
703 lfmt->uplevel(LIST_TIMES);
704 lfmt->downlevel(LIST_TIMES);
705 }
706 if (ferror(fp)) {
707 nasm_error(ERR_NONFATAL,
708 "`incbin': error while"
709 " reading file `%s'", fname);
710 }
711 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700712 if (buf)
713 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700714 if (map)
715 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700716 fclose(fp);
717 done:
718 ;
719 } else {
720 /* "Real" instruction */
721
722 /* Check to see if we need an address-size prefix */
723 add_asp(instruction, bits);
724
725 m = find_match(&temp, instruction, data.segment, data.offset, bits);
726
727 if (m == MOK_GOOD) {
728 /* Matches! */
729 int64_t insn_size = calcsize(data.segment, data.offset,
730 bits, instruction, temp);
731 itimes = instruction->times;
732 if (insn_size < 0) /* shouldn't be, on pass two */
733 nasm_panic(0, "errors made it through from pass one");
734
735 data.itemp = temp;
736 data.bits = bits;
737
738 while (itimes--) {
739 data.insoffs = 0;
740 data.inslen = insn_size;
741
742 gencode(&data, instruction);
743 nasm_assert(data.insoffs == insn_size);
744
745 if (itimes > 0 && itimes == instruction->times - 1) {
746 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800747 lfmt->uplevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400748 }
749 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700750 if (instruction->times > 1)
751 lfmt->downlevel(LIST_TIMES);
752 } else {
753 /* No match */
754 switch (m) {
755 case MERR_OPSIZEMISSING:
756 nasm_error(ERR_NONFATAL, "operation size not specified");
757 break;
758 case MERR_OPSIZEMISMATCH:
759 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
760 break;
761 case MERR_BRNUMMISMATCH:
762 nasm_error(ERR_NONFATAL,
763 "mismatch in the number of broadcasting elements");
764 break;
765 case MERR_BADCPU:
766 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
767 break;
768 case MERR_BADMODE:
769 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
770 bits);
771 break;
772 case MERR_ENCMISMATCH:
773 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
774 break;
775 case MERR_BADBND:
776 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
777 break;
778 case MERR_BADREPNE:
779 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
780 (has_prefix(instruction, PPS_REP, P_REPNE) ?
781 "repne" : "repnz"));
782 break;
783 default:
784 nasm_error(ERR_NONFATAL,
785 "invalid combination of opcode and operands");
786 break;
787 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400788 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000789 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700790 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000791}
792
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400793int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800794 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000795{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000796 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700797 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000798
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000799 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000800
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400801 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000803
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700804 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
805 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400806 instruction->opcode == I_DT || instruction->opcode == I_DO ||
807 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000808 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300809 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000810
H. Peter Anvine2c80182005-01-15 22:15:51 +0000811 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300812 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000813
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400814 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000815 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000816
H. Peter Anvine2c80182005-01-15 22:15:51 +0000817 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400818 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400820 warn_overflow_const(e->offset, wsize);
821 } else if (e->type == EOT_DB_STRING ||
822 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000823 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000824
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 align = (-osize) % wsize;
826 if (align < 0)
827 align += wsize;
828 isize += osize + align;
829 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700830 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000831 }
832
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400834 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700835 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000836
H. Peter Anvind81a2352016-09-21 14:03:18 -0700837 len = nasm_file_size_by_path(fname);
838 if (len == (off_t)-1) {
839 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
840 fname);
841 return 0;
842 }
843
844 if (instruction->eops->next) {
845 if (len <= (off_t)instruction->eops->next->offset) {
846 len = 0;
847 } else {
848 len -= instruction->eops->next->offset;
849 if (instruction->eops->next->next &&
850 len > (off_t)instruction->eops->next->next->offset) {
851 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000852 }
853 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700855
856 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000857 }
858
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700859 /* Check to see if we need an address-size prefix */
860 add_asp(instruction, bits);
861
H. Peter Anvin23595f52009-07-25 17:44:25 -0700862 m = find_match(&temp, instruction, segment, offset, bits);
863 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400864 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700865 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700866 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400867 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000868 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000869}
870
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800871static void bad_hle_warn(const insn * ins, uint8_t hleok)
872{
873 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800874 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800875 static const enum whatwarn warn[2][4] =
876 {
877 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
878 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
879 };
880 unsigned int n;
881
882 n = (unsigned int)rep_pfx - P_XACQUIRE;
883 if (n > 1)
884 return; /* Not XACQUIRE/XRELEASE */
885
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800886 ww = warn[n][hleok];
887 if (!is_class(MEMORY, ins->oprs[0].type))
888 ww = w_inval; /* HLE requires operand 0 to be memory */
889
890 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800891 case w_none:
892 break;
893
894 case w_lock:
895 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800896 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800897 "%s with this instruction requires lock",
898 prefix_name(rep_pfx));
899 }
900 break;
901
902 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800903 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800904 "%s invalid with this instruction",
905 prefix_name(rep_pfx));
906 break;
907 }
908}
909
H. Peter Anvin507ae032008-10-09 15:37:10 -0700910/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400911#define case3(x) case (x): case (x)+1: case (x)+2
912#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700913
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800914static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800915 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000916{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800917 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800918 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000919 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000920 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700921 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700922 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700923 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700924 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800925 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800926 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700927 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000928
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700929 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700930 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700931 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700932
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700933 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400934 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700935
H. Peter Anvine2c80182005-01-15 22:15:51 +0000936 (void)segment; /* Don't warn that this parameter is unused */
937 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000938
H. Peter Anvin839eca22007-10-29 23:12:47 -0700939 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400940 c = *codes++;
941 op1 = (c & 3) + ((opex & 1) << 2);
942 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
943 opx = &ins->oprs[op1];
944 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700945
H. Peter Anvin839eca22007-10-29 23:12:47 -0700946 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400947 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 codes += c, length += c;
949 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700950
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400951 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400952 opex = c;
953 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700954
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400955 case4(010):
956 ins->rex |=
957 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 codes++, length++;
959 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700960
Jin Kyu Song164d6072013-10-15 19:10:13 -0700961 case4(014):
962 /* this is an index reg of MIB operand */
963 mib_index = opx->basereg;
964 break;
965
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400966 case4(020):
967 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000968 length++;
969 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700970
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400971 case4(030):
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(034):
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(040):
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(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700987 length += ins->addr_size >> 3;
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 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 length++;
992 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700993
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400994 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000995 length += 8; /* MOV reg64/imm */
996 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700997
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400998 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 length += 2;
1000 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001001
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001002 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001003 if (opx->type & (BITS16 | BITS32 | BITS64))
1004 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 else
1006 length += (bits == 16) ? 2 : 4;
1007 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001008
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001009 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 length += 4;
1011 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001012
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001013 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001014 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001015 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001016
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001017 case 0172:
1018 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001019 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001020 length++;
1021 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001022
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001023 case4(0174):
1024 length++;
1025 break;
1026
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001027 case4(0240):
1028 ins->rex |= REX_EV;
1029 ins->vexreg = regval(opx);
1030 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1031 ins->vex_cm = *codes++;
1032 ins->vex_wlp = *codes++;
1033 ins->evex_tuple = (*codes++ - 0300);
1034 break;
1035
1036 case 0250:
1037 ins->rex |= REX_EV;
1038 ins->vexreg = 0;
1039 ins->vex_cm = *codes++;
1040 ins->vex_wlp = *codes++;
1041 ins->evex_tuple = (*codes++ - 0300);
1042 break;
1043
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001044 case4(0254):
1045 length += 4;
1046 break;
1047
1048 case4(0260):
1049 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001050 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001051 ins->vex_cm = *codes++;
1052 ins->vex_wlp = *codes++;
1053 break;
1054
1055 case 0270:
1056 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001057 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001058 ins->vex_cm = *codes++;
1059 ins->vex_wlp = *codes++;
1060 break;
1061
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001062 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001063 hleok = c & 3;
1064 break;
1065
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001066 case4(0274):
1067 length++;
1068 break;
1069
1070 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001072
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001074 if (bits == 64)
1075 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001076 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001077 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001078
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001080 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001082
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001084 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001085
Keith Kaniosb7a89542007-04-12 02:40:54 +00001086 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001087 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1088 has_prefix(ins, PPS_ASIZE, P_A32))
1089 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001091
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001092 case4(0314):
1093 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001094
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001096 {
1097 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1098 if (pfx == P_O16)
1099 break;
1100 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001101 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001102 else
1103 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001105 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001106
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001108 {
1109 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1110 if (pfx == P_O32)
1111 break;
1112 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001113 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001114 else
1115 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001117 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001118
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 case 0322:
1120 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001121
Keith Kaniosb7a89542007-04-12 02:40:54 +00001122 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001123 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001124 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001125
Keith Kaniosb7a89542007-04-12 02:40:54 +00001126 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001127 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001128 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001129
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001130 case 0325:
1131 ins->rex |= REX_NH;
1132 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001133
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001134 case 0326:
1135 break;
1136
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 case 0330:
1138 codes++, length++;
1139 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001140
H. Peter Anvine2c80182005-01-15 22:15:51 +00001141 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001142 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001143
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001144 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001145 case 0333:
1146 length++;
1147 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001148
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001149 case 0334:
1150 ins->rex |= REX_L;
1151 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001152
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001153 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001154 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001155
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001156 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001157 if (!ins->prefixes[PPS_REP])
1158 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001159 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001160
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001161 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001162 if (!ins->prefixes[PPS_REP])
1163 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001164 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001165
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001167 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001168 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001169 " quantity of BSS space");
1170 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001171 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001172 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001173
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001174 case 0341:
1175 if (!ins->prefixes[PPS_WAIT])
1176 ins->prefixes[PPS_WAIT] = P_WAIT;
1177 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001178
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001179 case 0360:
1180 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001181
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001182 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001183 length++;
1184 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001185
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001186 case 0364:
1187 case 0365:
1188 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001189
Keith Kanios48af1772007-08-17 07:37:52 +00001190 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001191 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001192 length++;
1193 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001194
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001195 case 0370:
1196 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001198
H. Peter Anvine2c80182005-01-15 22:15:51 +00001199 case 0373:
1200 length++;
1201 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001202
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001203 case 0374:
1204 eat = EA_XMMVSIB;
1205 break;
1206
1207 case 0375:
1208 eat = EA_YMMVSIB;
1209 break;
1210
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001211 case 0376:
1212 eat = EA_ZMMVSIB;
1213 break;
1214
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001215 case4(0100):
1216 case4(0110):
1217 case4(0120):
1218 case4(0130):
1219 case4(0200):
1220 case4(0204):
1221 case4(0210):
1222 case4(0214):
1223 case4(0220):
1224 case4(0224):
1225 case4(0230):
1226 case4(0234):
1227 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001228 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001229 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001230 opflags_t rflags;
1231 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001232 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001233
Keith Kaniosb7a89542007-04-12 02:40:54 +00001234 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001235
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001236 if (c <= 0177) {
1237 /* pick rfield from operand b (opx) */
1238 rflags = regflag(opx);
1239 rfield = nasm_regvals[opx->basereg];
1240 } else {
1241 rflags = 0;
1242 rfield = c & 7;
1243 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001244
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001245 /* EVEX.b1 : evex_brerop contains the operand position */
1246 op_er_sae = (ins->evex_brerop >= 0 ?
1247 &ins->oprs[ins->evex_brerop] : NULL);
1248
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001249 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1250 /* set EVEX.b */
1251 ins->evex_p[2] |= EVEX_P2B;
1252 if (op_er_sae->decoflags & ER) {
1253 /* set EVEX.RC (rounding control) */
1254 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1255 & EVEX_P2RC;
1256 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001257 } else {
1258 /* set EVEX.L'L (vector length) */
1259 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001260 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001261 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001262 /* set EVEX.b */
1263 ins->evex_p[2] |= EVEX_P2B;
1264 }
1265 }
1266
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001267 if (itemp_has(temp, IF_MIB)) {
1268 opy->eaflags |= EAF_MIB;
1269 /*
1270 * if a separate form of MIB (ICC style) is used,
1271 * the index reg info is merged into mem operand
1272 */
1273 if (mib_index != R_none) {
1274 opy->indexreg = mib_index;
1275 opy->scale = 1;
1276 opy->hintbase = mib_index;
1277 opy->hinttype = EAH_NOTBASE;
1278 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001279 }
1280
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001281 if (process_ea(opy, &ea_data, bits,
1282 rfield, rflags, ins) != eat) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001283 nasm_error(ERR_NONFATAL, "invalid effective address");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001284 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001285 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001286 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001287 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001288 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001289 }
1290 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001291
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001292 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001293 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001294 ": instruction code \\%o (0x%02X) given", c, c);
1295 break;
1296 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001297 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001298
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001299 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001300
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001301 if (ins->rex & REX_NH) {
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, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001304 return -1;
1305 }
1306 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001307 }
1308
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001309 switch (ins->prefixes[PPS_VEX]) {
1310 case P_EVEX:
1311 if (!(ins->rex & REX_EV))
1312 return -1;
1313 break;
1314 case P_VEX3:
1315 case P_VEX2:
1316 if (!(ins->rex & REX_V))
1317 return -1;
1318 break;
1319 default:
1320 break;
1321 }
1322
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001323 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001324 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001325
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001326 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001327 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001328 return -1;
1329 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001330 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001331 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001332 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001333 ins->rex &= ~REX_W;
1334 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001335 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001336 ins->rex |= REX_W;
1337 bad32 &= ~REX_W;
1338 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001339 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001340 /* Follow REX_W */
1341 break;
1342 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001343
H. Peter Anvinfc561202011-07-07 16:58:22 -07001344 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001345 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001346 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001347 } else if (!(ins->rex & REX_EV) &&
1348 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001349 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001350 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001351 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001352 if (ins->rex & REX_EV)
1353 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001354 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1355 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001356 length += 3;
1357 else
1358 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001359 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001360 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001361 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001362 return -1;
1363 } else if (bits == 64) {
1364 length++;
1365 } else if ((ins->rex & REX_L) &&
1366 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001367 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001368 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001369 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001370 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001371 length++;
1372 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001373 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001374 return -1;
1375 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001376 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001377
1378 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001379 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001380 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001381 "instruction is not lockable");
1382 }
1383
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001384 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001385
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001386 /*
1387 * when BND prefix is set by DEFAULT directive,
1388 * BND prefix is added to every appropriate instruction line
1389 * unless it is overridden by NOBND prefix.
1390 */
1391 if (globalbnd &&
1392 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1393 ins->prefixes[PPS_REP] = P_BND;
1394
H. Peter Anvina77692b2016-09-20 14:04:33 -07001395 /*
1396 * Add length of legacy prefixes
1397 */
1398 length += emit_prefix(NULL, bits, ins);
1399
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001400 return length;
1401}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001402
H. Peter Anvina77692b2016-09-20 14:04:33 -07001403static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001404{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001405 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001406 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001407 !(ins->rex & (REX_V | REX_EV)) &&
1408 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001409 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1410 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001411 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001412 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001413 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001414}
1415
H. Peter Anvina77692b2016-09-20 14:04:33 -07001416static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1417{
1418 int bytes = 0;
1419 int j;
1420
1421 for (j = 0; j < MAXPREFIX; j++) {
1422 uint8_t c = 0;
1423 switch (ins->prefixes[j]) {
1424 case P_WAIT:
1425 c = 0x9B;
1426 break;
1427 case P_LOCK:
1428 c = 0xF0;
1429 break;
1430 case P_REPNE:
1431 case P_REPNZ:
1432 case P_XACQUIRE:
1433 case P_BND:
1434 c = 0xF2;
1435 break;
1436 case P_REPE:
1437 case P_REPZ:
1438 case P_REP:
1439 case P_XRELEASE:
1440 c = 0xF3;
1441 break;
1442 case R_CS:
1443 if (bits == 64) {
1444 nasm_error(ERR_WARNING | ERR_PASS2,
1445 "cs segment base generated, but will be ignored in 64-bit mode");
1446 }
1447 c = 0x2E;
1448 break;
1449 case R_DS:
1450 if (bits == 64) {
1451 nasm_error(ERR_WARNING | ERR_PASS2,
1452 "ds segment base generated, but will be ignored in 64-bit mode");
1453 }
1454 c = 0x3E;
1455 break;
1456 case R_ES:
1457 if (bits == 64) {
1458 nasm_error(ERR_WARNING | ERR_PASS2,
1459 "es segment base generated, but will be ignored in 64-bit mode");
1460 }
1461 c = 0x26;
1462 break;
1463 case R_FS:
1464 c = 0x64;
1465 break;
1466 case R_GS:
1467 c = 0x65;
1468 break;
1469 case R_SS:
1470 if (bits == 64) {
1471 nasm_error(ERR_WARNING | ERR_PASS2,
1472 "ss segment base generated, but will be ignored in 64-bit mode");
1473 }
1474 c = 0x36;
1475 break;
1476 case R_SEGR6:
1477 case R_SEGR7:
1478 nasm_error(ERR_NONFATAL,
1479 "segr6 and segr7 cannot be used as prefixes");
1480 break;
1481 case P_A16:
1482 if (bits == 64) {
1483 nasm_error(ERR_NONFATAL,
1484 "16-bit addressing is not supported "
1485 "in 64-bit mode");
1486 } else if (bits != 16)
1487 c = 0x67;
1488 break;
1489 case P_A32:
1490 if (bits != 32)
1491 c = 0x67;
1492 break;
1493 case P_A64:
1494 if (bits != 64) {
1495 nasm_error(ERR_NONFATAL,
1496 "64-bit addressing is only supported "
1497 "in 64-bit mode");
1498 }
1499 break;
1500 case P_ASP:
1501 c = 0x67;
1502 break;
1503 case P_O16:
1504 if (bits != 16)
1505 c = 0x66;
1506 break;
1507 case P_O32:
1508 if (bits == 16)
1509 c = 0x66;
1510 break;
1511 case P_O64:
1512 /* REX.W */
1513 break;
1514 case P_OSP:
1515 c = 0x66;
1516 break;
1517 case P_EVEX:
1518 case P_VEX3:
1519 case P_VEX2:
1520 case P_NOBND:
1521 case P_none:
1522 break;
1523 default:
1524 nasm_panic(0, "invalid instruction prefix");
1525 }
1526 if (c) {
1527 if (data)
1528 out_rawbyte(data, c);
1529 bytes++;
1530 }
1531 }
1532 return bytes;
1533}
1534
1535static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001536{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001537 uint8_t c;
1538 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001539 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001540 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001541 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001542 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001543 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001544 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001545 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001546 const int bits = data->bits;
H. Peter Anvin70653092007-10-19 14:42:29 -07001547
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001548 ins->rex_done = false;
1549
H. Peter Anvina77692b2016-09-20 14:04:33 -07001550 emit_prefix(data, bits, ins);
1551
H. Peter Anvin839eca22007-10-29 23:12:47 -07001552 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001553 c = *codes++;
1554 op1 = (c & 3) + ((opex & 1) << 2);
1555 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1556 opx = &ins->oprs[op1];
1557 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001558
H. Peter Anvina77692b2016-09-20 14:04:33 -07001559
H. Peter Anvin839eca22007-10-29 23:12:47 -07001560 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561 case 01:
1562 case 02:
1563 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001564 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001565 emit_rex(data, ins);
1566 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001569
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001570 case 05:
1571 case 06:
1572 case 07:
1573 opex = c;
1574 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001575
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001576 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001577 emit_rex(data, ins);
1578 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001579 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001580
Jin Kyu Song164d6072013-10-15 19:10:13 -07001581 case4(014):
1582 break;
1583
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001584 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001585 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001586 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001587 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001588 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001590
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001591 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001592 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001593 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001594 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001595 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001597
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001598 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001599 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001600 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001602
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001603 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001604 if (opx->type & (BITS16 | BITS32))
1605 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 else
1607 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001608 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001609 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001611
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001612 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001613 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001614 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001616
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001617 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001618 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001619 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001620 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001622
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001623 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001624 if (opx->segment == data->segment) {
1625 int64_t delta = opx->offset - data->offset
1626 - (data->inslen - data->insoffs);
1627 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001628 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001629 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001630 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001632
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001633 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001634 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001635 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001636
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001637 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001638 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001640
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001641 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001642 if (opx->type & (BITS16 | BITS32 | BITS64))
1643 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 else
1645 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001646
1647 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001648 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001649
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001650 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001651 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001652 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001653
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001654 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001655 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001656 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001657 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001658 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001660
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001661 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001662 {
1663 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1664 const struct operand *opy;
1665
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001666 c = *codes++;
1667 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001668 opy = &ins->oprs[c & 7];
1669 if (opy->segment != NO_SEG || opy->wrt != NO_SEG) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001670 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001671 "non-absolute expression not permitted as argument %d",
1672 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001673 } else if (opy->offset & ~mask) {
1674 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1675 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001676 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001677 c = opy->offset & mask;
1678 goto emit_is4;
1679 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001680
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001681 case 0173:
1682 c = *codes++;
1683 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001684 c &= 15;
1685 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001686
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001687 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001688 c = 0;
1689 emit_is4:
1690 r = nasm_regvals[opx->basereg];
1691 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001692 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001693
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001694 case4(0254):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001695 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001696 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001697 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001698 "signed dword immediate exceeds bounds");
1699 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001700 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001701 break;
1702
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001703 case4(0240):
1704 case 0250:
1705 codes += 3;
1706 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1707 EVEX_P2Z | EVEX_P2AAA, 2);
1708 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1709 bytes[0] = 0x62;
1710 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001711 bytes[1] = ((((ins->rex & 7) << 5) |
1712 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001713 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001714 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1715 ((~ins->vexreg & 15) << 3) |
1716 (1 << 2) | (ins->vex_wlp & 3);
1717 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001718 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001719 break;
1720
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001721 case4(0260):
1722 case 0270:
1723 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001724 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1725 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001726 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1727 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1728 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001729 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001730 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001731 } else {
1732 bytes[0] = 0xc5;
1733 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001734 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001735 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001736 }
1737 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001738
H. Peter Anvine014f352012-02-25 22:35:19 -08001739 case 0271:
1740 case 0272:
1741 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001742 break;
1743
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001744 case4(0274):
1745 {
1746 uint64_t uv, um;
1747 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001748
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001749 if (ins->rex & REX_W)
1750 s = 64;
1751 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1752 s = 16;
1753 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1754 s = 32;
1755 else
1756 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001757
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001758 um = (uint64_t)2 << (s-1);
1759 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001760
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001761 if (uv > 127 && uv < (uint64_t)-128 &&
1762 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001763 /* If this wasn't explicitly byte-sized, warn as though we
1764 * had fallen through to the imm16/32/64 case.
1765 */
H. Peter Anvin215186f2016-02-17 20:27:41 -08001766 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001767 "%s value exceeds bounds",
1768 (opx->type & BITS8) ? "signed byte" :
1769 s == 16 ? "word" :
1770 s == 32 ? "dword" :
1771 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001772 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001773 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001774 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001775 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001776
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001777 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001779
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001781 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1782 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001784
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001786 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1787 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001789
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 case 0312:
1791 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001792
Keith Kaniosb7a89542007-04-12 02:40:54 +00001793 case 0313:
1794 ins->rex = 0;
1795 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001796
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001797 case4(0314):
1798 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001799
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001803
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001805 case 0323:
1806 break;
1807
Keith Kaniosb7a89542007-04-12 02:40:54 +00001808 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001809 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001811
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001812 case 0325:
1813 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001814
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001815 case 0326:
1816 break;
1817
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001819 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001820 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001821
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001824
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001825 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001827 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001829
Keith Kanios48af1772007-08-17 07:37:52 +00001830 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001831 if (ins->rex & REX_R)
1832 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001833 ins->rex &= ~(REX_L|REX_R);
1834 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001835
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001836 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001837 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001838
H. Peter Anvin962e3052008-08-28 17:47:16 -07001839 case 0336:
1840 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001841 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001842
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001845 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001846
1847 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001849
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001850 case 0341:
1851 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001852
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001853 case 0360:
1854 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001855
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001856 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001857 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001858 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001859
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001860 case 0364:
1861 case 0365:
1862 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001863
Keith Kanios48af1772007-08-17 07:37:52 +00001864 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001865 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001866 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001867 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001868
Jin Kyu Song03041092013-10-15 19:38:51 -07001869 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001871
H. Peter Anvine2c80182005-01-15 22:15:51 +00001872 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001873 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001875
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001876 case 0374:
1877 eat = EA_XMMVSIB;
1878 break;
1879
1880 case 0375:
1881 eat = EA_YMMVSIB;
1882 break;
1883
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001884 case 0376:
1885 eat = EA_ZMMVSIB;
1886 break;
1887
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001888 case4(0100):
1889 case4(0110):
1890 case4(0120):
1891 case4(0130):
1892 case4(0200):
1893 case4(0204):
1894 case4(0210):
1895 case4(0214):
1896 case4(0220):
1897 case4(0224):
1898 case4(0230):
1899 case4(0234):
1900 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 ea ea_data;
1902 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001903 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001904 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001905 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001906
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001907 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001908 /* pick rfield from operand b (opx) */
1909 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001910 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001911 } else {
1912 /* rfield is constant */
1913 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001915 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001916
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001917 if (process_ea(opy, &ea_data, bits,
1918 rfield, rflags, ins) != eat)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001919 nasm_error(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001920
H. Peter Anvine2c80182005-01-15 22:15:51 +00001921 p = bytes;
1922 *p++ = ea_data.modrm;
1923 if (ea_data.sib_present)
1924 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001925 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001926
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001927 /*
1928 * Make sure the address gets the right offset in case
1929 * the line breaks in the .lst file (BR 1197827)
1930 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001931
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001932 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001933 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001934 if (ea_data.disp8) {
1935 out_rawbyte(data, ea_data.disp8);
1936 } else if (ea_data.rip) {
1937 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001938 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001939 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001940
H. Peter Anvina77692b2016-09-20 14:04:33 -07001941 if (overflow_general(opy->offset, asize) ||
1942 signed_bits(opy->offset, ins->addr_size) !=
1943 signed_bits(opy->offset, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001944 warn_overflow(ERR_PASS2, ea_data.bytes);
1945
H. Peter Anvina77692b2016-09-20 14:04:33 -07001946 out_imm(data, opy, ea_data.bytes,
1947 (asize > ea_data.bytes) ? OUT_SIGNED : OUT_UNSIGNED);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001948 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001949 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001950 }
1951 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001952
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001953 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001954 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001955 ": instruction code \\%o (0x%02X) given", c, c);
1956 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001957 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001958 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001959}
1960
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001961static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001962{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001963 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001964 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001965 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001966}
1967
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001968static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001969{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001970 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001971 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001972 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001973}
1974
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001975static int op_rexflags(const operand * o, int mask)
1976{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001977 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001978 int val;
1979
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001980 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001981 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001982
H. Peter Anvina4835d42008-05-20 14:21:29 -07001983 flags = nasm_reg_flags[o->basereg];
1984 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001985
1986 return rexflags(val, flags, mask);
1987}
1988
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001989static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001990{
1991 int rex = 0;
1992
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001993 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001994 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001995 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001996 rex |= REX_W;
1997 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1998 rex |= REX_H;
1999 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2000 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002001
2002 return rex & mask;
2003}
2004
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002005static int evexflags(int val, decoflags_t deco,
2006 int mask, uint8_t byte)
2007{
2008 int evex = 0;
2009
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002010 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002011 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002012 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002013 evex |= (EVEX_P0RP | EVEX_P0X);
2014 break;
2015 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002016 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002017 evex |= EVEX_P2VP;
2018 if (deco & Z)
2019 evex |= EVEX_P2Z;
2020 if (deco & OPMASK_MASK)
2021 evex |= deco & EVEX_P2AAA;
2022 break;
2023 }
2024 return evex & mask;
2025}
2026
2027static int op_evexflags(const operand * o, int mask, uint8_t byte)
2028{
2029 int val;
2030
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002031 val = nasm_regvals[o->basereg];
2032
2033 return evexflags(val, o->decoflags, mask, byte);
2034}
2035
H. Peter Anvin23595f52009-07-25 17:44:25 -07002036static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002037 insn *instruction,
2038 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002039{
2040 const struct itemplate *temp;
2041 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002042 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002043 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002044 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002045 int i;
2046
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002047 /* broadcasting uses a different data element size */
2048 for (i = 0; i < instruction->operands; i++)
2049 if (i == broadcast)
2050 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2051 else
2052 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002053
2054 merr = MERR_INVALOP;
2055
2056 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002057 temp->opcode != I_none; temp++) {
2058 m = matches(temp, instruction, bits);
2059 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002060 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002061 m = MOK_GOOD;
2062 else
2063 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002064 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002065 /*
2066 * Missing operand size and a candidate for fuzzy matching...
2067 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002068 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002069 if (i == broadcast)
2070 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2071 else
2072 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002073 opsizemissing = true;
2074 }
2075 if (m > merr)
2076 merr = m;
2077 if (merr == MOK_GOOD)
2078 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002079 }
2080
2081 /* No match, but see if we can get a fuzzy operand size match... */
2082 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002083 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002084
2085 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002086 /*
2087 * We ignore extrinsic operand sizes on registers, so we should
2088 * never try to fuzzy-match on them. This also resolves the case
2089 * when we have e.g. "xmmrm128" in two different positions.
2090 */
2091 if (is_class(REGISTER, instruction->oprs[i].type))
2092 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002093
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002094 /* This tests if xsizeflags[i] has more than one bit set */
2095 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2096 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002097
Jin Kyu Song7903c072013-10-30 03:00:12 -07002098 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002099 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002100 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2101 BITS32 : BITS64);
2102 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002103 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002104 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002105 }
2106
2107 /* Try matching again... */
2108 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002109 temp->opcode != I_none; temp++) {
2110 m = matches(temp, instruction, bits);
2111 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002112 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002113 m = MOK_GOOD;
2114 else
2115 m = MERR_INVALOP;
2116 }
2117 if (m > merr)
2118 merr = m;
2119 if (merr == MOK_GOOD)
2120 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002121 }
2122
H. Peter Anvina81655b2009-07-25 18:15:28 -07002123done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002124 *tempp = temp;
2125 return merr;
2126}
2127
Mark Charneydcaef4b2014-10-09 13:45:17 -04002128static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2129{
2130 opflags_t opsize = opflags & SIZE_MASK;
2131 uint8_t brcast_num;
2132
2133 /*
2134 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2135 * this cannot be a simple arithmetic calculation.
2136 */
2137 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002138 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002139 "size of broadcasting element is greater than 64 bits");
2140
2141 switch (opsize) {
2142 case BITS64:
2143 brcast_num = BITS64 / brsize;
2144 break;
2145 default:
2146 brcast_num = (opsize / BITS128) * (BITS64 / brsize) * 2;
2147 break;
2148 }
2149
2150 return brcast_num;
2151}
2152
H. Peter Anvin65289e82009-07-25 17:25:11 -07002153static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002154 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002155{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002156 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002157 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002158 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002159
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002160 /*
2161 * Check the opcode
2162 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002163 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002164 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002165
2166 /*
2167 * Count the operands
2168 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002169 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002170 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002171
2172 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002173 * Is it legal?
2174 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002175 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002176 return MERR_INVALOP;
2177
2178 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002179 * {evex} available?
2180 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002181 switch (instruction->prefixes[PPS_VEX]) {
2182 case P_EVEX:
2183 if (!itemp_has(itemp, IF_EVEX))
2184 return MERR_ENCMISMATCH;
2185 break;
2186 case P_VEX3:
2187 case P_VEX2:
2188 if (!itemp_has(itemp, IF_VEX))
2189 return MERR_ENCMISMATCH;
2190 break;
2191 default:
2192 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002193 }
2194
2195 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002196 * Check that no spurious colons or TOs are present
2197 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002198 for (i = 0; i < itemp->operands; i++)
2199 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002200 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002201
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002202 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002203 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002204 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002205 switch (itemp_smask(itemp)) {
2206 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002207 asize = BITS8;
2208 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002209 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002210 asize = BITS16;
2211 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002212 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002213 asize = BITS32;
2214 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002215 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002216 asize = BITS64;
2217 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002218 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002219 asize = BITS128;
2220 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002221 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002222 asize = BITS256;
2223 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002224 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002225 asize = BITS512;
2226 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002227 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002228 switch (bits) {
2229 case 16:
2230 asize = BITS16;
2231 break;
2232 case 32:
2233 asize = BITS32;
2234 break;
2235 case 64:
2236 asize = BITS64;
2237 break;
2238 default:
2239 asize = 0;
2240 break;
2241 }
2242 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002243 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002244 asize = 0;
2245 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002246 }
2247
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002248 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002249 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002250 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002251 memset(size, 0, sizeof size);
2252 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002253 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002254 /* S- flags apply to all operands */
2255 for (i = 0; i < MAX_OPERANDS; i++)
2256 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002257 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002258
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002259 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002260 * Check that the operand flags all match up,
2261 * it's a bit tricky so lets be verbose:
2262 *
2263 * 1) Find out the size of operand. If instruction
2264 * doesn't have one specified -- we're trying to
2265 * guess it either from template (IF_S* flag) or
2266 * from code bits.
2267 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002268 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002269 * template has an operand size specified AND this size differ
2270 * from which instruction has (perhaps we got it from code bits)
2271 * we are:
2272 * a) Check that only size of instruction and operand is differ
2273 * other characteristics do match
2274 * b) Perhaps it's a register specified in instruction so
2275 * for such a case we just mark that operand as "size
2276 * missing" and this will turn on fuzzy operand size
2277 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002278 */
2279 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002280 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002281 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002282 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002283 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002284 opflags_t template_opsize, insn_opsize;
2285
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002286 if (!(type & SIZE_MASK))
2287 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002288
Jin Kyu Song7903c072013-10-30 03:00:12 -07002289 insn_opsize = type & SIZE_MASK;
2290 if (!is_broadcast) {
2291 template_opsize = itemp->opd[i] & SIZE_MASK;
2292 } else {
2293 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2294 /*
2295 * when broadcasting, the element size depends on
2296 * the instruction type. decorator flag should match.
2297 */
2298
2299 if (deco_brsize) {
2300 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002301 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002302 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002303 } else {
2304 template_opsize = 0;
2305 }
2306 }
2307
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002308 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002309 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002310 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002311 } else if (template_opsize) {
2312 if (template_opsize != insn_opsize) {
2313 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002314 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002315 } else if (!is_class(REGISTER, type)) {
2316 /*
2317 * Note: we don't honor extrinsic operand sizes for registers,
2318 * so "missing operand size" for a register should be
2319 * considered a wildcard match rather than an error.
2320 */
2321 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002322 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002323 } else if (is_broadcast &&
2324 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002325 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002326 /*
2327 * broadcasting opsize matches but the number of repeated memory
2328 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002329 * if 64b double precision float is broadcasted to ymm (256b),
2330 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002331 */
2332 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002333 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002334 }
2335 }
2336
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002337 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002338 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002339
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002340 /*
2341 * Check operand sizes
2342 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002343 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2344 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002345 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002346 asize = itemp->opd[i] & SIZE_MASK;
2347 if (asize) {
2348 for (i = 0; i < oprs; i++)
2349 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002350 break;
2351 }
2352 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002353 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002354 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002355 }
2356
Keith Kaniosb7a89542007-04-12 02:40:54 +00002357 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002358 if (!(itemp->opd[i] & SIZE_MASK) &&
2359 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002360 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002361 }
2362
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002363 /*
2364 * Check template is okay at the set cpu level
2365 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002366 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002367 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002368
Keith Kaniosb7a89542007-04-12 02:40:54 +00002369 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002370 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002371 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002372 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002373 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002375 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002376 * If we have a HLE prefix, look for the NOHLE flag
2377 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002378 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002379 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2380 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2381 return MERR_BADHLE;
2382
2383 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002384 * Check if special handling needed for Jumps
2385 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002386 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002387 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388
Jin Kyu Song03041092013-10-15 19:38:51 -07002389 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002390 * Check if BND prefix is allowed.
2391 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002392 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002393 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002394 (has_prefix(instruction, PPS_REP, P_BND) ||
2395 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002396 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002397 else if (itemp_has(itemp, IF_BND) &&
2398 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2399 has_prefix(instruction, PPS_REP, P_REPNZ)))
2400 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002401
H. Peter Anvin60926242009-07-26 16:25:38 -07002402 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002403}
2404
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002405/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002406 * Check if ModR/M.mod should/can be 01.
2407 * - EAF_BYTEOFFS is set
2408 * - offset can fit in a byte when EVEX is not used
2409 * - offset can be compressed when EVEX is used
2410 */
2411#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2412 (o >= -128 && o <= 127 && \
2413 seg == NO_SEG && !forw_ref && \
2414 !(input->eaflags & EAF_WORDOFFS) && \
2415 !(ins->rex & REX_EV)) || \
2416 (ins->rex & REX_EV && \
2417 is_disp8n(input, ins, &output->disp8)))
2418
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002419static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002420 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002421{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002422 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002423 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002424 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002425
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002426 output->type = EA_SCALAR;
2427 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002428 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002429
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002430 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002431 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002432 /* EVEX.R' flag for the REG operand */
2433 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002434
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002435 if (is_class(REGISTER, input->type)) {
2436 /*
2437 * It's a direct register.
2438 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002439 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002440 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002441
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002442 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002443 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002444
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002445 /* broadcasting is not available with a direct register operand. */
2446 if (input->decoflags & BRDCAST_MASK) {
2447 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2448 goto err;
2449 }
2450
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002451 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002452 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002453 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002454 output->bytes = 0; /* no offset necessary either */
2455 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2456 } else {
2457 /*
2458 * It's a memory reference.
2459 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002460
2461 /* Embedded rounding or SAE is not available with a mem ref operand. */
2462 if (input->decoflags & (ER | SAE)) {
2463 nasm_error(ERR_NONFATAL,
2464 "Embedded rounding is available only with reg-reg op.");
2465 return -1;
2466 }
2467
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002468 if (input->basereg == -1 &&
2469 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002470 /*
2471 * It's a pure offset.
2472 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002473 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2474 input->segment == NO_SEG) {
2475 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2476 input->type &= ~IP_REL;
2477 input->type |= MEMORY;
2478 }
2479
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002480 if (bits == 64 &&
2481 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2482 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2483 return -1;
2484 }
2485
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002486 if (eaflags & EAF_BYTEOFFS ||
2487 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002488 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2489 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2490 }
2491
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002492 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002493 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002494 output->sib = GEN_SIB(0, 4, 5);
2495 output->bytes = 4;
2496 output->modrm = GEN_MODRM(0, rfield, 4);
2497 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002498 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002499 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002500 output->bytes = (addrbits != 16 ? 4 : 2);
2501 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2502 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002503 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002504 } else {
2505 /*
2506 * It's an indirection.
2507 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002509 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002511 int t, it, bt; /* register numbers */
2512 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002513
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 if (s == 0)
2515 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002516
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002517 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002518 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002519 ix = nasm_reg_flags[i];
2520 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002521 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002522 ix = 0;
2523 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002524
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002525 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002526 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002527 bx = nasm_reg_flags[b];
2528 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002529 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002530 bx = 0;
2531 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002532
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002533 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002534 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002535 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002536 int32_t o = input->offset;
2537 int mod, scale, index, base;
2538
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002539 /*
2540 * For a vector SIB, one has to be a vector and the other,
2541 * if present, a GPR. The vector must be the index operand.
2542 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002543 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002544 if (s == 0)
2545 s = 1;
2546 else if (s != 1)
2547 goto err;
2548
2549 t = bt, bt = it, it = t;
2550 x = bx, bx = ix, ix = x;
2551 }
2552
2553 if (bt != -1) {
2554 if (REG_GPR & ~bx)
2555 goto err;
2556 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2557 sok &= bx;
2558 else
2559 goto err;
2560 }
2561
2562 /*
2563 * While we're here, ensure the user didn't specify
2564 * WORD or QWORD
2565 */
2566 if (input->disp_size == 16 || input->disp_size == 64)
2567 goto err;
2568
2569 if (addrbits == 16 ||
2570 (addrbits == 32 && !(sok & BITS32)) ||
2571 (addrbits == 64 && !(sok & BITS64)))
2572 goto err;
2573
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002574 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2575 : ((ix & YMMREG & ~REG_EA)
2576 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002577
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002578 output->rex |= rexflags(it, ix, REX_X);
2579 output->rex |= rexflags(bt, bx, REX_B);
2580 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002581
2582 index = it & 7; /* it is known to be != -1 */
2583
2584 switch (s) {
2585 case 1:
2586 scale = 0;
2587 break;
2588 case 2:
2589 scale = 1;
2590 break;
2591 case 4:
2592 scale = 2;
2593 break;
2594 case 8:
2595 scale = 3;
2596 break;
2597 default: /* then what the smeg is it? */
2598 goto err; /* panic */
2599 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002600
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002601 if (bt == -1) {
2602 base = 5;
2603 mod = 0;
2604 } else {
2605 base = (bt & 7);
2606 if (base != REG_NUM_EBP && o == 0 &&
2607 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002608 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002609 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002610 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002611 mod = 1;
2612 else
2613 mod = 2;
2614 }
2615
2616 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002617 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2618 output->modrm = GEN_MODRM(mod, rfield, 4);
2619 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002620 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002621 /*
2622 * it must be a 32/64-bit memory reference. Firstly we have
2623 * to check that all registers involved are type E/Rxx.
2624 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002625 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002626 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002627
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002628 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002629 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2630 sok &= ix;
2631 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002632 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002633 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002634
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002635 if (bt != -1) {
2636 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002637 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002638 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002639 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002640 sok &= bx;
2641 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002642
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002643 /*
2644 * While we're here, ensure the user didn't specify
2645 * WORD or QWORD
2646 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002647 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002648 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002649
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002650 if (addrbits == 16 ||
2651 (addrbits == 32 && !(sok & BITS32)) ||
2652 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002653 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002654
Keith Kaniosb7a89542007-04-12 02:40:54 +00002655 /* now reorganize base/index */
2656 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002657 ((hb == b && ht == EAH_NOTBASE) ||
2658 (hb == i && ht == EAH_MAKEBASE))) {
2659 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002660 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002661 x = bx, bx = ix, ix = x;
2662 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002663
Jin Kyu Song164d6072013-10-15 19:10:13 -07002664 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002665 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002666 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002667 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002668 if (eaflags & EAF_MIB) {
2669 /* only for mib operands */
2670 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2671 /*
2672 * make a single reg index [reg*1].
2673 * gas uses this form for an explicit index register.
2674 */
2675 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2676 }
2677 if ((ht == EAH_SUMMED) && bt == -1) {
2678 /* separate once summed index into [base, index] */
2679 bt = it, bx = ix, s--;
2680 }
2681 } else {
2682 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002683 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002684 s == 3 || s == 5 || s == 9) && bt == -1) {
2685 /* convert 3*EAX to EAX+2*EAX */
2686 bt = it, bx = ix, s--;
2687 }
2688 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002689 (eaflags & EAF_TIMESTWO) &&
2690 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002691 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002692 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002693 * to sib format with 0x0 displacement - [EAX*1+0].
2694 */
2695 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2696 }
2697 }
Keith Kanios48af1772007-08-17 07:37:52 +00002698 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002699 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002700 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002701 x = ix, ix = bx, bx = x;
2702 }
2703 if (it == REG_NUM_ESP ||
2704 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002705 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002706
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002707 output->rex |= rexflags(it, ix, REX_X);
2708 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002709
Keith Kanios48af1772007-08-17 07:37:52 +00002710 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002711 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002713
Keith Kaniosb7a89542007-04-12 02:40:54 +00002714 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002717 } else {
2718 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002719 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002720 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002721 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002722 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002723 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002724 mod = 1;
2725 else
2726 mod = 2;
2727 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002728
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002729 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002730 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2731 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002732 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002733 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002735
Keith Kaniosb7a89542007-04-12 02:40:54 +00002736 if (it == -1)
2737 index = 4, s = 1;
2738 else
2739 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002740
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 switch (s) {
2742 case 1:
2743 scale = 0;
2744 break;
2745 case 2:
2746 scale = 1;
2747 break;
2748 case 4:
2749 scale = 2;
2750 break;
2751 case 8:
2752 scale = 3;
2753 break;
2754 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002755 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002757
Keith Kaniosb7a89542007-04-12 02:40:54 +00002758 if (bt == -1) {
2759 base = 5;
2760 mod = 0;
2761 } else {
2762 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002763 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002764 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002765 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002766 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002767 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002768 mod = 1;
2769 else
2770 mod = 2;
2771 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002772
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002773 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002774 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2775 output->modrm = GEN_MODRM(mod, rfield, 4);
2776 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002777 }
2778 } else { /* it's 16-bit */
2779 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002780 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002781
Keith Kaniosb7a89542007-04-12 02:40:54 +00002782 /* check for 64-bit long mode */
2783 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002784 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002785
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002787 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2788 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002789 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002790
Keith Kaniosb7a89542007-04-12 02:40:54 +00002791 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002792 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002793 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002794
H. Peter Anvine2c80182005-01-15 22:15:51 +00002795 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002796 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 if (b == -1 && i != -1) {
2798 int tmp = b;
2799 b = i;
2800 i = tmp;
2801 } /* swap */
2802 if ((b == R_SI || b == R_DI) && i != -1) {
2803 int tmp = b;
2804 b = i;
2805 i = tmp;
2806 }
2807 /* have BX/BP as base, SI/DI index */
2808 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002809 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 if (i != -1 && b != -1 &&
2811 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002812 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002813 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002814 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002815
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 rm = -1;
2817 if (i != -1)
2818 switch (i * 256 + b) {
2819 case R_SI * 256 + R_BX:
2820 rm = 0;
2821 break;
2822 case R_DI * 256 + R_BX:
2823 rm = 1;
2824 break;
2825 case R_SI * 256 + R_BP:
2826 rm = 2;
2827 break;
2828 case R_DI * 256 + R_BP:
2829 rm = 3;
2830 break;
2831 } else
2832 switch (b) {
2833 case R_SI:
2834 rm = 4;
2835 break;
2836 case R_DI:
2837 rm = 5;
2838 break;
2839 case R_BP:
2840 rm = 6;
2841 break;
2842 case R_BX:
2843 rm = 7;
2844 break;
2845 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002846 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002847 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002848
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002849 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002850 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002852 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 mod = 1;
2854 else
2855 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002856
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002857 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002858 output->bytes = mod; /* bytes of offset needed */
2859 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002860 }
2861 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002862 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002863
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002864 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002865 return output->type;
2866
2867err:
2868 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002869}
2870
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002871static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002872{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002873 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002874 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002875
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002876 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002877
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002878 switch (ins->prefixes[PPS_ASIZE]) {
2879 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002880 valid &= 16;
2881 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002882 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002883 valid &= 32;
2884 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002885 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002886 valid &= 64;
2887 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002888 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002889 valid &= (addrbits == 32) ? 16 : 32;
2890 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002891 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002892 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002893 }
2894
2895 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002896 if (is_class(MEMORY, ins->oprs[j].type)) {
2897 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002898
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002899 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002900 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002901 i = 0;
2902 else
2903 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002904
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002906 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002907 b = 0;
2908 else
2909 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002910
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 if (ins->oprs[j].scale == 0)
2912 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002913
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002914 if (!i && !b) {
2915 int ds = ins->oprs[j].disp_size;
2916 if ((addrbits != 64 && ds > 8) ||
2917 (addrbits == 64 && ds == 16))
2918 valid &= ds;
2919 } else {
2920 if (!(REG16 & ~b))
2921 valid &= 16;
2922 if (!(REG32 & ~b))
2923 valid &= 32;
2924 if (!(REG64 & ~b))
2925 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002926
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002927 if (!(REG16 & ~i))
2928 valid &= 16;
2929 if (!(REG32 & ~i))
2930 valid &= 32;
2931 if (!(REG64 & ~i))
2932 valid &= 64;
2933 }
2934 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002935 }
2936
2937 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002938 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002939 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002940 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002941 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002942 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002943 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002944 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002945 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002946 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002947 }
2948
2949 defdisp = ins->addr_size == 16 ? 16 : 32;
2950
2951 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002952 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2953 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2954 /*
2955 * mem_offs sizes must match the address size; if not,
2956 * strip the MEM_OFFS bit and match only EA instructions
2957 */
2958 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2959 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002960 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002961}