blob: 7bb34bb7e8183d34d9a05fa7fdafe63a1472f750 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvin164d2462017-02-20 02:39:56 -08003 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040037 * Bytecode specification
38 * ----------------------
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070039 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040040 *
41 * Codes Mnemonic Explanation
42 *
43 * \0 terminates the code. (Unless it's a literal of course.)
44 * \1..\4 that many literal bytes follow in the code stream
45 * \5 add 4 to the primary operand number (b, low octdigit)
46 * \6 add 4 to the secondary operand number (a, middle octdigit)
47 * \7 add 4 to both the primary and the secondary operand number
48 * \10..\13 a literal byte follows in the code stream, to be added
49 * to the register value of operand 0..3
50 * \14..\17 the position of index register operand in MIB (BND insns)
51 * \20..\23 ib a byte immediate operand, from operand 0..3
52 * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
53 * \30..\33 iw a word immediate operand, from operand 0..3
54 * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
55 * assembly mode or the operand-size override on the operand
56 * \40..\43 id a long immediate operand, from operand 0..3
57 * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
58 * depending on the address size of the instruction.
59 * \50..\53 rel8 a byte relative operand, from operand 0..3
60 * \54..\57 iq a qword immediate operand, from operand 0..3
61 * \60..\63 rel16 a word relative operand, from operand 0..3
62 * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
63 * assembly mode or the operand-size override on the operand
64 * \70..\73 rel32 a long relative operand, from operand 0..3
65 * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
66 * \1ab a ModRM, calculated on EA in operand a, with the spare
67 * field the register value of operand b.
68 * \172\ab the register number from operand a in bits 7..4, with
69 * the 4-bit immediate from operand b in bits 3..0.
70 * \173\xab the register number from operand a in bits 7..4, with
71 * the value b in bits 3..0.
72 * \174..\177 the register number from operand 0..3 in bits 7..4, and
73 * an arbitrary value in bits 3..0 (assembled as zero.)
74 * \2ab a ModRM, calculated on EA in operand a, with the spare
75 * field equal to digit b.
76 *
77 * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
78 * V field taken from operand 0..3.
79 * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
80 * V field set to 1111b.
81 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070082 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -070084 * cc 00m mmm
85 * c = 2 for EVEX and mmmm is the M field (EVEX.P0[3:0])
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070086 * and wlp is:
87 * 00 wwl lpp
88 * [l0] ll = 0 (.128, .lz)
89 * [l1] ll = 1 (.256)
90 * [l2] ll = 2 (.512)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
92 *
93 * [w0] ww = 0 for W = 0
94 * [w1] ww = 1 for W = 1
95 * [wig] ww = 2 for W don't care (always assembled as 0)
96 * [ww] ww = 3 for W used as REX.W
97 *
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
100 * [f3] pp = 2
101 * [f2] pp = 3
102 *
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
105 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400106 * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
107 * \260..\263 this instruction uses VEX/XOP rather than REX, with the
108 * V field taken from operand 0..3.
109 * \270 this instruction uses VEX/XOP rather than REX, with the
110 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700111 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700114 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700115 * [l0] ll = 0 for L = 0 (.128, .lz)
116 * [l1] ll = 1 for L = 1 (.256)
117 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700118 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700119 * [w0] ww = 0 for W = 0
120 * [w1 ] ww = 1 for W = 1
121 * [wig] ww = 2 for W don't care (always assembled as 0)
122 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700123 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700125 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400126 * \271 hlexr instruction takes XRELEASE (F3) with or without lock
127 * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
128 * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
129 * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
130 * to the operand size (if o16/o32/o64 present) or the bit size
131 * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
132 * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
133 * \312 adf (disassembler only) invalid with non-default address size.
134 * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
135 * \314 norexb (disassembler only) invalid with REX.B
136 * \315 norexx (disassembler only) invalid with REX.X
137 * \316 norexr (disassembler only) invalid with REX.R
138 * \317 norexw (disassembler only) invalid with REX.W
139 * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
140 * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
141 * \322 odf indicates that this instruction is only valid when the
142 * operand size is the default (instruction to disassembler,
143 * generates no code in the assembler)
144 * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
145 * \324 o64 indicates 64-bit operand size requiring REX prefix.
146 * \325 nohi instruction which always uses spl/bpl/sil/dil
147 * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
148 disassembler only; for SSE instructions.
149 * \330 a literal byte follows in the code stream, to be added
150 * to the condition code value of the instruction.
151 * \331 norep instruction not valid with REP prefix. Hint for
152 * disassembler only; for SSE instructions.
153 * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
154 * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
155 * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
156 * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
157 * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
158 * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
159 * \336-\337 are still listed as prefixes in the disassembler.
160 * \340 resb reserve <operand 0> bytes of uninitialized storage.
161 * Operand 0 had better be a segmentless constant.
162 * \341 wait this instruction needs a WAIT "prefix"
Cyrill Gorcunov8a5d3e62014-08-25 20:04:30 +0400163 * \360 np no SSE prefix (== \364\331)
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400164 * \361 66 SSE prefix (== \366\331)
165 * \364 !osp operand-size prefix (0x66) not permitted
166 * \365 !asp address-size prefix (0x67) not permitted
167 * \366 operand-size prefix (0x66) used as opcode extension
168 * \367 address-size prefix (0x67) used as opcode extension
169 * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
170 * jmp8 370 is used for Jcc, 371 is used for JMP.
171 * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
172 * used for conditional jump over longer jump
173 * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
174 * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
175 * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 */
177
H. Peter Anvinfe501952007-10-02 21:53:51 -0700178#include "compiler.h"
179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180#include <stdio.h>
181#include <string.h>
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800182#include <stdlib.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000183
184#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000185#include "nasmlib.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000186#include "assemble.h"
187#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700188#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800189#include "disp8.h"
H. Peter Anvin172b8402016-02-18 01:16:18 -0800190#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000191
H. Peter Anvin65289e82009-07-25 17:25:11 -0700192enum match_result {
193 /*
194 * Matching errors. These should be sorted so that more specific
195 * errors come later in the sequence.
196 */
197 MERR_INVALOP,
198 MERR_OPSIZEMISSING,
199 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700200 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700201 MERR_BADCPU,
202 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800203 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700204 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700205 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800206 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700207 /*
208 * Matching success; the conditional ones first
209 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400210 MOK_JUMP, /* Matching OK but needs jmp_match() */
211 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700212};
213
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700215 enum ea_type type; /* what kind of EA is this? */
216 int sib_present; /* is a SIB byte necessary? */
217 int bytes; /* # of bytes of offset needed */
218 int size; /* lazy - this is sib+bytes+1 */
219 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700220 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221} ea;
222
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400223#define GEN_SIB(scale, index, base) \
224 (((scale) << 6) | ((index) << 3) | ((base)))
225
226#define GEN_MODRM(mod, reg, rm) \
227 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
228
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400229static iflag_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000230
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800231static int64_t calcsize(int32_t, int64_t, int, insn *,
232 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700233static int emit_prefix(struct out_data *data, const int bits, insn *ins);
234static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700235static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400236 insn *instruction,
237 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700238static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700239static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000240static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700241static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000242static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700243static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700244static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000245
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700246static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700247
H. Peter Anvin164d2462017-02-20 02:39:56 -0800248static inline bool absolute_op(const struct operand *o)
249{
250 return o->segment == NO_SEG && o->wrt == NO_SEG &&
251 !(o->opflags & OPFLAG_RELATIVE);
252}
253
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400254static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000255{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700256 return ins->prefixes[pos] == prefix;
257}
258
259static void assert_no_prefix(insn * ins, enum prefix_pos pos)
260{
261 if (ins->prefixes[pos])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800262 nasm_error(ERR_NONFATAL, "invalid %s prefix",
263 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700264}
265
266static const char *size_name(int size)
267{
268 switch (size) {
269 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700271 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700273 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700275 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400276 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700277 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400278 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700279 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400280 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700281 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400282 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700283 case 64:
284 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700285 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400286 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000287 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700288}
289
H. Peter Anvin285222f2017-03-01 13:27:33 -0800290static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400291{
H. Peter Anvin285222f2017-03-01 13:27:33 -0800292 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400293 "%s data exceeds bounds", size_name(size));
294}
295
296static void warn_overflow_const(int64_t data, int size)
297{
298 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800299 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400300}
301
302static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700303{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800304 if (absolute_op(o)) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400305 if (overflow_general(o->offset, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800306 warn_overflow(size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700307 }
308}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400309
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800310static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
311{
312 bool err;
313
314 switch (sign) {
315 case OUT_WRAP:
316 err = overflow_general(data, size);
317 break;
318 case OUT_SIGNED:
319 err = overflow_signed(data, size);
320 break;
321 case OUT_UNSIGNED:
322 err = overflow_unsigned(data, size);
323 break;
324 default:
325 panic();
326 break;
327 }
328
329 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800330 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800331}
332
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000333/*
334 * This routine wrappers the real output format's output routine,
335 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800336 * generator at the same time, flatten unnecessary relocations,
337 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000338 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700339static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000340{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000341 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700342 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700343 int asize;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800344 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700345 union {
346 uint8_t b[8];
347 uint64_t q;
348 } xdata;
349 uint64_t size = data->size;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800350 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800351 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000352
H. Peter Anvina77692b2016-09-20 14:04:33 -0700353 if (!data->size)
354 return; /* Nothing to do */
355
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700356 /*
357 * Convert addresses to RAWDATA if possible
358 * XXX: not all backends want this for global symbols!!!!
359 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700360 switch (data->type) {
361 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800362 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800363 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800364 goto address;
365
366 case OUT_RELADDR:
367 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800368 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800369 goto address;
370
371 address:
H. Peter Anvina77692b2016-09-20 14:04:33 -0700372 asize = data->size;
373 nasm_assert(asize <= 8);
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800374 if (data->tsegment == fixseg && data->twrt == NO_SEG) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700375 uint8_t *q = xdata.b;
376
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800377 warn_overflow_out(addrval, asize, data->sign);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700378
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800379 WRITEADDR(q, addrval, asize);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700380 data->data = xdata.b;
381 data->type = OUT_RAWDATA;
382 asize = 0; /* No longer an address */
383 }
384 break;
385
386 default:
387 asize = 0; /* Not an address */
388 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000389 }
390
H. Peter Anvina77692b2016-09-20 14:04:33 -0700391 lfmt->output(data);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800392
Frank Kotlerabebb082003-09-06 04:45:37 +0000393 /*
394 * this call to src_get determines when we call the
395 * debug-format-specific "linenum" function
396 * it updates lineno and lnfname to the current values
397 * returning 0 if "same as last time", -2 if lnfname
398 * changed, and the amount by which lineno changed,
399 * if it did. thus, these variables must be static
400 */
401
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400402 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700403 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000404
H. Peter Anvinb6412502016-02-11 21:07:40 -0800405 if (asize && asize > amax) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700406 if (data->type != OUT_ADDRESS || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800407 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800408 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800409 asize << 3, ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800410 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800411 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800412 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800413 asize << 3, ofmt->maxbits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700414 data->size = amax;
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700415 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700416 data->insoffs += amax;
417 data->offset += amax;
418 data->size = size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800419 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700420 data->data = zero_buffer;
421 data->type = OUT_RAWDATA;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800422 }
423
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700424 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700425 data->offset += size;
426 data->insoffs += size;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000427}
428
H. Peter Anvina77692b2016-09-20 14:04:33 -0700429static inline void out_rawdata(struct out_data *data, const void *rawdata,
430 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400431{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700432 data->type = OUT_RAWDATA;
433 data->data = rawdata;
434 data->size = size;
435 out(data);
436}
437
438static void out_rawbyte(struct out_data *data, uint8_t byte)
439{
440 data->type = OUT_RAWDATA;
441 data->data = &byte;
442 data->size = 1;
443 out(data);
444}
445
446static inline void out_reserve(struct out_data *data, uint64_t size)
447{
448 data->type = OUT_RESERVE;
449 data->size = size;
450 out(data);
451}
452
H. Peter Anvin164d2462017-02-20 02:39:56 -0800453static inline void out_imm(struct out_data *data, const struct operand *opx,
H. Peter Anvina77692b2016-09-20 14:04:33 -0700454 int size, enum out_sign sign)
455{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800456 data->type =
457 (opx->opflags & OPFLAG_RELATIVE) ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700458 data->sign = sign;
459 data->size = size;
460 data->toffset = opx->offset;
461 data->tsegment = opx->segment;
462 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800463 /*
464 * XXX: improve this if at some point in the future we can
465 * distinguish the subtrahend in expressions like [foo - bar]
466 * where bar is a symbol in the current segment. However, at the
467 * current point, if OPFLAG_RELATIVE is set that subtraction has
468 * already occurred.
469 */
470 data->relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700471 out(data);
472}
473
H. Peter Anvin164d2462017-02-20 02:39:56 -0800474static void out_reladdr(struct out_data *data, const struct operand *opx,
475 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700476{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800477 if (opx->opflags & OPFLAG_RELATIVE)
478 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
479
H. Peter Anvina77692b2016-09-20 14:04:33 -0700480 data->type = OUT_RELADDR;
481 data->sign = OUT_SIGNED;
482 data->size = size;
483 data->toffset = opx->offset;
484 data->tsegment = opx->segment;
485 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800486 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700487 out(data);
488}
489
H. Peter Anvin164d2462017-02-20 02:39:56 -0800490static inline void out_segment(struct out_data *data,
491 const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700492{
493 data->type = OUT_SEGMENT;
494 data->sign = OUT_UNSIGNED;
495 data->size = 2;
496 data->toffset = opx->offset;
497 data->tsegment = ofmt->segbase(opx->segment + 1);
498 data->twrt = opx->wrt;
499 out(data);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400500}
501
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700502static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800503 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000504{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800505 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800506 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000507 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800508 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000509
H. Peter Anvin755f5212012-02-25 11:41:34 -0800510 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700511 return false;
512 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400513 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700514 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400515 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700516
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800517 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100518
Victor van den Elzen154e5922009-02-25 17:32:00 +0100519 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100520 /* Be optimistic in pass 1 */
521 return true;
522
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700524 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000525
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700526 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800527 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
528
529 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
530 /* jmp short (opcode eb) cannot be used with bnd prefix. */
531 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800532 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800533 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800534 }
535
536 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000537}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000538
H. Peter Anvin04445362016-09-21 15:56:19 -0700539/* This is totally just a wild guess what is reasonable... */
540#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
541
H. Peter Anvina77692b2016-09-20 14:04:33 -0700542int64_t assemble(int32_t segment, int64_t start, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800543 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000544{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700545 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000546 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700547 enum match_result m;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000548 int32_t itimes;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300549 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000550
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000551 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000552
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800553 nasm_zero(&data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700554 data.offset = start;
555 data.segment = segment;
556 data.itemp = NULL;
557 data.sign = OUT_WRAP;
558 data.bits = bits;
559
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300560 wsize = idata_bytes(instruction->opcode);
561 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000562 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000563
H. Peter Anvineba20a72002-04-30 20:53:55 +0000564 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000565 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000566 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000567 if (t < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800568 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000569
H. Peter Anvine2c80182005-01-15 22:15:51 +0000570 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400571 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000572 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400573 if (wsize > 8) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800574 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400575 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000576 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700577 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700578 data.insoffs = 0;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700579 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700580 data.inslen = data.size = wsize;
581 data.toffset = e->offset;
582 data.tsegment = e->segment;
583 data.twrt = e->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800584 data.relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700585 out(&data);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400586 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700587 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400588 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700589 int align = e->stringlen % wsize;
590 if (align)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000591 align = wsize - align;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700592
593 data.insoffs = 0;
594 data.inslen = e->stringlen + align;
595
596 out_rawdata(&data, e->stringval, e->stringlen);
597 out_rawdata(&data, zero_buffer, align);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000598 }
599 }
600 if (t > 0 && t == instruction->times - 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700601 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800602 lfmt->uplevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 }
604 }
605 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800606 lfmt->downlevel(LIST_TIMES);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700607 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700608 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 FILE *fp;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700610 size_t t = instruction->times;
611 off_t base = 0;
612 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700613 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700614 char *buf = NULL;
615 size_t blk = 0; /* Buffered I/O block size */
616 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000617
H. Peter Anvind81a2352016-09-21 14:03:18 -0700618 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400619 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800620 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700622 goto done;
623 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000624
H. Peter Anvind81a2352016-09-21 14:03:18 -0700625 len = nasm_file_size(fp);
626
627 if (len == (off_t)-1) {
628 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700629 fname);
630 goto close_done;
631 }
632
H. Peter Anvina77692b2016-09-20 14:04:33 -0700633 if (instruction->eops->next) {
634 base = instruction->eops->next->offset;
635 if (base >= len) {
636 len = 0;
637 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 len -= base;
639 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700640 len > (off_t)instruction->eops->next->next->offset)
641 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000642 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000643 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700644
H. Peter Anvina77692b2016-09-20 14:04:33 -0700645 lfmt->set_offset(data.offset);
646 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000647
H. Peter Anvind81a2352016-09-21 14:03:18 -0700648 if (!len)
649 goto end_incbin;
650
651 /* Try to map file data */
652 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700653 if (!map) {
654 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
655 buf = nasm_malloc(blk);
656 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700657
658 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700659 /*
660 * Consider these irrelevant for INCBIN, since it is fully
661 * possible that these might be (way) bigger than an int
662 * can hold; there is, however, no reason to widen these
663 * types just for INCBIN. data.inslen == 0 signals to the
664 * backend that these fields are meaningless, if at all
665 * needed.
666 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700667 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700668 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700669
H. Peter Anvind81a2352016-09-21 14:03:18 -0700670 if (map) {
671 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700672 } else if ((off_t)m == len) {
673 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700674 } else {
675 off_t l = len;
676
677 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700678 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700679 "`incbin': unable to seek on file `%s'",
680 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700681 goto end_incbin;
682 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700683 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700684 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700685 if (!m || feof(fp)) {
686 /*
687 * This shouldn't happen unless the file
688 * actually changes while we are reading
689 * it.
690 */
691 nasm_error(ERR_NONFATAL,
692 "`incbin': unexpected EOF while"
693 " reading file `%s'", fname);
694 goto end_incbin;
695 }
696 out_rawdata(&data, buf, m);
697 l -= m;
698 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700699 }
700 }
701 end_incbin:
702 lfmt->downlevel(LIST_INCBIN);
703 if (instruction->times > 1) {
704 lfmt->set_offset(data.offset);
705 lfmt->uplevel(LIST_TIMES);
706 lfmt->downlevel(LIST_TIMES);
707 }
708 if (ferror(fp)) {
709 nasm_error(ERR_NONFATAL,
710 "`incbin': error while"
711 " reading file `%s'", fname);
712 }
713 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700714 if (buf)
715 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700716 if (map)
717 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700718 fclose(fp);
719 done:
720 ;
721 } else {
722 /* "Real" instruction */
723
724 /* Check to see if we need an address-size prefix */
725 add_asp(instruction, bits);
726
727 m = find_match(&temp, instruction, data.segment, data.offset, bits);
728
729 if (m == MOK_GOOD) {
730 /* Matches! */
731 int64_t insn_size = calcsize(data.segment, data.offset,
732 bits, instruction, temp);
733 itimes = instruction->times;
734 if (insn_size < 0) /* shouldn't be, on pass two */
735 nasm_panic(0, "errors made it through from pass one");
736
737 data.itemp = temp;
738 data.bits = bits;
739
740 while (itimes--) {
741 data.insoffs = 0;
742 data.inslen = insn_size;
743
744 gencode(&data, instruction);
745 nasm_assert(data.insoffs == insn_size);
746
747 if (itimes > 0 && itimes == instruction->times - 1) {
748 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800749 lfmt->uplevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400750 }
751 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700752 if (instruction->times > 1)
753 lfmt->downlevel(LIST_TIMES);
754 } else {
755 /* No match */
756 switch (m) {
757 case MERR_OPSIZEMISSING:
758 nasm_error(ERR_NONFATAL, "operation size not specified");
759 break;
760 case MERR_OPSIZEMISMATCH:
761 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
762 break;
763 case MERR_BRNUMMISMATCH:
764 nasm_error(ERR_NONFATAL,
765 "mismatch in the number of broadcasting elements");
766 break;
767 case MERR_BADCPU:
768 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
769 break;
770 case MERR_BADMODE:
771 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
772 bits);
773 break;
774 case MERR_ENCMISMATCH:
775 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
776 break;
777 case MERR_BADBND:
778 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
779 break;
780 case MERR_BADREPNE:
781 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
782 (has_prefix(instruction, PPS_REP, P_REPNE) ?
783 "repne" : "repnz"));
784 break;
785 default:
786 nasm_error(ERR_NONFATAL,
787 "invalid combination of opcode and operands");
788 break;
789 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400790 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000791 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700792 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000793}
794
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400795int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
H. Peter Anvin215186f2016-02-17 20:27:41 -0800796 insn * instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000797{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000798 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700799 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000800
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000801 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000802
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400803 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000805
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700806 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
807 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400808 instruction->opcode == I_DT || instruction->opcode == I_DO ||
809 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300811 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000812
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300814 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000815
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400816 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000817 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000818
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400820 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400822 warn_overflow_const(e->offset, wsize);
823 } else if (e->type == EOT_DB_STRING ||
824 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000826
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 align = (-osize) % wsize;
828 if (align < 0)
829 align += wsize;
830 isize += osize + align;
831 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700832 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000833 }
834
H. Peter Anvine2c80182005-01-15 22:15:51 +0000835 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400836 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700837 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000838
H. Peter Anvind81a2352016-09-21 14:03:18 -0700839 len = nasm_file_size_by_path(fname);
840 if (len == (off_t)-1) {
841 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
842 fname);
843 return 0;
844 }
845
846 if (instruction->eops->next) {
847 if (len <= (off_t)instruction->eops->next->offset) {
848 len = 0;
849 } else {
850 len -= instruction->eops->next->offset;
851 if (instruction->eops->next->next &&
852 len > (off_t)instruction->eops->next->next->offset) {
853 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000854 }
855 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000856 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700857
858 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000859 }
860
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700861 /* Check to see if we need an address-size prefix */
862 add_asp(instruction, bits);
863
H. Peter Anvin23595f52009-07-25 17:44:25 -0700864 m = find_match(&temp, instruction, segment, offset, bits);
865 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400866 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700867 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700868 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400869 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000870 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000871}
872
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800873static void bad_hle_warn(const insn * ins, uint8_t hleok)
874{
875 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800876 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800877 static const enum whatwarn warn[2][4] =
878 {
879 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
880 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
881 };
882 unsigned int n;
883
884 n = (unsigned int)rep_pfx - P_XACQUIRE;
885 if (n > 1)
886 return; /* Not XACQUIRE/XRELEASE */
887
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800888 ww = warn[n][hleok];
889 if (!is_class(MEMORY, ins->oprs[0].type))
890 ww = w_inval; /* HLE requires operand 0 to be memory */
891
892 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800893 case w_none:
894 break;
895
896 case w_lock:
897 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800898 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800899 "%s with this instruction requires lock",
900 prefix_name(rep_pfx));
901 }
902 break;
903
904 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800905 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800906 "%s invalid with this instruction",
907 prefix_name(rep_pfx));
908 break;
909 }
910}
911
H. Peter Anvin507ae032008-10-09 15:37:10 -0700912/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400913#define case3(x) case (x): case (x)+1: case (x)+2
914#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700915
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800916static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800917 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000918{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800919 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800920 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000921 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000922 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700923 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700924 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700925 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700926 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800927 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800928 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700929 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000930
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700931 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700932 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700933 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700934
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700935 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400936 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700937
H. Peter Anvine2c80182005-01-15 22:15:51 +0000938 (void)segment; /* Don't warn that this parameter is unused */
939 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000940
H. Peter Anvin839eca22007-10-29 23:12:47 -0700941 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400942 c = *codes++;
943 op1 = (c & 3) + ((opex & 1) << 2);
944 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
945 opx = &ins->oprs[op1];
946 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700947
H. Peter Anvin839eca22007-10-29 23:12:47 -0700948 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400949 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000950 codes += c, length += c;
951 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700952
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400953 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400954 opex = c;
955 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700956
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400957 case4(010):
958 ins->rex |=
959 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960 codes++, length++;
961 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700962
Jin Kyu Song164d6072013-10-15 19:10:13 -0700963 case4(014):
964 /* this is an index reg of MIB operand */
965 mib_index = opx->basereg;
966 break;
967
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400968 case4(020):
969 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 length++;
971 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700972
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400973 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 length += 2;
975 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700976
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400977 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700978 if (opx->type & (BITS16 | BITS32 | BITS64))
979 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 else
981 length += (bits == 16) ? 2 : 4;
982 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700983
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400984 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 length += 4;
986 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700987
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400988 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700989 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700991
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400992 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 length++;
994 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700995
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400996 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000997 length += 8; /* MOV reg64/imm */
998 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700999
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001000 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 length += 2;
1002 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001003
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001004 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001005 if (opx->type & (BITS16 | BITS32 | BITS64))
1006 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001007 else
1008 length += (bits == 16) ? 2 : 4;
1009 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001010
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001011 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 length += 4;
1013 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001014
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001015 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001016 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001018
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001019 case 0172:
1020 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001021 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001022 length++;
1023 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001024
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001025 case4(0174):
1026 length++;
1027 break;
1028
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001029 case4(0240):
1030 ins->rex |= REX_EV;
1031 ins->vexreg = regval(opx);
1032 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1033 ins->vex_cm = *codes++;
1034 ins->vex_wlp = *codes++;
1035 ins->evex_tuple = (*codes++ - 0300);
1036 break;
1037
1038 case 0250:
1039 ins->rex |= REX_EV;
1040 ins->vexreg = 0;
1041 ins->vex_cm = *codes++;
1042 ins->vex_wlp = *codes++;
1043 ins->evex_tuple = (*codes++ - 0300);
1044 break;
1045
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001046 case4(0254):
1047 length += 4;
1048 break;
1049
1050 case4(0260):
1051 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001052 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001053 ins->vex_cm = *codes++;
1054 ins->vex_wlp = *codes++;
1055 break;
1056
1057 case 0270:
1058 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001059 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001060 ins->vex_cm = *codes++;
1061 ins->vex_wlp = *codes++;
1062 break;
1063
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001064 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001065 hleok = c & 3;
1066 break;
1067
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001068 case4(0274):
1069 length++;
1070 break;
1071
1072 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001073 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001074
H. Peter Anvine2c80182005-01-15 22:15:51 +00001075 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001076 if (bits == 64)
1077 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001078 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001079 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001080
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001082 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001084
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001086 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001087
Keith Kaniosb7a89542007-04-12 02:40:54 +00001088 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001089 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1090 has_prefix(ins, PPS_ASIZE, P_A32))
1091 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001093
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001094 case4(0314):
1095 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001096
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001098 {
1099 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1100 if (pfx == P_O16)
1101 break;
1102 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001103 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001104 else
1105 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001106 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001107 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001108
H. Peter Anvine2c80182005-01-15 22:15:51 +00001109 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001110 {
1111 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1112 if (pfx == P_O32)
1113 break;
1114 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001115 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001116 else
1117 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001118 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001119 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001120
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 case 0322:
1122 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001123
Keith Kaniosb7a89542007-04-12 02:40:54 +00001124 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001125 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001126 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001127
Keith Kaniosb7a89542007-04-12 02:40:54 +00001128 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001129 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001130 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001131
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001132 case 0325:
1133 ins->rex |= REX_NH;
1134 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001135
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001136 case 0326:
1137 break;
1138
H. Peter Anvine2c80182005-01-15 22:15:51 +00001139 case 0330:
1140 codes++, length++;
1141 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001142
H. Peter Anvine2c80182005-01-15 22:15:51 +00001143 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001144 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001145
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001146 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001147 case 0333:
1148 length++;
1149 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001150
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001151 case 0334:
1152 ins->rex |= REX_L;
1153 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001154
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001155 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001156 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001157
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001158 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001159 if (!ins->prefixes[PPS_REP])
1160 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001161 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001162
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001163 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001164 if (!ins->prefixes[PPS_REP])
1165 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001166 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001167
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001169 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001170 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001171 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001172 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1173 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001174 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001175 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001176 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001177 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001178
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001179 case 0341:
1180 if (!ins->prefixes[PPS_WAIT])
1181 ins->prefixes[PPS_WAIT] = P_WAIT;
1182 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001183
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001184 case 0360:
1185 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001186
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001187 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001188 length++;
1189 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001190
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001191 case 0364:
1192 case 0365:
1193 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001194
Keith Kanios48af1772007-08-17 07:37:52 +00001195 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001196 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001197 length++;
1198 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001199
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001200 case 0370:
1201 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001202 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001203
H. Peter Anvine2c80182005-01-15 22:15:51 +00001204 case 0373:
1205 length++;
1206 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001207
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001208 case 0374:
1209 eat = EA_XMMVSIB;
1210 break;
1211
1212 case 0375:
1213 eat = EA_YMMVSIB;
1214 break;
1215
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001216 case 0376:
1217 eat = EA_ZMMVSIB;
1218 break;
1219
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001220 case4(0100):
1221 case4(0110):
1222 case4(0120):
1223 case4(0130):
1224 case4(0200):
1225 case4(0204):
1226 case4(0210):
1227 case4(0214):
1228 case4(0220):
1229 case4(0224):
1230 case4(0230):
1231 case4(0234):
1232 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001233 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001234 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001235 opflags_t rflags;
1236 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001237 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001238
Keith Kaniosb7a89542007-04-12 02:40:54 +00001239 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001240
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001241 if (c <= 0177) {
1242 /* pick rfield from operand b (opx) */
1243 rflags = regflag(opx);
1244 rfield = nasm_regvals[opx->basereg];
1245 } else {
1246 rflags = 0;
1247 rfield = c & 7;
1248 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001249
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001250 /* EVEX.b1 : evex_brerop contains the operand position */
1251 op_er_sae = (ins->evex_brerop >= 0 ?
1252 &ins->oprs[ins->evex_brerop] : NULL);
1253
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001254 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1255 /* set EVEX.b */
1256 ins->evex_p[2] |= EVEX_P2B;
1257 if (op_er_sae->decoflags & ER) {
1258 /* set EVEX.RC (rounding control) */
1259 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1260 & EVEX_P2RC;
1261 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001262 } else {
1263 /* set EVEX.L'L (vector length) */
1264 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001265 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001266 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001267 /* set EVEX.b */
1268 ins->evex_p[2] |= EVEX_P2B;
1269 }
1270 }
1271
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001272 if (itemp_has(temp, IF_MIB)) {
1273 opy->eaflags |= EAF_MIB;
1274 /*
1275 * if a separate form of MIB (ICC style) is used,
1276 * the index reg info is merged into mem operand
1277 */
1278 if (mib_index != R_none) {
1279 opy->indexreg = mib_index;
1280 opy->scale = 1;
1281 opy->hintbase = mib_index;
1282 opy->hinttype = EAH_NOTBASE;
1283 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001284 }
1285
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001286 if (process_ea(opy, &ea_data, bits,
1287 rfield, rflags, ins) != eat) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001288 nasm_error(ERR_NONFATAL, "invalid effective address");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001289 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001290 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001291 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001292 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001293 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001294 }
1295 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001296
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001297 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001298 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001299 ": instruction code \\%o (0x%02X) given", c, c);
1300 break;
1301 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001302 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001303
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001304 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001305
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001306 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001307 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001308 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001309 return -1;
1310 }
1311 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001312 }
1313
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001314 switch (ins->prefixes[PPS_VEX]) {
1315 case P_EVEX:
1316 if (!(ins->rex & REX_EV))
1317 return -1;
1318 break;
1319 case P_VEX3:
1320 case P_VEX2:
1321 if (!(ins->rex & REX_V))
1322 return -1;
1323 break;
1324 default:
1325 break;
1326 }
1327
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001328 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001329 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001330
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001331 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001332 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001333 return -1;
1334 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001335 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001336 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001337 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001338 ins->rex &= ~REX_W;
1339 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001340 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001341 ins->rex |= REX_W;
1342 bad32 &= ~REX_W;
1343 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001344 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001345 /* Follow REX_W */
1346 break;
1347 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001348
H. Peter Anvinfc561202011-07-07 16:58:22 -07001349 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001350 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001351 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001352 } else if (!(ins->rex & REX_EV) &&
1353 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001354 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001355 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001356 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001357 if (ins->rex & REX_EV)
1358 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001359 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1360 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001361 length += 3;
1362 else
1363 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001364 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001365 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001366 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001367 return -1;
1368 } else if (bits == 64) {
1369 length++;
1370 } else if ((ins->rex & REX_L) &&
1371 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001372 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001373 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001374 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001375 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001376 length++;
1377 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001378 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001379 return -1;
1380 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001381 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001382
1383 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001384 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001385 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001386 "instruction is not lockable");
1387 }
1388
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001389 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001390
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001391 /*
1392 * when BND prefix is set by DEFAULT directive,
1393 * BND prefix is added to every appropriate instruction line
1394 * unless it is overridden by NOBND prefix.
1395 */
1396 if (globalbnd &&
1397 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1398 ins->prefixes[PPS_REP] = P_BND;
1399
H. Peter Anvina77692b2016-09-20 14:04:33 -07001400 /*
1401 * Add length of legacy prefixes
1402 */
1403 length += emit_prefix(NULL, bits, ins);
1404
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001405 return length;
1406}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001407
H. Peter Anvina77692b2016-09-20 14:04:33 -07001408static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001409{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001410 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001411 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001412 !(ins->rex & (REX_V | REX_EV)) &&
1413 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001414 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1415 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001416 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001417 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001418 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001419}
1420
H. Peter Anvina77692b2016-09-20 14:04:33 -07001421static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1422{
1423 int bytes = 0;
1424 int j;
1425
1426 for (j = 0; j < MAXPREFIX; j++) {
1427 uint8_t c = 0;
1428 switch (ins->prefixes[j]) {
1429 case P_WAIT:
1430 c = 0x9B;
1431 break;
1432 case P_LOCK:
1433 c = 0xF0;
1434 break;
1435 case P_REPNE:
1436 case P_REPNZ:
1437 case P_XACQUIRE:
1438 case P_BND:
1439 c = 0xF2;
1440 break;
1441 case P_REPE:
1442 case P_REPZ:
1443 case P_REP:
1444 case P_XRELEASE:
1445 c = 0xF3;
1446 break;
1447 case R_CS:
1448 if (bits == 64) {
1449 nasm_error(ERR_WARNING | ERR_PASS2,
1450 "cs segment base generated, but will be ignored in 64-bit mode");
1451 }
1452 c = 0x2E;
1453 break;
1454 case R_DS:
1455 if (bits == 64) {
1456 nasm_error(ERR_WARNING | ERR_PASS2,
1457 "ds segment base generated, but will be ignored in 64-bit mode");
1458 }
1459 c = 0x3E;
1460 break;
1461 case R_ES:
1462 if (bits == 64) {
1463 nasm_error(ERR_WARNING | ERR_PASS2,
1464 "es segment base generated, but will be ignored in 64-bit mode");
1465 }
1466 c = 0x26;
1467 break;
1468 case R_FS:
1469 c = 0x64;
1470 break;
1471 case R_GS:
1472 c = 0x65;
1473 break;
1474 case R_SS:
1475 if (bits == 64) {
1476 nasm_error(ERR_WARNING | ERR_PASS2,
1477 "ss segment base generated, but will be ignored in 64-bit mode");
1478 }
1479 c = 0x36;
1480 break;
1481 case R_SEGR6:
1482 case R_SEGR7:
1483 nasm_error(ERR_NONFATAL,
1484 "segr6 and segr7 cannot be used as prefixes");
1485 break;
1486 case P_A16:
1487 if (bits == 64) {
1488 nasm_error(ERR_NONFATAL,
1489 "16-bit addressing is not supported "
1490 "in 64-bit mode");
1491 } else if (bits != 16)
1492 c = 0x67;
1493 break;
1494 case P_A32:
1495 if (bits != 32)
1496 c = 0x67;
1497 break;
1498 case P_A64:
1499 if (bits != 64) {
1500 nasm_error(ERR_NONFATAL,
1501 "64-bit addressing is only supported "
1502 "in 64-bit mode");
1503 }
1504 break;
1505 case P_ASP:
1506 c = 0x67;
1507 break;
1508 case P_O16:
1509 if (bits != 16)
1510 c = 0x66;
1511 break;
1512 case P_O32:
1513 if (bits == 16)
1514 c = 0x66;
1515 break;
1516 case P_O64:
1517 /* REX.W */
1518 break;
1519 case P_OSP:
1520 c = 0x66;
1521 break;
1522 case P_EVEX:
1523 case P_VEX3:
1524 case P_VEX2:
1525 case P_NOBND:
1526 case P_none:
1527 break;
1528 default:
1529 nasm_panic(0, "invalid instruction prefix");
1530 }
1531 if (c) {
1532 if (data)
1533 out_rawbyte(data, c);
1534 bytes++;
1535 }
1536 }
1537 return bytes;
1538}
1539
1540static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001541{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001542 uint8_t c;
1543 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001544 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001545 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001546 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001547 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001548 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001549 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001550 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001551 const int bits = data->bits;
H. Peter Anvin70653092007-10-19 14:42:29 -07001552
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001553 ins->rex_done = false;
1554
H. Peter Anvina77692b2016-09-20 14:04:33 -07001555 emit_prefix(data, bits, ins);
1556
H. Peter Anvin839eca22007-10-29 23:12:47 -07001557 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001558 c = *codes++;
1559 op1 = (c & 3) + ((opex & 1) << 2);
1560 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1561 opx = &ins->oprs[op1];
1562 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001563
H. Peter Anvina77692b2016-09-20 14:04:33 -07001564
H. Peter Anvin839eca22007-10-29 23:12:47 -07001565 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 case 01:
1567 case 02:
1568 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001569 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001570 emit_rex(data, ins);
1571 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001572 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001573 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001574
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001575 case 05:
1576 case 06:
1577 case 07:
1578 opex = c;
1579 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001580
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001581 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001582 emit_rex(data, ins);
1583 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001585
Jin Kyu Song164d6072013-10-15 19:10:13 -07001586 case4(014):
1587 break;
1588
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001589 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001590 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001591 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001592 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001593 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001594 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001595
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001596 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001597 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001598 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001599 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001600 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001602
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001603 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001604 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001605 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001607
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001608 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001609 if (opx->type & (BITS16 | BITS32))
1610 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 else
1612 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001613 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001614 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001616
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001617 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001618 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001619 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001620 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001621
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001622 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001623 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001624 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001625 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001627
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001628 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001629 if (opx->segment == data->segment) {
1630 int64_t delta = opx->offset - data->offset
1631 - (data->inslen - data->insoffs);
1632 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001633 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001634 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001635 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001637
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001638 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001639 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001640 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001641
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001642 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001643 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001645
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001646 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001647 if (opx->type & (BITS16 | BITS32 | BITS64))
1648 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 else
1650 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001651
1652 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001654
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001655 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001656 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001658
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001659 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001660 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001661 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001662 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001663 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001664 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001665
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001666 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001667 {
1668 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1669 const struct operand *opy;
1670
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001671 c = *codes++;
1672 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001673 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001674 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001675 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001676 "non-absolute expression not permitted as argument %d",
1677 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001678 } else if (opy->offset & ~mask) {
1679 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1680 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001681 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001682 c = opy->offset & mask;
1683 goto emit_is4;
1684 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001685
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001686 case 0173:
1687 c = *codes++;
1688 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001689 c &= 15;
1690 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001691
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001692 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001693 c = 0;
1694 emit_is4:
1695 r = nasm_regvals[opx->basereg];
1696 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001697 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001698
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001699 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001700 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001701 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001702 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001703 "signed dword immediate exceeds bounds");
1704 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001705 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001706 break;
1707
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001708 case4(0240):
1709 case 0250:
1710 codes += 3;
1711 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1712 EVEX_P2Z | EVEX_P2AAA, 2);
1713 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1714 bytes[0] = 0x62;
1715 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001716 bytes[1] = ((((ins->rex & 7) << 5) |
1717 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001718 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001719 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1720 ((~ins->vexreg & 15) << 3) |
1721 (1 << 2) | (ins->vex_wlp & 3);
1722 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001723 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001724 break;
1725
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001726 case4(0260):
1727 case 0270:
1728 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001729 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1730 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001731 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1732 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1733 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001734 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001735 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001736 } else {
1737 bytes[0] = 0xc5;
1738 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001739 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001740 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001741 }
1742 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001743
H. Peter Anvine014f352012-02-25 22:35:19 -08001744 case 0271:
1745 case 0272:
1746 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001747 break;
1748
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001749 case4(0274):
1750 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001751 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001752 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001753
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001754 if (absolute_op(opx)) {
1755 if (ins->rex & REX_W)
1756 s = 64;
1757 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1758 s = 16;
1759 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1760 s = 32;
1761 else
1762 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001763
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001764 um = (uint64_t)2 << (s-1);
1765 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001766
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001767 if (uv > 127 && uv < (uint64_t)-128 &&
1768 (uv < um-128 || uv > um-1)) {
1769 /* If this wasn't explicitly byte-sized, warn as though we
1770 * had fallen through to the imm16/32/64 case.
1771 */
1772 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1773 "%s value exceeds bounds",
1774 (opx->type & BITS8) ? "signed byte" :
1775 s == 16 ? "word" :
1776 s == 32 ? "dword" :
1777 "signed dword");
1778 }
1779
1780 /* Output as a raw byte to avoid byte overflow check */
1781 out_rawbyte(data, (uint8_t)uv);
1782 } else {
1783 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001784 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001785 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001786 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001787
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001788 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001790
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001792 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1793 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001795
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001797 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1798 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001800
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 case 0312:
1802 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001803
Keith Kaniosb7a89542007-04-12 02:40:54 +00001804 case 0313:
1805 ins->rex = 0;
1806 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001807
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001808 case4(0314):
1809 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001810
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001814
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001816 case 0323:
1817 break;
1818
Keith Kaniosb7a89542007-04-12 02:40:54 +00001819 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001820 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001822
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001823 case 0325:
1824 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001825
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001826 case 0326:
1827 break;
1828
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001830 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001832
H. Peter Anvine2c80182005-01-15 22:15:51 +00001833 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001835
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001836 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001838 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001840
Keith Kanios48af1772007-08-17 07:37:52 +00001841 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001842 if (ins->rex & REX_R)
1843 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001844 ins->rex &= ~(REX_L|REX_R);
1845 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001846
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001847 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001848 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001849
H. Peter Anvin962e3052008-08-28 17:47:16 -07001850 case 0336:
1851 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001852 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001853
H. Peter Anvine2c80182005-01-15 22:15:51 +00001854 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001856 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001857
1858 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001859 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001860
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001861 case 0341:
1862 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001863
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001864 case 0360:
1865 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001866
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001867 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001868 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001869 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001870
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001871 case 0364:
1872 case 0365:
1873 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001874
Keith Kanios48af1772007-08-17 07:37:52 +00001875 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001876 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001877 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001878 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001879
Jin Kyu Song03041092013-10-15 19:38:51 -07001880 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001881 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001882
H. Peter Anvine2c80182005-01-15 22:15:51 +00001883 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001884 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001885 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001886
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001887 case 0374:
1888 eat = EA_XMMVSIB;
1889 break;
1890
1891 case 0375:
1892 eat = EA_YMMVSIB;
1893 break;
1894
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001895 case 0376:
1896 eat = EA_ZMMVSIB;
1897 break;
1898
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001899 case4(0100):
1900 case4(0110):
1901 case4(0120):
1902 case4(0130):
1903 case4(0200):
1904 case4(0204):
1905 case4(0210):
1906 case4(0214):
1907 case4(0220):
1908 case4(0224):
1909 case4(0230):
1910 case4(0234):
1911 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 ea ea_data;
1913 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001914 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001915 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001916 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001917
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001918 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001919 /* pick rfield from operand b (opx) */
1920 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001921 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001922 } else {
1923 /* rfield is constant */
1924 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001926 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001928 if (process_ea(opy, &ea_data, bits,
1929 rfield, rflags, ins) != eat)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001930 nasm_error(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001931
H. Peter Anvine2c80182005-01-15 22:15:51 +00001932 p = bytes;
1933 *p++ = ea_data.modrm;
1934 if (ea_data.sib_present)
1935 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001936 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001938 /*
1939 * Make sure the address gets the right offset in case
1940 * the line breaks in the .lst file (BR 1197827)
1941 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001942
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001943 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001944 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001945 if (ea_data.disp8) {
1946 out_rawbyte(data, ea_data.disp8);
1947 } else if (ea_data.rip) {
1948 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001949 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001950 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001951
H. Peter Anvina77692b2016-09-20 14:04:33 -07001952 if (overflow_general(opy->offset, asize) ||
1953 signed_bits(opy->offset, ins->addr_size) !=
1954 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001955 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001956
H. Peter Anvina77692b2016-09-20 14:04:33 -07001957 out_imm(data, opy, ea_data.bytes,
1958 (asize > ea_data.bytes) ? OUT_SIGNED : OUT_UNSIGNED);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001959 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001960 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001961 }
1962 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001963
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001964 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001965 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001966 ": instruction code \\%o (0x%02X) given", c, c);
1967 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001968 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001969 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001970}
1971
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001972static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001973{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001974 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001975 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001976 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001977}
1978
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001979static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001980{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001981 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001982 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001983 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001984}
1985
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001986static int op_rexflags(const operand * o, int mask)
1987{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001988 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001989 int val;
1990
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001991 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001992 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001993
H. Peter Anvina4835d42008-05-20 14:21:29 -07001994 flags = nasm_reg_flags[o->basereg];
1995 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001996
1997 return rexflags(val, flags, mask);
1998}
1999
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002000static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002001{
2002 int rex = 0;
2003
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002004 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002005 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002006 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002007 rex |= REX_W;
2008 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2009 rex |= REX_H;
2010 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2011 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002012
2013 return rex & mask;
2014}
2015
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002016static int evexflags(int val, decoflags_t deco,
2017 int mask, uint8_t byte)
2018{
2019 int evex = 0;
2020
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002021 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002022 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002023 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002024 evex |= (EVEX_P0RP | EVEX_P0X);
2025 break;
2026 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002027 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002028 evex |= EVEX_P2VP;
2029 if (deco & Z)
2030 evex |= EVEX_P2Z;
2031 if (deco & OPMASK_MASK)
2032 evex |= deco & EVEX_P2AAA;
2033 break;
2034 }
2035 return evex & mask;
2036}
2037
2038static int op_evexflags(const operand * o, int mask, uint8_t byte)
2039{
2040 int val;
2041
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002042 val = nasm_regvals[o->basereg];
2043
2044 return evexflags(val, o->decoflags, mask, byte);
2045}
2046
H. Peter Anvin23595f52009-07-25 17:44:25 -07002047static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002048 insn *instruction,
2049 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002050{
2051 const struct itemplate *temp;
2052 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002053 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002054 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002055 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002056 int i;
2057
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002058 /* broadcasting uses a different data element size */
2059 for (i = 0; i < instruction->operands; i++)
2060 if (i == broadcast)
2061 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2062 else
2063 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002064
2065 merr = MERR_INVALOP;
2066
2067 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002068 temp->opcode != I_none; temp++) {
2069 m = matches(temp, instruction, bits);
2070 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002071 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002072 m = MOK_GOOD;
2073 else
2074 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002075 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002076 /*
2077 * Missing operand size and a candidate for fuzzy matching...
2078 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002079 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002080 if (i == broadcast)
2081 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2082 else
2083 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002084 opsizemissing = true;
2085 }
2086 if (m > merr)
2087 merr = m;
2088 if (merr == MOK_GOOD)
2089 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002090 }
2091
2092 /* No match, but see if we can get a fuzzy operand size match... */
2093 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002094 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002095
2096 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002097 /*
2098 * We ignore extrinsic operand sizes on registers, so we should
2099 * never try to fuzzy-match on them. This also resolves the case
2100 * when we have e.g. "xmmrm128" in two different positions.
2101 */
2102 if (is_class(REGISTER, instruction->oprs[i].type))
2103 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002104
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002105 /* This tests if xsizeflags[i] has more than one bit set */
2106 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2107 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002108
Jin Kyu Song7903c072013-10-30 03:00:12 -07002109 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002110 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002111 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2112 BITS32 : BITS64);
2113 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002114 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002115 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002116 }
2117
2118 /* Try matching again... */
2119 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002120 temp->opcode != I_none; temp++) {
2121 m = matches(temp, instruction, bits);
2122 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002123 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002124 m = MOK_GOOD;
2125 else
2126 m = MERR_INVALOP;
2127 }
2128 if (m > merr)
2129 merr = m;
2130 if (merr == MOK_GOOD)
2131 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002132 }
2133
H. Peter Anvina81655b2009-07-25 18:15:28 -07002134done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002135 *tempp = temp;
2136 return merr;
2137}
2138
Mark Charneydcaef4b2014-10-09 13:45:17 -04002139static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2140{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002141 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002142 uint8_t brcast_num;
2143
Mark Charneydcaef4b2014-10-09 13:45:17 -04002144 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002145 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002146 "size of broadcasting element is greater than 64 bits");
2147
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002148 /*
2149 * The shift term is to take care of the extra BITS80 inserted
2150 * between BITS64 and BITS128.
2151 */
2152 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2153 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002154
2155 return brcast_num;
2156}
2157
H. Peter Anvin65289e82009-07-25 17:25:11 -07002158static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002159 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002160{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002161 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002162 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002163 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002164
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002165 /*
2166 * Check the opcode
2167 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002169 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002170
2171 /*
2172 * Count the operands
2173 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002174 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002175 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002176
2177 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002178 * Is it legal?
2179 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002180 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002181 return MERR_INVALOP;
2182
2183 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002184 * {evex} available?
2185 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002186 switch (instruction->prefixes[PPS_VEX]) {
2187 case P_EVEX:
2188 if (!itemp_has(itemp, IF_EVEX))
2189 return MERR_ENCMISMATCH;
2190 break;
2191 case P_VEX3:
2192 case P_VEX2:
2193 if (!itemp_has(itemp, IF_VEX))
2194 return MERR_ENCMISMATCH;
2195 break;
2196 default:
2197 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002198 }
2199
2200 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002201 * Check that no spurious colons or TOs are present
2202 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002203 for (i = 0; i < itemp->operands; i++)
2204 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002205 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002206
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002207 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002208 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002209 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002210 switch (itemp_smask(itemp)) {
2211 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002212 asize = BITS8;
2213 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002214 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002215 asize = BITS16;
2216 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002217 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002218 asize = BITS32;
2219 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002220 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002221 asize = BITS64;
2222 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002223 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002224 asize = BITS128;
2225 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002226 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002227 asize = BITS256;
2228 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002229 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002230 asize = BITS512;
2231 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002232 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002233 switch (bits) {
2234 case 16:
2235 asize = BITS16;
2236 break;
2237 case 32:
2238 asize = BITS32;
2239 break;
2240 case 64:
2241 asize = BITS64;
2242 break;
2243 default:
2244 asize = 0;
2245 break;
2246 }
2247 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002248 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002249 asize = 0;
2250 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002251 }
2252
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002253 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002254 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002255 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002256 memset(size, 0, sizeof size);
2257 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002258 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002259 /* S- flags apply to all operands */
2260 for (i = 0; i < MAX_OPERANDS; i++)
2261 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002262 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002263
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002264 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002265 * Check that the operand flags all match up,
2266 * it's a bit tricky so lets be verbose:
2267 *
2268 * 1) Find out the size of operand. If instruction
2269 * doesn't have one specified -- we're trying to
2270 * guess it either from template (IF_S* flag) or
2271 * from code bits.
2272 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002273 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002274 * template has an operand size specified AND this size differ
2275 * from which instruction has (perhaps we got it from code bits)
2276 * we are:
2277 * a) Check that only size of instruction and operand is differ
2278 * other characteristics do match
2279 * b) Perhaps it's a register specified in instruction so
2280 * for such a case we just mark that operand as "size
2281 * missing" and this will turn on fuzzy operand size
2282 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002283 */
2284 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002285 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002286 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002287 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002288 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002289 opflags_t template_opsize, insn_opsize;
2290
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002291 if (!(type & SIZE_MASK))
2292 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002293
Jin Kyu Song7903c072013-10-30 03:00:12 -07002294 insn_opsize = type & SIZE_MASK;
2295 if (!is_broadcast) {
2296 template_opsize = itemp->opd[i] & SIZE_MASK;
2297 } else {
2298 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2299 /*
2300 * when broadcasting, the element size depends on
2301 * the instruction type. decorator flag should match.
2302 */
2303
2304 if (deco_brsize) {
2305 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002306 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002307 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002308 } else {
2309 template_opsize = 0;
2310 }
2311 }
2312
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002313 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002314 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002315 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002316 } else if (template_opsize) {
2317 if (template_opsize != insn_opsize) {
2318 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002319 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002320 } else if (!is_class(REGISTER, type)) {
2321 /*
2322 * Note: we don't honor extrinsic operand sizes for registers,
2323 * so "missing operand size" for a register should be
2324 * considered a wildcard match rather than an error.
2325 */
2326 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002327 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002328 } else if (is_broadcast &&
2329 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002330 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002331 /*
2332 * broadcasting opsize matches but the number of repeated memory
2333 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002334 * if 64b double precision float is broadcasted to ymm (256b),
2335 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002336 */
2337 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002338 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002339 }
2340 }
2341
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002342 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002343 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002344
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002345 /*
2346 * Check operand sizes
2347 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002348 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2349 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002350 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002351 asize = itemp->opd[i] & SIZE_MASK;
2352 if (asize) {
2353 for (i = 0; i < oprs; i++)
2354 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 break;
2356 }
2357 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002358 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002360 }
2361
Keith Kaniosb7a89542007-04-12 02:40:54 +00002362 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 if (!(itemp->opd[i] & SIZE_MASK) &&
2364 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002365 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002366 }
2367
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002368 /*
2369 * Check template is okay at the set cpu level
2370 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002371 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002372 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002373
Keith Kaniosb7a89542007-04-12 02:40:54 +00002374 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002375 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002376 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002377 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002378 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002379
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002380 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002381 * If we have a HLE prefix, look for the NOHLE flag
2382 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002383 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002384 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2385 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2386 return MERR_BADHLE;
2387
2388 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002389 * Check if special handling needed for Jumps
2390 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002391 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002392 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393
Jin Kyu Song03041092013-10-15 19:38:51 -07002394 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002395 * Check if BND prefix is allowed.
2396 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002397 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002398 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002399 (has_prefix(instruction, PPS_REP, P_BND) ||
2400 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002401 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002402 else if (itemp_has(itemp, IF_BND) &&
2403 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2404 has_prefix(instruction, PPS_REP, P_REPNZ)))
2405 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002406
H. Peter Anvin60926242009-07-26 16:25:38 -07002407 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002408}
2409
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002410/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002411 * Check if ModR/M.mod should/can be 01.
2412 * - EAF_BYTEOFFS is set
2413 * - offset can fit in a byte when EVEX is not used
2414 * - offset can be compressed when EVEX is used
2415 */
2416#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2417 (o >= -128 && o <= 127 && \
2418 seg == NO_SEG && !forw_ref && \
2419 !(input->eaflags & EAF_WORDOFFS) && \
2420 !(ins->rex & REX_EV)) || \
2421 (ins->rex & REX_EV && \
2422 is_disp8n(input, ins, &output->disp8)))
2423
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002424static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002425 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002426{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002427 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002428 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002429 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002430
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002431 output->type = EA_SCALAR;
2432 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002433 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002434
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002435 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002436 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002437 /* EVEX.R' flag for the REG operand */
2438 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002439
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002440 if (is_class(REGISTER, input->type)) {
2441 /*
2442 * It's a direct register.
2443 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002444 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002445 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002446
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002447 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002448 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002449
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002450 /* broadcasting is not available with a direct register operand. */
2451 if (input->decoflags & BRDCAST_MASK) {
2452 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2453 goto err;
2454 }
2455
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002456 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002457 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002458 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002459 output->bytes = 0; /* no offset necessary either */
2460 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2461 } else {
2462 /*
2463 * It's a memory reference.
2464 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002465
2466 /* Embedded rounding or SAE is not available with a mem ref operand. */
2467 if (input->decoflags & (ER | SAE)) {
2468 nasm_error(ERR_NONFATAL,
2469 "Embedded rounding is available only with reg-reg op.");
2470 return -1;
2471 }
2472
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002473 if (input->basereg == -1 &&
2474 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002475 /*
2476 * It's a pure offset.
2477 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002478 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
2479 if (input->segment == NO_SEG || (input->opflags & OPFLAG_RELATIVE)) {
2480 nasm_error(ERR_WARNING | ERR_PASS2, "absolute address can not be RIP-relative");
2481 input->type &= ~IP_REL;
2482 input->type |= MEMORY;
2483 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002484 }
2485
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002486 if (bits == 64 &&
2487 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2488 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2489 return -1;
2490 }
2491
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002492 if (eaflags & EAF_BYTEOFFS ||
2493 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002494 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2495 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2496 }
2497
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002498 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002499 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002500 output->sib = GEN_SIB(0, 4, 5);
2501 output->bytes = 4;
2502 output->modrm = GEN_MODRM(0, rfield, 4);
2503 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002504 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002505 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002506 output->bytes = (addrbits != 16 ? 4 : 2);
2507 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2508 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002509 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002510 } else {
2511 /*
2512 * It's an indirection.
2513 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002515 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002516 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002517 int t, it, bt; /* register numbers */
2518 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002519
H. Peter Anvine2c80182005-01-15 22:15:51 +00002520 if (s == 0)
2521 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002522
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002523 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002524 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002525 ix = nasm_reg_flags[i];
2526 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002527 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002528 ix = 0;
2529 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002530
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002531 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002532 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002533 bx = nasm_reg_flags[b];
2534 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002535 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002536 bx = 0;
2537 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002538
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002539 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002540 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002541 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002542 int32_t o = input->offset;
2543 int mod, scale, index, base;
2544
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002545 /*
2546 * For a vector SIB, one has to be a vector and the other,
2547 * if present, a GPR. The vector must be the index operand.
2548 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002549 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002550 if (s == 0)
2551 s = 1;
2552 else if (s != 1)
2553 goto err;
2554
2555 t = bt, bt = it, it = t;
2556 x = bx, bx = ix, ix = x;
2557 }
2558
2559 if (bt != -1) {
2560 if (REG_GPR & ~bx)
2561 goto err;
2562 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2563 sok &= bx;
2564 else
2565 goto err;
2566 }
2567
2568 /*
2569 * While we're here, ensure the user didn't specify
2570 * WORD or QWORD
2571 */
2572 if (input->disp_size == 16 || input->disp_size == 64)
2573 goto err;
2574
2575 if (addrbits == 16 ||
2576 (addrbits == 32 && !(sok & BITS32)) ||
2577 (addrbits == 64 && !(sok & BITS64)))
2578 goto err;
2579
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002580 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2581 : ((ix & YMMREG & ~REG_EA)
2582 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002583
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002584 output->rex |= rexflags(it, ix, REX_X);
2585 output->rex |= rexflags(bt, bx, REX_B);
2586 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002587
2588 index = it & 7; /* it is known to be != -1 */
2589
2590 switch (s) {
2591 case 1:
2592 scale = 0;
2593 break;
2594 case 2:
2595 scale = 1;
2596 break;
2597 case 4:
2598 scale = 2;
2599 break;
2600 case 8:
2601 scale = 3;
2602 break;
2603 default: /* then what the smeg is it? */
2604 goto err; /* panic */
2605 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002606
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002607 if (bt == -1) {
2608 base = 5;
2609 mod = 0;
2610 } else {
2611 base = (bt & 7);
2612 if (base != REG_NUM_EBP && o == 0 &&
2613 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002614 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002615 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002616 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002617 mod = 1;
2618 else
2619 mod = 2;
2620 }
2621
2622 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002623 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2624 output->modrm = GEN_MODRM(mod, rfield, 4);
2625 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002626 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002627 /*
2628 * it must be a 32/64-bit memory reference. Firstly we have
2629 * to check that all registers involved are type E/Rxx.
2630 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002631 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002632 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002633
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002634 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002635 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2636 sok &= ix;
2637 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002638 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002639 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002640
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002641 if (bt != -1) {
2642 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002643 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002644 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002645 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002646 sok &= bx;
2647 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002648
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002649 /*
2650 * While we're here, ensure the user didn't specify
2651 * WORD or QWORD
2652 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002653 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002654 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002655
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002656 if (addrbits == 16 ||
2657 (addrbits == 32 && !(sok & BITS32)) ||
2658 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002659 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002660
Keith Kaniosb7a89542007-04-12 02:40:54 +00002661 /* now reorganize base/index */
2662 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002663 ((hb == b && ht == EAH_NOTBASE) ||
2664 (hb == i && ht == EAH_MAKEBASE))) {
2665 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002666 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002667 x = bx, bx = ix, ix = x;
2668 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002669
Jin Kyu Song164d6072013-10-15 19:10:13 -07002670 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002671 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002672 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002673 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002674 if (eaflags & EAF_MIB) {
2675 /* only for mib operands */
2676 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2677 /*
2678 * make a single reg index [reg*1].
2679 * gas uses this form for an explicit index register.
2680 */
2681 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2682 }
2683 if ((ht == EAH_SUMMED) && bt == -1) {
2684 /* separate once summed index into [base, index] */
2685 bt = it, bx = ix, s--;
2686 }
2687 } else {
2688 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002689 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002690 s == 3 || s == 5 || s == 9) && bt == -1) {
2691 /* convert 3*EAX to EAX+2*EAX */
2692 bt = it, bx = ix, s--;
2693 }
2694 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002695 (eaflags & EAF_TIMESTWO) &&
2696 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002697 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002698 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002699 * to sib format with 0x0 displacement - [EAX*1+0].
2700 */
2701 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2702 }
2703 }
Keith Kanios48af1772007-08-17 07:37:52 +00002704 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002705 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002706 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002707 x = ix, ix = bx, bx = x;
2708 }
2709 if (it == REG_NUM_ESP ||
2710 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002711 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002712
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002713 output->rex |= rexflags(it, ix, REX_X);
2714 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002715
Keith Kanios48af1772007-08-17 07:37:52 +00002716 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002717 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002718 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002719
Keith Kaniosb7a89542007-04-12 02:40:54 +00002720 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002721 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002722 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002723 } else {
2724 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002725 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002726 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002727 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002728 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002729 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002730 mod = 1;
2731 else
2732 mod = 2;
2733 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002734
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002735 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002736 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2737 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002738 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002739 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002740 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002741
Keith Kaniosb7a89542007-04-12 02:40:54 +00002742 if (it == -1)
2743 index = 4, s = 1;
2744 else
2745 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002746
H. Peter Anvine2c80182005-01-15 22:15:51 +00002747 switch (s) {
2748 case 1:
2749 scale = 0;
2750 break;
2751 case 2:
2752 scale = 1;
2753 break;
2754 case 4:
2755 scale = 2;
2756 break;
2757 case 8:
2758 scale = 3;
2759 break;
2760 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002761 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002762 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002763
Keith Kaniosb7a89542007-04-12 02:40:54 +00002764 if (bt == -1) {
2765 base = 5;
2766 mod = 0;
2767 } else {
2768 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002769 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002770 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002771 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002772 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002773 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002774 mod = 1;
2775 else
2776 mod = 2;
2777 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002778
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002779 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002780 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2781 output->modrm = GEN_MODRM(mod, rfield, 4);
2782 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002783 }
2784 } else { /* it's 16-bit */
2785 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002786 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002787
Keith Kaniosb7a89542007-04-12 02:40:54 +00002788 /* check for 64-bit long mode */
2789 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002790 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002791
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002793 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2794 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002795 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002796
Keith Kaniosb7a89542007-04-12 02:40:54 +00002797 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002798 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002799 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002800
H. Peter Anvine2c80182005-01-15 22:15:51 +00002801 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002802 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002803 if (b == -1 && i != -1) {
2804 int tmp = b;
2805 b = i;
2806 i = tmp;
2807 } /* swap */
2808 if ((b == R_SI || b == R_DI) && i != -1) {
2809 int tmp = b;
2810 b = i;
2811 i = tmp;
2812 }
2813 /* have BX/BP as base, SI/DI index */
2814 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002815 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 if (i != -1 && b != -1 &&
2817 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002818 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002819 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002820 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002821
H. Peter Anvine2c80182005-01-15 22:15:51 +00002822 rm = -1;
2823 if (i != -1)
2824 switch (i * 256 + b) {
2825 case R_SI * 256 + R_BX:
2826 rm = 0;
2827 break;
2828 case R_DI * 256 + R_BX:
2829 rm = 1;
2830 break;
2831 case R_SI * 256 + R_BP:
2832 rm = 2;
2833 break;
2834 case R_DI * 256 + R_BP:
2835 rm = 3;
2836 break;
2837 } else
2838 switch (b) {
2839 case R_SI:
2840 rm = 4;
2841 break;
2842 case R_DI:
2843 rm = 5;
2844 break;
2845 case R_BP:
2846 rm = 6;
2847 break;
2848 case R_BX:
2849 rm = 7;
2850 break;
2851 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002852 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002853 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002854
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002855 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002856 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002857 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002858 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002859 mod = 1;
2860 else
2861 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002862
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002863 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002864 output->bytes = mod; /* bytes of offset needed */
2865 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002866 }
2867 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002868 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002869
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002870 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002871 return output->type;
2872
2873err:
2874 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002875}
2876
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002877static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002878{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002879 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002880 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002881
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002882 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002883
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002884 switch (ins->prefixes[PPS_ASIZE]) {
2885 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002886 valid &= 16;
2887 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002888 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002889 valid &= 32;
2890 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002891 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002892 valid &= 64;
2893 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002894 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002895 valid &= (addrbits == 32) ? 16 : 32;
2896 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002897 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002898 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002899 }
2900
2901 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002902 if (is_class(MEMORY, ins->oprs[j].type)) {
2903 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002904
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002906 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002907 i = 0;
2908 else
2909 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002910
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002912 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002913 b = 0;
2914 else
2915 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002916
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002917 if (ins->oprs[j].scale == 0)
2918 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002919
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002920 if (!i && !b) {
2921 int ds = ins->oprs[j].disp_size;
2922 if ((addrbits != 64 && ds > 8) ||
2923 (addrbits == 64 && ds == 16))
2924 valid &= ds;
2925 } else {
2926 if (!(REG16 & ~b))
2927 valid &= 16;
2928 if (!(REG32 & ~b))
2929 valid &= 32;
2930 if (!(REG64 & ~b))
2931 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002932
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002933 if (!(REG16 & ~i))
2934 valid &= 16;
2935 if (!(REG32 & ~i))
2936 valid &= 32;
2937 if (!(REG64 & ~i))
2938 valid &= 64;
2939 }
2940 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002941 }
2942
2943 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002944 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002945 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002946 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002947 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002948 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002949 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002950 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002951 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002952 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002953 }
2954
2955 defdisp = ins->addr_size == 16 ? 16 : 32;
2956
2957 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002958 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2959 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2960 /*
2961 * mem_offs sizes must match the address size; if not,
2962 * strip the MEM_OFFS bit and match only EA instructions
2963 */
2964 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2965 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002966 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002967}