blob: 04d4d43532ac313fa8d5e5303811c06c621bdf76 [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 Anvina77692b2016-09-20 14:04:33 -0700617 data.insoffs = 0;
618 data.inslen = len;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700619
H. Peter Anvind81a2352016-09-21 14:03:18 -0700620 if (map) {
621 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700622 } else if ((off_t)m == len) {
623 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700624 } else {
625 off_t l = len;
626
627 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700628 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700629 "`incbin': unable to seek on file `%s'",
630 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700631 goto end_incbin;
632 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700633 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700634 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700635 if (!m || feof(fp)) {
636 /*
637 * This shouldn't happen unless the file
638 * actually changes while we are reading
639 * it.
640 */
641 nasm_error(ERR_NONFATAL,
642 "`incbin': unexpected EOF while"
643 " reading file `%s'", fname);
644 goto end_incbin;
645 }
646 out_rawdata(&data, buf, m);
647 l -= m;
648 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700649 }
650 }
651 end_incbin:
652 lfmt->downlevel(LIST_INCBIN);
653 if (instruction->times > 1) {
654 lfmt->set_offset(data.offset);
655 lfmt->uplevel(LIST_TIMES);
656 lfmt->downlevel(LIST_TIMES);
657 }
658 if (ferror(fp)) {
659 nasm_error(ERR_NONFATAL,
660 "`incbin': error while"
661 " reading file `%s'", fname);
662 }
663 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700664 if (buf)
665 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700666 if (map)
667 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700668 fclose(fp);
669 done:
670 ;
671 } else {
672 /* "Real" instruction */
673
674 /* Check to see if we need an address-size prefix */
675 add_asp(instruction, bits);
676
677 m = find_match(&temp, instruction, data.segment, data.offset, bits);
678
679 if (m == MOK_GOOD) {
680 /* Matches! */
681 int64_t insn_size = calcsize(data.segment, data.offset,
682 bits, instruction, temp);
683 itimes = instruction->times;
684 if (insn_size < 0) /* shouldn't be, on pass two */
685 nasm_panic(0, "errors made it through from pass one");
686
687 data.itemp = temp;
688 data.bits = bits;
689
690 while (itimes--) {
691 data.insoffs = 0;
692 data.inslen = insn_size;
693
694 gencode(&data, instruction);
695 nasm_assert(data.insoffs == insn_size);
696
697 if (itimes > 0 && itimes == instruction->times - 1) {
698 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800699 lfmt->uplevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400700 }
701 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700702 if (instruction->times > 1)
703 lfmt->downlevel(LIST_TIMES);
704 } else {
705 /* No match */
706 switch (m) {
707 case MERR_OPSIZEMISSING:
708 nasm_error(ERR_NONFATAL, "operation size not specified");
709 break;
710 case MERR_OPSIZEMISMATCH:
711 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
712 break;
713 case MERR_BRNUMMISMATCH:
714 nasm_error(ERR_NONFATAL,
715 "mismatch in the number of broadcasting elements");
716 break;
717 case MERR_BADCPU:
718 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
719 break;
720 case MERR_BADMODE:
721 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
722 bits);
723 break;
724 case MERR_ENCMISMATCH:
725 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
726 break;
727 case MERR_BADBND:
728 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
729 break;
730 case MERR_BADREPNE:
731 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
732 (has_prefix(instruction, PPS_REP, P_REPNE) ?
733 "repne" : "repnz"));
734 break;
735 default:
736 nasm_error(ERR_NONFATAL,
737 "invalid combination of opcode and operands");
738 break;
739 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400740 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000741 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700742 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000743}
744
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400745int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800746 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000747{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000748 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700749 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000750
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000751 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000752
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400753 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000754 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000755
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700756 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
757 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400758 instruction->opcode == I_DT || instruction->opcode == I_DO ||
759 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000760 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300761 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000762
H. Peter Anvine2c80182005-01-15 22:15:51 +0000763 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300764 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000765
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400766 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000767 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000768
H. Peter Anvine2c80182005-01-15 22:15:51 +0000769 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400770 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000771 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400772 warn_overflow_const(e->offset, wsize);
773 } else if (e->type == EOT_DB_STRING ||
774 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000775 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000776
H. Peter Anvine2c80182005-01-15 22:15:51 +0000777 align = (-osize) % wsize;
778 if (align < 0)
779 align += wsize;
780 isize += osize + align;
781 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700782 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000783 }
784
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400786 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700787 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000788
H. Peter Anvind81a2352016-09-21 14:03:18 -0700789 len = nasm_file_size_by_path(fname);
790 if (len == (off_t)-1) {
791 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
792 fname);
793 return 0;
794 }
795
796 if (instruction->eops->next) {
797 if (len <= (off_t)instruction->eops->next->offset) {
798 len = 0;
799 } else {
800 len -= instruction->eops->next->offset;
801 if (instruction->eops->next->next &&
802 len > (off_t)instruction->eops->next->next->offset) {
803 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 }
805 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700807
808 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000809 }
810
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700811 /* Check to see if we need an address-size prefix */
812 add_asp(instruction, bits);
813
H. Peter Anvin23595f52009-07-25 17:44:25 -0700814 m = find_match(&temp, instruction, segment, offset, bits);
815 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400816 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700817 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700818 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400819 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000820 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000821}
822
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800823static void bad_hle_warn(const insn * ins, uint8_t hleok)
824{
825 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800826 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800827 static const enum whatwarn warn[2][4] =
828 {
829 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
830 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
831 };
832 unsigned int n;
833
834 n = (unsigned int)rep_pfx - P_XACQUIRE;
835 if (n > 1)
836 return; /* Not XACQUIRE/XRELEASE */
837
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800838 ww = warn[n][hleok];
839 if (!is_class(MEMORY, ins->oprs[0].type))
840 ww = w_inval; /* HLE requires operand 0 to be memory */
841
842 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800843 case w_none:
844 break;
845
846 case w_lock:
847 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800848 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800849 "%s with this instruction requires lock",
850 prefix_name(rep_pfx));
851 }
852 break;
853
854 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800855 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800856 "%s invalid with this instruction",
857 prefix_name(rep_pfx));
858 break;
859 }
860}
861
H. Peter Anvin507ae032008-10-09 15:37:10 -0700862/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400863#define case3(x) case (x): case (x)+1: case (x)+2
864#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700865
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800866static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800867 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000868{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800869 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800870 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000871 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000872 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700873 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700874 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700875 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700876 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800877 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800878 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700879 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000880
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700881 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700882 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700883 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700884
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700885 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400886 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700887
H. Peter Anvine2c80182005-01-15 22:15:51 +0000888 (void)segment; /* Don't warn that this parameter is unused */
889 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000890
H. Peter Anvin839eca22007-10-29 23:12:47 -0700891 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400892 c = *codes++;
893 op1 = (c & 3) + ((opex & 1) << 2);
894 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
895 opx = &ins->oprs[op1];
896 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700897
H. Peter Anvin839eca22007-10-29 23:12:47 -0700898 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400899 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000900 codes += c, length += c;
901 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700902
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400903 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400904 opex = c;
905 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700906
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400907 case4(010):
908 ins->rex |=
909 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000910 codes++, length++;
911 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700912
Jin Kyu Song164d6072013-10-15 19:10:13 -0700913 case4(014):
914 /* this is an index reg of MIB operand */
915 mib_index = opx->basereg;
916 break;
917
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400918 case4(020):
919 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000920 length++;
921 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700922
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400923 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000924 length += 2;
925 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700926
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400927 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700928 if (opx->type & (BITS16 | BITS32 | BITS64))
929 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000930 else
931 length += (bits == 16) ? 2 : 4;
932 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700933
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400934 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000935 length += 4;
936 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700937
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400938 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700939 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000940 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700941
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400942 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 length++;
944 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700945
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400946 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000947 length += 8; /* MOV reg64/imm */
948 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700949
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400950 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000951 length += 2;
952 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700953
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400954 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700955 if (opx->type & (BITS16 | BITS32 | BITS64))
956 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 else
958 length += (bits == 16) ? 2 : 4;
959 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700960
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400961 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000962 length += 4;
963 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700964
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400965 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -0700966 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700968
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400969 case 0172:
970 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400971 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -0700972 length++;
973 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700974
H. Peter Anvincffe61e2011-07-07 17:21:24 -0700975 case4(0174):
976 length++;
977 break;
978
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700979 case4(0240):
980 ins->rex |= REX_EV;
981 ins->vexreg = regval(opx);
982 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
983 ins->vex_cm = *codes++;
984 ins->vex_wlp = *codes++;
985 ins->evex_tuple = (*codes++ - 0300);
986 break;
987
988 case 0250:
989 ins->rex |= REX_EV;
990 ins->vexreg = 0;
991 ins->vex_cm = *codes++;
992 ins->vex_wlp = *codes++;
993 ins->evex_tuple = (*codes++ - 0300);
994 break;
995
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400996 case4(0254):
997 length += 4;
998 break;
999
1000 case4(0260):
1001 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001002 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001003 ins->vex_cm = *codes++;
1004 ins->vex_wlp = *codes++;
1005 break;
1006
1007 case 0270:
1008 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001009 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001010 ins->vex_cm = *codes++;
1011 ins->vex_wlp = *codes++;
1012 break;
1013
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001014 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001015 hleok = c & 3;
1016 break;
1017
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001018 case4(0274):
1019 length++;
1020 break;
1021
1022 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001024
H. Peter Anvine2c80182005-01-15 22:15:51 +00001025 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001026 if (bits == 64)
1027 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001028 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001029 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001030
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001032 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001034
H. Peter Anvine2c80182005-01-15 22:15:51 +00001035 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001036 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001037
Keith Kaniosb7a89542007-04-12 02:40:54 +00001038 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001039 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1040 has_prefix(ins, PPS_ASIZE, P_A32))
1041 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001042 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001043
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001044 case4(0314):
1045 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001046
H. Peter Anvine2c80182005-01-15 22:15:51 +00001047 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001048 {
1049 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1050 if (pfx == P_O16)
1051 break;
1052 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001053 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001054 else
1055 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001056 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001057 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001060 {
1061 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1062 if (pfx == P_O32)
1063 break;
1064 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001065 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001066 else
1067 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001069 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001070
H. Peter Anvine2c80182005-01-15 22:15:51 +00001071 case 0322:
1072 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001073
Keith Kaniosb7a89542007-04-12 02:40:54 +00001074 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001075 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001076 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001077
Keith Kaniosb7a89542007-04-12 02:40:54 +00001078 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001079 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001080 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001081
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001082 case 0325:
1083 ins->rex |= REX_NH;
1084 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001085
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001086 case 0326:
1087 break;
1088
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 case 0330:
1090 codes++, length++;
1091 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001092
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001095
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001096 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 case 0333:
1098 length++;
1099 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001100
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001101 case 0334:
1102 ins->rex |= REX_L;
1103 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001104
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001105 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001106 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001107
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001108 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001109 if (!ins->prefixes[PPS_REP])
1110 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001111 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001112
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001113 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001114 if (!ins->prefixes[PPS_REP])
1115 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001116 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001117
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001120 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 " quantity of BSS space");
1122 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001123 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001125
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001126 case 0341:
1127 if (!ins->prefixes[PPS_WAIT])
1128 ins->prefixes[PPS_WAIT] = P_WAIT;
1129 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001130
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001131 case 0360:
1132 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001133
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001134 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001135 length++;
1136 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001137
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001138 case 0364:
1139 case 0365:
1140 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001141
Keith Kanios48af1772007-08-17 07:37:52 +00001142 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001143 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001144 length++;
1145 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001146
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001147 case 0370:
1148 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001150
H. Peter Anvine2c80182005-01-15 22:15:51 +00001151 case 0373:
1152 length++;
1153 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001154
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001155 case 0374:
1156 eat = EA_XMMVSIB;
1157 break;
1158
1159 case 0375:
1160 eat = EA_YMMVSIB;
1161 break;
1162
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001163 case 0376:
1164 eat = EA_ZMMVSIB;
1165 break;
1166
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001167 case4(0100):
1168 case4(0110):
1169 case4(0120):
1170 case4(0130):
1171 case4(0200):
1172 case4(0204):
1173 case4(0210):
1174 case4(0214):
1175 case4(0220):
1176 case4(0224):
1177 case4(0230):
1178 case4(0234):
1179 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001180 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001181 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001182 opflags_t rflags;
1183 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001184 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001185
Keith Kaniosb7a89542007-04-12 02:40:54 +00001186 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001187
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001188 if (c <= 0177) {
1189 /* pick rfield from operand b (opx) */
1190 rflags = regflag(opx);
1191 rfield = nasm_regvals[opx->basereg];
1192 } else {
1193 rflags = 0;
1194 rfield = c & 7;
1195 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001196
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001197 /* EVEX.b1 : evex_brerop contains the operand position */
1198 op_er_sae = (ins->evex_brerop >= 0 ?
1199 &ins->oprs[ins->evex_brerop] : NULL);
1200
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001201 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1202 /* set EVEX.b */
1203 ins->evex_p[2] |= EVEX_P2B;
1204 if (op_er_sae->decoflags & ER) {
1205 /* set EVEX.RC (rounding control) */
1206 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1207 & EVEX_P2RC;
1208 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001209 } else {
1210 /* set EVEX.L'L (vector length) */
1211 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001212 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001213 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001214 /* set EVEX.b */
1215 ins->evex_p[2] |= EVEX_P2B;
1216 }
1217 }
1218
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001219 if (itemp_has(temp, IF_MIB)) {
1220 opy->eaflags |= EAF_MIB;
1221 /*
1222 * if a separate form of MIB (ICC style) is used,
1223 * the index reg info is merged into mem operand
1224 */
1225 if (mib_index != R_none) {
1226 opy->indexreg = mib_index;
1227 opy->scale = 1;
1228 opy->hintbase = mib_index;
1229 opy->hinttype = EAH_NOTBASE;
1230 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001231 }
1232
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001233 if (process_ea(opy, &ea_data, bits,
1234 rfield, rflags, ins) != eat) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001235 nasm_error(ERR_NONFATAL, "invalid effective address");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001236 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001237 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001238 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001239 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001240 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001241 }
1242 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001243
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001244 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001245 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001246 ": instruction code \\%o (0x%02X) given", c, c);
1247 break;
1248 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001249 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001250
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001251 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001252
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001253 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001254 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001255 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001256 return -1;
1257 }
1258 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001259 }
1260
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001261 switch (ins->prefixes[PPS_VEX]) {
1262 case P_EVEX:
1263 if (!(ins->rex & REX_EV))
1264 return -1;
1265 break;
1266 case P_VEX3:
1267 case P_VEX2:
1268 if (!(ins->rex & REX_V))
1269 return -1;
1270 break;
1271 default:
1272 break;
1273 }
1274
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001275 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001276 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001277
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001278 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001279 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001280 return -1;
1281 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001282 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001283 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001284 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001285 ins->rex &= ~REX_W;
1286 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001287 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001288 ins->rex |= REX_W;
1289 bad32 &= ~REX_W;
1290 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001291 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001292 /* Follow REX_W */
1293 break;
1294 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001295
H. Peter Anvinfc561202011-07-07 16:58:22 -07001296 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001297 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001298 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001299 } else if (!(ins->rex & REX_EV) &&
1300 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001301 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001302 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001303 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001304 if (ins->rex & REX_EV)
1305 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001306 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1307 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001308 length += 3;
1309 else
1310 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001311 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001312 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001313 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001314 return -1;
1315 } else if (bits == 64) {
1316 length++;
1317 } else if ((ins->rex & REX_L) &&
1318 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001319 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001320 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001321 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001322 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001323 length++;
1324 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001325 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001326 return -1;
1327 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001328 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001329
1330 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001331 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001332 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001333 "instruction is not lockable");
1334 }
1335
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001336 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001337
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001338 /*
1339 * when BND prefix is set by DEFAULT directive,
1340 * BND prefix is added to every appropriate instruction line
1341 * unless it is overridden by NOBND prefix.
1342 */
1343 if (globalbnd &&
1344 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1345 ins->prefixes[PPS_REP] = P_BND;
1346
H. Peter Anvina77692b2016-09-20 14:04:33 -07001347 /*
1348 * Add length of legacy prefixes
1349 */
1350 length += emit_prefix(NULL, bits, ins);
1351
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001352 return length;
1353}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001354
H. Peter Anvina77692b2016-09-20 14:04:33 -07001355static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001356{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001357 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001358 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001359 !(ins->rex & (REX_V | REX_EV)) &&
1360 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001361 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1362 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001363 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001364 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001365 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001366}
1367
H. Peter Anvina77692b2016-09-20 14:04:33 -07001368static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1369{
1370 int bytes = 0;
1371 int j;
1372
1373 for (j = 0; j < MAXPREFIX; j++) {
1374 uint8_t c = 0;
1375 switch (ins->prefixes[j]) {
1376 case P_WAIT:
1377 c = 0x9B;
1378 break;
1379 case P_LOCK:
1380 c = 0xF0;
1381 break;
1382 case P_REPNE:
1383 case P_REPNZ:
1384 case P_XACQUIRE:
1385 case P_BND:
1386 c = 0xF2;
1387 break;
1388 case P_REPE:
1389 case P_REPZ:
1390 case P_REP:
1391 case P_XRELEASE:
1392 c = 0xF3;
1393 break;
1394 case R_CS:
1395 if (bits == 64) {
1396 nasm_error(ERR_WARNING | ERR_PASS2,
1397 "cs segment base generated, but will be ignored in 64-bit mode");
1398 }
1399 c = 0x2E;
1400 break;
1401 case R_DS:
1402 if (bits == 64) {
1403 nasm_error(ERR_WARNING | ERR_PASS2,
1404 "ds segment base generated, but will be ignored in 64-bit mode");
1405 }
1406 c = 0x3E;
1407 break;
1408 case R_ES:
1409 if (bits == 64) {
1410 nasm_error(ERR_WARNING | ERR_PASS2,
1411 "es segment base generated, but will be ignored in 64-bit mode");
1412 }
1413 c = 0x26;
1414 break;
1415 case R_FS:
1416 c = 0x64;
1417 break;
1418 case R_GS:
1419 c = 0x65;
1420 break;
1421 case R_SS:
1422 if (bits == 64) {
1423 nasm_error(ERR_WARNING | ERR_PASS2,
1424 "ss segment base generated, but will be ignored in 64-bit mode");
1425 }
1426 c = 0x36;
1427 break;
1428 case R_SEGR6:
1429 case R_SEGR7:
1430 nasm_error(ERR_NONFATAL,
1431 "segr6 and segr7 cannot be used as prefixes");
1432 break;
1433 case P_A16:
1434 if (bits == 64) {
1435 nasm_error(ERR_NONFATAL,
1436 "16-bit addressing is not supported "
1437 "in 64-bit mode");
1438 } else if (bits != 16)
1439 c = 0x67;
1440 break;
1441 case P_A32:
1442 if (bits != 32)
1443 c = 0x67;
1444 break;
1445 case P_A64:
1446 if (bits != 64) {
1447 nasm_error(ERR_NONFATAL,
1448 "64-bit addressing is only supported "
1449 "in 64-bit mode");
1450 }
1451 break;
1452 case P_ASP:
1453 c = 0x67;
1454 break;
1455 case P_O16:
1456 if (bits != 16)
1457 c = 0x66;
1458 break;
1459 case P_O32:
1460 if (bits == 16)
1461 c = 0x66;
1462 break;
1463 case P_O64:
1464 /* REX.W */
1465 break;
1466 case P_OSP:
1467 c = 0x66;
1468 break;
1469 case P_EVEX:
1470 case P_VEX3:
1471 case P_VEX2:
1472 case P_NOBND:
1473 case P_none:
1474 break;
1475 default:
1476 nasm_panic(0, "invalid instruction prefix");
1477 }
1478 if (c) {
1479 if (data)
1480 out_rawbyte(data, c);
1481 bytes++;
1482 }
1483 }
1484 return bytes;
1485}
1486
1487static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001488{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001489 uint8_t c;
1490 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001491 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001492 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001493 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001494 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001495 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001496 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001497 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001498 const int bits = data->bits;
H. Peter Anvin70653092007-10-19 14:42:29 -07001499
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001500 ins->rex_done = false;
1501
H. Peter Anvina77692b2016-09-20 14:04:33 -07001502 emit_prefix(data, bits, ins);
1503
H. Peter Anvin839eca22007-10-29 23:12:47 -07001504 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001505 c = *codes++;
1506 op1 = (c & 3) + ((opex & 1) << 2);
1507 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1508 opx = &ins->oprs[op1];
1509 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001510
H. Peter Anvina77692b2016-09-20 14:04:33 -07001511
H. Peter Anvin839eca22007-10-29 23:12:47 -07001512 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001513 case 01:
1514 case 02:
1515 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001516 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001517 emit_rex(data, ins);
1518 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001519 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001520 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001521
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001522 case 05:
1523 case 06:
1524 case 07:
1525 opex = c;
1526 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001527
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001528 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001529 emit_rex(data, ins);
1530 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001531 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001532
Jin Kyu Song164d6072013-10-15 19:10:13 -07001533 case4(014):
1534 break;
1535
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001536 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001537 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001538 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001539 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001540 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001541 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001542
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001543 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001544 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001545 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001546 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001547 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001549
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001550 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001551 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001552 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001553 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001554
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001555 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001556 if (opx->type & (BITS16 | BITS32))
1557 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 else
1559 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001560 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001561 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001563
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001564 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001565 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001566 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001567 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001568
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001569 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001570 size = ins->addr_size >> 3;
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(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001576 if (opx->segment == data->segment) {
1577 int64_t delta = opx->offset - data->offset
1578 - (data->inslen - data->insoffs);
1579 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001580 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001581 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001582 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001584
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001585 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001586 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001587 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001588
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001589 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001590 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001592
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001594 if (opx->type & (BITS16 | BITS32 | BITS64))
1595 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 else
1597 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001598
1599 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001601
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001602 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001603 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001605
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001606 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001607 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001608 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001609 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001610 out_segment(data, opx);
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 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001614 {
1615 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1616 const struct operand *opy;
1617
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001618 c = *codes++;
1619 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001620 opy = &ins->oprs[c & 7];
1621 if (opy->segment != NO_SEG || opy->wrt != NO_SEG) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001622 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001623 "non-absolute expression not permitted as argument %d",
1624 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001625 } else if (opy->offset & ~mask) {
1626 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1627 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001628 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001629 c = opy->offset & mask;
1630 goto emit_is4;
1631 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001632
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001633 case 0173:
1634 c = *codes++;
1635 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001636 c &= 15;
1637 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001638
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001639 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001640 c = 0;
1641 emit_is4:
1642 r = nasm_regvals[opx->basereg];
1643 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001644 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001645
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001646 case4(0254):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001647 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001648 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001649 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001650 "signed dword immediate exceeds bounds");
1651 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001652 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001653 break;
1654
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001655 case4(0240):
1656 case 0250:
1657 codes += 3;
1658 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1659 EVEX_P2Z | EVEX_P2AAA, 2);
1660 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1661 bytes[0] = 0x62;
1662 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001663 bytes[1] = ((((ins->rex & 7) << 5) |
1664 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001665 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001666 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1667 ((~ins->vexreg & 15) << 3) |
1668 (1 << 2) | (ins->vex_wlp & 3);
1669 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001670 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001671 break;
1672
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001673 case4(0260):
1674 case 0270:
1675 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001676 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1677 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001678 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1679 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1680 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001681 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001682 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001683 } else {
1684 bytes[0] = 0xc5;
1685 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001686 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001687 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001688 }
1689 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001690
H. Peter Anvine014f352012-02-25 22:35:19 -08001691 case 0271:
1692 case 0272:
1693 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001694 break;
1695
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001696 case4(0274):
1697 {
1698 uint64_t uv, um;
1699 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001700
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001701 if (ins->rex & REX_W)
1702 s = 64;
1703 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1704 s = 16;
1705 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1706 s = 32;
1707 else
1708 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001709
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001710 um = (uint64_t)2 << (s-1);
1711 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001712
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001713 if (uv > 127 && uv < (uint64_t)-128 &&
1714 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001715 /* If this wasn't explicitly byte-sized, warn as though we
1716 * had fallen through to the imm16/32/64 case.
1717 */
H. Peter Anvin215186f2016-02-17 20:27:41 -08001718 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001719 "%s value exceeds bounds",
1720 (opx->type & BITS8) ? "signed byte" :
1721 s == 16 ? "word" :
1722 s == 32 ? "dword" :
1723 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001724 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001725 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001726 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001727 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001728
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001729 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001730 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001731
H. Peter Anvine2c80182005-01-15 22:15:51 +00001732 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001733 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1734 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001735 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001736
H. Peter Anvine2c80182005-01-15 22:15:51 +00001737 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001738 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1739 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001740 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001741
H. Peter Anvine2c80182005-01-15 22:15:51 +00001742 case 0312:
1743 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001744
Keith Kaniosb7a89542007-04-12 02:40:54 +00001745 case 0313:
1746 ins->rex = 0;
1747 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001748
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001749 case4(0314):
1750 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001751
H. Peter Anvine2c80182005-01-15 22:15:51 +00001752 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001754 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001755
H. Peter Anvine2c80182005-01-15 22:15:51 +00001756 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001757 case 0323:
1758 break;
1759
Keith Kaniosb7a89542007-04-12 02:40:54 +00001760 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001761 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001763
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001764 case 0325:
1765 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001766
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001767 case 0326:
1768 break;
1769
H. Peter Anvine2c80182005-01-15 22:15:51 +00001770 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001771 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001772 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001773
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001775 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001776
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001777 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001778 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001779 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001781
Keith Kanios48af1772007-08-17 07:37:52 +00001782 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001783 if (ins->rex & REX_R)
1784 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001785 ins->rex &= ~(REX_L|REX_R);
1786 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001787
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001788 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001789 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001790
H. Peter Anvin962e3052008-08-28 17:47:16 -07001791 case 0336:
1792 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001793 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001794
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001797 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001798
1799 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001801
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001802 case 0341:
1803 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001804
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001805 case 0360:
1806 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001807
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001808 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001809 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001810 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001811
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001812 case 0364:
1813 case 0365:
1814 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001815
Keith Kanios48af1772007-08-17 07:37:52 +00001816 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001817 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001818 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001819 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001820
Jin Kyu Song03041092013-10-15 19:38:51 -07001821 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001823
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001825 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001827
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001828 case 0374:
1829 eat = EA_XMMVSIB;
1830 break;
1831
1832 case 0375:
1833 eat = EA_YMMVSIB;
1834 break;
1835
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001836 case 0376:
1837 eat = EA_ZMMVSIB;
1838 break;
1839
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001840 case4(0100):
1841 case4(0110):
1842 case4(0120):
1843 case4(0130):
1844 case4(0200):
1845 case4(0204):
1846 case4(0210):
1847 case4(0214):
1848 case4(0220):
1849 case4(0224):
1850 case4(0230):
1851 case4(0234):
1852 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001853 ea ea_data;
1854 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001855 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001856 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001857 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001858
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001859 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001860 /* pick rfield from operand b (opx) */
1861 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001862 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001863 } else {
1864 /* rfield is constant */
1865 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001866 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001867 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001868
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001869 if (process_ea(opy, &ea_data, bits,
1870 rfield, rflags, ins) != eat)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001871 nasm_error(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001872
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873 p = bytes;
1874 *p++ = ea_data.modrm;
1875 if (ea_data.sib_present)
1876 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001877 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001879 /*
1880 * Make sure the address gets the right offset in case
1881 * the line breaks in the .lst file (BR 1197827)
1882 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001883
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001884 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001885 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001886 if (ea_data.disp8) {
1887 out_rawbyte(data, ea_data.disp8);
1888 } else if (ea_data.rip) {
1889 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001890 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001891 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001892
H. Peter Anvina77692b2016-09-20 14:04:33 -07001893 if (overflow_general(opy->offset, asize) ||
1894 signed_bits(opy->offset, ins->addr_size) !=
1895 signed_bits(opy->offset, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001896 warn_overflow(ERR_PASS2, ea_data.bytes);
1897
H. Peter Anvina77692b2016-09-20 14:04:33 -07001898 out_imm(data, opy, ea_data.bytes,
1899 (asize > ea_data.bytes) ? OUT_SIGNED : OUT_UNSIGNED);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001900 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001901 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001902 }
1903 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001904
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001905 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001906 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001907 ": instruction code \\%o (0x%02X) given", c, c);
1908 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001910 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001911}
1912
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001913static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001914{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001915 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001916 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001917 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001918}
1919
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001920static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001921{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001922 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001923 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001924 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001925}
1926
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001927static int op_rexflags(const operand * o, int mask)
1928{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001929 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001930 int val;
1931
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001932 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001933 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001934
H. Peter Anvina4835d42008-05-20 14:21:29 -07001935 flags = nasm_reg_flags[o->basereg];
1936 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001937
1938 return rexflags(val, flags, mask);
1939}
1940
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001941static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001942{
1943 int rex = 0;
1944
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001945 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001946 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001947 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001948 rex |= REX_W;
1949 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1950 rex |= REX_H;
1951 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1952 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001953
1954 return rex & mask;
1955}
1956
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001957static int evexflags(int val, decoflags_t deco,
1958 int mask, uint8_t byte)
1959{
1960 int evex = 0;
1961
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001962 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001963 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001964 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001965 evex |= (EVEX_P0RP | EVEX_P0X);
1966 break;
1967 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001968 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001969 evex |= EVEX_P2VP;
1970 if (deco & Z)
1971 evex |= EVEX_P2Z;
1972 if (deco & OPMASK_MASK)
1973 evex |= deco & EVEX_P2AAA;
1974 break;
1975 }
1976 return evex & mask;
1977}
1978
1979static int op_evexflags(const operand * o, int mask, uint8_t byte)
1980{
1981 int val;
1982
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001983 val = nasm_regvals[o->basereg];
1984
1985 return evexflags(val, o->decoflags, mask, byte);
1986}
1987
H. Peter Anvin23595f52009-07-25 17:44:25 -07001988static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001989 insn *instruction,
1990 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07001991{
1992 const struct itemplate *temp;
1993 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07001994 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07001995 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001996 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07001997 int i;
1998
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07001999 /* broadcasting uses a different data element size */
2000 for (i = 0; i < instruction->operands; i++)
2001 if (i == broadcast)
2002 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2003 else
2004 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002005
2006 merr = MERR_INVALOP;
2007
2008 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002009 temp->opcode != I_none; temp++) {
2010 m = matches(temp, instruction, bits);
2011 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002012 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002013 m = MOK_GOOD;
2014 else
2015 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002016 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002017 /*
2018 * Missing operand size and a candidate for fuzzy matching...
2019 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002020 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002021 if (i == broadcast)
2022 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2023 else
2024 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002025 opsizemissing = true;
2026 }
2027 if (m > merr)
2028 merr = m;
2029 if (merr == MOK_GOOD)
2030 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002031 }
2032
2033 /* No match, but see if we can get a fuzzy operand size match... */
2034 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002035 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002036
2037 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002038 /*
2039 * We ignore extrinsic operand sizes on registers, so we should
2040 * never try to fuzzy-match on them. This also resolves the case
2041 * when we have e.g. "xmmrm128" in two different positions.
2042 */
2043 if (is_class(REGISTER, instruction->oprs[i].type))
2044 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002045
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002046 /* This tests if xsizeflags[i] has more than one bit set */
2047 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2048 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002049
Jin Kyu Song7903c072013-10-30 03:00:12 -07002050 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002051 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002052 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2053 BITS32 : BITS64);
2054 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002055 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002056 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002057 }
2058
2059 /* Try matching again... */
2060 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002061 temp->opcode != I_none; temp++) {
2062 m = matches(temp, instruction, bits);
2063 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002064 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002065 m = MOK_GOOD;
2066 else
2067 m = MERR_INVALOP;
2068 }
2069 if (m > merr)
2070 merr = m;
2071 if (merr == MOK_GOOD)
2072 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002073 }
2074
H. Peter Anvina81655b2009-07-25 18:15:28 -07002075done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002076 *tempp = temp;
2077 return merr;
2078}
2079
Mark Charneydcaef4b2014-10-09 13:45:17 -04002080static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2081{
2082 opflags_t opsize = opflags & SIZE_MASK;
2083 uint8_t brcast_num;
2084
2085 /*
2086 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2087 * this cannot be a simple arithmetic calculation.
2088 */
2089 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002090 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002091 "size of broadcasting element is greater than 64 bits");
2092
2093 switch (opsize) {
2094 case BITS64:
2095 brcast_num = BITS64 / brsize;
2096 break;
2097 default:
2098 brcast_num = (opsize / BITS128) * (BITS64 / brsize) * 2;
2099 break;
2100 }
2101
2102 return brcast_num;
2103}
2104
H. Peter Anvin65289e82009-07-25 17:25:11 -07002105static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002106 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002107{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002108 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002109 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002110 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002111
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002112 /*
2113 * Check the opcode
2114 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002115 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002116 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002117
2118 /*
2119 * Count the operands
2120 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002121 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002122 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002123
2124 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002125 * Is it legal?
2126 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002127 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002128 return MERR_INVALOP;
2129
2130 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002131 * {evex} available?
2132 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002133 switch (instruction->prefixes[PPS_VEX]) {
2134 case P_EVEX:
2135 if (!itemp_has(itemp, IF_EVEX))
2136 return MERR_ENCMISMATCH;
2137 break;
2138 case P_VEX3:
2139 case P_VEX2:
2140 if (!itemp_has(itemp, IF_VEX))
2141 return MERR_ENCMISMATCH;
2142 break;
2143 default:
2144 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002145 }
2146
2147 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002148 * Check that no spurious colons or TOs are present
2149 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002150 for (i = 0; i < itemp->operands; i++)
2151 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002152 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002153
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002154 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002155 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002156 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002157 switch (itemp_smask(itemp)) {
2158 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002159 asize = BITS8;
2160 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002161 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002162 asize = BITS16;
2163 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002164 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002165 asize = BITS32;
2166 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002167 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002168 asize = BITS64;
2169 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002170 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002171 asize = BITS128;
2172 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002173 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002174 asize = BITS256;
2175 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002176 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002177 asize = BITS512;
2178 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002179 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002180 switch (bits) {
2181 case 16:
2182 asize = BITS16;
2183 break;
2184 case 32:
2185 asize = BITS32;
2186 break;
2187 case 64:
2188 asize = BITS64;
2189 break;
2190 default:
2191 asize = 0;
2192 break;
2193 }
2194 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002195 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002196 asize = 0;
2197 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002198 }
2199
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002200 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002201 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002202 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002203 memset(size, 0, sizeof size);
2204 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002205 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002206 /* S- flags apply to all operands */
2207 for (i = 0; i < MAX_OPERANDS; i++)
2208 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002209 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002210
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002211 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002212 * Check that the operand flags all match up,
2213 * it's a bit tricky so lets be verbose:
2214 *
2215 * 1) Find out the size of operand. If instruction
2216 * doesn't have one specified -- we're trying to
2217 * guess it either from template (IF_S* flag) or
2218 * from code bits.
2219 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002220 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002221 * template has an operand size specified AND this size differ
2222 * from which instruction has (perhaps we got it from code bits)
2223 * we are:
2224 * a) Check that only size of instruction and operand is differ
2225 * other characteristics do match
2226 * b) Perhaps it's a register specified in instruction so
2227 * for such a case we just mark that operand as "size
2228 * missing" and this will turn on fuzzy operand size
2229 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002230 */
2231 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002232 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002233 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002234 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002235 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002236 opflags_t template_opsize, insn_opsize;
2237
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002238 if (!(type & SIZE_MASK))
2239 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002240
Jin Kyu Song7903c072013-10-30 03:00:12 -07002241 insn_opsize = type & SIZE_MASK;
2242 if (!is_broadcast) {
2243 template_opsize = itemp->opd[i] & SIZE_MASK;
2244 } else {
2245 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2246 /*
2247 * when broadcasting, the element size depends on
2248 * the instruction type. decorator flag should match.
2249 */
2250
2251 if (deco_brsize) {
2252 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002253 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002254 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002255 } else {
2256 template_opsize = 0;
2257 }
2258 }
2259
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002260 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002261 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002262 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002263 } else if (template_opsize) {
2264 if (template_opsize != insn_opsize) {
2265 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002266 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002267 } else if (!is_class(REGISTER, type)) {
2268 /*
2269 * Note: we don't honor extrinsic operand sizes for registers,
2270 * so "missing operand size" for a register should be
2271 * considered a wildcard match rather than an error.
2272 */
2273 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002274 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002275 } else if (is_broadcast &&
2276 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002277 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002278 /*
2279 * broadcasting opsize matches but the number of repeated memory
2280 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002281 * if 64b double precision float is broadcasted to ymm (256b),
2282 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002283 */
2284 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002285 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002286 }
2287 }
2288
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002289 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002290 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002291
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002292 /*
2293 * Check operand sizes
2294 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002295 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2296 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002297 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002298 asize = itemp->opd[i] & SIZE_MASK;
2299 if (asize) {
2300 for (i = 0; i < oprs; i++)
2301 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002302 break;
2303 }
2304 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002305 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002306 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002307 }
2308
Keith Kaniosb7a89542007-04-12 02:40:54 +00002309 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002310 if (!(itemp->opd[i] & SIZE_MASK) &&
2311 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002312 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002313 }
2314
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002315 /*
2316 * Check template is okay at the set cpu level
2317 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002318 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002319 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002320
Keith Kaniosb7a89542007-04-12 02:40:54 +00002321 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002322 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002323 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002324 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002325 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002326
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002327 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002328 * If we have a HLE prefix, look for the NOHLE flag
2329 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002330 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002331 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2332 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2333 return MERR_BADHLE;
2334
2335 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002336 * Check if special handling needed for Jumps
2337 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002338 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002339 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340
Jin Kyu Song03041092013-10-15 19:38:51 -07002341 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002342 * Check if BND prefix is allowed.
2343 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002344 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002345 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002346 (has_prefix(instruction, PPS_REP, P_BND) ||
2347 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002348 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002349 else if (itemp_has(itemp, IF_BND) &&
2350 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2351 has_prefix(instruction, PPS_REP, P_REPNZ)))
2352 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002353
H. Peter Anvin60926242009-07-26 16:25:38 -07002354 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002355}
2356
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002357/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002358 * Check if ModR/M.mod should/can be 01.
2359 * - EAF_BYTEOFFS is set
2360 * - offset can fit in a byte when EVEX is not used
2361 * - offset can be compressed when EVEX is used
2362 */
2363#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2364 (o >= -128 && o <= 127 && \
2365 seg == NO_SEG && !forw_ref && \
2366 !(input->eaflags & EAF_WORDOFFS) && \
2367 !(ins->rex & REX_EV)) || \
2368 (ins->rex & REX_EV && \
2369 is_disp8n(input, ins, &output->disp8)))
2370
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002371static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002372 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002373{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002374 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002375 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002376 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002377
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002378 output->type = EA_SCALAR;
2379 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002380 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002381
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002382 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002383 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002384 /* EVEX.R' flag for the REG operand */
2385 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002386
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002387 if (is_class(REGISTER, input->type)) {
2388 /*
2389 * It's a direct register.
2390 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002391 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002392 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002393
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002394 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002395 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002396
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002397 /* broadcasting is not available with a direct register operand. */
2398 if (input->decoflags & BRDCAST_MASK) {
2399 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2400 goto err;
2401 }
2402
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002403 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002404 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002405 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002406 output->bytes = 0; /* no offset necessary either */
2407 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2408 } else {
2409 /*
2410 * It's a memory reference.
2411 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002412
2413 /* Embedded rounding or SAE is not available with a mem ref operand. */
2414 if (input->decoflags & (ER | SAE)) {
2415 nasm_error(ERR_NONFATAL,
2416 "Embedded rounding is available only with reg-reg op.");
2417 return -1;
2418 }
2419
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002420 if (input->basereg == -1 &&
2421 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002422 /*
2423 * It's a pure offset.
2424 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002425 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2426 input->segment == NO_SEG) {
2427 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2428 input->type &= ~IP_REL;
2429 input->type |= MEMORY;
2430 }
2431
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002432 if (bits == 64 &&
2433 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2434 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2435 return -1;
2436 }
2437
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002438 if (eaflags & EAF_BYTEOFFS ||
2439 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002440 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2441 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2442 }
2443
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002444 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002445 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002446 output->sib = GEN_SIB(0, 4, 5);
2447 output->bytes = 4;
2448 output->modrm = GEN_MODRM(0, rfield, 4);
2449 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002450 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002451 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002452 output->bytes = (addrbits != 16 ? 4 : 2);
2453 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2454 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002455 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002456 } else {
2457 /*
2458 * It's an indirection.
2459 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002460 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002461 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002462 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002463 int t, it, bt; /* register numbers */
2464 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002465
H. Peter Anvine2c80182005-01-15 22:15:51 +00002466 if (s == 0)
2467 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002468
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002469 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002470 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002471 ix = nasm_reg_flags[i];
2472 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002473 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002474 ix = 0;
2475 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002476
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002477 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002478 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002479 bx = nasm_reg_flags[b];
2480 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002481 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002482 bx = 0;
2483 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002484
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002485 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002486 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002487 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002488 int32_t o = input->offset;
2489 int mod, scale, index, base;
2490
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002491 /*
2492 * For a vector SIB, one has to be a vector and the other,
2493 * if present, a GPR. The vector must be the index operand.
2494 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002495 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002496 if (s == 0)
2497 s = 1;
2498 else if (s != 1)
2499 goto err;
2500
2501 t = bt, bt = it, it = t;
2502 x = bx, bx = ix, ix = x;
2503 }
2504
2505 if (bt != -1) {
2506 if (REG_GPR & ~bx)
2507 goto err;
2508 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2509 sok &= bx;
2510 else
2511 goto err;
2512 }
2513
2514 /*
2515 * While we're here, ensure the user didn't specify
2516 * WORD or QWORD
2517 */
2518 if (input->disp_size == 16 || input->disp_size == 64)
2519 goto err;
2520
2521 if (addrbits == 16 ||
2522 (addrbits == 32 && !(sok & BITS32)) ||
2523 (addrbits == 64 && !(sok & BITS64)))
2524 goto err;
2525
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002526 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2527 : ((ix & YMMREG & ~REG_EA)
2528 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002529
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002530 output->rex |= rexflags(it, ix, REX_X);
2531 output->rex |= rexflags(bt, bx, REX_B);
2532 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002533
2534 index = it & 7; /* it is known to be != -1 */
2535
2536 switch (s) {
2537 case 1:
2538 scale = 0;
2539 break;
2540 case 2:
2541 scale = 1;
2542 break;
2543 case 4:
2544 scale = 2;
2545 break;
2546 case 8:
2547 scale = 3;
2548 break;
2549 default: /* then what the smeg is it? */
2550 goto err; /* panic */
2551 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002552
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002553 if (bt == -1) {
2554 base = 5;
2555 mod = 0;
2556 } else {
2557 base = (bt & 7);
2558 if (base != REG_NUM_EBP && o == 0 &&
2559 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002560 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002561 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002562 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002563 mod = 1;
2564 else
2565 mod = 2;
2566 }
2567
2568 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002569 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2570 output->modrm = GEN_MODRM(mod, rfield, 4);
2571 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002572 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002573 /*
2574 * it must be a 32/64-bit memory reference. Firstly we have
2575 * to check that all registers involved are type E/Rxx.
2576 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002577 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002578 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002579
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002580 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002581 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2582 sok &= ix;
2583 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002584 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002585 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002586
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002587 if (bt != -1) {
2588 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002589 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002590 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002591 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002592 sok &= bx;
2593 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002594
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002595 /*
2596 * While we're here, ensure the user didn't specify
2597 * WORD or QWORD
2598 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002599 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002600 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002601
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002602 if (addrbits == 16 ||
2603 (addrbits == 32 && !(sok & BITS32)) ||
2604 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002605 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002606
Keith Kaniosb7a89542007-04-12 02:40:54 +00002607 /* now reorganize base/index */
2608 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002609 ((hb == b && ht == EAH_NOTBASE) ||
2610 (hb == i && ht == EAH_MAKEBASE))) {
2611 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002612 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002613 x = bx, bx = ix, ix = x;
2614 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002615
Jin Kyu Song164d6072013-10-15 19:10:13 -07002616 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002617 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002618 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002619 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002620 if (eaflags & EAF_MIB) {
2621 /* only for mib operands */
2622 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2623 /*
2624 * make a single reg index [reg*1].
2625 * gas uses this form for an explicit index register.
2626 */
2627 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2628 }
2629 if ((ht == EAH_SUMMED) && bt == -1) {
2630 /* separate once summed index into [base, index] */
2631 bt = it, bx = ix, s--;
2632 }
2633 } else {
2634 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002635 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002636 s == 3 || s == 5 || s == 9) && bt == -1) {
2637 /* convert 3*EAX to EAX+2*EAX */
2638 bt = it, bx = ix, s--;
2639 }
2640 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002641 (eaflags & EAF_TIMESTWO) &&
2642 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002643 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002644 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002645 * to sib format with 0x0 displacement - [EAX*1+0].
2646 */
2647 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2648 }
2649 }
Keith Kanios48af1772007-08-17 07:37:52 +00002650 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002651 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002652 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002653 x = ix, ix = bx, bx = x;
2654 }
2655 if (it == REG_NUM_ESP ||
2656 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002657 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002658
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002659 output->rex |= rexflags(it, ix, REX_X);
2660 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002661
Keith Kanios48af1772007-08-17 07:37:52 +00002662 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002663 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002664 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002665
Keith Kaniosb7a89542007-04-12 02:40:54 +00002666 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002667 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002668 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002669 } else {
2670 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002671 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002672 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002673 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002674 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002675 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002676 mod = 1;
2677 else
2678 mod = 2;
2679 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002680
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002681 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002682 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2683 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002684 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002685 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002686 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002687
Keith Kaniosb7a89542007-04-12 02:40:54 +00002688 if (it == -1)
2689 index = 4, s = 1;
2690 else
2691 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002692
H. Peter Anvine2c80182005-01-15 22:15:51 +00002693 switch (s) {
2694 case 1:
2695 scale = 0;
2696 break;
2697 case 2:
2698 scale = 1;
2699 break;
2700 case 4:
2701 scale = 2;
2702 break;
2703 case 8:
2704 scale = 3;
2705 break;
2706 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002707 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002708 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002709
Keith Kaniosb7a89542007-04-12 02:40:54 +00002710 if (bt == -1) {
2711 base = 5;
2712 mod = 0;
2713 } else {
2714 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002715 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002716 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002717 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002718 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002719 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002720 mod = 1;
2721 else
2722 mod = 2;
2723 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002724
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002725 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002726 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2727 output->modrm = GEN_MODRM(mod, rfield, 4);
2728 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002729 }
2730 } else { /* it's 16-bit */
2731 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002732 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002733
Keith Kaniosb7a89542007-04-12 02:40:54 +00002734 /* check for 64-bit long mode */
2735 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002736 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002737
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002739 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2740 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002741 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002742
Keith Kaniosb7a89542007-04-12 02:40:54 +00002743 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002744 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002745 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002746
H. Peter Anvine2c80182005-01-15 22:15:51 +00002747 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002748 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002749 if (b == -1 && i != -1) {
2750 int tmp = b;
2751 b = i;
2752 i = tmp;
2753 } /* swap */
2754 if ((b == R_SI || b == R_DI) && i != -1) {
2755 int tmp = b;
2756 b = i;
2757 i = tmp;
2758 }
2759 /* have BX/BP as base, SI/DI index */
2760 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002761 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762 if (i != -1 && b != -1 &&
2763 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002764 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002765 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002766 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002767
H. Peter Anvine2c80182005-01-15 22:15:51 +00002768 rm = -1;
2769 if (i != -1)
2770 switch (i * 256 + b) {
2771 case R_SI * 256 + R_BX:
2772 rm = 0;
2773 break;
2774 case R_DI * 256 + R_BX:
2775 rm = 1;
2776 break;
2777 case R_SI * 256 + R_BP:
2778 rm = 2;
2779 break;
2780 case R_DI * 256 + R_BP:
2781 rm = 3;
2782 break;
2783 } else
2784 switch (b) {
2785 case R_SI:
2786 rm = 4;
2787 break;
2788 case R_DI:
2789 rm = 5;
2790 break;
2791 case R_BP:
2792 rm = 6;
2793 break;
2794 case R_BX:
2795 rm = 7;
2796 break;
2797 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002798 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002799 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002800
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002801 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002802 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002804 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 mod = 1;
2806 else
2807 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002808
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002809 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002810 output->bytes = mod; /* bytes of offset needed */
2811 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812 }
2813 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002814 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002815
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002816 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002817 return output->type;
2818
2819err:
2820 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002821}
2822
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002823static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002824{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002825 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002826 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002827
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002828 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002829
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002830 switch (ins->prefixes[PPS_ASIZE]) {
2831 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002832 valid &= 16;
2833 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002834 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002835 valid &= 32;
2836 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002837 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002838 valid &= 64;
2839 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002840 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002841 valid &= (addrbits == 32) ? 16 : 32;
2842 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002843 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002844 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002845 }
2846
2847 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002848 if (is_class(MEMORY, ins->oprs[j].type)) {
2849 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002850
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002851 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002852 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002853 i = 0;
2854 else
2855 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002856
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002857 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002858 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002859 b = 0;
2860 else
2861 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002862
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002863 if (ins->oprs[j].scale == 0)
2864 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002865
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002866 if (!i && !b) {
2867 int ds = ins->oprs[j].disp_size;
2868 if ((addrbits != 64 && ds > 8) ||
2869 (addrbits == 64 && ds == 16))
2870 valid &= ds;
2871 } else {
2872 if (!(REG16 & ~b))
2873 valid &= 16;
2874 if (!(REG32 & ~b))
2875 valid &= 32;
2876 if (!(REG64 & ~b))
2877 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002878
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002879 if (!(REG16 & ~i))
2880 valid &= 16;
2881 if (!(REG32 & ~i))
2882 valid &= 32;
2883 if (!(REG64 & ~i))
2884 valid &= 64;
2885 }
2886 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002887 }
2888
2889 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002890 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002891 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002892 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002893 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002894 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002895 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002896 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002897 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002898 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002899 }
2900
2901 defdisp = ins->addr_size == 16 ? 16 : 32;
2902
2903 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002904 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2905 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2906 /*
2907 * mem_offs sizes must match the address size; if not,
2908 * strip the MEM_OFFS bit and match only EA instructions
2909 */
2910 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2911 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002912 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002913}