blob: 78dbb60932dd89bf1b8f171f64e6edfbbe7b719a [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvind24dd5f2016-02-08 10:32:13 -08003 * Copyright 1996-2016 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040037 * Bytecode specification
38 * ----------------------
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070039 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040040 *
41 * Codes Mnemonic Explanation
42 *
43 * \0 terminates the code. (Unless it's a literal of course.)
44 * \1..\4 that many literal bytes follow in the code stream
45 * \5 add 4 to the primary operand number (b, low octdigit)
46 * \6 add 4 to the secondary operand number (a, middle octdigit)
47 * \7 add 4 to both the primary and the secondary operand number
48 * \10..\13 a literal byte follows in the code stream, to be added
49 * to the register value of operand 0..3
50 * \14..\17 the position of index register operand in MIB (BND insns)
51 * \20..\23 ib a byte immediate operand, from operand 0..3
52 * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
53 * \30..\33 iw a word immediate operand, from operand 0..3
54 * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
55 * assembly mode or the operand-size override on the operand
56 * \40..\43 id a long immediate operand, from operand 0..3
57 * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
58 * depending on the address size of the instruction.
59 * \50..\53 rel8 a byte relative operand, from operand 0..3
60 * \54..\57 iq a qword immediate operand, from operand 0..3
61 * \60..\63 rel16 a word relative operand, from operand 0..3
62 * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
63 * assembly mode or the operand-size override on the operand
64 * \70..\73 rel32 a long relative operand, from operand 0..3
65 * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
66 * \1ab a ModRM, calculated on EA in operand a, with the spare
67 * field the register value of operand b.
68 * \172\ab the register number from operand a in bits 7..4, with
69 * the 4-bit immediate from operand b in bits 3..0.
70 * \173\xab the register number from operand a in bits 7..4, with
71 * the value b in bits 3..0.
72 * \174..\177 the register number from operand 0..3 in bits 7..4, and
73 * an arbitrary value in bits 3..0 (assembled as zero.)
74 * \2ab a ModRM, calculated on EA in operand a, with the spare
75 * field equal to digit b.
76 *
77 * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
78 * V field taken from operand 0..3.
79 * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
80 * V field set to 1111b.
81 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070082 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -070084 * cc 00m mmm
85 * c = 2 for EVEX and mmmm is the M field (EVEX.P0[3:0])
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070086 * and wlp is:
87 * 00 wwl lpp
88 * [l0] ll = 0 (.128, .lz)
89 * [l1] ll = 1 (.256)
90 * [l2] ll = 2 (.512)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
92 *
93 * [w0] ww = 0 for W = 0
94 * [w1] ww = 1 for W = 1
95 * [wig] ww = 2 for W don't care (always assembled as 0)
96 * [ww] ww = 3 for W used as REX.W
97 *
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
100 * [f3] pp = 2
101 * [f2] pp = 3
102 *
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
105 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400106 * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
107 * \260..\263 this instruction uses VEX/XOP rather than REX, with the
108 * V field taken from operand 0..3.
109 * \270 this instruction uses VEX/XOP rather than REX, with the
110 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700111 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700114 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700115 * [l0] ll = 0 for L = 0 (.128, .lz)
116 * [l1] ll = 1 for L = 1 (.256)
117 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700118 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700119 * [w0] ww = 0 for W = 0
120 * [w1 ] ww = 1 for W = 1
121 * [wig] ww = 2 for W don't care (always assembled as 0)
122 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700123 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700125 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400126 * \271 hlexr instruction takes XRELEASE (F3) with or without lock
127 * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
128 * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
129 * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
130 * to the operand size (if o16/o32/o64 present) or the bit size
131 * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
132 * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
133 * \312 adf (disassembler only) invalid with non-default address size.
134 * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
135 * \314 norexb (disassembler only) invalid with REX.B
136 * \315 norexx (disassembler only) invalid with REX.X
137 * \316 norexr (disassembler only) invalid with REX.R
138 * \317 norexw (disassembler only) invalid with REX.W
139 * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
140 * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
141 * \322 odf indicates that this instruction is only valid when the
142 * operand size is the default (instruction to disassembler,
143 * generates no code in the assembler)
144 * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
145 * \324 o64 indicates 64-bit operand size requiring REX prefix.
146 * \325 nohi instruction which always uses spl/bpl/sil/dil
147 * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
148 disassembler only; for SSE instructions.
149 * \330 a literal byte follows in the code stream, to be added
150 * to the condition code value of the instruction.
151 * \331 norep instruction not valid with REP prefix. Hint for
152 * disassembler only; for SSE instructions.
153 * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
154 * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
155 * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
156 * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
157 * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
158 * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
159 * \336-\337 are still listed as prefixes in the disassembler.
160 * \340 resb reserve <operand 0> bytes of uninitialized storage.
161 * Operand 0 had better be a segmentless constant.
162 * \341 wait this instruction needs a WAIT "prefix"
Cyrill Gorcunov8a5d3e62014-08-25 20:04:30 +0400163 * \360 np no SSE prefix (== \364\331)
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400164 * \361 66 SSE prefix (== \366\331)
165 * \364 !osp operand-size prefix (0x66) not permitted
166 * \365 !asp address-size prefix (0x67) not permitted
167 * \366 operand-size prefix (0x66) used as opcode extension
168 * \367 address-size prefix (0x67) used as opcode extension
169 * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
170 * jmp8 370 is used for Jcc, 371 is used for JMP.
171 * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
172 * used for conditional jump over longer jump
173 * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
174 * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
175 * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 */
177
H. Peter Anvinfe501952007-10-02 21:53:51 -0700178#include "compiler.h"
179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180#include <stdio.h>
181#include <string.h>
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800182#include <stdlib.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000183
184#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000185#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000186#include "assemble.h"
187#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700188#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800189#include "disp8.h"
H. Peter Anvin172b8402016-02-18 01:16:18 -0800190#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000191
H. Peter Anvin65289e82009-07-25 17:25:11 -0700192enum match_result {
193 /*
194 * Matching errors. These should be sorted so that more specific
195 * errors come later in the sequence.
196 */
197 MERR_INVALOP,
198 MERR_OPSIZEMISSING,
199 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700200 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700201 MERR_BADCPU,
202 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800203 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700204 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700205 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800206 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700207 /*
208 * Matching success; the conditional ones first
209 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400210 MOK_JUMP, /* Matching OK but needs jmp_match() */
211 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700212};
213
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700215 enum ea_type type; /* what kind of EA is this? */
216 int sib_present; /* is a SIB byte necessary? */
217 int bytes; /* # of bytes of offset needed */
218 int size; /* lazy - this is sib+bytes+1 */
219 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700220 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221} ea;
222
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400223#define GEN_SIB(scale, index, base) \
224 (((scale) << 6) | ((index) << 3) | ((base)))
225
226#define GEN_MODRM(mod, reg, rm) \
227 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
228
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400229static iflag_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000230
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800231static int64_t calcsize(int32_t, int64_t, int, insn *,
232 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700233static int emit_prefix(struct out_data *data, const int bits, insn *ins);
234static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700235static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400236 insn *instruction,
237 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700238static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700239static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000240static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700241static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000242static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700243static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700244static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000245
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700246static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700247
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400248static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000249{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700250 return ins->prefixes[pos] == prefix;
251}
252
253static void assert_no_prefix(insn * ins, enum prefix_pos pos)
254{
255 if (ins->prefixes[pos])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800256 nasm_error(ERR_NONFATAL, "invalid %s prefix",
257 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700258}
259
260static const char *size_name(int size)
261{
262 switch (size) {
263 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400264 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700265 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400266 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700267 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700269 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700271 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700273 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700275 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400276 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700277 case 64:
278 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700279 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400280 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000281 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700282}
283
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400284static void warn_overflow(int pass, int size)
285{
H. Peter Anvin215186f2016-02-17 20:27:41 -0800286 nasm_error(ERR_WARNING | pass | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400287 "%s data exceeds bounds", size_name(size));
288}
289
290static void warn_overflow_const(int64_t data, int size)
291{
292 if (overflow_general(data, size))
293 warn_overflow(ERR_PASS1, size);
294}
295
296static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700297{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100298 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400299 if (overflow_general(o->offset, size))
300 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700301 }
302}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400303
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000304/*
305 * This routine wrappers the real output format's output routine,
306 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800307 * generator at the same time, flatten unnecessary relocations,
308 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000309 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700310static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000311{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000312 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700313 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700314 int asize;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800315 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700316 union {
317 uint8_t b[8];
318 uint64_t q;
319 } xdata;
320 uint64_t size = data->size;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000321
H. Peter Anvina77692b2016-09-20 14:04:33 -0700322 if (!data->size)
323 return; /* Nothing to do */
324
325 switch (data->type) {
326 case OUT_ADDRESS:
327 asize = data->size;
328 nasm_assert(asize <= 8);
329 if (data->tsegment == NO_SEG && data->twrt == NO_SEG) {
330 /* Convert to RAWDATA */
331 /* XXX: check for overflow */
332 uint8_t *q = xdata.b;
333
334 WRITEADDR(q, data->toffset, asize);
335 data->data = xdata.b;
336 data->type = OUT_RAWDATA;
337 asize = 0; /* No longer an address */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400338 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700339 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -0700340
H. Peter Anvina77692b2016-09-20 14:04:33 -0700341 case OUT_RELADDR:
342 asize = data->size;
343 nasm_assert(asize <= 8);
344 if (data->tsegment == data->segment && data->twrt == NO_SEG) {
345 /* Convert to RAWDATA */
346 uint8_t *q = xdata.b;
347 int64_t delta = data->toffset - data->offset
348 - (data->inslen - data->insoffs);
349
350 if (overflow_signed(delta, asize))
351 warn_overflow(ERR_PASS2, asize);
352
353 WRITEADDR(q, delta, asize);
354 data->data = xdata.b;
355 data->type = OUT_RAWDATA;
356 asize = 0; /* No longer an address */
357 }
358 break;
359
360 default:
361 asize = 0; /* Not an address */
362 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000363 }
364
H. Peter Anvina77692b2016-09-20 14:04:33 -0700365 lfmt->output(data);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800366
Frank Kotlerabebb082003-09-06 04:45:37 +0000367 /*
368 * this call to src_get determines when we call the
369 * debug-format-specific "linenum" function
370 * it updates lineno and lnfname to the current values
371 * returning 0 if "same as last time", -2 if lnfname
372 * changed, and the amount by which lineno changed,
373 * if it did. thus, these variables must be static
374 */
375
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400376 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700377 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000378
H. Peter Anvinb6412502016-02-11 21:07:40 -0800379 if (asize && asize > amax) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700380 if (data->type != OUT_ADDRESS || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800381 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800382 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800383 asize << 3, ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800384 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800385 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800386 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800387 asize << 3, ofmt->maxbits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700388 data->size = amax;
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700389 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700390 data->insoffs += amax;
391 data->offset += amax;
392 data->size = size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800393 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700394 data->data = zero_buffer;
395 data->type = OUT_RAWDATA;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800396 }
397
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700398 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700399 data->offset += size;
400 data->insoffs += size;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000401}
402
H. Peter Anvina77692b2016-09-20 14:04:33 -0700403static inline void out_rawdata(struct out_data *data, const void *rawdata,
404 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400405{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700406 data->type = OUT_RAWDATA;
407 data->data = rawdata;
408 data->size = size;
409 out(data);
410}
411
412static void out_rawbyte(struct out_data *data, uint8_t byte)
413{
414 data->type = OUT_RAWDATA;
415 data->data = &byte;
416 data->size = 1;
417 out(data);
418}
419
420static inline void out_reserve(struct out_data *data, uint64_t size)
421{
422 data->type = OUT_RESERVE;
423 data->size = size;
424 out(data);
425}
426
427static inline void out_imm(struct out_data *data, struct operand *opx,
428 int size, enum out_sign sign)
429{
430 data->type = OUT_ADDRESS;
431 data->sign = sign;
432 data->size = size;
433 data->toffset = opx->offset;
434 data->tsegment = opx->segment;
435 data->twrt = opx->wrt;
436 out(data);
437}
438
439static inline void out_reladdr(struct out_data *data, struct operand *opx,
440 int size)
441{
442 data->type = OUT_RELADDR;
443 data->sign = OUT_SIGNED;
444 data->size = size;
445 data->toffset = opx->offset;
446 data->tsegment = opx->segment;
447 data->twrt = opx->wrt;
448 out(data);
449}
450
451static inline void out_segment(struct out_data *data, struct operand *opx)
452{
453 data->type = OUT_SEGMENT;
454 data->sign = OUT_UNSIGNED;
455 data->size = 2;
456 data->toffset = opx->offset;
457 data->tsegment = ofmt->segbase(opx->segment + 1);
458 data->twrt = opx->wrt;
459 out(data);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400460}
461
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700462static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800463 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000464{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800465 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800466 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000467 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800468 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000469
H. Peter Anvin755f5212012-02-25 11:41:34 -0800470 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700471 return false;
472 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400473 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700474 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400475 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700476
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800477 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100478
Victor van den Elzen154e5922009-02-25 17:32:00 +0100479 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100480 /* Be optimistic in pass 1 */
481 return true;
482
H. Peter Anvine2c80182005-01-15 22:15:51 +0000483 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700484 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000485
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700486 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800487 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
488
489 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
490 /* jmp short (opcode eb) cannot be used with bnd prefix. */
491 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800492 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800493 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800494 }
495
496 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000497}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000498
H. Peter Anvin04445362016-09-21 15:56:19 -0700499/* This is totally just a wild guess what is reasonable... */
500#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
501
H. Peter Anvina77692b2016-09-20 14:04:33 -0700502int64_t assemble(int32_t segment, int64_t start, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800503 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000504{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700505 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000506 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700507 enum match_result m;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000508 int32_t itimes;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300509 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000510
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000511 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000512
H. Peter Anvina77692b2016-09-20 14:04:33 -0700513 data.offset = start;
514 data.segment = segment;
515 data.itemp = NULL;
516 data.sign = OUT_WRAP;
517 data.bits = bits;
518
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300519 wsize = idata_bytes(instruction->opcode);
520 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000522
H. Peter Anvineba20a72002-04-30 20:53:55 +0000523 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000524 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000525 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 if (t < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800527 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000528
H. Peter Anvine2c80182005-01-15 22:15:51 +0000529 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400530 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000531 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400532 if (wsize > 8) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800533 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400534 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000535 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700536 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700537 data.insoffs = 0;
538 data.type = OUT_ADDRESS;
539 data.inslen = data.size = wsize;
540 data.toffset = e->offset;
541 data.tsegment = e->segment;
542 data.twrt = e->wrt;
543 out(&data);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400544 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700545 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400546 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700547 int align = e->stringlen % wsize;
548 if (align)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 align = wsize - align;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700550
551 data.insoffs = 0;
552 data.inslen = e->stringlen + align;
553
554 out_rawdata(&data, e->stringval, e->stringlen);
555 out_rawdata(&data, zero_buffer, align);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 }
557 }
558 if (t > 0 && t == instruction->times - 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700559 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800560 lfmt->uplevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 }
562 }
563 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800564 lfmt->downlevel(LIST_TIMES);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700565 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700566 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567 FILE *fp;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700568 size_t t = instruction->times;
569 off_t base = 0;
570 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700571 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700572 char *buf = NULL;
573 size_t blk = 0; /* Buffered I/O block size */
574 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000575
H. Peter Anvind81a2352016-09-21 14:03:18 -0700576 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400577 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800578 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700580 goto done;
581 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000582
H. Peter Anvind81a2352016-09-21 14:03:18 -0700583 len = nasm_file_size(fp);
584
585 if (len == (off_t)-1) {
586 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700587 fname);
588 goto close_done;
589 }
590
H. Peter Anvina77692b2016-09-20 14:04:33 -0700591 if (instruction->eops->next) {
592 base = instruction->eops->next->offset;
593 if (base >= len) {
594 len = 0;
595 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000596 len -= base;
597 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700598 len > (off_t)instruction->eops->next->next->offset)
599 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700602
H. Peter Anvina77692b2016-09-20 14:04:33 -0700603 lfmt->set_offset(data.offset);
604 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000605
H. Peter Anvind81a2352016-09-21 14:03:18 -0700606 if (!len)
607 goto end_incbin;
608
609 /* Try to map file data */
610 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700611 if (!map) {
612 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
613 buf = nasm_malloc(blk);
614 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700615
616 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700617 /*
618 * Consider these irrelevant for INCBIN, since it is fully
619 * possible that these might be (way) bigger than an int
620 * can hold; there is, however, no reason to widen these
621 * types just for INCBIN. data.inslen == 0 signals to the
622 * backend that these fields are meaningless, if at all
623 * needed.
624 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700625 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700626 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700627
H. Peter Anvind81a2352016-09-21 14:03:18 -0700628 if (map) {
629 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700630 } else if ((off_t)m == len) {
631 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700632 } else {
633 off_t l = len;
634
635 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700636 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700637 "`incbin': unable to seek on file `%s'",
638 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700639 goto end_incbin;
640 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700641 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700642 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700643 if (!m || feof(fp)) {
644 /*
645 * This shouldn't happen unless the file
646 * actually changes while we are reading
647 * it.
648 */
649 nasm_error(ERR_NONFATAL,
650 "`incbin': unexpected EOF while"
651 " reading file `%s'", fname);
652 goto end_incbin;
653 }
654 out_rawdata(&data, buf, m);
655 l -= m;
656 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700657 }
658 }
659 end_incbin:
660 lfmt->downlevel(LIST_INCBIN);
661 if (instruction->times > 1) {
662 lfmt->set_offset(data.offset);
663 lfmt->uplevel(LIST_TIMES);
664 lfmt->downlevel(LIST_TIMES);
665 }
666 if (ferror(fp)) {
667 nasm_error(ERR_NONFATAL,
668 "`incbin': error while"
669 " reading file `%s'", fname);
670 }
671 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700672 if (buf)
673 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700674 if (map)
675 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700676 fclose(fp);
677 done:
678 ;
679 } else {
680 /* "Real" instruction */
681
682 /* Check to see if we need an address-size prefix */
683 add_asp(instruction, bits);
684
685 m = find_match(&temp, instruction, data.segment, data.offset, bits);
686
687 if (m == MOK_GOOD) {
688 /* Matches! */
689 int64_t insn_size = calcsize(data.segment, data.offset,
690 bits, instruction, temp);
691 itimes = instruction->times;
692 if (insn_size < 0) /* shouldn't be, on pass two */
693 nasm_panic(0, "errors made it through from pass one");
694
695 data.itemp = temp;
696 data.bits = bits;
697
698 while (itimes--) {
699 data.insoffs = 0;
700 data.inslen = insn_size;
701
702 gencode(&data, instruction);
703 nasm_assert(data.insoffs == insn_size);
704
705 if (itimes > 0 && itimes == instruction->times - 1) {
706 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800707 lfmt->uplevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400708 }
709 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700710 if (instruction->times > 1)
711 lfmt->downlevel(LIST_TIMES);
712 } else {
713 /* No match */
714 switch (m) {
715 case MERR_OPSIZEMISSING:
716 nasm_error(ERR_NONFATAL, "operation size not specified");
717 break;
718 case MERR_OPSIZEMISMATCH:
719 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
720 break;
721 case MERR_BRNUMMISMATCH:
722 nasm_error(ERR_NONFATAL,
723 "mismatch in the number of broadcasting elements");
724 break;
725 case MERR_BADCPU:
726 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
727 break;
728 case MERR_BADMODE:
729 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
730 bits);
731 break;
732 case MERR_ENCMISMATCH:
733 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
734 break;
735 case MERR_BADBND:
736 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
737 break;
738 case MERR_BADREPNE:
739 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
740 (has_prefix(instruction, PPS_REP, P_REPNE) ?
741 "repne" : "repnz"));
742 break;
743 default:
744 nasm_error(ERR_NONFATAL,
745 "invalid combination of opcode and operands");
746 break;
747 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400748 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000749 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700750 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000751}
752
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400753int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800754 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000755{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000756 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700757 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000758
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000759 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000760
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400761 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000762 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000763
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700764 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
765 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400766 instruction->opcode == I_DT || instruction->opcode == I_DO ||
767 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000768 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300769 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000770
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300772 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000773
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400774 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000775 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000776
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400778 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400780 warn_overflow_const(e->offset, wsize);
781 } else if (e->type == EOT_DB_STRING ||
782 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000783 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000784
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785 align = (-osize) % wsize;
786 if (align < 0)
787 align += wsize;
788 isize += osize + align;
789 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700790 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000791 }
792
H. Peter Anvine2c80182005-01-15 22:15:51 +0000793 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400794 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700795 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000796
H. Peter Anvind81a2352016-09-21 14:03:18 -0700797 len = nasm_file_size_by_path(fname);
798 if (len == (off_t)-1) {
799 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
800 fname);
801 return 0;
802 }
803
804 if (instruction->eops->next) {
805 if (len <= (off_t)instruction->eops->next->offset) {
806 len = 0;
807 } else {
808 len -= instruction->eops->next->offset;
809 if (instruction->eops->next->next &&
810 len > (off_t)instruction->eops->next->next->offset) {
811 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 }
813 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700815
816 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000817 }
818
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700819 /* Check to see if we need an address-size prefix */
820 add_asp(instruction, bits);
821
H. Peter Anvin23595f52009-07-25 17:44:25 -0700822 m = find_match(&temp, instruction, segment, offset, bits);
823 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400824 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700825 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700826 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400827 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000828 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000829}
830
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800831static void bad_hle_warn(const insn * ins, uint8_t hleok)
832{
833 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800834 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800835 static const enum whatwarn warn[2][4] =
836 {
837 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
838 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
839 };
840 unsigned int n;
841
842 n = (unsigned int)rep_pfx - P_XACQUIRE;
843 if (n > 1)
844 return; /* Not XACQUIRE/XRELEASE */
845
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800846 ww = warn[n][hleok];
847 if (!is_class(MEMORY, ins->oprs[0].type))
848 ww = w_inval; /* HLE requires operand 0 to be memory */
849
850 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800851 case w_none:
852 break;
853
854 case w_lock:
855 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800856 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800857 "%s with this instruction requires lock",
858 prefix_name(rep_pfx));
859 }
860 break;
861
862 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800863 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800864 "%s invalid with this instruction",
865 prefix_name(rep_pfx));
866 break;
867 }
868}
869
H. Peter Anvin507ae032008-10-09 15:37:10 -0700870/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400871#define case3(x) case (x): case (x)+1: case (x)+2
872#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700873
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800874static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800875 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000876{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800877 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800878 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000879 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000880 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700881 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700882 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700883 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700884 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800885 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800886 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700887 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000888
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700889 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700890 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700891 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700892
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700893 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400894 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700895
H. Peter Anvine2c80182005-01-15 22:15:51 +0000896 (void)segment; /* Don't warn that this parameter is unused */
897 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000898
H. Peter Anvin839eca22007-10-29 23:12:47 -0700899 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400900 c = *codes++;
901 op1 = (c & 3) + ((opex & 1) << 2);
902 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
903 opx = &ins->oprs[op1];
904 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700905
H. Peter Anvin839eca22007-10-29 23:12:47 -0700906 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400907 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000908 codes += c, length += c;
909 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700910
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400911 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400912 opex = c;
913 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700914
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400915 case4(010):
916 ins->rex |=
917 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000918 codes++, length++;
919 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700920
Jin Kyu Song164d6072013-10-15 19:10:13 -0700921 case4(014):
922 /* this is an index reg of MIB operand */
923 mib_index = opx->basereg;
924 break;
925
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400926 case4(020):
927 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000928 length++;
929 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700930
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400931 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000932 length += 2;
933 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700934
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400935 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700936 if (opx->type & (BITS16 | BITS32 | BITS64))
937 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 else
939 length += (bits == 16) ? 2 : 4;
940 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700941
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400942 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 length += 4;
944 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700945
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400946 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700947 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700949
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400950 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 length++;
952 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700953
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400954 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000955 length += 8; /* MOV reg64/imm */
956 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700957
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400958 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 length += 2;
960 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700961
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400962 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700963 if (opx->type & (BITS16 | BITS32 | BITS64))
964 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 else
966 length += (bits == 16) ? 2 : 4;
967 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700968
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400969 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 length += 4;
971 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700972
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400973 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700974 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700976
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400977 case 0172:
978 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400979 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700980 length++;
981 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700982
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700983 case4(0174):
984 length++;
985 break;
986
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700987 case4(0240):
988 ins->rex |= REX_EV;
989 ins->vexreg = regval(opx);
990 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
991 ins->vex_cm = *codes++;
992 ins->vex_wlp = *codes++;
993 ins->evex_tuple = (*codes++ - 0300);
994 break;
995
996 case 0250:
997 ins->rex |= REX_EV;
998 ins->vexreg = 0;
999 ins->vex_cm = *codes++;
1000 ins->vex_wlp = *codes++;
1001 ins->evex_tuple = (*codes++ - 0300);
1002 break;
1003
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001004 case4(0254):
1005 length += 4;
1006 break;
1007
1008 case4(0260):
1009 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001010 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001011 ins->vex_cm = *codes++;
1012 ins->vex_wlp = *codes++;
1013 break;
1014
1015 case 0270:
1016 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001017 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001018 ins->vex_cm = *codes++;
1019 ins->vex_wlp = *codes++;
1020 break;
1021
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001022 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001023 hleok = c & 3;
1024 break;
1025
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001026 case4(0274):
1027 length++;
1028 break;
1029
1030 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001032
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001034 if (bits == 64)
1035 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001036 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001037 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001038
H. Peter Anvine2c80182005-01-15 22:15:51 +00001039 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001040 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001041 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001042
H. Peter Anvine2c80182005-01-15 22:15:51 +00001043 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001044 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001045
Keith Kaniosb7a89542007-04-12 02:40:54 +00001046 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001047 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1048 has_prefix(ins, PPS_ASIZE, P_A32))
1049 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001050 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001051
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001052 case4(0314):
1053 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001054
H. Peter Anvine2c80182005-01-15 22:15:51 +00001055 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001056 {
1057 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1058 if (pfx == P_O16)
1059 break;
1060 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001061 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001062 else
1063 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001065 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001066
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001068 {
1069 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1070 if (pfx == P_O32)
1071 break;
1072 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001073 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001074 else
1075 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001077 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001078
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 case 0322:
1080 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001081
Keith Kaniosb7a89542007-04-12 02:40:54 +00001082 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001083 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001084 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001085
Keith Kaniosb7a89542007-04-12 02:40:54 +00001086 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001087 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001088 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001089
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001090 case 0325:
1091 ins->rex |= REX_NH;
1092 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001093
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001094 case 0326:
1095 break;
1096
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 case 0330:
1098 codes++, length++;
1099 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001100
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001103
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001104 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 case 0333:
1106 length++;
1107 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001108
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001109 case 0334:
1110 ins->rex |= REX_L;
1111 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001112
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001113 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001114 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001115
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001116 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001117 if (!ins->prefixes[PPS_REP])
1118 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001119 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001120
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001121 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001122 if (!ins->prefixes[PPS_REP])
1123 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001124 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001125
H. Peter Anvine2c80182005-01-15 22:15:51 +00001126 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001128 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001130 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1131 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001132 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001134 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001135 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001136
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001137 case 0341:
1138 if (!ins->prefixes[PPS_WAIT])
1139 ins->prefixes[PPS_WAIT] = P_WAIT;
1140 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001141
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001142 case 0360:
1143 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001144
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001145 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001146 length++;
1147 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001148
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001149 case 0364:
1150 case 0365:
1151 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001152
Keith Kanios48af1772007-08-17 07:37:52 +00001153 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001154 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001155 length++;
1156 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001157
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001158 case 0370:
1159 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001161
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 case 0373:
1163 length++;
1164 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001165
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001166 case 0374:
1167 eat = EA_XMMVSIB;
1168 break;
1169
1170 case 0375:
1171 eat = EA_YMMVSIB;
1172 break;
1173
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001174 case 0376:
1175 eat = EA_ZMMVSIB;
1176 break;
1177
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001178 case4(0100):
1179 case4(0110):
1180 case4(0120):
1181 case4(0130):
1182 case4(0200):
1183 case4(0204):
1184 case4(0210):
1185 case4(0214):
1186 case4(0220):
1187 case4(0224):
1188 case4(0230):
1189 case4(0234):
1190 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001192 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001193 opflags_t rflags;
1194 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001195 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001196
Keith Kaniosb7a89542007-04-12 02:40:54 +00001197 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001198
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001199 if (c <= 0177) {
1200 /* pick rfield from operand b (opx) */
1201 rflags = regflag(opx);
1202 rfield = nasm_regvals[opx->basereg];
1203 } else {
1204 rflags = 0;
1205 rfield = c & 7;
1206 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001207
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001208 /* EVEX.b1 : evex_brerop contains the operand position */
1209 op_er_sae = (ins->evex_brerop >= 0 ?
1210 &ins->oprs[ins->evex_brerop] : NULL);
1211
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001212 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1213 /* set EVEX.b */
1214 ins->evex_p[2] |= EVEX_P2B;
1215 if (op_er_sae->decoflags & ER) {
1216 /* set EVEX.RC (rounding control) */
1217 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1218 & EVEX_P2RC;
1219 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001220 } else {
1221 /* set EVEX.L'L (vector length) */
1222 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001223 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001224 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001225 /* set EVEX.b */
1226 ins->evex_p[2] |= EVEX_P2B;
1227 }
1228 }
1229
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001230 if (itemp_has(temp, IF_MIB)) {
1231 opy->eaflags |= EAF_MIB;
1232 /*
1233 * if a separate form of MIB (ICC style) is used,
1234 * the index reg info is merged into mem operand
1235 */
1236 if (mib_index != R_none) {
1237 opy->indexreg = mib_index;
1238 opy->scale = 1;
1239 opy->hintbase = mib_index;
1240 opy->hinttype = EAH_NOTBASE;
1241 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001242 }
1243
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001244 if (process_ea(opy, &ea_data, bits,
1245 rfield, rflags, ins) != eat) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001246 nasm_error(ERR_NONFATAL, "invalid effective address");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001247 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001248 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001249 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001250 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001251 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001252 }
1253 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001254
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001255 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001256 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001257 ": instruction code \\%o (0x%02X) given", c, c);
1258 break;
1259 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001260 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001261
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001262 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001263
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001264 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001265 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001266 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001267 return -1;
1268 }
1269 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001270 }
1271
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001272 switch (ins->prefixes[PPS_VEX]) {
1273 case P_EVEX:
1274 if (!(ins->rex & REX_EV))
1275 return -1;
1276 break;
1277 case P_VEX3:
1278 case P_VEX2:
1279 if (!(ins->rex & REX_V))
1280 return -1;
1281 break;
1282 default:
1283 break;
1284 }
1285
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001286 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001287 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001288
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001289 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001290 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001291 return -1;
1292 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001293 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001294 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001295 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001296 ins->rex &= ~REX_W;
1297 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001298 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001299 ins->rex |= REX_W;
1300 bad32 &= ~REX_W;
1301 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001302 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001303 /* Follow REX_W */
1304 break;
1305 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001306
H. Peter Anvinfc561202011-07-07 16:58:22 -07001307 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001308 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001309 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001310 } else if (!(ins->rex & REX_EV) &&
1311 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001312 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001313 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001314 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001315 if (ins->rex & REX_EV)
1316 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001317 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1318 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001319 length += 3;
1320 else
1321 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001322 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001323 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001324 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001325 return -1;
1326 } else if (bits == 64) {
1327 length++;
1328 } else if ((ins->rex & REX_L) &&
1329 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001330 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001331 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001332 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001333 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001334 length++;
1335 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001336 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001337 return -1;
1338 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001339 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001340
1341 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001342 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001343 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001344 "instruction is not lockable");
1345 }
1346
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001347 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001348
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001349 /*
1350 * when BND prefix is set by DEFAULT directive,
1351 * BND prefix is added to every appropriate instruction line
1352 * unless it is overridden by NOBND prefix.
1353 */
1354 if (globalbnd &&
1355 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1356 ins->prefixes[PPS_REP] = P_BND;
1357
H. Peter Anvina77692b2016-09-20 14:04:33 -07001358 /*
1359 * Add length of legacy prefixes
1360 */
1361 length += emit_prefix(NULL, bits, ins);
1362
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001363 return length;
1364}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001365
H. Peter Anvina77692b2016-09-20 14:04:33 -07001366static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001367{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001368 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001369 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001370 !(ins->rex & (REX_V | REX_EV)) &&
1371 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001372 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1373 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001374 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001375 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001376 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001377}
1378
H. Peter Anvina77692b2016-09-20 14:04:33 -07001379static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1380{
1381 int bytes = 0;
1382 int j;
1383
1384 for (j = 0; j < MAXPREFIX; j++) {
1385 uint8_t c = 0;
1386 switch (ins->prefixes[j]) {
1387 case P_WAIT:
1388 c = 0x9B;
1389 break;
1390 case P_LOCK:
1391 c = 0xF0;
1392 break;
1393 case P_REPNE:
1394 case P_REPNZ:
1395 case P_XACQUIRE:
1396 case P_BND:
1397 c = 0xF2;
1398 break;
1399 case P_REPE:
1400 case P_REPZ:
1401 case P_REP:
1402 case P_XRELEASE:
1403 c = 0xF3;
1404 break;
1405 case R_CS:
1406 if (bits == 64) {
1407 nasm_error(ERR_WARNING | ERR_PASS2,
1408 "cs segment base generated, but will be ignored in 64-bit mode");
1409 }
1410 c = 0x2E;
1411 break;
1412 case R_DS:
1413 if (bits == 64) {
1414 nasm_error(ERR_WARNING | ERR_PASS2,
1415 "ds segment base generated, but will be ignored in 64-bit mode");
1416 }
1417 c = 0x3E;
1418 break;
1419 case R_ES:
1420 if (bits == 64) {
1421 nasm_error(ERR_WARNING | ERR_PASS2,
1422 "es segment base generated, but will be ignored in 64-bit mode");
1423 }
1424 c = 0x26;
1425 break;
1426 case R_FS:
1427 c = 0x64;
1428 break;
1429 case R_GS:
1430 c = 0x65;
1431 break;
1432 case R_SS:
1433 if (bits == 64) {
1434 nasm_error(ERR_WARNING | ERR_PASS2,
1435 "ss segment base generated, but will be ignored in 64-bit mode");
1436 }
1437 c = 0x36;
1438 break;
1439 case R_SEGR6:
1440 case R_SEGR7:
1441 nasm_error(ERR_NONFATAL,
1442 "segr6 and segr7 cannot be used as prefixes");
1443 break;
1444 case P_A16:
1445 if (bits == 64) {
1446 nasm_error(ERR_NONFATAL,
1447 "16-bit addressing is not supported "
1448 "in 64-bit mode");
1449 } else if (bits != 16)
1450 c = 0x67;
1451 break;
1452 case P_A32:
1453 if (bits != 32)
1454 c = 0x67;
1455 break;
1456 case P_A64:
1457 if (bits != 64) {
1458 nasm_error(ERR_NONFATAL,
1459 "64-bit addressing is only supported "
1460 "in 64-bit mode");
1461 }
1462 break;
1463 case P_ASP:
1464 c = 0x67;
1465 break;
1466 case P_O16:
1467 if (bits != 16)
1468 c = 0x66;
1469 break;
1470 case P_O32:
1471 if (bits == 16)
1472 c = 0x66;
1473 break;
1474 case P_O64:
1475 /* REX.W */
1476 break;
1477 case P_OSP:
1478 c = 0x66;
1479 break;
1480 case P_EVEX:
1481 case P_VEX3:
1482 case P_VEX2:
1483 case P_NOBND:
1484 case P_none:
1485 break;
1486 default:
1487 nasm_panic(0, "invalid instruction prefix");
1488 }
1489 if (c) {
1490 if (data)
1491 out_rawbyte(data, c);
1492 bytes++;
1493 }
1494 }
1495 return bytes;
1496}
1497
1498static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001499{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001500 uint8_t c;
1501 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001502 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001503 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001504 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001505 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001506 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001507 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001508 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001509 const int bits = data->bits;
H. Peter Anvin70653092007-10-19 14:42:29 -07001510
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001511 ins->rex_done = false;
1512
H. Peter Anvina77692b2016-09-20 14:04:33 -07001513 emit_prefix(data, bits, ins);
1514
H. Peter Anvin839eca22007-10-29 23:12:47 -07001515 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001516 c = *codes++;
1517 op1 = (c & 3) + ((opex & 1) << 2);
1518 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1519 opx = &ins->oprs[op1];
1520 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001521
H. Peter Anvina77692b2016-09-20 14:04:33 -07001522
H. Peter Anvin839eca22007-10-29 23:12:47 -07001523 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001524 case 01:
1525 case 02:
1526 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001527 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001528 emit_rex(data, ins);
1529 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001530 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001532
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001533 case 05:
1534 case 06:
1535 case 07:
1536 opex = c;
1537 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001538
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001539 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001540 emit_rex(data, ins);
1541 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001542 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001543
Jin Kyu Song164d6072013-10-15 19:10:13 -07001544 case4(014):
1545 break;
1546
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001547 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001548 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001549 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001550 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001551 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001552 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001553
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001554 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001555 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001556 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001557 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001558 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001560
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001561 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001562 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001563 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001564 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001565
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001566 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001567 if (opx->type & (BITS16 | BITS32))
1568 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 else
1570 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001571 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001572 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001574
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001575 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001576 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001577 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001578 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001579
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001580 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001581 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001582 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001583 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001585
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001586 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001587 if (opx->segment == data->segment) {
1588 int64_t delta = opx->offset - data->offset
1589 - (data->inslen - data->insoffs);
1590 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001591 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001592 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001593 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001594 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001595
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001596 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001597 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001598 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001599
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001600 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001601 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001603
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001604 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001605 if (opx->type & (BITS16 | BITS32 | BITS64))
1606 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 else
1608 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001609
1610 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001612
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001613 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001614 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001616
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001617 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001618 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001619 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001620 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001621 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001623
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001624 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001625 {
1626 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1627 const struct operand *opy;
1628
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001629 c = *codes++;
1630 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001631 opy = &ins->oprs[c & 7];
1632 if (opy->segment != NO_SEG || opy->wrt != NO_SEG) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001633 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001634 "non-absolute expression not permitted as argument %d",
1635 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001636 } else if (opy->offset & ~mask) {
1637 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1638 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001639 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001640 c = opy->offset & mask;
1641 goto emit_is4;
1642 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001643
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001644 case 0173:
1645 c = *codes++;
1646 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001647 c &= 15;
1648 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001649
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001650 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001651 c = 0;
1652 emit_is4:
1653 r = nasm_regvals[opx->basereg];
1654 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001655 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001656
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001657 case4(0254):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001658 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001659 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001660 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001661 "signed dword immediate exceeds bounds");
1662 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001663 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001664 break;
1665
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001666 case4(0240):
1667 case 0250:
1668 codes += 3;
1669 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1670 EVEX_P2Z | EVEX_P2AAA, 2);
1671 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1672 bytes[0] = 0x62;
1673 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001674 bytes[1] = ((((ins->rex & 7) << 5) |
1675 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001676 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001677 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1678 ((~ins->vexreg & 15) << 3) |
1679 (1 << 2) | (ins->vex_wlp & 3);
1680 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001681 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001682 break;
1683
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001684 case4(0260):
1685 case 0270:
1686 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001687 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1688 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001689 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1690 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1691 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001692 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001693 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001694 } else {
1695 bytes[0] = 0xc5;
1696 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001697 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001698 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001699 }
1700 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001701
H. Peter Anvine014f352012-02-25 22:35:19 -08001702 case 0271:
1703 case 0272:
1704 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001705 break;
1706
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001707 case4(0274):
1708 {
1709 uint64_t uv, um;
1710 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001711
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001712 if (ins->rex & REX_W)
1713 s = 64;
1714 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1715 s = 16;
1716 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1717 s = 32;
1718 else
1719 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001720
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001721 um = (uint64_t)2 << (s-1);
1722 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001723
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001724 if (uv > 127 && uv < (uint64_t)-128 &&
1725 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001726 /* If this wasn't explicitly byte-sized, warn as though we
1727 * had fallen through to the imm16/32/64 case.
1728 */
H. Peter Anvin215186f2016-02-17 20:27:41 -08001729 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001730 "%s value exceeds bounds",
1731 (opx->type & BITS8) ? "signed byte" :
1732 s == 16 ? "word" :
1733 s == 32 ? "dword" :
1734 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001735 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001736 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001737 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001738 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001739
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001740 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001741 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001742
H. Peter Anvine2c80182005-01-15 22:15:51 +00001743 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001744 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1745 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001746 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001747
H. Peter Anvine2c80182005-01-15 22:15:51 +00001748 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001749 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1750 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001752
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 case 0312:
1754 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001755
Keith Kaniosb7a89542007-04-12 02:40:54 +00001756 case 0313:
1757 ins->rex = 0;
1758 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001759
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001760 case4(0314):
1761 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001762
H. Peter Anvine2c80182005-01-15 22:15:51 +00001763 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001765 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001766
H. Peter Anvine2c80182005-01-15 22:15:51 +00001767 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001768 case 0323:
1769 break;
1770
Keith Kaniosb7a89542007-04-12 02:40:54 +00001771 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001772 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001773 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001774
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001775 case 0325:
1776 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001777
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001778 case 0326:
1779 break;
1780
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001782 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001784
H. Peter Anvine2c80182005-01-15 22:15:51 +00001785 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001787
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001788 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001790 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001792
Keith Kanios48af1772007-08-17 07:37:52 +00001793 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001794 if (ins->rex & REX_R)
1795 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001796 ins->rex &= ~(REX_L|REX_R);
1797 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001798
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001799 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001800 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001801
H. Peter Anvin962e3052008-08-28 17:47:16 -07001802 case 0336:
1803 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001804 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001805
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001808 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001809
1810 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001812
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001813 case 0341:
1814 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001815
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001816 case 0360:
1817 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001818
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001819 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001820 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001821 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001822
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001823 case 0364:
1824 case 0365:
1825 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001826
Keith Kanios48af1772007-08-17 07:37:52 +00001827 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001828 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001829 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001830 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001831
Jin Kyu Song03041092013-10-15 19:38:51 -07001832 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001833 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001834
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001836 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001838
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001839 case 0374:
1840 eat = EA_XMMVSIB;
1841 break;
1842
1843 case 0375:
1844 eat = EA_YMMVSIB;
1845 break;
1846
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001847 case 0376:
1848 eat = EA_ZMMVSIB;
1849 break;
1850
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001851 case4(0100):
1852 case4(0110):
1853 case4(0120):
1854 case4(0130):
1855 case4(0200):
1856 case4(0204):
1857 case4(0210):
1858 case4(0214):
1859 case4(0220):
1860 case4(0224):
1861 case4(0230):
1862 case4(0234):
1863 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864 ea ea_data;
1865 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001866 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001867 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001868 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001869
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001870 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001871 /* pick rfield from operand b (opx) */
1872 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001873 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001874 } else {
1875 /* rfield is constant */
1876 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001877 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001878 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001879
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001880 if (process_ea(opy, &ea_data, bits,
1881 rfield, rflags, ins) != eat)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001882 nasm_error(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001883
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 p = bytes;
1885 *p++ = ea_data.modrm;
1886 if (ea_data.sib_present)
1887 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001888 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001889
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001890 /*
1891 * Make sure the address gets the right offset in case
1892 * the line breaks in the .lst file (BR 1197827)
1893 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001894
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001895 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001896 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001897 if (ea_data.disp8) {
1898 out_rawbyte(data, ea_data.disp8);
1899 } else if (ea_data.rip) {
1900 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001901 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001902 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001903
H. Peter Anvina77692b2016-09-20 14:04:33 -07001904 if (overflow_general(opy->offset, asize) ||
1905 signed_bits(opy->offset, ins->addr_size) !=
1906 signed_bits(opy->offset, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001907 warn_overflow(ERR_PASS2, ea_data.bytes);
1908
H. Peter Anvina77692b2016-09-20 14:04:33 -07001909 out_imm(data, opy, ea_data.bytes,
1910 (asize > ea_data.bytes) ? OUT_SIGNED : OUT_UNSIGNED);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001911 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001913 }
1914 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001915
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001916 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001917 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001918 ": instruction code \\%o (0x%02X) given", c, c);
1919 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001921 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001922}
1923
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001924static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001925{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001926 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001927 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001928 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001929}
1930
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001931static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001932{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001933 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001934 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001935 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001936}
1937
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001938static int op_rexflags(const operand * o, int mask)
1939{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001940 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001941 int val;
1942
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001943 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001944 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001945
H. Peter Anvina4835d42008-05-20 14:21:29 -07001946 flags = nasm_reg_flags[o->basereg];
1947 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001948
1949 return rexflags(val, flags, mask);
1950}
1951
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001952static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001953{
1954 int rex = 0;
1955
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001956 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001957 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001958 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001959 rex |= REX_W;
1960 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1961 rex |= REX_H;
1962 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1963 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001964
1965 return rex & mask;
1966}
1967
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001968static int evexflags(int val, decoflags_t deco,
1969 int mask, uint8_t byte)
1970{
1971 int evex = 0;
1972
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001973 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001974 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001975 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001976 evex |= (EVEX_P0RP | EVEX_P0X);
1977 break;
1978 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001979 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001980 evex |= EVEX_P2VP;
1981 if (deco & Z)
1982 evex |= EVEX_P2Z;
1983 if (deco & OPMASK_MASK)
1984 evex |= deco & EVEX_P2AAA;
1985 break;
1986 }
1987 return evex & mask;
1988}
1989
1990static int op_evexflags(const operand * o, int mask, uint8_t byte)
1991{
1992 int val;
1993
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001994 val = nasm_regvals[o->basereg];
1995
1996 return evexflags(val, o->decoflags, mask, byte);
1997}
1998
H. Peter Anvin23595f52009-07-25 17:44:25 -07001999static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002000 insn *instruction,
2001 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002002{
2003 const struct itemplate *temp;
2004 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002005 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002006 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002007 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002008 int i;
2009
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002010 /* broadcasting uses a different data element size */
2011 for (i = 0; i < instruction->operands; i++)
2012 if (i == broadcast)
2013 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2014 else
2015 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002016
2017 merr = MERR_INVALOP;
2018
2019 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002020 temp->opcode != I_none; temp++) {
2021 m = matches(temp, instruction, bits);
2022 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002023 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002024 m = MOK_GOOD;
2025 else
2026 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002027 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002028 /*
2029 * Missing operand size and a candidate for fuzzy matching...
2030 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002031 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002032 if (i == broadcast)
2033 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2034 else
2035 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002036 opsizemissing = true;
2037 }
2038 if (m > merr)
2039 merr = m;
2040 if (merr == MOK_GOOD)
2041 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002042 }
2043
2044 /* No match, but see if we can get a fuzzy operand size match... */
2045 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002046 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002047
2048 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002049 /*
2050 * We ignore extrinsic operand sizes on registers, so we should
2051 * never try to fuzzy-match on them. This also resolves the case
2052 * when we have e.g. "xmmrm128" in two different positions.
2053 */
2054 if (is_class(REGISTER, instruction->oprs[i].type))
2055 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002056
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002057 /* This tests if xsizeflags[i] has more than one bit set */
2058 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2059 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002060
Jin Kyu Song7903c072013-10-30 03:00:12 -07002061 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002062 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002063 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2064 BITS32 : BITS64);
2065 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002066 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002067 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002068 }
2069
2070 /* Try matching again... */
2071 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002072 temp->opcode != I_none; temp++) {
2073 m = matches(temp, instruction, bits);
2074 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002075 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002076 m = MOK_GOOD;
2077 else
2078 m = MERR_INVALOP;
2079 }
2080 if (m > merr)
2081 merr = m;
2082 if (merr == MOK_GOOD)
2083 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002084 }
2085
H. Peter Anvina81655b2009-07-25 18:15:28 -07002086done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002087 *tempp = temp;
2088 return merr;
2089}
2090
Mark Charneydcaef4b2014-10-09 13:45:17 -04002091static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2092{
2093 opflags_t opsize = opflags & SIZE_MASK;
2094 uint8_t brcast_num;
2095
2096 /*
2097 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2098 * this cannot be a simple arithmetic calculation.
2099 */
2100 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002101 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002102 "size of broadcasting element is greater than 64 bits");
2103
2104 switch (opsize) {
2105 case BITS64:
2106 brcast_num = BITS64 / brsize;
2107 break;
2108 default:
2109 brcast_num = (opsize / BITS128) * (BITS64 / brsize) * 2;
2110 break;
2111 }
2112
2113 return brcast_num;
2114}
2115
H. Peter Anvin65289e82009-07-25 17:25:11 -07002116static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002117 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002118{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002119 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002120 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002121 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002122
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002123 /*
2124 * Check the opcode
2125 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002126 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002127 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002128
2129 /*
2130 * Count the operands
2131 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002132 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002133 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002134
2135 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002136 * Is it legal?
2137 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002138 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002139 return MERR_INVALOP;
2140
2141 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002142 * {evex} available?
2143 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002144 switch (instruction->prefixes[PPS_VEX]) {
2145 case P_EVEX:
2146 if (!itemp_has(itemp, IF_EVEX))
2147 return MERR_ENCMISMATCH;
2148 break;
2149 case P_VEX3:
2150 case P_VEX2:
2151 if (!itemp_has(itemp, IF_VEX))
2152 return MERR_ENCMISMATCH;
2153 break;
2154 default:
2155 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002156 }
2157
2158 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002159 * Check that no spurious colons or TOs are present
2160 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002161 for (i = 0; i < itemp->operands; i++)
2162 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002163 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002164
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002165 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002166 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002167 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002168 switch (itemp_smask(itemp)) {
2169 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002170 asize = BITS8;
2171 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002172 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002173 asize = BITS16;
2174 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002175 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002176 asize = BITS32;
2177 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002178 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002179 asize = BITS64;
2180 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002181 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002182 asize = BITS128;
2183 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002184 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002185 asize = BITS256;
2186 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002187 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002188 asize = BITS512;
2189 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002190 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002191 switch (bits) {
2192 case 16:
2193 asize = BITS16;
2194 break;
2195 case 32:
2196 asize = BITS32;
2197 break;
2198 case 64:
2199 asize = BITS64;
2200 break;
2201 default:
2202 asize = 0;
2203 break;
2204 }
2205 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002206 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002207 asize = 0;
2208 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002209 }
2210
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002211 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002212 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002213 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002214 memset(size, 0, sizeof size);
2215 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002216 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002217 /* S- flags apply to all operands */
2218 for (i = 0; i < MAX_OPERANDS; i++)
2219 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002220 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002221
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002222 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002223 * Check that the operand flags all match up,
2224 * it's a bit tricky so lets be verbose:
2225 *
2226 * 1) Find out the size of operand. If instruction
2227 * doesn't have one specified -- we're trying to
2228 * guess it either from template (IF_S* flag) or
2229 * from code bits.
2230 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002231 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002232 * template has an operand size specified AND this size differ
2233 * from which instruction has (perhaps we got it from code bits)
2234 * we are:
2235 * a) Check that only size of instruction and operand is differ
2236 * other characteristics do match
2237 * b) Perhaps it's a register specified in instruction so
2238 * for such a case we just mark that operand as "size
2239 * missing" and this will turn on fuzzy operand size
2240 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002241 */
2242 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002243 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002244 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002245 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002246 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002247 opflags_t template_opsize, insn_opsize;
2248
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002249 if (!(type & SIZE_MASK))
2250 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002251
Jin Kyu Song7903c072013-10-30 03:00:12 -07002252 insn_opsize = type & SIZE_MASK;
2253 if (!is_broadcast) {
2254 template_opsize = itemp->opd[i] & SIZE_MASK;
2255 } else {
2256 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2257 /*
2258 * when broadcasting, the element size depends on
2259 * the instruction type. decorator flag should match.
2260 */
2261
2262 if (deco_brsize) {
2263 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002264 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002265 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002266 } else {
2267 template_opsize = 0;
2268 }
2269 }
2270
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002271 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002272 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002273 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002274 } else if (template_opsize) {
2275 if (template_opsize != insn_opsize) {
2276 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002277 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002278 } else if (!is_class(REGISTER, type)) {
2279 /*
2280 * Note: we don't honor extrinsic operand sizes for registers,
2281 * so "missing operand size" for a register should be
2282 * considered a wildcard match rather than an error.
2283 */
2284 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002285 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002286 } else if (is_broadcast &&
2287 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002288 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002289 /*
2290 * broadcasting opsize matches but the number of repeated memory
2291 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002292 * if 64b double precision float is broadcasted to ymm (256b),
2293 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002294 */
2295 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002296 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002297 }
2298 }
2299
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002300 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002301 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002302
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002303 /*
2304 * Check operand sizes
2305 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002306 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2307 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002308 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002309 asize = itemp->opd[i] & SIZE_MASK;
2310 if (asize) {
2311 for (i = 0; i < oprs; i++)
2312 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002313 break;
2314 }
2315 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002316 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002317 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002318 }
2319
Keith Kaniosb7a89542007-04-12 02:40:54 +00002320 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002321 if (!(itemp->opd[i] & SIZE_MASK) &&
2322 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002323 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002324 }
2325
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002326 /*
2327 * Check template is okay at the set cpu level
2328 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002329 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002330 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002331
Keith Kaniosb7a89542007-04-12 02:40:54 +00002332 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002333 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002334 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002335 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002336 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002337
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002338 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002339 * If we have a HLE prefix, look for the NOHLE flag
2340 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002341 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002342 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2343 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2344 return MERR_BADHLE;
2345
2346 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002347 * Check if special handling needed for Jumps
2348 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002349 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002350 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002351
Jin Kyu Song03041092013-10-15 19:38:51 -07002352 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002353 * Check if BND prefix is allowed.
2354 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002355 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002356 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002357 (has_prefix(instruction, PPS_REP, P_BND) ||
2358 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002359 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002360 else if (itemp_has(itemp, IF_BND) &&
2361 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2362 has_prefix(instruction, PPS_REP, P_REPNZ)))
2363 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002364
H. Peter Anvin60926242009-07-26 16:25:38 -07002365 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002366}
2367
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002368/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002369 * Check if ModR/M.mod should/can be 01.
2370 * - EAF_BYTEOFFS is set
2371 * - offset can fit in a byte when EVEX is not used
2372 * - offset can be compressed when EVEX is used
2373 */
2374#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2375 (o >= -128 && o <= 127 && \
2376 seg == NO_SEG && !forw_ref && \
2377 !(input->eaflags & EAF_WORDOFFS) && \
2378 !(ins->rex & REX_EV)) || \
2379 (ins->rex & REX_EV && \
2380 is_disp8n(input, ins, &output->disp8)))
2381
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002382static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002383 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002384{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002385 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002386 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002387 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002388
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002389 output->type = EA_SCALAR;
2390 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002391 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002392
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002393 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002394 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002395 /* EVEX.R' flag for the REG operand */
2396 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002397
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002398 if (is_class(REGISTER, input->type)) {
2399 /*
2400 * It's a direct register.
2401 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002402 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002403 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002404
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002405 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002406 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002407
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002408 /* broadcasting is not available with a direct register operand. */
2409 if (input->decoflags & BRDCAST_MASK) {
2410 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2411 goto err;
2412 }
2413
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002414 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002415 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002416 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002417 output->bytes = 0; /* no offset necessary either */
2418 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2419 } else {
2420 /*
2421 * It's a memory reference.
2422 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002423
2424 /* Embedded rounding or SAE is not available with a mem ref operand. */
2425 if (input->decoflags & (ER | SAE)) {
2426 nasm_error(ERR_NONFATAL,
2427 "Embedded rounding is available only with reg-reg op.");
2428 return -1;
2429 }
2430
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002431 if (input->basereg == -1 &&
2432 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002433 /*
2434 * It's a pure offset.
2435 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002436 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2437 input->segment == NO_SEG) {
2438 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2439 input->type &= ~IP_REL;
2440 input->type |= MEMORY;
2441 }
2442
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002443 if (bits == 64 &&
2444 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2445 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2446 return -1;
2447 }
2448
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002449 if (eaflags & EAF_BYTEOFFS ||
2450 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002451 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2452 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2453 }
2454
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002455 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002456 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002457 output->sib = GEN_SIB(0, 4, 5);
2458 output->bytes = 4;
2459 output->modrm = GEN_MODRM(0, rfield, 4);
2460 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002461 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002462 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002463 output->bytes = (addrbits != 16 ? 4 : 2);
2464 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2465 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002466 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002467 } else {
2468 /*
2469 * It's an indirection.
2470 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002471 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002472 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002473 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002474 int t, it, bt; /* register numbers */
2475 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002476
H. Peter Anvine2c80182005-01-15 22:15:51 +00002477 if (s == 0)
2478 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002479
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002480 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002481 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002482 ix = nasm_reg_flags[i];
2483 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002484 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002485 ix = 0;
2486 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002487
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002488 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002489 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002490 bx = nasm_reg_flags[b];
2491 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002492 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002493 bx = 0;
2494 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002495
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002496 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002497 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002498 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002499 int32_t o = input->offset;
2500 int mod, scale, index, base;
2501
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002502 /*
2503 * For a vector SIB, one has to be a vector and the other,
2504 * if present, a GPR. The vector must be the index operand.
2505 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002506 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002507 if (s == 0)
2508 s = 1;
2509 else if (s != 1)
2510 goto err;
2511
2512 t = bt, bt = it, it = t;
2513 x = bx, bx = ix, ix = x;
2514 }
2515
2516 if (bt != -1) {
2517 if (REG_GPR & ~bx)
2518 goto err;
2519 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2520 sok &= bx;
2521 else
2522 goto err;
2523 }
2524
2525 /*
2526 * While we're here, ensure the user didn't specify
2527 * WORD or QWORD
2528 */
2529 if (input->disp_size == 16 || input->disp_size == 64)
2530 goto err;
2531
2532 if (addrbits == 16 ||
2533 (addrbits == 32 && !(sok & BITS32)) ||
2534 (addrbits == 64 && !(sok & BITS64)))
2535 goto err;
2536
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002537 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2538 : ((ix & YMMREG & ~REG_EA)
2539 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002540
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002541 output->rex |= rexflags(it, ix, REX_X);
2542 output->rex |= rexflags(bt, bx, REX_B);
2543 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002544
2545 index = it & 7; /* it is known to be != -1 */
2546
2547 switch (s) {
2548 case 1:
2549 scale = 0;
2550 break;
2551 case 2:
2552 scale = 1;
2553 break;
2554 case 4:
2555 scale = 2;
2556 break;
2557 case 8:
2558 scale = 3;
2559 break;
2560 default: /* then what the smeg is it? */
2561 goto err; /* panic */
2562 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002563
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002564 if (bt == -1) {
2565 base = 5;
2566 mod = 0;
2567 } else {
2568 base = (bt & 7);
2569 if (base != REG_NUM_EBP && o == 0 &&
2570 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002571 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002572 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002573 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002574 mod = 1;
2575 else
2576 mod = 2;
2577 }
2578
2579 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002580 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2581 output->modrm = GEN_MODRM(mod, rfield, 4);
2582 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002583 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002584 /*
2585 * it must be a 32/64-bit memory reference. Firstly we have
2586 * to check that all registers involved are type E/Rxx.
2587 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002588 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002589 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002590
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002591 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002592 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2593 sok &= ix;
2594 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002595 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002596 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002597
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002598 if (bt != -1) {
2599 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002600 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002601 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002602 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002603 sok &= bx;
2604 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002605
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002606 /*
2607 * While we're here, ensure the user didn't specify
2608 * WORD or QWORD
2609 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002610 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002611 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002612
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002613 if (addrbits == 16 ||
2614 (addrbits == 32 && !(sok & BITS32)) ||
2615 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002616 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002617
Keith Kaniosb7a89542007-04-12 02:40:54 +00002618 /* now reorganize base/index */
2619 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002620 ((hb == b && ht == EAH_NOTBASE) ||
2621 (hb == i && ht == EAH_MAKEBASE))) {
2622 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002623 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002624 x = bx, bx = ix, ix = x;
2625 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002626
Jin Kyu Song164d6072013-10-15 19:10:13 -07002627 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002628 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002629 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002630 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002631 if (eaflags & EAF_MIB) {
2632 /* only for mib operands */
2633 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2634 /*
2635 * make a single reg index [reg*1].
2636 * gas uses this form for an explicit index register.
2637 */
2638 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2639 }
2640 if ((ht == EAH_SUMMED) && bt == -1) {
2641 /* separate once summed index into [base, index] */
2642 bt = it, bx = ix, s--;
2643 }
2644 } else {
2645 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002646 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002647 s == 3 || s == 5 || s == 9) && bt == -1) {
2648 /* convert 3*EAX to EAX+2*EAX */
2649 bt = it, bx = ix, s--;
2650 }
2651 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002652 (eaflags & EAF_TIMESTWO) &&
2653 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002654 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002655 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002656 * to sib format with 0x0 displacement - [EAX*1+0].
2657 */
2658 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2659 }
2660 }
Keith Kanios48af1772007-08-17 07:37:52 +00002661 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002662 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002663 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002664 x = ix, ix = bx, bx = x;
2665 }
2666 if (it == REG_NUM_ESP ||
2667 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002668 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002669
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002670 output->rex |= rexflags(it, ix, REX_X);
2671 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002672
Keith Kanios48af1772007-08-17 07:37:52 +00002673 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002674 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002675 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002676
Keith Kaniosb7a89542007-04-12 02:40:54 +00002677 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002678 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002679 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002680 } else {
2681 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002682 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002683 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002684 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002685 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002686 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002687 mod = 1;
2688 else
2689 mod = 2;
2690 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002691
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002692 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002693 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2694 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002695 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002696 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002697 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002698
Keith Kaniosb7a89542007-04-12 02:40:54 +00002699 if (it == -1)
2700 index = 4, s = 1;
2701 else
2702 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002703
H. Peter Anvine2c80182005-01-15 22:15:51 +00002704 switch (s) {
2705 case 1:
2706 scale = 0;
2707 break;
2708 case 2:
2709 scale = 1;
2710 break;
2711 case 4:
2712 scale = 2;
2713 break;
2714 case 8:
2715 scale = 3;
2716 break;
2717 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002718 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002719 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002720
Keith Kaniosb7a89542007-04-12 02:40:54 +00002721 if (bt == -1) {
2722 base = 5;
2723 mod = 0;
2724 } else {
2725 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002726 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002727 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002728 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002729 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002730 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002731 mod = 1;
2732 else
2733 mod = 2;
2734 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002735
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002736 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002737 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2738 output->modrm = GEN_MODRM(mod, rfield, 4);
2739 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 }
2741 } else { /* it's 16-bit */
2742 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002743 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002744
Keith Kaniosb7a89542007-04-12 02:40:54 +00002745 /* check for 64-bit long mode */
2746 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002747 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002748
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002750 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2751 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002752 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002753
Keith Kaniosb7a89542007-04-12 02:40:54 +00002754 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002755 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002756 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002757
H. Peter Anvine2c80182005-01-15 22:15:51 +00002758 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002759 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 if (b == -1 && i != -1) {
2761 int tmp = b;
2762 b = i;
2763 i = tmp;
2764 } /* swap */
2765 if ((b == R_SI || b == R_DI) && i != -1) {
2766 int tmp = b;
2767 b = i;
2768 i = tmp;
2769 }
2770 /* have BX/BP as base, SI/DI index */
2771 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002772 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 if (i != -1 && b != -1 &&
2774 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002775 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002776 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002777 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002778
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 rm = -1;
2780 if (i != -1)
2781 switch (i * 256 + b) {
2782 case R_SI * 256 + R_BX:
2783 rm = 0;
2784 break;
2785 case R_DI * 256 + R_BX:
2786 rm = 1;
2787 break;
2788 case R_SI * 256 + R_BP:
2789 rm = 2;
2790 break;
2791 case R_DI * 256 + R_BP:
2792 rm = 3;
2793 break;
2794 } else
2795 switch (b) {
2796 case R_SI:
2797 rm = 4;
2798 break;
2799 case R_DI:
2800 rm = 5;
2801 break;
2802 case R_BP:
2803 rm = 6;
2804 break;
2805 case R_BX:
2806 rm = 7;
2807 break;
2808 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002809 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002810 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002811
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002812 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002813 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002815 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 mod = 1;
2817 else
2818 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002819
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002820 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002821 output->bytes = mod; /* bytes of offset needed */
2822 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002823 }
2824 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002825 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002826
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002827 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002828 return output->type;
2829
2830err:
2831 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002832}
2833
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002834static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002835{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002836 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002837 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002838
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002839 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002840
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002841 switch (ins->prefixes[PPS_ASIZE]) {
2842 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002843 valid &= 16;
2844 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002845 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002846 valid &= 32;
2847 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002848 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002849 valid &= 64;
2850 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002851 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002852 valid &= (addrbits == 32) ? 16 : 32;
2853 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002854 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002855 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002856 }
2857
2858 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002859 if (is_class(MEMORY, ins->oprs[j].type)) {
2860 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002861
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002862 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002863 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002864 i = 0;
2865 else
2866 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002867
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002868 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002869 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002870 b = 0;
2871 else
2872 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002873
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002874 if (ins->oprs[j].scale == 0)
2875 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002876
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002877 if (!i && !b) {
2878 int ds = ins->oprs[j].disp_size;
2879 if ((addrbits != 64 && ds > 8) ||
2880 (addrbits == 64 && ds == 16))
2881 valid &= ds;
2882 } else {
2883 if (!(REG16 & ~b))
2884 valid &= 16;
2885 if (!(REG32 & ~b))
2886 valid &= 32;
2887 if (!(REG64 & ~b))
2888 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002889
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002890 if (!(REG16 & ~i))
2891 valid &= 16;
2892 if (!(REG32 & ~i))
2893 valid &= 32;
2894 if (!(REG64 & ~i))
2895 valid &= 64;
2896 }
2897 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002898 }
2899
2900 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002901 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002902 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002903 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002904 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002906 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002907 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002908 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002909 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002910 }
2911
2912 defdisp = ins->addr_size == 16 ? 16 : 32;
2913
2914 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002915 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2916 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2917 /*
2918 * mem_offs sizes must match the address size; if not,
2919 * strip the MEM_OFFS bit and match only EA instructions
2920 */
2921 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2922 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002923 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002924}