blob: 10011c99384374e62fbb52a6b23800db825d123b [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
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700325 /*
326 * Convert addresses to RAWDATA if possible
327 * XXX: not all backends want this for global symbols!!!!
328 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700329 switch (data->type) {
330 case OUT_ADDRESS:
331 asize = data->size;
332 nasm_assert(asize <= 8);
333 if (data->tsegment == NO_SEG && data->twrt == NO_SEG) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700334 /* XXX: check for overflow */
335 uint8_t *q = xdata.b;
336
337 WRITEADDR(q, data->toffset, asize);
338 data->data = xdata.b;
339 data->type = OUT_RAWDATA;
340 asize = 0; /* No longer an address */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400341 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700342 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700343
H. Peter Anvina77692b2016-09-20 14:04:33 -0700344 case OUT_RELADDR:
345 asize = data->size;
346 nasm_assert(asize <= 8);
347 if (data->tsegment == data->segment && data->twrt == NO_SEG) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700348 uint8_t *q = xdata.b;
349 int64_t delta = data->toffset - data->offset
350 - (data->inslen - data->insoffs);
351
352 if (overflow_signed(delta, asize))
353 warn_overflow(ERR_PASS2, asize);
354
355 WRITEADDR(q, delta, asize);
356 data->data = xdata.b;
357 data->type = OUT_RAWDATA;
358 asize = 0; /* No longer an address */
359 }
360 break;
361
362 default:
363 asize = 0; /* Not an address */
364 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000365 }
366
H. Peter Anvina77692b2016-09-20 14:04:33 -0700367 lfmt->output(data);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800368
Frank Kotlerabebb082003-09-06 04:45:37 +0000369 /*
370 * this call to src_get determines when we call the
371 * debug-format-specific "linenum" function
372 * it updates lineno and lnfname to the current values
373 * returning 0 if "same as last time", -2 if lnfname
374 * changed, and the amount by which lineno changed,
375 * if it did. thus, these variables must be static
376 */
377
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400378 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700379 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000380
H. Peter Anvinb6412502016-02-11 21:07:40 -0800381 if (asize && asize > amax) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700382 if (data->type != OUT_ADDRESS || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800383 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800384 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800385 asize << 3, ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800386 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800387 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800388 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800389 asize << 3, ofmt->maxbits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700390 data->size = amax;
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700391 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700392 data->insoffs += amax;
393 data->offset += amax;
394 data->size = size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800395 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700396 data->data = zero_buffer;
397 data->type = OUT_RAWDATA;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800398 }
399
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700400 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700401 data->offset += size;
402 data->insoffs += size;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000403}
404
H. Peter Anvina77692b2016-09-20 14:04:33 -0700405static inline void out_rawdata(struct out_data *data, const void *rawdata,
406 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400407{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700408 data->type = OUT_RAWDATA;
409 data->data = rawdata;
410 data->size = size;
411 out(data);
412}
413
414static void out_rawbyte(struct out_data *data, uint8_t byte)
415{
416 data->type = OUT_RAWDATA;
417 data->data = &byte;
418 data->size = 1;
419 out(data);
420}
421
422static inline void out_reserve(struct out_data *data, uint64_t size)
423{
424 data->type = OUT_RESERVE;
425 data->size = size;
426 out(data);
427}
428
429static inline void out_imm(struct out_data *data, struct operand *opx,
430 int size, enum out_sign sign)
431{
432 data->type = OUT_ADDRESS;
433 data->sign = sign;
434 data->size = size;
435 data->toffset = opx->offset;
436 data->tsegment = opx->segment;
437 data->twrt = opx->wrt;
438 out(data);
439}
440
441static inline void out_reladdr(struct out_data *data, struct operand *opx,
442 int size)
443{
444 data->type = OUT_RELADDR;
445 data->sign = OUT_SIGNED;
446 data->size = size;
447 data->toffset = opx->offset;
448 data->tsegment = opx->segment;
449 data->twrt = opx->wrt;
450 out(data);
451}
452
453static inline void out_segment(struct out_data *data, struct operand *opx)
454{
455 data->type = OUT_SEGMENT;
456 data->sign = OUT_UNSIGNED;
457 data->size = 2;
458 data->toffset = opx->offset;
459 data->tsegment = ofmt->segbase(opx->segment + 1);
460 data->twrt = opx->wrt;
461 out(data);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400462}
463
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700464static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800465 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000466{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800467 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800468 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000469 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800470 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000471
H. Peter Anvin755f5212012-02-25 11:41:34 -0800472 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700473 return false;
474 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400475 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700476 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400477 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700478
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800479 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100480
Victor van den Elzen154e5922009-02-25 17:32:00 +0100481 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100482 /* Be optimistic in pass 1 */
483 return true;
484
H. Peter Anvine2c80182005-01-15 22:15:51 +0000485 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700486 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000487
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700488 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800489 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
490
491 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
492 /* jmp short (opcode eb) cannot be used with bnd prefix. */
493 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800494 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800495 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800496 }
497
498 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000500
H. Peter Anvin04445362016-09-21 15:56:19 -0700501/* This is totally just a wild guess what is reasonable... */
502#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
503
H. Peter Anvina77692b2016-09-20 14:04:33 -0700504int64_t assemble(int32_t segment, int64_t start, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800505 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000506{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700507 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000508 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700509 enum match_result m;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000510 int32_t itimes;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300511 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000512
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000513 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000514
H. Peter Anvina77692b2016-09-20 14:04:33 -0700515 data.offset = start;
516 data.segment = segment;
517 data.itemp = NULL;
518 data.sign = OUT_WRAP;
519 data.bits = bits;
520
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300521 wsize = idata_bytes(instruction->opcode);
522 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000524
H. Peter Anvineba20a72002-04-30 20:53:55 +0000525 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000527 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000528 if (t < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800529 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000530
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400532 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000533 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400534 if (wsize > 8) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800535 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400536 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000537 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700538 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700539 data.insoffs = 0;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700540 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700541 data.inslen = data.size = wsize;
542 data.toffset = e->offset;
543 data.tsegment = e->segment;
544 data.twrt = e->wrt;
545 out(&data);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400546 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700547 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400548 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700549 int align = e->stringlen % wsize;
550 if (align)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551 align = wsize - align;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700552
553 data.insoffs = 0;
554 data.inslen = e->stringlen + align;
555
556 out_rawdata(&data, e->stringval, e->stringlen);
557 out_rawdata(&data, zero_buffer, align);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 }
559 }
560 if (t > 0 && t == instruction->times - 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700561 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800562 lfmt->uplevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563 }
564 }
565 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800566 lfmt->downlevel(LIST_TIMES);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700567 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700568 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 FILE *fp;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700570 size_t t = instruction->times;
571 off_t base = 0;
572 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700573 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700574 char *buf = NULL;
575 size_t blk = 0; /* Buffered I/O block size */
576 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000577
H. Peter Anvind81a2352016-09-21 14:03:18 -0700578 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400579 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800580 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700582 goto done;
583 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000584
H. Peter Anvind81a2352016-09-21 14:03:18 -0700585 len = nasm_file_size(fp);
586
587 if (len == (off_t)-1) {
588 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700589 fname);
590 goto close_done;
591 }
592
H. Peter Anvina77692b2016-09-20 14:04:33 -0700593 if (instruction->eops->next) {
594 base = instruction->eops->next->offset;
595 if (base >= len) {
596 len = 0;
597 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 len -= base;
599 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700600 len > (off_t)instruction->eops->next->next->offset)
601 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000602 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700604
H. Peter Anvina77692b2016-09-20 14:04:33 -0700605 lfmt->set_offset(data.offset);
606 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000607
H. Peter Anvind81a2352016-09-21 14:03:18 -0700608 if (!len)
609 goto end_incbin;
610
611 /* Try to map file data */
612 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700613 if (!map) {
614 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
615 buf = nasm_malloc(blk);
616 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700617
618 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700619 /*
620 * Consider these irrelevant for INCBIN, since it is fully
621 * possible that these might be (way) bigger than an int
622 * can hold; there is, however, no reason to widen these
623 * types just for INCBIN. data.inslen == 0 signals to the
624 * backend that these fields are meaningless, if at all
625 * needed.
626 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700627 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700628 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700629
H. Peter Anvind81a2352016-09-21 14:03:18 -0700630 if (map) {
631 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700632 } else if ((off_t)m == len) {
633 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700634 } else {
635 off_t l = len;
636
637 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700638 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700639 "`incbin': unable to seek on file `%s'",
640 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700641 goto end_incbin;
642 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700643 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700644 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700645 if (!m || feof(fp)) {
646 /*
647 * This shouldn't happen unless the file
648 * actually changes while we are reading
649 * it.
650 */
651 nasm_error(ERR_NONFATAL,
652 "`incbin': unexpected EOF while"
653 " reading file `%s'", fname);
654 goto end_incbin;
655 }
656 out_rawdata(&data, buf, m);
657 l -= m;
658 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700659 }
660 }
661 end_incbin:
662 lfmt->downlevel(LIST_INCBIN);
663 if (instruction->times > 1) {
664 lfmt->set_offset(data.offset);
665 lfmt->uplevel(LIST_TIMES);
666 lfmt->downlevel(LIST_TIMES);
667 }
668 if (ferror(fp)) {
669 nasm_error(ERR_NONFATAL,
670 "`incbin': error while"
671 " reading file `%s'", fname);
672 }
673 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700674 if (buf)
675 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700676 if (map)
677 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700678 fclose(fp);
679 done:
680 ;
681 } else {
682 /* "Real" instruction */
683
684 /* Check to see if we need an address-size prefix */
685 add_asp(instruction, bits);
686
687 m = find_match(&temp, instruction, data.segment, data.offset, bits);
688
689 if (m == MOK_GOOD) {
690 /* Matches! */
691 int64_t insn_size = calcsize(data.segment, data.offset,
692 bits, instruction, temp);
693 itimes = instruction->times;
694 if (insn_size < 0) /* shouldn't be, on pass two */
695 nasm_panic(0, "errors made it through from pass one");
696
697 data.itemp = temp;
698 data.bits = bits;
699
700 while (itimes--) {
701 data.insoffs = 0;
702 data.inslen = insn_size;
703
704 gencode(&data, instruction);
705 nasm_assert(data.insoffs == insn_size);
706
707 if (itimes > 0 && itimes == instruction->times - 1) {
708 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800709 lfmt->uplevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400710 }
711 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700712 if (instruction->times > 1)
713 lfmt->downlevel(LIST_TIMES);
714 } else {
715 /* No match */
716 switch (m) {
717 case MERR_OPSIZEMISSING:
718 nasm_error(ERR_NONFATAL, "operation size not specified");
719 break;
720 case MERR_OPSIZEMISMATCH:
721 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
722 break;
723 case MERR_BRNUMMISMATCH:
724 nasm_error(ERR_NONFATAL,
725 "mismatch in the number of broadcasting elements");
726 break;
727 case MERR_BADCPU:
728 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
729 break;
730 case MERR_BADMODE:
731 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
732 bits);
733 break;
734 case MERR_ENCMISMATCH:
735 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
736 break;
737 case MERR_BADBND:
738 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
739 break;
740 case MERR_BADREPNE:
741 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
742 (has_prefix(instruction, PPS_REP, P_REPNE) ?
743 "repne" : "repnz"));
744 break;
745 default:
746 nasm_error(ERR_NONFATAL,
747 "invalid combination of opcode and operands");
748 break;
749 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400750 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000751 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700752 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000753}
754
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400755int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800756 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000757{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000758 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700759 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000760
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000761 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000762
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400763 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000764 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000765
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700766 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
767 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400768 instruction->opcode == I_DT || instruction->opcode == I_DO ||
769 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000770 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300771 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000772
H. Peter Anvine2c80182005-01-15 22:15:51 +0000773 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300774 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000775
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400776 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000777 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000778
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400780 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000781 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400782 warn_overflow_const(e->offset, wsize);
783 } else if (e->type == EOT_DB_STRING ||
784 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000786
H. Peter Anvine2c80182005-01-15 22:15:51 +0000787 align = (-osize) % wsize;
788 if (align < 0)
789 align += wsize;
790 isize += osize + align;
791 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700792 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000793 }
794
H. Peter Anvine2c80182005-01-15 22:15:51 +0000795 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400796 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700797 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000798
H. Peter Anvind81a2352016-09-21 14:03:18 -0700799 len = nasm_file_size_by_path(fname);
800 if (len == (off_t)-1) {
801 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
802 fname);
803 return 0;
804 }
805
806 if (instruction->eops->next) {
807 if (len <= (off_t)instruction->eops->next->offset) {
808 len = 0;
809 } else {
810 len -= instruction->eops->next->offset;
811 if (instruction->eops->next->next &&
812 len > (off_t)instruction->eops->next->next->offset) {
813 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 }
815 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700817
818 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000819 }
820
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700821 /* Check to see if we need an address-size prefix */
822 add_asp(instruction, bits);
823
H. Peter Anvin23595f52009-07-25 17:44:25 -0700824 m = find_match(&temp, instruction, segment, offset, bits);
825 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400826 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700827 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700828 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400829 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000830 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000831}
832
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800833static void bad_hle_warn(const insn * ins, uint8_t hleok)
834{
835 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800836 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800837 static const enum whatwarn warn[2][4] =
838 {
839 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
840 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
841 };
842 unsigned int n;
843
844 n = (unsigned int)rep_pfx - P_XACQUIRE;
845 if (n > 1)
846 return; /* Not XACQUIRE/XRELEASE */
847
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800848 ww = warn[n][hleok];
849 if (!is_class(MEMORY, ins->oprs[0].type))
850 ww = w_inval; /* HLE requires operand 0 to be memory */
851
852 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800853 case w_none:
854 break;
855
856 case w_lock:
857 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800858 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800859 "%s with this instruction requires lock",
860 prefix_name(rep_pfx));
861 }
862 break;
863
864 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800865 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800866 "%s invalid with this instruction",
867 prefix_name(rep_pfx));
868 break;
869 }
870}
871
H. Peter Anvin507ae032008-10-09 15:37:10 -0700872/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400873#define case3(x) case (x): case (x)+1: case (x)+2
874#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700875
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800876static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800877 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000878{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800879 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800880 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000881 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000882 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700883 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700884 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700885 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700886 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800887 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800888 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700889 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000890
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700891 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700892 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700893 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700894
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700895 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400896 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700897
H. Peter Anvine2c80182005-01-15 22:15:51 +0000898 (void)segment; /* Don't warn that this parameter is unused */
899 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000900
H. Peter Anvin839eca22007-10-29 23:12:47 -0700901 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400902 c = *codes++;
903 op1 = (c & 3) + ((opex & 1) << 2);
904 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
905 opx = &ins->oprs[op1];
906 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700907
H. Peter Anvin839eca22007-10-29 23:12:47 -0700908 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400909 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 codes += c, length += c;
911 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700912
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400913 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400914 opex = c;
915 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700916
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400917 case4(010):
918 ins->rex |=
919 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 codes++, length++;
921 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700922
Jin Kyu Song164d6072013-10-15 19:10:13 -0700923 case4(014):
924 /* this is an index reg of MIB operand */
925 mib_index = opx->basereg;
926 break;
927
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400928 case4(020):
929 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 length++;
931 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700932
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400933 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 length += 2;
935 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700936
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400937 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700938 if (opx->type & (BITS16 | BITS32 | BITS64))
939 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 else
941 length += (bits == 16) ? 2 : 4;
942 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700943
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400944 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000945 length += 4;
946 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700947
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400948 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700949 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700951
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400952 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 length++;
954 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700955
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400956 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000957 length += 8; /* MOV reg64/imm */
958 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700959
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400960 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 length += 2;
962 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700963
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400964 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700965 if (opx->type & (BITS16 | BITS32 | BITS64))
966 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 else
968 length += (bits == 16) ? 2 : 4;
969 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700970
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400971 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000972 length += 4;
973 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700974
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400975 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700976 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700978
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400979 case 0172:
980 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400981 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700982 length++;
983 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700984
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700985 case4(0174):
986 length++;
987 break;
988
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700989 case4(0240):
990 ins->rex |= REX_EV;
991 ins->vexreg = regval(opx);
992 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
993 ins->vex_cm = *codes++;
994 ins->vex_wlp = *codes++;
995 ins->evex_tuple = (*codes++ - 0300);
996 break;
997
998 case 0250:
999 ins->rex |= REX_EV;
1000 ins->vexreg = 0;
1001 ins->vex_cm = *codes++;
1002 ins->vex_wlp = *codes++;
1003 ins->evex_tuple = (*codes++ - 0300);
1004 break;
1005
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001006 case4(0254):
1007 length += 4;
1008 break;
1009
1010 case4(0260):
1011 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001012 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001013 ins->vex_cm = *codes++;
1014 ins->vex_wlp = *codes++;
1015 break;
1016
1017 case 0270:
1018 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001019 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001020 ins->vex_cm = *codes++;
1021 ins->vex_wlp = *codes++;
1022 break;
1023
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001024 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001025 hleok = c & 3;
1026 break;
1027
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001028 case4(0274):
1029 length++;
1030 break;
1031
1032 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001034
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001036 if (bits == 64)
1037 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001038 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001040
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001042 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001044
H. Peter Anvine2c80182005-01-15 22:15:51 +00001045 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001046 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001047
Keith Kaniosb7a89542007-04-12 02:40:54 +00001048 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001049 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1050 has_prefix(ins, PPS_ASIZE, P_A32))
1051 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001052 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001053
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001054 case4(0314):
1055 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001056
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001058 {
1059 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1060 if (pfx == P_O16)
1061 break;
1062 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001063 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001064 else
1065 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001067 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001068
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001070 {
1071 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1072 if (pfx == P_O32)
1073 break;
1074 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001075 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001076 else
1077 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001079 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001080
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 case 0322:
1082 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001083
Keith Kaniosb7a89542007-04-12 02:40:54 +00001084 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001085 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001086 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001087
Keith Kaniosb7a89542007-04-12 02:40:54 +00001088 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001089 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001090 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001091
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001092 case 0325:
1093 ins->rex |= REX_NH;
1094 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001095
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001096 case 0326:
1097 break;
1098
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 case 0330:
1100 codes++, length++;
1101 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001102
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001105
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001106 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 case 0333:
1108 length++;
1109 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001110
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001111 case 0334:
1112 ins->rex |= REX_L;
1113 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001114
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001115 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001116 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001117
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001118 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001119 if (!ins->prefixes[PPS_REP])
1120 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001121 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001122
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001123 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001124 if (!ins->prefixes[PPS_REP])
1125 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001126 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001127
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001130 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001132 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1133 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001134 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001136 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001138
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001139 case 0341:
1140 if (!ins->prefixes[PPS_WAIT])
1141 ins->prefixes[PPS_WAIT] = P_WAIT;
1142 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001143
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001144 case 0360:
1145 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001146
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001147 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001148 length++;
1149 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001150
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001151 case 0364:
1152 case 0365:
1153 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001154
Keith Kanios48af1772007-08-17 07:37:52 +00001155 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001156 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001157 length++;
1158 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001159
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001160 case 0370:
1161 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001163
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 case 0373:
1165 length++;
1166 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001167
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001168 case 0374:
1169 eat = EA_XMMVSIB;
1170 break;
1171
1172 case 0375:
1173 eat = EA_YMMVSIB;
1174 break;
1175
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001176 case 0376:
1177 eat = EA_ZMMVSIB;
1178 break;
1179
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001180 case4(0100):
1181 case4(0110):
1182 case4(0120):
1183 case4(0130):
1184 case4(0200):
1185 case4(0204):
1186 case4(0210):
1187 case4(0214):
1188 case4(0220):
1189 case4(0224):
1190 case4(0230):
1191 case4(0234):
1192 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001194 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001195 opflags_t rflags;
1196 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001197 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001198
Keith Kaniosb7a89542007-04-12 02:40:54 +00001199 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001200
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001201 if (c <= 0177) {
1202 /* pick rfield from operand b (opx) */
1203 rflags = regflag(opx);
1204 rfield = nasm_regvals[opx->basereg];
1205 } else {
1206 rflags = 0;
1207 rfield = c & 7;
1208 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001209
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001210 /* EVEX.b1 : evex_brerop contains the operand position */
1211 op_er_sae = (ins->evex_brerop >= 0 ?
1212 &ins->oprs[ins->evex_brerop] : NULL);
1213
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001214 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1215 /* set EVEX.b */
1216 ins->evex_p[2] |= EVEX_P2B;
1217 if (op_er_sae->decoflags & ER) {
1218 /* set EVEX.RC (rounding control) */
1219 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1220 & EVEX_P2RC;
1221 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001222 } else {
1223 /* set EVEX.L'L (vector length) */
1224 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001225 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001226 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001227 /* set EVEX.b */
1228 ins->evex_p[2] |= EVEX_P2B;
1229 }
1230 }
1231
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001232 if (itemp_has(temp, IF_MIB)) {
1233 opy->eaflags |= EAF_MIB;
1234 /*
1235 * if a separate form of MIB (ICC style) is used,
1236 * the index reg info is merged into mem operand
1237 */
1238 if (mib_index != R_none) {
1239 opy->indexreg = mib_index;
1240 opy->scale = 1;
1241 opy->hintbase = mib_index;
1242 opy->hinttype = EAH_NOTBASE;
1243 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001244 }
1245
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001246 if (process_ea(opy, &ea_data, bits,
1247 rfield, rflags, ins) != eat) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001248 nasm_error(ERR_NONFATAL, "invalid effective address");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001250 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001251 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001253 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001254 }
1255 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001256
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001257 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001258 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001259 ": instruction code \\%o (0x%02X) given", c, c);
1260 break;
1261 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001262 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001263
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001264 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001265
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001266 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001267 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001268 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001269 return -1;
1270 }
1271 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001272 }
1273
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001274 switch (ins->prefixes[PPS_VEX]) {
1275 case P_EVEX:
1276 if (!(ins->rex & REX_EV))
1277 return -1;
1278 break;
1279 case P_VEX3:
1280 case P_VEX2:
1281 if (!(ins->rex & REX_V))
1282 return -1;
1283 break;
1284 default:
1285 break;
1286 }
1287
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001288 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001289 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001290
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001291 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001292 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001293 return -1;
1294 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001295 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001296 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001297 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001298 ins->rex &= ~REX_W;
1299 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001300 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001301 ins->rex |= REX_W;
1302 bad32 &= ~REX_W;
1303 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001304 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001305 /* Follow REX_W */
1306 break;
1307 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001308
H. Peter Anvinfc561202011-07-07 16:58:22 -07001309 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001310 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001311 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001312 } else if (!(ins->rex & REX_EV) &&
1313 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001314 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001315 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001316 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001317 if (ins->rex & REX_EV)
1318 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001319 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1320 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001321 length += 3;
1322 else
1323 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001324 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001325 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001326 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001327 return -1;
1328 } else if (bits == 64) {
1329 length++;
1330 } else if ((ins->rex & REX_L) &&
1331 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001332 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001333 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001334 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001335 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001336 length++;
1337 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001338 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001339 return -1;
1340 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001341 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001342
1343 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001344 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001345 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001346 "instruction is not lockable");
1347 }
1348
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001349 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001350
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001351 /*
1352 * when BND prefix is set by DEFAULT directive,
1353 * BND prefix is added to every appropriate instruction line
1354 * unless it is overridden by NOBND prefix.
1355 */
1356 if (globalbnd &&
1357 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1358 ins->prefixes[PPS_REP] = P_BND;
1359
H. Peter Anvina77692b2016-09-20 14:04:33 -07001360 /*
1361 * Add length of legacy prefixes
1362 */
1363 length += emit_prefix(NULL, bits, ins);
1364
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001365 return length;
1366}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001367
H. Peter Anvina77692b2016-09-20 14:04:33 -07001368static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001369{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001370 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001371 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001372 !(ins->rex & (REX_V | REX_EV)) &&
1373 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001374 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1375 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001376 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001377 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001378 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001379}
1380
H. Peter Anvina77692b2016-09-20 14:04:33 -07001381static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1382{
1383 int bytes = 0;
1384 int j;
1385
1386 for (j = 0; j < MAXPREFIX; j++) {
1387 uint8_t c = 0;
1388 switch (ins->prefixes[j]) {
1389 case P_WAIT:
1390 c = 0x9B;
1391 break;
1392 case P_LOCK:
1393 c = 0xF0;
1394 break;
1395 case P_REPNE:
1396 case P_REPNZ:
1397 case P_XACQUIRE:
1398 case P_BND:
1399 c = 0xF2;
1400 break;
1401 case P_REPE:
1402 case P_REPZ:
1403 case P_REP:
1404 case P_XRELEASE:
1405 c = 0xF3;
1406 break;
1407 case R_CS:
1408 if (bits == 64) {
1409 nasm_error(ERR_WARNING | ERR_PASS2,
1410 "cs segment base generated, but will be ignored in 64-bit mode");
1411 }
1412 c = 0x2E;
1413 break;
1414 case R_DS:
1415 if (bits == 64) {
1416 nasm_error(ERR_WARNING | ERR_PASS2,
1417 "ds segment base generated, but will be ignored in 64-bit mode");
1418 }
1419 c = 0x3E;
1420 break;
1421 case R_ES:
1422 if (bits == 64) {
1423 nasm_error(ERR_WARNING | ERR_PASS2,
1424 "es segment base generated, but will be ignored in 64-bit mode");
1425 }
1426 c = 0x26;
1427 break;
1428 case R_FS:
1429 c = 0x64;
1430 break;
1431 case R_GS:
1432 c = 0x65;
1433 break;
1434 case R_SS:
1435 if (bits == 64) {
1436 nasm_error(ERR_WARNING | ERR_PASS2,
1437 "ss segment base generated, but will be ignored in 64-bit mode");
1438 }
1439 c = 0x36;
1440 break;
1441 case R_SEGR6:
1442 case R_SEGR7:
1443 nasm_error(ERR_NONFATAL,
1444 "segr6 and segr7 cannot be used as prefixes");
1445 break;
1446 case P_A16:
1447 if (bits == 64) {
1448 nasm_error(ERR_NONFATAL,
1449 "16-bit addressing is not supported "
1450 "in 64-bit mode");
1451 } else if (bits != 16)
1452 c = 0x67;
1453 break;
1454 case P_A32:
1455 if (bits != 32)
1456 c = 0x67;
1457 break;
1458 case P_A64:
1459 if (bits != 64) {
1460 nasm_error(ERR_NONFATAL,
1461 "64-bit addressing is only supported "
1462 "in 64-bit mode");
1463 }
1464 break;
1465 case P_ASP:
1466 c = 0x67;
1467 break;
1468 case P_O16:
1469 if (bits != 16)
1470 c = 0x66;
1471 break;
1472 case P_O32:
1473 if (bits == 16)
1474 c = 0x66;
1475 break;
1476 case P_O64:
1477 /* REX.W */
1478 break;
1479 case P_OSP:
1480 c = 0x66;
1481 break;
1482 case P_EVEX:
1483 case P_VEX3:
1484 case P_VEX2:
1485 case P_NOBND:
1486 case P_none:
1487 break;
1488 default:
1489 nasm_panic(0, "invalid instruction prefix");
1490 }
1491 if (c) {
1492 if (data)
1493 out_rawbyte(data, c);
1494 bytes++;
1495 }
1496 }
1497 return bytes;
1498}
1499
1500static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001501{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001502 uint8_t c;
1503 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001504 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001505 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001506 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001507 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001508 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001509 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001510 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001511 const int bits = data->bits;
H. Peter Anvin70653092007-10-19 14:42:29 -07001512
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001513 ins->rex_done = false;
1514
H. Peter Anvina77692b2016-09-20 14:04:33 -07001515 emit_prefix(data, bits, ins);
1516
H. Peter Anvin839eca22007-10-29 23:12:47 -07001517 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001518 c = *codes++;
1519 op1 = (c & 3) + ((opex & 1) << 2);
1520 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1521 opx = &ins->oprs[op1];
1522 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001523
H. Peter Anvina77692b2016-09-20 14:04:33 -07001524
H. Peter Anvin839eca22007-10-29 23:12:47 -07001525 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001526 case 01:
1527 case 02:
1528 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001529 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001530 emit_rex(data, ins);
1531 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001533 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001534
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001535 case 05:
1536 case 06:
1537 case 07:
1538 opex = c;
1539 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001540
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001541 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001542 emit_rex(data, ins);
1543 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001544 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001545
Jin Kyu Song164d6072013-10-15 19:10:13 -07001546 case4(014):
1547 break;
1548
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001549 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001550 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001551 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001552 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001553 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001554 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001555
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001556 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001557 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001558 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001559 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001560 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001562
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001563 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001564 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001565 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001567
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001568 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001569 if (opx->type & (BITS16 | BITS32))
1570 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001571 else
1572 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001573 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001574 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001576
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001577 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001578 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001579 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001581
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001582 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001583 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001584 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001585 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001587
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001588 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001589 if (opx->segment == data->segment) {
1590 int64_t delta = opx->offset - data->offset
1591 - (data->inslen - data->insoffs);
1592 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001593 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001594 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001595 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001597
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001598 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001599 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001600 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001601
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001602 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001603 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001605
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001606 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001607 if (opx->type & (BITS16 | BITS32 | BITS64))
1608 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 else
1610 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001611
1612 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001614
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001615 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001616 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001618
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001619 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001620 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001621 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001622 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001623 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001625
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001626 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001627 {
1628 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1629 const struct operand *opy;
1630
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001631 c = *codes++;
1632 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001633 opy = &ins->oprs[c & 7];
1634 if (opy->segment != NO_SEG || opy->wrt != NO_SEG) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001635 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001636 "non-absolute expression not permitted as argument %d",
1637 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001638 } else if (opy->offset & ~mask) {
1639 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1640 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001641 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001642 c = opy->offset & mask;
1643 goto emit_is4;
1644 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001645
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001646 case 0173:
1647 c = *codes++;
1648 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001649 c &= 15;
1650 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001651
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001652 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001653 c = 0;
1654 emit_is4:
1655 r = nasm_regvals[opx->basereg];
1656 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001657 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001658
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001659 case4(0254):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001660 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001661 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001662 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001663 "signed dword immediate exceeds bounds");
1664 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001665 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001666 break;
1667
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001668 case4(0240):
1669 case 0250:
1670 codes += 3;
1671 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1672 EVEX_P2Z | EVEX_P2AAA, 2);
1673 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1674 bytes[0] = 0x62;
1675 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001676 bytes[1] = ((((ins->rex & 7) << 5) |
1677 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001678 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001679 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1680 ((~ins->vexreg & 15) << 3) |
1681 (1 << 2) | (ins->vex_wlp & 3);
1682 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001683 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001684 break;
1685
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001686 case4(0260):
1687 case 0270:
1688 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001689 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1690 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001691 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1692 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1693 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001694 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001695 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001696 } else {
1697 bytes[0] = 0xc5;
1698 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001699 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001700 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001701 }
1702 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001703
H. Peter Anvine014f352012-02-25 22:35:19 -08001704 case 0271:
1705 case 0272:
1706 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001707 break;
1708
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001709 case4(0274):
1710 {
1711 uint64_t uv, um;
1712 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001713
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001714 if (ins->rex & REX_W)
1715 s = 64;
1716 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1717 s = 16;
1718 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1719 s = 32;
1720 else
1721 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001722
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001723 um = (uint64_t)2 << (s-1);
1724 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001725
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001726 if (uv > 127 && uv < (uint64_t)-128 &&
1727 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001728 /* If this wasn't explicitly byte-sized, warn as though we
1729 * had fallen through to the imm16/32/64 case.
1730 */
H. Peter Anvin215186f2016-02-17 20:27:41 -08001731 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001732 "%s value exceeds bounds",
1733 (opx->type & BITS8) ? "signed byte" :
1734 s == 16 ? "word" :
1735 s == 32 ? "dword" :
1736 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001737 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001738 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001739 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001740 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001741
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001742 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001744
H. Peter Anvine2c80182005-01-15 22:15:51 +00001745 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001746 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1747 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001749
H. Peter Anvine2c80182005-01-15 22:15:51 +00001750 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001751 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1752 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001754
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 case 0312:
1756 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001757
Keith Kaniosb7a89542007-04-12 02:40:54 +00001758 case 0313:
1759 ins->rex = 0;
1760 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001761
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001762 case4(0314):
1763 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001764
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001768
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001770 case 0323:
1771 break;
1772
Keith Kaniosb7a89542007-04-12 02:40:54 +00001773 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001774 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001776
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001777 case 0325:
1778 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001779
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001780 case 0326:
1781 break;
1782
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001784 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001786
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001789
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001790 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001792 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001794
Keith Kanios48af1772007-08-17 07:37:52 +00001795 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001796 if (ins->rex & REX_R)
1797 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001798 ins->rex &= ~(REX_L|REX_R);
1799 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001800
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001801 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001802 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001803
H. Peter Anvin962e3052008-08-28 17:47:16 -07001804 case 0336:
1805 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001806 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001807
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001810 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001811
1812 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001814
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001815 case 0341:
1816 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001817
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001818 case 0360:
1819 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001820
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001821 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001822 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001823 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001824
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001825 case 0364:
1826 case 0365:
1827 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001828
Keith Kanios48af1772007-08-17 07:37:52 +00001829 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001830 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001831 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001832 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001833
Jin Kyu Song03041092013-10-15 19:38:51 -07001834 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001836
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001838 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001840
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001841 case 0374:
1842 eat = EA_XMMVSIB;
1843 break;
1844
1845 case 0375:
1846 eat = EA_YMMVSIB;
1847 break;
1848
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001849 case 0376:
1850 eat = EA_ZMMVSIB;
1851 break;
1852
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001853 case4(0100):
1854 case4(0110):
1855 case4(0120):
1856 case4(0130):
1857 case4(0200):
1858 case4(0204):
1859 case4(0210):
1860 case4(0214):
1861 case4(0220):
1862 case4(0224):
1863 case4(0230):
1864 case4(0234):
1865 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 ea ea_data;
1867 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001868 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001869 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001870 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001871
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001872 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001873 /* pick rfield from operand b (opx) */
1874 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001875 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001876 } else {
1877 /* rfield is constant */
1878 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001880 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001882 if (process_ea(opy, &ea_data, bits,
1883 rfield, rflags, ins) != eat)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001884 nasm_error(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001885
H. Peter Anvine2c80182005-01-15 22:15:51 +00001886 p = bytes;
1887 *p++ = ea_data.modrm;
1888 if (ea_data.sib_present)
1889 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001890 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001892 /*
1893 * Make sure the address gets the right offset in case
1894 * the line breaks in the .lst file (BR 1197827)
1895 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001896
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001897 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001898 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001899 if (ea_data.disp8) {
1900 out_rawbyte(data, ea_data.disp8);
1901 } else if (ea_data.rip) {
1902 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001903 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001904 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001905
H. Peter Anvina77692b2016-09-20 14:04:33 -07001906 if (overflow_general(opy->offset, asize) ||
1907 signed_bits(opy->offset, ins->addr_size) !=
1908 signed_bits(opy->offset, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001909 warn_overflow(ERR_PASS2, ea_data.bytes);
1910
H. Peter Anvina77692b2016-09-20 14:04:33 -07001911 out_imm(data, opy, ea_data.bytes,
1912 (asize > ea_data.bytes) ? OUT_SIGNED : OUT_UNSIGNED);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001913 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001915 }
1916 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001917
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001918 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001919 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001920 ": instruction code \\%o (0x%02X) given", c, c);
1921 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001923 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001924}
1925
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001926static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001927{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001928 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001929 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001930 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001931}
1932
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001933static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001934{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001935 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001936 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001937 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001938}
1939
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001940static int op_rexflags(const operand * o, int mask)
1941{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001942 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001943 int val;
1944
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001945 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001946 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001947
H. Peter Anvina4835d42008-05-20 14:21:29 -07001948 flags = nasm_reg_flags[o->basereg];
1949 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001950
1951 return rexflags(val, flags, mask);
1952}
1953
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001954static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001955{
1956 int rex = 0;
1957
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001958 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001959 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001960 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001961 rex |= REX_W;
1962 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1963 rex |= REX_H;
1964 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1965 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001966
1967 return rex & mask;
1968}
1969
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001970static int evexflags(int val, decoflags_t deco,
1971 int mask, uint8_t byte)
1972{
1973 int evex = 0;
1974
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001975 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001976 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001977 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001978 evex |= (EVEX_P0RP | EVEX_P0X);
1979 break;
1980 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001981 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001982 evex |= EVEX_P2VP;
1983 if (deco & Z)
1984 evex |= EVEX_P2Z;
1985 if (deco & OPMASK_MASK)
1986 evex |= deco & EVEX_P2AAA;
1987 break;
1988 }
1989 return evex & mask;
1990}
1991
1992static int op_evexflags(const operand * o, int mask, uint8_t byte)
1993{
1994 int val;
1995
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001996 val = nasm_regvals[o->basereg];
1997
1998 return evexflags(val, o->decoflags, mask, byte);
1999}
2000
H. Peter Anvin23595f52009-07-25 17:44:25 -07002001static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002002 insn *instruction,
2003 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002004{
2005 const struct itemplate *temp;
2006 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002007 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002008 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002009 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002010 int i;
2011
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002012 /* broadcasting uses a different data element size */
2013 for (i = 0; i < instruction->operands; i++)
2014 if (i == broadcast)
2015 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2016 else
2017 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002018
2019 merr = MERR_INVALOP;
2020
2021 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002022 temp->opcode != I_none; temp++) {
2023 m = matches(temp, instruction, bits);
2024 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002025 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002026 m = MOK_GOOD;
2027 else
2028 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002029 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002030 /*
2031 * Missing operand size and a candidate for fuzzy matching...
2032 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002033 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002034 if (i == broadcast)
2035 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2036 else
2037 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002038 opsizemissing = true;
2039 }
2040 if (m > merr)
2041 merr = m;
2042 if (merr == MOK_GOOD)
2043 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002044 }
2045
2046 /* No match, but see if we can get a fuzzy operand size match... */
2047 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002048 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002049
2050 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002051 /*
2052 * We ignore extrinsic operand sizes on registers, so we should
2053 * never try to fuzzy-match on them. This also resolves the case
2054 * when we have e.g. "xmmrm128" in two different positions.
2055 */
2056 if (is_class(REGISTER, instruction->oprs[i].type))
2057 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002058
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002059 /* This tests if xsizeflags[i] has more than one bit set */
2060 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2061 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002062
Jin Kyu Song7903c072013-10-30 03:00:12 -07002063 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002064 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002065 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2066 BITS32 : BITS64);
2067 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002068 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002069 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002070 }
2071
2072 /* Try matching again... */
2073 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002074 temp->opcode != I_none; temp++) {
2075 m = matches(temp, instruction, bits);
2076 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002077 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002078 m = MOK_GOOD;
2079 else
2080 m = MERR_INVALOP;
2081 }
2082 if (m > merr)
2083 merr = m;
2084 if (merr == MOK_GOOD)
2085 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002086 }
2087
H. Peter Anvina81655b2009-07-25 18:15:28 -07002088done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002089 *tempp = temp;
2090 return merr;
2091}
2092
Mark Charneydcaef4b2014-10-09 13:45:17 -04002093static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2094{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002095 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002096 uint8_t brcast_num;
2097
Mark Charneydcaef4b2014-10-09 13:45:17 -04002098 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002099 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002100 "size of broadcasting element is greater than 64 bits");
2101
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002102 /*
2103 * The shift term is to take care of the extra BITS80 inserted
2104 * between BITS64 and BITS128.
2105 */
2106 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2107 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002108
2109 return brcast_num;
2110}
2111
H. Peter Anvin65289e82009-07-25 17:25:11 -07002112static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002113 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002114{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002115 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002116 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002117 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002118
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002119 /*
2120 * Check the opcode
2121 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002122 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002123 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002124
2125 /*
2126 * Count the operands
2127 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002128 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002129 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002130
2131 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002132 * Is it legal?
2133 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002134 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002135 return MERR_INVALOP;
2136
2137 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002138 * {evex} available?
2139 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002140 switch (instruction->prefixes[PPS_VEX]) {
2141 case P_EVEX:
2142 if (!itemp_has(itemp, IF_EVEX))
2143 return MERR_ENCMISMATCH;
2144 break;
2145 case P_VEX3:
2146 case P_VEX2:
2147 if (!itemp_has(itemp, IF_VEX))
2148 return MERR_ENCMISMATCH;
2149 break;
2150 default:
2151 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002152 }
2153
2154 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002155 * Check that no spurious colons or TOs are present
2156 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002157 for (i = 0; i < itemp->operands; i++)
2158 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002159 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002160
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002161 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002162 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002163 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002164 switch (itemp_smask(itemp)) {
2165 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002166 asize = BITS8;
2167 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002168 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002169 asize = BITS16;
2170 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002171 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002172 asize = BITS32;
2173 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002174 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002175 asize = BITS64;
2176 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002177 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002178 asize = BITS128;
2179 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002180 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002181 asize = BITS256;
2182 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002183 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002184 asize = BITS512;
2185 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002186 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002187 switch (bits) {
2188 case 16:
2189 asize = BITS16;
2190 break;
2191 case 32:
2192 asize = BITS32;
2193 break;
2194 case 64:
2195 asize = BITS64;
2196 break;
2197 default:
2198 asize = 0;
2199 break;
2200 }
2201 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002202 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002203 asize = 0;
2204 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002205 }
2206
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002207 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002208 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002209 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002210 memset(size, 0, sizeof size);
2211 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002212 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002213 /* S- flags apply to all operands */
2214 for (i = 0; i < MAX_OPERANDS; i++)
2215 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002216 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002217
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002218 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002219 * Check that the operand flags all match up,
2220 * it's a bit tricky so lets be verbose:
2221 *
2222 * 1) Find out the size of operand. If instruction
2223 * doesn't have one specified -- we're trying to
2224 * guess it either from template (IF_S* flag) or
2225 * from code bits.
2226 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002227 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002228 * template has an operand size specified AND this size differ
2229 * from which instruction has (perhaps we got it from code bits)
2230 * we are:
2231 * a) Check that only size of instruction and operand is differ
2232 * other characteristics do match
2233 * b) Perhaps it's a register specified in instruction so
2234 * for such a case we just mark that operand as "size
2235 * missing" and this will turn on fuzzy operand size
2236 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002237 */
2238 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002239 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002240 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002241 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002242 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002243 opflags_t template_opsize, insn_opsize;
2244
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002245 if (!(type & SIZE_MASK))
2246 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002247
Jin Kyu Song7903c072013-10-30 03:00:12 -07002248 insn_opsize = type & SIZE_MASK;
2249 if (!is_broadcast) {
2250 template_opsize = itemp->opd[i] & SIZE_MASK;
2251 } else {
2252 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2253 /*
2254 * when broadcasting, the element size depends on
2255 * the instruction type. decorator flag should match.
2256 */
2257
2258 if (deco_brsize) {
2259 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002260 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002261 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002262 } else {
2263 template_opsize = 0;
2264 }
2265 }
2266
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002267 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002268 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002269 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002270 } else if (template_opsize) {
2271 if (template_opsize != insn_opsize) {
2272 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002273 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002274 } else if (!is_class(REGISTER, type)) {
2275 /*
2276 * Note: we don't honor extrinsic operand sizes for registers,
2277 * so "missing operand size" for a register should be
2278 * considered a wildcard match rather than an error.
2279 */
2280 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002281 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002282 } else if (is_broadcast &&
2283 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002284 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002285 /*
2286 * broadcasting opsize matches but the number of repeated memory
2287 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002288 * if 64b double precision float is broadcasted to ymm (256b),
2289 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002290 */
2291 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002292 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002293 }
2294 }
2295
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002296 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002297 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002298
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002299 /*
2300 * Check operand sizes
2301 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002302 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2303 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002304 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002305 asize = itemp->opd[i] & SIZE_MASK;
2306 if (asize) {
2307 for (i = 0; i < oprs; i++)
2308 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002309 break;
2310 }
2311 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002312 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002313 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002314 }
2315
Keith Kaniosb7a89542007-04-12 02:40:54 +00002316 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002317 if (!(itemp->opd[i] & SIZE_MASK) &&
2318 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002319 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002320 }
2321
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002322 /*
2323 * Check template is okay at the set cpu level
2324 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002325 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002326 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002327
Keith Kaniosb7a89542007-04-12 02:40:54 +00002328 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002329 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002330 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002331 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002332 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002333
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002334 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002335 * If we have a HLE prefix, look for the NOHLE flag
2336 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002337 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002338 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2339 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2340 return MERR_BADHLE;
2341
2342 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002343 * Check if special handling needed for Jumps
2344 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002345 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002346 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002347
Jin Kyu Song03041092013-10-15 19:38:51 -07002348 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002349 * Check if BND prefix is allowed.
2350 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002351 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002352 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002353 (has_prefix(instruction, PPS_REP, P_BND) ||
2354 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002355 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002356 else if (itemp_has(itemp, IF_BND) &&
2357 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2358 has_prefix(instruction, PPS_REP, P_REPNZ)))
2359 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002360
H. Peter Anvin60926242009-07-26 16:25:38 -07002361 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002362}
2363
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002364/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002365 * Check if ModR/M.mod should/can be 01.
2366 * - EAF_BYTEOFFS is set
2367 * - offset can fit in a byte when EVEX is not used
2368 * - offset can be compressed when EVEX is used
2369 */
2370#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2371 (o >= -128 && o <= 127 && \
2372 seg == NO_SEG && !forw_ref && \
2373 !(input->eaflags & EAF_WORDOFFS) && \
2374 !(ins->rex & REX_EV)) || \
2375 (ins->rex & REX_EV && \
2376 is_disp8n(input, ins, &output->disp8)))
2377
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002378static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002379 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002380{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002381 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002382 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002383 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002384
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002385 output->type = EA_SCALAR;
2386 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002387 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002388
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002389 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002390 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002391 /* EVEX.R' flag for the REG operand */
2392 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002393
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002394 if (is_class(REGISTER, input->type)) {
2395 /*
2396 * It's a direct register.
2397 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002398 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002399 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002400
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002401 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002402 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002403
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002404 /* broadcasting is not available with a direct register operand. */
2405 if (input->decoflags & BRDCAST_MASK) {
2406 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2407 goto err;
2408 }
2409
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002410 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002411 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002412 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002413 output->bytes = 0; /* no offset necessary either */
2414 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2415 } else {
2416 /*
2417 * It's a memory reference.
2418 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002419
2420 /* Embedded rounding or SAE is not available with a mem ref operand. */
2421 if (input->decoflags & (ER | SAE)) {
2422 nasm_error(ERR_NONFATAL,
2423 "Embedded rounding is available only with reg-reg op.");
2424 return -1;
2425 }
2426
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002427 if (input->basereg == -1 &&
2428 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002429 /*
2430 * It's a pure offset.
2431 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002432 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2433 input->segment == NO_SEG) {
2434 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2435 input->type &= ~IP_REL;
2436 input->type |= MEMORY;
2437 }
2438
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002439 if (bits == 64 &&
2440 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2441 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2442 return -1;
2443 }
2444
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002445 if (eaflags & EAF_BYTEOFFS ||
2446 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002447 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2448 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2449 }
2450
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002451 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002452 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002453 output->sib = GEN_SIB(0, 4, 5);
2454 output->bytes = 4;
2455 output->modrm = GEN_MODRM(0, rfield, 4);
2456 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002457 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002458 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002459 output->bytes = (addrbits != 16 ? 4 : 2);
2460 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2461 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002462 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002463 } else {
2464 /*
2465 * It's an indirection.
2466 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002467 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002468 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002469 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002470 int t, it, bt; /* register numbers */
2471 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002472
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 if (s == 0)
2474 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002475
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002476 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002477 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002478 ix = nasm_reg_flags[i];
2479 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002480 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002481 ix = 0;
2482 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002483
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002484 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002485 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002486 bx = nasm_reg_flags[b];
2487 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002488 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002489 bx = 0;
2490 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002491
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002492 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002493 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002494 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002495 int32_t o = input->offset;
2496 int mod, scale, index, base;
2497
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002498 /*
2499 * For a vector SIB, one has to be a vector and the other,
2500 * if present, a GPR. The vector must be the index operand.
2501 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002502 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002503 if (s == 0)
2504 s = 1;
2505 else if (s != 1)
2506 goto err;
2507
2508 t = bt, bt = it, it = t;
2509 x = bx, bx = ix, ix = x;
2510 }
2511
2512 if (bt != -1) {
2513 if (REG_GPR & ~bx)
2514 goto err;
2515 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2516 sok &= bx;
2517 else
2518 goto err;
2519 }
2520
2521 /*
2522 * While we're here, ensure the user didn't specify
2523 * WORD or QWORD
2524 */
2525 if (input->disp_size == 16 || input->disp_size == 64)
2526 goto err;
2527
2528 if (addrbits == 16 ||
2529 (addrbits == 32 && !(sok & BITS32)) ||
2530 (addrbits == 64 && !(sok & BITS64)))
2531 goto err;
2532
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002533 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2534 : ((ix & YMMREG & ~REG_EA)
2535 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002536
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002537 output->rex |= rexflags(it, ix, REX_X);
2538 output->rex |= rexflags(bt, bx, REX_B);
2539 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002540
2541 index = it & 7; /* it is known to be != -1 */
2542
2543 switch (s) {
2544 case 1:
2545 scale = 0;
2546 break;
2547 case 2:
2548 scale = 1;
2549 break;
2550 case 4:
2551 scale = 2;
2552 break;
2553 case 8:
2554 scale = 3;
2555 break;
2556 default: /* then what the smeg is it? */
2557 goto err; /* panic */
2558 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002559
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002560 if (bt == -1) {
2561 base = 5;
2562 mod = 0;
2563 } else {
2564 base = (bt & 7);
2565 if (base != REG_NUM_EBP && o == 0 &&
2566 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002567 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002568 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002569 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002570 mod = 1;
2571 else
2572 mod = 2;
2573 }
2574
2575 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002576 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2577 output->modrm = GEN_MODRM(mod, rfield, 4);
2578 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002579 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002580 /*
2581 * it must be a 32/64-bit memory reference. Firstly we have
2582 * to check that all registers involved are type E/Rxx.
2583 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002584 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002585 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002586
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002587 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002588 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2589 sok &= ix;
2590 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002591 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002592 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002593
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002594 if (bt != -1) {
2595 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002596 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002597 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002598 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002599 sok &= bx;
2600 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002601
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002602 /*
2603 * While we're here, ensure the user didn't specify
2604 * WORD or QWORD
2605 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002606 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002607 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002608
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002609 if (addrbits == 16 ||
2610 (addrbits == 32 && !(sok & BITS32)) ||
2611 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002612 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002613
Keith Kaniosb7a89542007-04-12 02:40:54 +00002614 /* now reorganize base/index */
2615 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002616 ((hb == b && ht == EAH_NOTBASE) ||
2617 (hb == i && ht == EAH_MAKEBASE))) {
2618 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002619 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002620 x = bx, bx = ix, ix = x;
2621 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002622
Jin Kyu Song164d6072013-10-15 19:10:13 -07002623 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002624 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002625 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002626 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002627 if (eaflags & EAF_MIB) {
2628 /* only for mib operands */
2629 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2630 /*
2631 * make a single reg index [reg*1].
2632 * gas uses this form for an explicit index register.
2633 */
2634 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2635 }
2636 if ((ht == EAH_SUMMED) && bt == -1) {
2637 /* separate once summed index into [base, index] */
2638 bt = it, bx = ix, s--;
2639 }
2640 } else {
2641 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002642 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002643 s == 3 || s == 5 || s == 9) && bt == -1) {
2644 /* convert 3*EAX to EAX+2*EAX */
2645 bt = it, bx = ix, s--;
2646 }
2647 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002648 (eaflags & EAF_TIMESTWO) &&
2649 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002650 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002651 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002652 * to sib format with 0x0 displacement - [EAX*1+0].
2653 */
2654 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2655 }
2656 }
Keith Kanios48af1772007-08-17 07:37:52 +00002657 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002658 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002659 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002660 x = ix, ix = bx, bx = x;
2661 }
2662 if (it == REG_NUM_ESP ||
2663 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002664 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002665
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002666 output->rex |= rexflags(it, ix, REX_X);
2667 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002668
Keith Kanios48af1772007-08-17 07:37:52 +00002669 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002670 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002671 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002672
Keith Kaniosb7a89542007-04-12 02:40:54 +00002673 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002674 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002676 } else {
2677 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002678 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002679 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002680 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002681 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002682 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002683 mod = 1;
2684 else
2685 mod = 2;
2686 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002687
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002688 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002689 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2690 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002691 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002692 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002694
Keith Kaniosb7a89542007-04-12 02:40:54 +00002695 if (it == -1)
2696 index = 4, s = 1;
2697 else
2698 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002699
H. Peter Anvine2c80182005-01-15 22:15:51 +00002700 switch (s) {
2701 case 1:
2702 scale = 0;
2703 break;
2704 case 2:
2705 scale = 1;
2706 break;
2707 case 4:
2708 scale = 2;
2709 break;
2710 case 8:
2711 scale = 3;
2712 break;
2713 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002714 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002716
Keith Kaniosb7a89542007-04-12 02:40:54 +00002717 if (bt == -1) {
2718 base = 5;
2719 mod = 0;
2720 } else {
2721 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002722 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002723 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002724 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002725 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002726 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002727 mod = 1;
2728 else
2729 mod = 2;
2730 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002731
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002732 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002733 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2734 output->modrm = GEN_MODRM(mod, rfield, 4);
2735 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002736 }
2737 } else { /* it's 16-bit */
2738 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002739 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002740
Keith Kaniosb7a89542007-04-12 02:40:54 +00002741 /* check for 64-bit long mode */
2742 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002743 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002744
H. Peter Anvine2c80182005-01-15 22:15:51 +00002745 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002746 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2747 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002748 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002749
Keith Kaniosb7a89542007-04-12 02:40:54 +00002750 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002751 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002752 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002753
H. Peter Anvine2c80182005-01-15 22:15:51 +00002754 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002755 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 if (b == -1 && i != -1) {
2757 int tmp = b;
2758 b = i;
2759 i = tmp;
2760 } /* swap */
2761 if ((b == R_SI || b == R_DI) && i != -1) {
2762 int tmp = b;
2763 b = i;
2764 i = tmp;
2765 }
2766 /* have BX/BP as base, SI/DI index */
2767 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002768 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002769 if (i != -1 && b != -1 &&
2770 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002771 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002772 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002773 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002774
H. Peter Anvine2c80182005-01-15 22:15:51 +00002775 rm = -1;
2776 if (i != -1)
2777 switch (i * 256 + b) {
2778 case R_SI * 256 + R_BX:
2779 rm = 0;
2780 break;
2781 case R_DI * 256 + R_BX:
2782 rm = 1;
2783 break;
2784 case R_SI * 256 + R_BP:
2785 rm = 2;
2786 break;
2787 case R_DI * 256 + R_BP:
2788 rm = 3;
2789 break;
2790 } else
2791 switch (b) {
2792 case R_SI:
2793 rm = 4;
2794 break;
2795 case R_DI:
2796 rm = 5;
2797 break;
2798 case R_BP:
2799 rm = 6;
2800 break;
2801 case R_BX:
2802 rm = 7;
2803 break;
2804 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002805 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002806 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002807
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002808 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002809 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002811 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812 mod = 1;
2813 else
2814 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002815
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002816 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002817 output->bytes = mod; /* bytes of offset needed */
2818 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002819 }
2820 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002821 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002822
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002823 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002824 return output->type;
2825
2826err:
2827 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002828}
2829
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002830static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002831{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002832 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002833 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002834
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002835 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002836
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002837 switch (ins->prefixes[PPS_ASIZE]) {
2838 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002839 valid &= 16;
2840 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002841 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002842 valid &= 32;
2843 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002844 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002845 valid &= 64;
2846 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002847 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002848 valid &= (addrbits == 32) ? 16 : 32;
2849 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002850 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002851 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002852 }
2853
2854 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002855 if (is_class(MEMORY, ins->oprs[j].type)) {
2856 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002857
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002858 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002859 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002860 i = 0;
2861 else
2862 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002863
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002864 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002865 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002866 b = 0;
2867 else
2868 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002869
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002870 if (ins->oprs[j].scale == 0)
2871 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002872
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002873 if (!i && !b) {
2874 int ds = ins->oprs[j].disp_size;
2875 if ((addrbits != 64 && ds > 8) ||
2876 (addrbits == 64 && ds == 16))
2877 valid &= ds;
2878 } else {
2879 if (!(REG16 & ~b))
2880 valid &= 16;
2881 if (!(REG32 & ~b))
2882 valid &= 32;
2883 if (!(REG64 & ~b))
2884 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002885
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002886 if (!(REG16 & ~i))
2887 valid &= 16;
2888 if (!(REG32 & ~i))
2889 valid &= 32;
2890 if (!(REG64 & ~i))
2891 valid &= 64;
2892 }
2893 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002894 }
2895
2896 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002897 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002898 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002899 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002900 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002901 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002902 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002903 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002904 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002906 }
2907
2908 defdisp = ins->addr_size == 16 ? 16 : 32;
2909
2910 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2912 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2913 /*
2914 * mem_offs sizes must match the address size; if not,
2915 * strip the MEM_OFFS bit and match only EA instructions
2916 */
2917 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2918 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002919 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002920}