blob: 7902c6cc61453941f900ce3fbbe372a16a286aec [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 Anvinb20bc732017-03-07 19:23:03 -0800186#include "error.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000187#include "assemble.h"
188#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700189#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800190#include "disp8.h"
H. Peter Anvin172b8402016-02-18 01:16:18 -0800191#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000192
H. Peter Anvin65289e82009-07-25 17:25:11 -0700193enum match_result {
194 /*
195 * Matching errors. These should be sorted so that more specific
196 * errors come later in the sequence.
197 */
198 MERR_INVALOP,
199 MERR_OPSIZEMISSING,
200 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700201 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700202 MERR_BADCPU,
203 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800204 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700205 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700206 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800207 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700208 /*
209 * Matching success; the conditional ones first
210 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400211 MOK_JUMP, /* Matching OK but needs jmp_match() */
212 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700213};
214
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000215typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700216 enum ea_type type; /* what kind of EA is this? */
217 int sib_present; /* is a SIB byte necessary? */
218 int bytes; /* # of bytes of offset needed */
219 int size; /* lazy - this is sib+bytes+1 */
220 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700221 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000222} ea;
223
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400224#define GEN_SIB(scale, index, base) \
225 (((scale) << 6) | ((index) << 3) | ((base)))
226
227#define GEN_MODRM(mod, reg, rm) \
228 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
229
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800230static int64_t calcsize(int32_t, int64_t, int, insn *,
231 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700232static int emit_prefix(struct out_data *data, const int bits, insn *ins);
233static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700234static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400235 insn *instruction,
236 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700237static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700238static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000239static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700240static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000241static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700242static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700243static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000244
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700245static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700246
H. Peter Anvin164d2462017-02-20 02:39:56 -0800247static inline bool absolute_op(const struct operand *o)
248{
249 return o->segment == NO_SEG && o->wrt == NO_SEG &&
250 !(o->opflags & OPFLAG_RELATIVE);
251}
252
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400253static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000254{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700255 return ins->prefixes[pos] == prefix;
256}
257
258static void assert_no_prefix(insn * ins, enum prefix_pos pos)
259{
260 if (ins->prefixes[pos])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800261 nasm_error(ERR_NONFATAL, "invalid %s prefix",
262 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700263}
264
265static const char *size_name(int size)
266{
267 switch (size) {
268 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400269 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700270 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400271 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700272 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400273 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700274 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700276 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400277 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700278 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400279 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700280 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700282 case 64:
283 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700284 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400285 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000286 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700287}
288
H. Peter Anvin285222f2017-03-01 13:27:33 -0800289static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400290{
H. Peter Anvin285222f2017-03-01 13:27:33 -0800291 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400292 "%s data exceeds bounds", size_name(size));
293}
294
295static void warn_overflow_const(int64_t data, int size)
296{
297 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800298 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400299}
300
301static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700302{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800303 if (absolute_op(o)) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400304 if (overflow_general(o->offset, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800305 warn_overflow(size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700306 }
307}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400308
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800309static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
310{
311 bool err;
312
313 switch (sign) {
314 case OUT_WRAP:
315 err = overflow_general(data, size);
316 break;
317 case OUT_SIGNED:
318 err = overflow_signed(data, size);
319 break;
320 case OUT_UNSIGNED:
321 err = overflow_unsigned(data, size);
322 break;
323 default:
324 panic();
325 break;
326 }
327
328 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800329 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800330}
331
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000332/*
333 * This routine wrappers the real output format's output routine,
334 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800335 * generator at the same time, flatten unnecessary relocations,
336 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000337 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700338static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000339{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000340 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700341 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700342 int asize;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800343 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700344 union {
345 uint8_t b[8];
346 uint64_t q;
347 } xdata;
348 uint64_t size = data->size;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800349 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800350 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000351
H. Peter Anvina77692b2016-09-20 14:04:33 -0700352 if (!data->size)
353 return; /* Nothing to do */
354
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700355 /*
356 * Convert addresses to RAWDATA if possible
357 * XXX: not all backends want this for global symbols!!!!
358 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700359 switch (data->type) {
360 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800361 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800362 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800363 goto address;
364
365 case OUT_RELADDR:
366 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800367 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800368 goto address;
369
370 address:
H. Peter Anvina77692b2016-09-20 14:04:33 -0700371 asize = data->size;
372 nasm_assert(asize <= 8);
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800373 if (data->tsegment == fixseg && data->twrt == NO_SEG) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700374 uint8_t *q = xdata.b;
375
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800376 warn_overflow_out(addrval, asize, data->sign);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700377
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800378 WRITEADDR(q, addrval, asize);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700379 data->data = xdata.b;
380 data->type = OUT_RAWDATA;
381 asize = 0; /* No longer an address */
382 }
383 break;
384
385 default:
386 asize = 0; /* Not an address */
387 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000388 }
389
H. Peter Anvina77692b2016-09-20 14:04:33 -0700390 lfmt->output(data);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800391
Frank Kotlerabebb082003-09-06 04:45:37 +0000392 /*
393 * this call to src_get determines when we call the
394 * debug-format-specific "linenum" function
395 * it updates lineno and lnfname to the current values
396 * returning 0 if "same as last time", -2 if lnfname
397 * changed, and the amount by which lineno changed,
398 * if it did. thus, these variables must be static
399 */
400
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400401 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700402 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000403
H. Peter Anvinb6412502016-02-11 21:07:40 -0800404 if (asize && asize > amax) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700405 if (data->type != OUT_ADDRESS || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800406 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800407 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800408 asize << 3, ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800409 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800410 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800411 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800412 asize << 3, ofmt->maxbits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700413 data->size = amax;
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700414 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700415 data->insoffs += amax;
416 data->offset += amax;
417 data->size = size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800418 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700419 data->data = zero_buffer;
420 data->type = OUT_RAWDATA;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800421 }
422
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700423 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700424 data->offset += size;
425 data->insoffs += size;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000426}
427
H. Peter Anvina77692b2016-09-20 14:04:33 -0700428static inline void out_rawdata(struct out_data *data, const void *rawdata,
429 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400430{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700431 data->type = OUT_RAWDATA;
432 data->data = rawdata;
433 data->size = size;
434 out(data);
435}
436
437static void out_rawbyte(struct out_data *data, uint8_t byte)
438{
439 data->type = OUT_RAWDATA;
440 data->data = &byte;
441 data->size = 1;
442 out(data);
443}
444
445static inline void out_reserve(struct out_data *data, uint64_t size)
446{
447 data->type = OUT_RESERVE;
448 data->size = size;
449 out(data);
450}
451
H. Peter Anvin164d2462017-02-20 02:39:56 -0800452static inline void out_imm(struct out_data *data, const struct operand *opx,
H. Peter Anvina77692b2016-09-20 14:04:33 -0700453 int size, enum out_sign sign)
454{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800455 data->type =
456 (opx->opflags & OPFLAG_RELATIVE) ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700457 data->sign = sign;
458 data->size = size;
459 data->toffset = opx->offset;
460 data->tsegment = opx->segment;
461 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800462 /*
463 * XXX: improve this if at some point in the future we can
464 * distinguish the subtrahend in expressions like [foo - bar]
465 * where bar is a symbol in the current segment. However, at the
466 * current point, if OPFLAG_RELATIVE is set that subtraction has
467 * already occurred.
468 */
469 data->relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700470 out(data);
471}
472
H. Peter Anvin164d2462017-02-20 02:39:56 -0800473static void out_reladdr(struct out_data *data, const struct operand *opx,
474 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700475{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800476 if (opx->opflags & OPFLAG_RELATIVE)
477 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
478
H. Peter Anvina77692b2016-09-20 14:04:33 -0700479 data->type = OUT_RELADDR;
480 data->sign = OUT_SIGNED;
481 data->size = size;
482 data->toffset = opx->offset;
483 data->tsegment = opx->segment;
484 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800485 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700486 out(data);
487}
488
H. Peter Anvin164d2462017-02-20 02:39:56 -0800489static inline void out_segment(struct out_data *data,
490 const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700491{
492 data->type = OUT_SEGMENT;
493 data->sign = OUT_UNSIGNED;
494 data->size = 2;
495 data->toffset = opx->offset;
496 data->tsegment = ofmt->segbase(opx->segment + 1);
497 data->twrt = opx->wrt;
498 out(data);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400499}
500
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700501static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800502 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000503{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800504 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800505 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000506 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800507 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000508
H. Peter Anvin755f5212012-02-25 11:41:34 -0800509 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700510 return false;
511 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400512 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700513 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400514 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700515
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800516 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100517
Victor van den Elzen154e5922009-02-25 17:32:00 +0100518 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100519 /* Be optimistic in pass 1 */
520 return true;
521
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700523 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000524
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700525 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800526 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
527
528 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
529 /* jmp short (opcode eb) cannot be used with bnd prefix. */
530 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800531 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800532 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800533 }
534
535 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000536}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000537
H. Peter Anvin04445362016-09-21 15:56:19 -0700538/* This is totally just a wild guess what is reasonable... */
539#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
540
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800541int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000542{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700543 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000544 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700545 enum match_result m;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000546 int32_t itimes;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300547 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000548
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800549 nasm_zero(&data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700550 data.offset = start;
551 data.segment = segment;
552 data.itemp = NULL;
553 data.sign = OUT_WRAP;
554 data.bits = bits;
555
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300556 wsize = idata_bytes(instruction->opcode);
557 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000558 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000559
H. Peter Anvineba20a72002-04-30 20:53:55 +0000560 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000562 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563 if (t < 0)
H. Peter Anvin215186f2016-02-17 20:27:41 -0800564 nasm_panic(0, "instruction->times < 0 (%"PRId32") in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000565
H. Peter Anvine2c80182005-01-15 22:15:51 +0000566 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400567 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000568 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400569 if (wsize > 8) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800570 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400571 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000572 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700573 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700574 data.insoffs = 0;
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700575 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700576 data.inslen = data.size = wsize;
577 data.toffset = e->offset;
578 data.tsegment = e->segment;
579 data.twrt = e->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800580 data.relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700581 out(&data);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400582 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700583 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400584 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700585 int align = e->stringlen % wsize;
586 if (align)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000587 align = wsize - align;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700588
589 data.insoffs = 0;
590 data.inslen = e->stringlen + align;
591
592 out_rawdata(&data, e->stringval, e->stringlen);
593 out_rawdata(&data, zero_buffer, align);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 }
595 }
596 if (t > 0 && t == instruction->times - 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700597 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800598 lfmt->uplevel(LIST_TIMES);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000599 }
600 }
601 if (instruction->times > 1)
H. Peter Anvin172b8402016-02-18 01:16:18 -0800602 lfmt->downlevel(LIST_TIMES);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700603 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700604 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000605 FILE *fp;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700606 size_t t = instruction->times;
607 off_t base = 0;
608 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700609 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700610 char *buf = NULL;
611 size_t blk = 0; /* Buffered I/O block size */
612 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000613
H. Peter Anvind81a2352016-09-21 14:03:18 -0700614 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400615 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800616 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700618 goto done;
619 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000620
H. Peter Anvind81a2352016-09-21 14:03:18 -0700621 len = nasm_file_size(fp);
622
623 if (len == (off_t)-1) {
624 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700625 fname);
626 goto close_done;
627 }
628
H. Peter Anvina77692b2016-09-20 14:04:33 -0700629 if (instruction->eops->next) {
630 base = instruction->eops->next->offset;
631 if (base >= len) {
632 len = 0;
633 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000634 len -= base;
635 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700636 len > (off_t)instruction->eops->next->next->offset)
637 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000638 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000639 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700640
H. Peter Anvina77692b2016-09-20 14:04:33 -0700641 lfmt->set_offset(data.offset);
642 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000643
H. Peter Anvind81a2352016-09-21 14:03:18 -0700644 if (!len)
645 goto end_incbin;
646
647 /* Try to map file data */
648 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700649 if (!map) {
650 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
651 buf = nasm_malloc(blk);
652 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700653
654 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700655 /*
656 * Consider these irrelevant for INCBIN, since it is fully
657 * possible that these might be (way) bigger than an int
658 * can hold; there is, however, no reason to widen these
659 * types just for INCBIN. data.inslen == 0 signals to the
660 * backend that these fields are meaningless, if at all
661 * needed.
662 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700663 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700664 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700665
H. Peter Anvind81a2352016-09-21 14:03:18 -0700666 if (map) {
667 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700668 } else if ((off_t)m == len) {
669 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700670 } else {
671 off_t l = len;
672
673 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700674 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700675 "`incbin': unable to seek on file `%s'",
676 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700677 goto end_incbin;
678 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700679 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700680 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700681 if (!m || feof(fp)) {
682 /*
683 * This shouldn't happen unless the file
684 * actually changes while we are reading
685 * it.
686 */
687 nasm_error(ERR_NONFATAL,
688 "`incbin': unexpected EOF while"
689 " reading file `%s'", fname);
690 goto end_incbin;
691 }
692 out_rawdata(&data, buf, m);
693 l -= m;
694 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700695 }
696 }
697 end_incbin:
698 lfmt->downlevel(LIST_INCBIN);
699 if (instruction->times > 1) {
700 lfmt->set_offset(data.offset);
701 lfmt->uplevel(LIST_TIMES);
702 lfmt->downlevel(LIST_TIMES);
703 }
704 if (ferror(fp)) {
705 nasm_error(ERR_NONFATAL,
706 "`incbin': error while"
707 " reading file `%s'", fname);
708 }
709 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700710 if (buf)
711 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700712 if (map)
713 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700714 fclose(fp);
715 done:
716 ;
717 } else {
718 /* "Real" instruction */
719
720 /* Check to see if we need an address-size prefix */
721 add_asp(instruction, bits);
722
723 m = find_match(&temp, instruction, data.segment, data.offset, bits);
724
725 if (m == MOK_GOOD) {
726 /* Matches! */
727 int64_t insn_size = calcsize(data.segment, data.offset,
728 bits, instruction, temp);
729 itimes = instruction->times;
730 if (insn_size < 0) /* shouldn't be, on pass two */
731 nasm_panic(0, "errors made it through from pass one");
732
733 data.itemp = temp;
734 data.bits = bits;
735
736 while (itimes--) {
737 data.insoffs = 0;
738 data.inslen = insn_size;
739
740 gencode(&data, instruction);
741 nasm_assert(data.insoffs == insn_size);
742
743 if (itimes > 0 && itimes == instruction->times - 1) {
744 lfmt->set_offset(data.offset);
H. Peter Anvin172b8402016-02-18 01:16:18 -0800745 lfmt->uplevel(LIST_TIMES);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400746 }
747 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700748 if (instruction->times > 1)
749 lfmt->downlevel(LIST_TIMES);
750 } else {
751 /* No match */
752 switch (m) {
753 case MERR_OPSIZEMISSING:
754 nasm_error(ERR_NONFATAL, "operation size not specified");
755 break;
756 case MERR_OPSIZEMISMATCH:
757 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
758 break;
759 case MERR_BRNUMMISMATCH:
760 nasm_error(ERR_NONFATAL,
761 "mismatch in the number of broadcasting elements");
762 break;
763 case MERR_BADCPU:
764 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
765 break;
766 case MERR_BADMODE:
767 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
768 bits);
769 break;
770 case MERR_ENCMISMATCH:
771 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
772 break;
773 case MERR_BADBND:
774 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
775 break;
776 case MERR_BADREPNE:
777 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
778 (has_prefix(instruction, PPS_REP, P_REPNE) ?
779 "repne" : "repnz"));
780 break;
781 default:
782 nasm_error(ERR_NONFATAL,
783 "invalid combination of opcode and operands");
784 break;
785 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400786 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000787 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700788 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000789}
790
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800791int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000792{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000793 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700794 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000795
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400796 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000798
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700799 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
800 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400801 instruction->opcode == I_DT || instruction->opcode == I_DO ||
802 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000803 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300804 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000805
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300807 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000808
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400809 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000810 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000811
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400813 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000814 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400815 warn_overflow_const(e->offset, wsize);
816 } else if (e->type == EOT_DB_STRING ||
817 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000819
H. Peter Anvine2c80182005-01-15 22:15:51 +0000820 align = (-osize) % wsize;
821 if (align < 0)
822 align += wsize;
823 isize += osize + align;
824 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700825 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000826 }
827
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400829 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700830 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000831
H. Peter Anvind81a2352016-09-21 14:03:18 -0700832 len = nasm_file_size_by_path(fname);
833 if (len == (off_t)-1) {
834 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
835 fname);
836 return 0;
837 }
838
839 if (instruction->eops->next) {
840 if (len <= (off_t)instruction->eops->next->offset) {
841 len = 0;
842 } else {
843 len -= instruction->eops->next->offset;
844 if (instruction->eops->next->next &&
845 len > (off_t)instruction->eops->next->next->offset) {
846 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 }
848 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000849 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700850
851 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000852 }
853
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700854 /* Check to see if we need an address-size prefix */
855 add_asp(instruction, bits);
856
H. Peter Anvin23595f52009-07-25 17:44:25 -0700857 m = find_match(&temp, instruction, segment, offset, bits);
858 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400859 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700860 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700861 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400862 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000863 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000864}
865
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800866static void bad_hle_warn(const insn * ins, uint8_t hleok)
867{
868 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800869 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800870 static const enum whatwarn warn[2][4] =
871 {
872 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
873 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
874 };
875 unsigned int n;
876
877 n = (unsigned int)rep_pfx - P_XACQUIRE;
878 if (n > 1)
879 return; /* Not XACQUIRE/XRELEASE */
880
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800881 ww = warn[n][hleok];
882 if (!is_class(MEMORY, ins->oprs[0].type))
883 ww = w_inval; /* HLE requires operand 0 to be memory */
884
885 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800886 case w_none:
887 break;
888
889 case w_lock:
890 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800891 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800892 "%s with this instruction requires lock",
893 prefix_name(rep_pfx));
894 }
895 break;
896
897 case w_inval:
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 invalid with this instruction",
900 prefix_name(rep_pfx));
901 break;
902 }
903}
904
H. Peter Anvin507ae032008-10-09 15:37:10 -0700905/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400906#define case3(x) case (x): case (x)+1: case (x)+2
907#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700908
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800909static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800910 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000911{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800912 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800913 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000914 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000915 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700916 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700917 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700918 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700919 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800920 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800921 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700922 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000923
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700924 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700925 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700926 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700927
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700928 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400929 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700930
H. Peter Anvine2c80182005-01-15 22:15:51 +0000931 (void)segment; /* Don't warn that this parameter is unused */
932 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000933
H. Peter Anvin839eca22007-10-29 23:12:47 -0700934 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400935 c = *codes++;
936 op1 = (c & 3) + ((opex & 1) << 2);
937 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
938 opx = &ins->oprs[op1];
939 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700940
H. Peter Anvin839eca22007-10-29 23:12:47 -0700941 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400942 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000943 codes += c, length += c;
944 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700945
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400946 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400947 opex = c;
948 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700949
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400950 case4(010):
951 ins->rex |=
952 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 codes++, length++;
954 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700955
Jin Kyu Song164d6072013-10-15 19:10:13 -0700956 case4(014):
957 /* this is an index reg of MIB operand */
958 mib_index = opx->basereg;
959 break;
960
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400961 case4(020):
962 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 length++;
964 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700965
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400966 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000967 length += 2;
968 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700969
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400970 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700971 if (opx->type & (BITS16 | BITS32 | BITS64))
972 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000973 else
974 length += (bits == 16) ? 2 : 4;
975 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700976
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400977 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000978 length += 4;
979 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700980
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400981 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700982 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000983 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700984
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400985 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 length++;
987 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700988
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400989 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000990 length += 8; /* MOV reg64/imm */
991 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700992
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400993 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000994 length += 2;
995 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700996
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400997 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700998 if (opx->type & (BITS16 | BITS32 | BITS64))
999 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 else
1001 length += (bits == 16) ? 2 : 4;
1002 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001003
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001004 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 length += 4;
1006 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001007
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001008 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001009 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001010 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001011
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001012 case 0172:
1013 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001014 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001015 length++;
1016 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001017
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001018 case4(0174):
1019 length++;
1020 break;
1021
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001022 case4(0240):
1023 ins->rex |= REX_EV;
1024 ins->vexreg = regval(opx);
1025 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1026 ins->vex_cm = *codes++;
1027 ins->vex_wlp = *codes++;
1028 ins->evex_tuple = (*codes++ - 0300);
1029 break;
1030
1031 case 0250:
1032 ins->rex |= REX_EV;
1033 ins->vexreg = 0;
1034 ins->vex_cm = *codes++;
1035 ins->vex_wlp = *codes++;
1036 ins->evex_tuple = (*codes++ - 0300);
1037 break;
1038
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001039 case4(0254):
1040 length += 4;
1041 break;
1042
1043 case4(0260):
1044 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001045 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001046 ins->vex_cm = *codes++;
1047 ins->vex_wlp = *codes++;
1048 break;
1049
1050 case 0270:
1051 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001052 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001053 ins->vex_cm = *codes++;
1054 ins->vex_wlp = *codes++;
1055 break;
1056
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001057 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001058 hleok = c & 3;
1059 break;
1060
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001061 case4(0274):
1062 length++;
1063 break;
1064
1065 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001066 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001067
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001069 if (bits == 64)
1070 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001071 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001073
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001075 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001077
H. Peter Anvine2c80182005-01-15 22:15:51 +00001078 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001079 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001080
Keith Kaniosb7a89542007-04-12 02:40:54 +00001081 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001082 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1083 has_prefix(ins, PPS_ASIZE, P_A32))
1084 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001086
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001087 case4(0314):
1088 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001089
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001091 {
1092 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1093 if (pfx == P_O16)
1094 break;
1095 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001096 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001097 else
1098 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001100 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001101
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001103 {
1104 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1105 if (pfx == P_O32)
1106 break;
1107 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001108 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001109 else
1110 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001112 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001113
H. Peter Anvine2c80182005-01-15 22:15:51 +00001114 case 0322:
1115 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001116
Keith Kaniosb7a89542007-04-12 02:40:54 +00001117 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001118 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001119 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001120
Keith Kaniosb7a89542007-04-12 02:40:54 +00001121 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001122 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001123 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001124
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001125 case 0325:
1126 ins->rex |= REX_NH;
1127 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001128
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001129 case 0326:
1130 break;
1131
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 case 0330:
1133 codes++, length++;
1134 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001135
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001138
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001139 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 case 0333:
1141 length++;
1142 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001143
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001144 case 0334:
1145 ins->rex |= REX_L;
1146 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001147
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001148 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001149 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001150
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001151 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001152 if (!ins->prefixes[PPS_REP])
1153 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001154 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001155
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001156 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001157 if (!ins->prefixes[PPS_REP])
1158 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001159 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001160
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001162 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001163 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001165 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1166 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001167 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001168 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001169 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001170 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001171
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001172 case 0341:
1173 if (!ins->prefixes[PPS_WAIT])
1174 ins->prefixes[PPS_WAIT] = P_WAIT;
1175 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001176
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001177 case 0360:
1178 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001179
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001180 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001181 length++;
1182 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001183
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001184 case 0364:
1185 case 0365:
1186 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001187
Keith Kanios48af1772007-08-17 07:37:52 +00001188 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001189 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001190 length++;
1191 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001192
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001193 case 0370:
1194 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001195 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001196
H. Peter Anvine2c80182005-01-15 22:15:51 +00001197 case 0373:
1198 length++;
1199 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001200
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001201 case 0374:
1202 eat = EA_XMMVSIB;
1203 break;
1204
1205 case 0375:
1206 eat = EA_YMMVSIB;
1207 break;
1208
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001209 case 0376:
1210 eat = EA_ZMMVSIB;
1211 break;
1212
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001213 case4(0100):
1214 case4(0110):
1215 case4(0120):
1216 case4(0130):
1217 case4(0200):
1218 case4(0204):
1219 case4(0210):
1220 case4(0214):
1221 case4(0220):
1222 case4(0224):
1223 case4(0230):
1224 case4(0234):
1225 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001226 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001227 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001228 opflags_t rflags;
1229 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001230 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001231
Keith Kaniosb7a89542007-04-12 02:40:54 +00001232 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001233
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001234 if (c <= 0177) {
1235 /* pick rfield from operand b (opx) */
1236 rflags = regflag(opx);
1237 rfield = nasm_regvals[opx->basereg];
1238 } else {
1239 rflags = 0;
1240 rfield = c & 7;
1241 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001242
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001243 /* EVEX.b1 : evex_brerop contains the operand position */
1244 op_er_sae = (ins->evex_brerop >= 0 ?
1245 &ins->oprs[ins->evex_brerop] : NULL);
1246
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001247 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1248 /* set EVEX.b */
1249 ins->evex_p[2] |= EVEX_P2B;
1250 if (op_er_sae->decoflags & ER) {
1251 /* set EVEX.RC (rounding control) */
1252 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1253 & EVEX_P2RC;
1254 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001255 } else {
1256 /* set EVEX.L'L (vector length) */
1257 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001258 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001259 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001260 /* set EVEX.b */
1261 ins->evex_p[2] |= EVEX_P2B;
1262 }
1263 }
1264
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001265 if (itemp_has(temp, IF_MIB)) {
1266 opy->eaflags |= EAF_MIB;
1267 /*
1268 * if a separate form of MIB (ICC style) is used,
1269 * the index reg info is merged into mem operand
1270 */
1271 if (mib_index != R_none) {
1272 opy->indexreg = mib_index;
1273 opy->scale = 1;
1274 opy->hintbase = mib_index;
1275 opy->hinttype = EAH_NOTBASE;
1276 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001277 }
1278
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001279 if (process_ea(opy, &ea_data, bits,
1280 rfield, rflags, ins) != eat) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001281 nasm_error(ERR_NONFATAL, "invalid effective address");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001282 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001283 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001284 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001285 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001286 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001287 }
1288 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001289
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001290 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001291 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001292 ": instruction code \\%o (0x%02X) given", c, c);
1293 break;
1294 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001295 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001296
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001297 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001298
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001299 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001300 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001301 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001302 return -1;
1303 }
1304 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001305 }
1306
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001307 switch (ins->prefixes[PPS_VEX]) {
1308 case P_EVEX:
1309 if (!(ins->rex & REX_EV))
1310 return -1;
1311 break;
1312 case P_VEX3:
1313 case P_VEX2:
1314 if (!(ins->rex & REX_V))
1315 return -1;
1316 break;
1317 default:
1318 break;
1319 }
1320
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001321 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001322 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001323
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001324 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001325 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001326 return -1;
1327 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001328 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001329 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001330 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001331 ins->rex &= ~REX_W;
1332 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001333 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001334 ins->rex |= REX_W;
1335 bad32 &= ~REX_W;
1336 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001337 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001338 /* Follow REX_W */
1339 break;
1340 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001341
H. Peter Anvinfc561202011-07-07 16:58:22 -07001342 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001343 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001344 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001345 } else if (!(ins->rex & REX_EV) &&
1346 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001347 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001348 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001349 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001350 if (ins->rex & REX_EV)
1351 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001352 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1353 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001354 length += 3;
1355 else
1356 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001357 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001358 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001359 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001360 return -1;
1361 } else if (bits == 64) {
1362 length++;
1363 } else if ((ins->rex & REX_L) &&
1364 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001365 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001366 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001367 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001368 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001369 length++;
1370 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001371 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001372 return -1;
1373 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001374 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001375
1376 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001377 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001378 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001379 "instruction is not lockable");
1380 }
1381
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001382 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001383
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001384 /*
1385 * when BND prefix is set by DEFAULT directive,
1386 * BND prefix is added to every appropriate instruction line
1387 * unless it is overridden by NOBND prefix.
1388 */
1389 if (globalbnd &&
1390 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1391 ins->prefixes[PPS_REP] = P_BND;
1392
H. Peter Anvina77692b2016-09-20 14:04:33 -07001393 /*
1394 * Add length of legacy prefixes
1395 */
1396 length += emit_prefix(NULL, bits, ins);
1397
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001398 return length;
1399}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001400
H. Peter Anvina77692b2016-09-20 14:04:33 -07001401static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001402{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001403 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001404 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001405 !(ins->rex & (REX_V | REX_EV)) &&
1406 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001407 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1408 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001409 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001410 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001411 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001412}
1413
H. Peter Anvina77692b2016-09-20 14:04:33 -07001414static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1415{
1416 int bytes = 0;
1417 int j;
1418
1419 for (j = 0; j < MAXPREFIX; j++) {
1420 uint8_t c = 0;
1421 switch (ins->prefixes[j]) {
1422 case P_WAIT:
1423 c = 0x9B;
1424 break;
1425 case P_LOCK:
1426 c = 0xF0;
1427 break;
1428 case P_REPNE:
1429 case P_REPNZ:
1430 case P_XACQUIRE:
1431 case P_BND:
1432 c = 0xF2;
1433 break;
1434 case P_REPE:
1435 case P_REPZ:
1436 case P_REP:
1437 case P_XRELEASE:
1438 c = 0xF3;
1439 break;
1440 case R_CS:
1441 if (bits == 64) {
1442 nasm_error(ERR_WARNING | ERR_PASS2,
1443 "cs segment base generated, but will be ignored in 64-bit mode");
1444 }
1445 c = 0x2E;
1446 break;
1447 case R_DS:
1448 if (bits == 64) {
1449 nasm_error(ERR_WARNING | ERR_PASS2,
1450 "ds segment base generated, but will be ignored in 64-bit mode");
1451 }
1452 c = 0x3E;
1453 break;
1454 case R_ES:
1455 if (bits == 64) {
1456 nasm_error(ERR_WARNING | ERR_PASS2,
1457 "es segment base generated, but will be ignored in 64-bit mode");
1458 }
1459 c = 0x26;
1460 break;
1461 case R_FS:
1462 c = 0x64;
1463 break;
1464 case R_GS:
1465 c = 0x65;
1466 break;
1467 case R_SS:
1468 if (bits == 64) {
1469 nasm_error(ERR_WARNING | ERR_PASS2,
1470 "ss segment base generated, but will be ignored in 64-bit mode");
1471 }
1472 c = 0x36;
1473 break;
1474 case R_SEGR6:
1475 case R_SEGR7:
1476 nasm_error(ERR_NONFATAL,
1477 "segr6 and segr7 cannot be used as prefixes");
1478 break;
1479 case P_A16:
1480 if (bits == 64) {
1481 nasm_error(ERR_NONFATAL,
1482 "16-bit addressing is not supported "
1483 "in 64-bit mode");
1484 } else if (bits != 16)
1485 c = 0x67;
1486 break;
1487 case P_A32:
1488 if (bits != 32)
1489 c = 0x67;
1490 break;
1491 case P_A64:
1492 if (bits != 64) {
1493 nasm_error(ERR_NONFATAL,
1494 "64-bit addressing is only supported "
1495 "in 64-bit mode");
1496 }
1497 break;
1498 case P_ASP:
1499 c = 0x67;
1500 break;
1501 case P_O16:
1502 if (bits != 16)
1503 c = 0x66;
1504 break;
1505 case P_O32:
1506 if (bits == 16)
1507 c = 0x66;
1508 break;
1509 case P_O64:
1510 /* REX.W */
1511 break;
1512 case P_OSP:
1513 c = 0x66;
1514 break;
1515 case P_EVEX:
1516 case P_VEX3:
1517 case P_VEX2:
1518 case P_NOBND:
1519 case P_none:
1520 break;
1521 default:
1522 nasm_panic(0, "invalid instruction prefix");
1523 }
1524 if (c) {
1525 if (data)
1526 out_rawbyte(data, c);
1527 bytes++;
1528 }
1529 }
1530 return bytes;
1531}
1532
1533static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001534{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001535 uint8_t c;
1536 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001537 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001538 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001539 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001540 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001541 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001542 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001543 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001544 const int bits = data->bits;
H. Peter Anvin70653092007-10-19 14:42:29 -07001545
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001546 ins->rex_done = false;
1547
H. Peter Anvina77692b2016-09-20 14:04:33 -07001548 emit_prefix(data, bits, ins);
1549
H. Peter Anvin839eca22007-10-29 23:12:47 -07001550 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001551 c = *codes++;
1552 op1 = (c & 3) + ((opex & 1) << 2);
1553 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1554 opx = &ins->oprs[op1];
1555 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001556
H. Peter Anvina77692b2016-09-20 14:04:33 -07001557
H. Peter Anvin839eca22007-10-29 23:12:47 -07001558 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001559 case 01:
1560 case 02:
1561 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001562 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001563 emit_rex(data, ins);
1564 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001565 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001567
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001568 case 05:
1569 case 06:
1570 case 07:
1571 opex = c;
1572 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001573
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001574 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001575 emit_rex(data, ins);
1576 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001578
Jin Kyu Song164d6072013-10-15 19:10:13 -07001579 case4(014):
1580 break;
1581
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001582 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001583 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001584 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001585 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001586 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001587 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001588
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001589 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001590 if (opx->offset < 0 || 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 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001593 out_imm(data, opx, 1, OUT_UNSIGNED);
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(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001597 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001598 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001599 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001600
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001601 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001602 if (opx->type & (BITS16 | BITS32))
1603 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 else
1605 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001606 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001607 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001608 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001609
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001610 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001611 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001612 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001614
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001615 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001616 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001617 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001618 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001620
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001621 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001622 if (opx->segment == data->segment) {
1623 int64_t delta = opx->offset - data->offset
1624 - (data->inslen - data->insoffs);
1625 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001626 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001627 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001628 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001629 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001630
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001631 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001632 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001633 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001634
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001635 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001636 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001638
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001639 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001640 if (opx->type & (BITS16 | BITS32 | BITS64))
1641 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001642 else
1643 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001644
1645 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001647
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001648 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001649 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001651
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001653 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001654 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001655 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001656 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001658
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001659 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001660 {
1661 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1662 const struct operand *opy;
1663
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001664 c = *codes++;
1665 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001666 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001667 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001668 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001669 "non-absolute expression not permitted as argument %d",
1670 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001671 } else if (opy->offset & ~mask) {
1672 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1673 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001674 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001675 c = opy->offset & mask;
1676 goto emit_is4;
1677 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001678
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001679 case 0173:
1680 c = *codes++;
1681 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001682 c &= 15;
1683 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001684
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001685 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001686 c = 0;
1687 emit_is4:
1688 r = nasm_regvals[opx->basereg];
1689 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001690 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001691
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001692 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001693 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001694 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001695 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001696 "signed dword immediate exceeds bounds");
1697 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001698 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001699 break;
1700
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001701 case4(0240):
1702 case 0250:
1703 codes += 3;
1704 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1705 EVEX_P2Z | EVEX_P2AAA, 2);
1706 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1707 bytes[0] = 0x62;
1708 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001709 bytes[1] = ((((ins->rex & 7) << 5) |
1710 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001711 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001712 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1713 ((~ins->vexreg & 15) << 3) |
1714 (1 << 2) | (ins->vex_wlp & 3);
1715 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001716 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001717 break;
1718
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001719 case4(0260):
1720 case 0270:
1721 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001722 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1723 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001724 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1725 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1726 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001727 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001728 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001729 } else {
1730 bytes[0] = 0xc5;
1731 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001732 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001733 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001734 }
1735 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001736
H. Peter Anvine014f352012-02-25 22:35:19 -08001737 case 0271:
1738 case 0272:
1739 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001740 break;
1741
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001742 case4(0274):
1743 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001744 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001745 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001746
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001747 if (absolute_op(opx)) {
1748 if (ins->rex & REX_W)
1749 s = 64;
1750 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1751 s = 16;
1752 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1753 s = 32;
1754 else
1755 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001756
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001757 um = (uint64_t)2 << (s-1);
1758 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001759
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001760 if (uv > 127 && uv < (uint64_t)-128 &&
1761 (uv < um-128 || uv > um-1)) {
1762 /* If this wasn't explicitly byte-sized, warn as though we
1763 * had fallen through to the imm16/32/64 case.
1764 */
1765 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1766 "%s value exceeds bounds",
1767 (opx->type & BITS8) ? "signed byte" :
1768 s == 16 ? "word" :
1769 s == 32 ? "dword" :
1770 "signed dword");
1771 }
1772
1773 /* Output as a raw byte to avoid byte overflow check */
1774 out_rawbyte(data, (uint8_t)uv);
1775 } else {
1776 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001777 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001778 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001779 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001780
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001781 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001782 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001783
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001785 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1786 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001788
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001790 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1791 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001793
H. Peter Anvine2c80182005-01-15 22:15:51 +00001794 case 0312:
1795 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001796
Keith Kaniosb7a89542007-04-12 02:40:54 +00001797 case 0313:
1798 ins->rex = 0;
1799 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001800
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001801 case4(0314):
1802 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001803
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001807
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001809 case 0323:
1810 break;
1811
Keith Kaniosb7a89542007-04-12 02:40:54 +00001812 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001813 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001815
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001816 case 0325:
1817 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001818
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001819 case 0326:
1820 break;
1821
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001823 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001825
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001828
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001829 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001831 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001833
Keith Kanios48af1772007-08-17 07:37:52 +00001834 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001835 if (ins->rex & REX_R)
1836 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001837 ins->rex &= ~(REX_L|REX_R);
1838 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001839
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001840 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001841 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001842
H. Peter Anvin962e3052008-08-28 17:47:16 -07001843 case 0336:
1844 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001845 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001846
H. Peter Anvine2c80182005-01-15 22:15:51 +00001847 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001848 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001849 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001850
1851 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001853
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001854 case 0341:
1855 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001856
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001857 case 0360:
1858 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001859
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001860 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001861 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001862 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001863
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001864 case 0364:
1865 case 0365:
1866 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001867
Keith Kanios48af1772007-08-17 07:37:52 +00001868 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001869 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001870 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001871 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001872
Jin Kyu Song03041092013-10-15 19:38:51 -07001873 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001874 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001875
H. Peter Anvine2c80182005-01-15 22:15:51 +00001876 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001877 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001879
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001880 case 0374:
1881 eat = EA_XMMVSIB;
1882 break;
1883
1884 case 0375:
1885 eat = EA_YMMVSIB;
1886 break;
1887
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001888 case 0376:
1889 eat = EA_ZMMVSIB;
1890 break;
1891
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001892 case4(0100):
1893 case4(0110):
1894 case4(0120):
1895 case4(0130):
1896 case4(0200):
1897 case4(0204):
1898 case4(0210):
1899 case4(0214):
1900 case4(0220):
1901 case4(0224):
1902 case4(0230):
1903 case4(0234):
1904 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001905 ea ea_data;
1906 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001907 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001908 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001909 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001910
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001911 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001912 /* pick rfield from operand b (opx) */
1913 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001914 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001915 } else {
1916 /* rfield is constant */
1917 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001918 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001919 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001920
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001921 if (process_ea(opy, &ea_data, bits,
1922 rfield, rflags, ins) != eat)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001923 nasm_error(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001924
H. Peter Anvine2c80182005-01-15 22:15:51 +00001925 p = bytes;
1926 *p++ = ea_data.modrm;
1927 if (ea_data.sib_present)
1928 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001929 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001930
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001931 /*
1932 * Make sure the address gets the right offset in case
1933 * the line breaks in the .lst file (BR 1197827)
1934 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001935
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001936 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001937 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001938 if (ea_data.disp8) {
1939 out_rawbyte(data, ea_data.disp8);
1940 } else if (ea_data.rip) {
1941 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001942 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001943 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001944
H. Peter Anvina77692b2016-09-20 14:04:33 -07001945 if (overflow_general(opy->offset, asize) ||
1946 signed_bits(opy->offset, ins->addr_size) !=
1947 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001948 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001949
H. Peter Anvina77692b2016-09-20 14:04:33 -07001950 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001951 (asize > ea_data.bytes)
1952 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001953 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001954 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001955 }
1956 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001957
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001958 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001959 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001960 ": instruction code \\%o (0x%02X) given", c, c);
1961 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001963 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001964}
1965
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001966static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001967{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001968 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001969 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001970 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001971}
1972
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001973static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001974{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001975 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001976 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001977 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001978}
1979
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001980static int op_rexflags(const operand * o, int mask)
1981{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001982 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001983 int val;
1984
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001985 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001986 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001987
H. Peter Anvina4835d42008-05-20 14:21:29 -07001988 flags = nasm_reg_flags[o->basereg];
1989 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001990
1991 return rexflags(val, flags, mask);
1992}
1993
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001994static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001995{
1996 int rex = 0;
1997
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001998 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001999 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002000 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002001 rex |= REX_W;
2002 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2003 rex |= REX_H;
2004 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2005 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002006
2007 return rex & mask;
2008}
2009
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002010static int evexflags(int val, decoflags_t deco,
2011 int mask, uint8_t byte)
2012{
2013 int evex = 0;
2014
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002015 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002016 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002017 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002018 evex |= (EVEX_P0RP | EVEX_P0X);
2019 break;
2020 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002021 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002022 evex |= EVEX_P2VP;
2023 if (deco & Z)
2024 evex |= EVEX_P2Z;
2025 if (deco & OPMASK_MASK)
2026 evex |= deco & EVEX_P2AAA;
2027 break;
2028 }
2029 return evex & mask;
2030}
2031
2032static int op_evexflags(const operand * o, int mask, uint8_t byte)
2033{
2034 int val;
2035
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002036 val = nasm_regvals[o->basereg];
2037
2038 return evexflags(val, o->decoflags, mask, byte);
2039}
2040
H. Peter Anvin23595f52009-07-25 17:44:25 -07002041static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002042 insn *instruction,
2043 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002044{
2045 const struct itemplate *temp;
2046 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002047 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002048 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002049 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002050 int i;
2051
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002052 /* broadcasting uses a different data element size */
2053 for (i = 0; i < instruction->operands; i++)
2054 if (i == broadcast)
2055 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2056 else
2057 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002058
2059 merr = MERR_INVALOP;
2060
2061 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002062 temp->opcode != I_none; temp++) {
2063 m = matches(temp, instruction, bits);
2064 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002065 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002066 m = MOK_GOOD;
2067 else
2068 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002069 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002070 /*
2071 * Missing operand size and a candidate for fuzzy matching...
2072 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002073 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002074 if (i == broadcast)
2075 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2076 else
2077 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002078 opsizemissing = true;
2079 }
2080 if (m > merr)
2081 merr = m;
2082 if (merr == MOK_GOOD)
2083 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002084 }
2085
2086 /* No match, but see if we can get a fuzzy operand size match... */
2087 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002088 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002089
2090 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002091 /*
2092 * We ignore extrinsic operand sizes on registers, so we should
2093 * never try to fuzzy-match on them. This also resolves the case
2094 * when we have e.g. "xmmrm128" in two different positions.
2095 */
2096 if (is_class(REGISTER, instruction->oprs[i].type))
2097 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002098
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002099 /* This tests if xsizeflags[i] has more than one bit set */
2100 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2101 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002102
Jin Kyu Song7903c072013-10-30 03:00:12 -07002103 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002104 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002105 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2106 BITS32 : BITS64);
2107 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002108 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002109 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002110 }
2111
2112 /* Try matching again... */
2113 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002114 temp->opcode != I_none; temp++) {
2115 m = matches(temp, instruction, bits);
2116 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002117 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002118 m = MOK_GOOD;
2119 else
2120 m = MERR_INVALOP;
2121 }
2122 if (m > merr)
2123 merr = m;
2124 if (merr == MOK_GOOD)
2125 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002126 }
2127
H. Peter Anvina81655b2009-07-25 18:15:28 -07002128done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002129 *tempp = temp;
2130 return merr;
2131}
2132
Mark Charneydcaef4b2014-10-09 13:45:17 -04002133static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2134{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002135 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002136 uint8_t brcast_num;
2137
Mark Charneydcaef4b2014-10-09 13:45:17 -04002138 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002139 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002140 "size of broadcasting element is greater than 64 bits");
2141
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002142 /*
2143 * The shift term is to take care of the extra BITS80 inserted
2144 * between BITS64 and BITS128.
2145 */
2146 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2147 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002148
2149 return brcast_num;
2150}
2151
H. Peter Anvin65289e82009-07-25 17:25:11 -07002152static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002153 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002154{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002155 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002156 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002157 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002158
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002159 /*
2160 * Check the opcode
2161 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002162 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002163 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002164
2165 /*
2166 * Count the operands
2167 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002168 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002169 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002170
2171 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002172 * Is it legal?
2173 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002174 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002175 return MERR_INVALOP;
2176
2177 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002178 * {evex} available?
2179 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002180 switch (instruction->prefixes[PPS_VEX]) {
2181 case P_EVEX:
2182 if (!itemp_has(itemp, IF_EVEX))
2183 return MERR_ENCMISMATCH;
2184 break;
2185 case P_VEX3:
2186 case P_VEX2:
2187 if (!itemp_has(itemp, IF_VEX))
2188 return MERR_ENCMISMATCH;
2189 break;
2190 default:
2191 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002192 }
2193
2194 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002195 * Check that no spurious colons or TOs are present
2196 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002197 for (i = 0; i < itemp->operands; i++)
2198 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002199 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002200
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002201 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002202 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002203 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002204 switch (itemp_smask(itemp)) {
2205 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002206 asize = BITS8;
2207 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002208 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002209 asize = BITS16;
2210 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002211 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002212 asize = BITS32;
2213 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002214 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002215 asize = BITS64;
2216 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002217 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002218 asize = BITS128;
2219 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002220 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002221 asize = BITS256;
2222 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002223 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002224 asize = BITS512;
2225 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002226 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002227 switch (bits) {
2228 case 16:
2229 asize = BITS16;
2230 break;
2231 case 32:
2232 asize = BITS32;
2233 break;
2234 case 64:
2235 asize = BITS64;
2236 break;
2237 default:
2238 asize = 0;
2239 break;
2240 }
2241 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002242 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002243 asize = 0;
2244 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002245 }
2246
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002247 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002248 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002249 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002250 memset(size, 0, sizeof size);
2251 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002252 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002253 /* S- flags apply to all operands */
2254 for (i = 0; i < MAX_OPERANDS; i++)
2255 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002256 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002257
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002258 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002259 * Check that the operand flags all match up,
2260 * it's a bit tricky so lets be verbose:
2261 *
2262 * 1) Find out the size of operand. If instruction
2263 * doesn't have one specified -- we're trying to
2264 * guess it either from template (IF_S* flag) or
2265 * from code bits.
2266 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002267 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002268 * template has an operand size specified AND this size differ
2269 * from which instruction has (perhaps we got it from code bits)
2270 * we are:
2271 * a) Check that only size of instruction and operand is differ
2272 * other characteristics do match
2273 * b) Perhaps it's a register specified in instruction so
2274 * for such a case we just mark that operand as "size
2275 * missing" and this will turn on fuzzy operand size
2276 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002277 */
2278 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002279 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002280 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002281 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002282 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002283 opflags_t template_opsize, insn_opsize;
2284
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002285 if (!(type & SIZE_MASK))
2286 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002287
Jin Kyu Song7903c072013-10-30 03:00:12 -07002288 insn_opsize = type & SIZE_MASK;
2289 if (!is_broadcast) {
2290 template_opsize = itemp->opd[i] & SIZE_MASK;
2291 } else {
2292 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2293 /*
2294 * when broadcasting, the element size depends on
2295 * the instruction type. decorator flag should match.
2296 */
2297
2298 if (deco_brsize) {
2299 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002300 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002301 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002302 } else {
2303 template_opsize = 0;
2304 }
2305 }
2306
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002307 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002308 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002309 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002310 } else if (template_opsize) {
2311 if (template_opsize != insn_opsize) {
2312 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002313 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002314 } else if (!is_class(REGISTER, type)) {
2315 /*
2316 * Note: we don't honor extrinsic operand sizes for registers,
2317 * so "missing operand size" for a register should be
2318 * considered a wildcard match rather than an error.
2319 */
2320 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002321 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002322 } else if (is_broadcast &&
2323 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002324 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002325 /*
2326 * broadcasting opsize matches but the number of repeated memory
2327 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002328 * if 64b double precision float is broadcasted to ymm (256b),
2329 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002330 */
2331 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002332 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002333 }
2334 }
2335
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002336 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002337 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002338
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002339 /*
2340 * Check operand sizes
2341 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002342 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2343 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002345 asize = itemp->opd[i] & SIZE_MASK;
2346 if (asize) {
2347 for (i = 0; i < oprs; i++)
2348 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002349 break;
2350 }
2351 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002352 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002353 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002354 }
2355
Keith Kaniosb7a89542007-04-12 02:40:54 +00002356 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002357 if (!(itemp->opd[i] & SIZE_MASK) &&
2358 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002359 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002360 }
2361
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002362 /*
2363 * Check template is okay at the set cpu level
2364 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002365 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002366 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002367
Keith Kaniosb7a89542007-04-12 02:40:54 +00002368 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002369 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002370 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002371 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002372 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002373
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002374 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002375 * If we have a HLE prefix, look for the NOHLE flag
2376 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002377 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002378 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2379 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2380 return MERR_BADHLE;
2381
2382 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002383 * Check if special handling needed for Jumps
2384 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002385 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002386 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002387
Jin Kyu Song03041092013-10-15 19:38:51 -07002388 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002389 * Check if BND prefix is allowed.
2390 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002391 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002392 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002393 (has_prefix(instruction, PPS_REP, P_BND) ||
2394 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002395 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002396 else if (itemp_has(itemp, IF_BND) &&
2397 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2398 has_prefix(instruction, PPS_REP, P_REPNZ)))
2399 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002400
H. Peter Anvin60926242009-07-26 16:25:38 -07002401 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002402}
2403
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002404/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002405 * Check if ModR/M.mod should/can be 01.
2406 * - EAF_BYTEOFFS is set
2407 * - offset can fit in a byte when EVEX is not used
2408 * - offset can be compressed when EVEX is used
2409 */
2410#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2411 (o >= -128 && o <= 127 && \
2412 seg == NO_SEG && !forw_ref && \
2413 !(input->eaflags & EAF_WORDOFFS) && \
2414 !(ins->rex & REX_EV)) || \
2415 (ins->rex & REX_EV && \
2416 is_disp8n(input, ins, &output->disp8)))
2417
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002418static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002419 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002420{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002421 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002422 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002423 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002424
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002425 output->type = EA_SCALAR;
2426 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002427 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002428
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002429 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002430 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002431 /* EVEX.R' flag for the REG operand */
2432 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002433
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002434 if (is_class(REGISTER, input->type)) {
2435 /*
2436 * It's a direct register.
2437 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002438 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002439 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002440
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002441 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002442 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002443
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002444 /* broadcasting is not available with a direct register operand. */
2445 if (input->decoflags & BRDCAST_MASK) {
2446 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2447 goto err;
2448 }
2449
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002450 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002451 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002452 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002453 output->bytes = 0; /* no offset necessary either */
2454 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2455 } else {
2456 /*
2457 * It's a memory reference.
2458 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002459
2460 /* Embedded rounding or SAE is not available with a mem ref operand. */
2461 if (input->decoflags & (ER | SAE)) {
2462 nasm_error(ERR_NONFATAL,
2463 "Embedded rounding is available only with reg-reg op.");
2464 return -1;
2465 }
2466
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002467 if (input->basereg == -1 &&
2468 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002469 /*
2470 * It's a pure offset.
2471 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002472 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
2473 if (input->segment == NO_SEG || (input->opflags & OPFLAG_RELATIVE)) {
2474 nasm_error(ERR_WARNING | ERR_PASS2, "absolute address can not be RIP-relative");
2475 input->type &= ~IP_REL;
2476 input->type |= MEMORY;
2477 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002478 }
2479
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002480 if (bits == 64 &&
2481 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2482 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2483 return -1;
2484 }
2485
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002486 if (eaflags & EAF_BYTEOFFS ||
2487 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002488 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2489 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2490 }
2491
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002492 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002493 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002494 output->sib = GEN_SIB(0, 4, 5);
2495 output->bytes = 4;
2496 output->modrm = GEN_MODRM(0, rfield, 4);
2497 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002498 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002499 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002500 output->bytes = (addrbits != 16 ? 4 : 2);
2501 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2502 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002503 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002504 } else {
2505 /*
2506 * It's an indirection.
2507 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002508 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002509 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002511 int t, it, bt; /* register numbers */
2512 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002513
H. Peter Anvine2c80182005-01-15 22:15:51 +00002514 if (s == 0)
2515 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002516
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002517 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002518 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002519 ix = nasm_reg_flags[i];
2520 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002521 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002522 ix = 0;
2523 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002524
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002525 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002526 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002527 bx = nasm_reg_flags[b];
2528 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002529 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002530 bx = 0;
2531 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002532
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002533 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002534 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002535 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002536 int32_t o = input->offset;
2537 int mod, scale, index, base;
2538
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002539 /*
2540 * For a vector SIB, one has to be a vector and the other,
2541 * if present, a GPR. The vector must be the index operand.
2542 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002543 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002544 if (s == 0)
2545 s = 1;
2546 else if (s != 1)
2547 goto err;
2548
2549 t = bt, bt = it, it = t;
2550 x = bx, bx = ix, ix = x;
2551 }
2552
2553 if (bt != -1) {
2554 if (REG_GPR & ~bx)
2555 goto err;
2556 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2557 sok &= bx;
2558 else
2559 goto err;
2560 }
2561
2562 /*
2563 * While we're here, ensure the user didn't specify
2564 * WORD or QWORD
2565 */
2566 if (input->disp_size == 16 || input->disp_size == 64)
2567 goto err;
2568
2569 if (addrbits == 16 ||
2570 (addrbits == 32 && !(sok & BITS32)) ||
2571 (addrbits == 64 && !(sok & BITS64)))
2572 goto err;
2573
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002574 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2575 : ((ix & YMMREG & ~REG_EA)
2576 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002577
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002578 output->rex |= rexflags(it, ix, REX_X);
2579 output->rex |= rexflags(bt, bx, REX_B);
2580 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002581
2582 index = it & 7; /* it is known to be != -1 */
2583
2584 switch (s) {
2585 case 1:
2586 scale = 0;
2587 break;
2588 case 2:
2589 scale = 1;
2590 break;
2591 case 4:
2592 scale = 2;
2593 break;
2594 case 8:
2595 scale = 3;
2596 break;
2597 default: /* then what the smeg is it? */
2598 goto err; /* panic */
2599 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002600
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002601 if (bt == -1) {
2602 base = 5;
2603 mod = 0;
2604 } else {
2605 base = (bt & 7);
2606 if (base != REG_NUM_EBP && o == 0 &&
2607 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002608 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002609 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002610 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002611 mod = 1;
2612 else
2613 mod = 2;
2614 }
2615
2616 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002617 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2618 output->modrm = GEN_MODRM(mod, rfield, 4);
2619 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002620 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002621 /*
2622 * it must be a 32/64-bit memory reference. Firstly we have
2623 * to check that all registers involved are type E/Rxx.
2624 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002625 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002626 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002627
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002628 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002629 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2630 sok &= ix;
2631 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002632 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002633 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002634
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002635 if (bt != -1) {
2636 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002637 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002638 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002639 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002640 sok &= bx;
2641 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002642
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002643 /*
2644 * While we're here, ensure the user didn't specify
2645 * WORD or QWORD
2646 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002647 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002648 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002649
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002650 if (addrbits == 16 ||
2651 (addrbits == 32 && !(sok & BITS32)) ||
2652 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002653 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002654
Keith Kaniosb7a89542007-04-12 02:40:54 +00002655 /* now reorganize base/index */
2656 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002657 ((hb == b && ht == EAH_NOTBASE) ||
2658 (hb == i && ht == EAH_MAKEBASE))) {
2659 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002660 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002661 x = bx, bx = ix, ix = x;
2662 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002663
Jin Kyu Song164d6072013-10-15 19:10:13 -07002664 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002665 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002666 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002667 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002668 if (eaflags & EAF_MIB) {
2669 /* only for mib operands */
2670 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2671 /*
2672 * make a single reg index [reg*1].
2673 * gas uses this form for an explicit index register.
2674 */
2675 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2676 }
2677 if ((ht == EAH_SUMMED) && bt == -1) {
2678 /* separate once summed index into [base, index] */
2679 bt = it, bx = ix, s--;
2680 }
2681 } else {
2682 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002683 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002684 s == 3 || s == 5 || s == 9) && bt == -1) {
2685 /* convert 3*EAX to EAX+2*EAX */
2686 bt = it, bx = ix, s--;
2687 }
2688 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002689 (eaflags & EAF_TIMESTWO) &&
2690 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002691 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002692 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002693 * to sib format with 0x0 displacement - [EAX*1+0].
2694 */
2695 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2696 }
2697 }
Keith Kanios48af1772007-08-17 07:37:52 +00002698 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002699 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002700 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002701 x = ix, ix = bx, bx = x;
2702 }
2703 if (it == REG_NUM_ESP ||
2704 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002705 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002706
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002707 output->rex |= rexflags(it, ix, REX_X);
2708 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002709
Keith Kanios48af1772007-08-17 07:37:52 +00002710 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002711 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002713
Keith Kaniosb7a89542007-04-12 02:40:54 +00002714 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002715 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002716 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002717 } else {
2718 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002719 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002720 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002721 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002722 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002723 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002724 mod = 1;
2725 else
2726 mod = 2;
2727 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002728
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002729 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002730 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2731 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002732 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002733 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002735
Keith Kaniosb7a89542007-04-12 02:40:54 +00002736 if (it == -1)
2737 index = 4, s = 1;
2738 else
2739 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002740
H. Peter Anvine2c80182005-01-15 22:15:51 +00002741 switch (s) {
2742 case 1:
2743 scale = 0;
2744 break;
2745 case 2:
2746 scale = 1;
2747 break;
2748 case 4:
2749 scale = 2;
2750 break;
2751 case 8:
2752 scale = 3;
2753 break;
2754 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002755 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002757
Keith Kaniosb7a89542007-04-12 02:40:54 +00002758 if (bt == -1) {
2759 base = 5;
2760 mod = 0;
2761 } else {
2762 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002763 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002764 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002765 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002766 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002767 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002768 mod = 1;
2769 else
2770 mod = 2;
2771 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002772
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002773 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002774 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2775 output->modrm = GEN_MODRM(mod, rfield, 4);
2776 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002777 }
2778 } else { /* it's 16-bit */
2779 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002780 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002781
Keith Kaniosb7a89542007-04-12 02:40:54 +00002782 /* check for 64-bit long mode */
2783 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002784 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002785
H. Peter Anvine2c80182005-01-15 22:15:51 +00002786 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002787 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2788 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002789 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002790
Keith Kaniosb7a89542007-04-12 02:40:54 +00002791 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002792 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002793 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002794
H. Peter Anvine2c80182005-01-15 22:15:51 +00002795 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002796 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 if (b == -1 && i != -1) {
2798 int tmp = b;
2799 b = i;
2800 i = tmp;
2801 } /* swap */
2802 if ((b == R_SI || b == R_DI) && i != -1) {
2803 int tmp = b;
2804 b = i;
2805 i = tmp;
2806 }
2807 /* have BX/BP as base, SI/DI index */
2808 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002809 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002810 if (i != -1 && b != -1 &&
2811 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002812 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002813 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002814 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002815
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 rm = -1;
2817 if (i != -1)
2818 switch (i * 256 + b) {
2819 case R_SI * 256 + R_BX:
2820 rm = 0;
2821 break;
2822 case R_DI * 256 + R_BX:
2823 rm = 1;
2824 break;
2825 case R_SI * 256 + R_BP:
2826 rm = 2;
2827 break;
2828 case R_DI * 256 + R_BP:
2829 rm = 3;
2830 break;
2831 } else
2832 switch (b) {
2833 case R_SI:
2834 rm = 4;
2835 break;
2836 case R_DI:
2837 rm = 5;
2838 break;
2839 case R_BP:
2840 rm = 6;
2841 break;
2842 case R_BX:
2843 rm = 7;
2844 break;
2845 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002846 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002847 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002848
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002849 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002850 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002851 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002852 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002853 mod = 1;
2854 else
2855 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002856
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002857 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002858 output->bytes = mod; /* bytes of offset needed */
2859 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002860 }
2861 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002862 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002863
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002864 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002865 return output->type;
2866
2867err:
2868 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002869}
2870
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002871static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002872{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002873 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002874 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002875
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002876 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002877
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002878 switch (ins->prefixes[PPS_ASIZE]) {
2879 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002880 valid &= 16;
2881 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002882 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002883 valid &= 32;
2884 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002885 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002886 valid &= 64;
2887 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002888 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002889 valid &= (addrbits == 32) ? 16 : 32;
2890 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002891 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002892 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002893 }
2894
2895 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002896 if (is_class(MEMORY, ins->oprs[j].type)) {
2897 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002898
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002899 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002900 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002901 i = 0;
2902 else
2903 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002904
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002906 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002907 b = 0;
2908 else
2909 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002910
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 if (ins->oprs[j].scale == 0)
2912 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002913
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002914 if (!i && !b) {
2915 int ds = ins->oprs[j].disp_size;
2916 if ((addrbits != 64 && ds > 8) ||
2917 (addrbits == 64 && ds == 16))
2918 valid &= ds;
2919 } else {
2920 if (!(REG16 & ~b))
2921 valid &= 16;
2922 if (!(REG32 & ~b))
2923 valid &= 32;
2924 if (!(REG64 & ~b))
2925 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002926
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002927 if (!(REG16 & ~i))
2928 valid &= 16;
2929 if (!(REG32 & ~i))
2930 valid &= 32;
2931 if (!(REG64 & ~i))
2932 valid &= 64;
2933 }
2934 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002935 }
2936
2937 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002938 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002939 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002940 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002941 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002942 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002943 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002944 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002945 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002946 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002947 }
2948
2949 defdisp = ins->addr_size == 16 ? 16 : 32;
2950
2951 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002952 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2953 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2954 /*
2955 * mem_offs sizes must match the address size; if not,
2956 * strip the MEM_OFFS bit and match only EA instructions
2957 */
2958 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2959 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002960 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002961}