blob: a6bb0ee559a10dfdd1c2753e81c48a2d17bf8ba2 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvina7ecf262018-02-06 14:43:07 -08003 * Copyright 1996-2018 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,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700201 MERR_BRNOTHERE,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700202 MERR_BRNUMMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700203 MERR_MASKNOTHERE,
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700204 MERR_DECONOTHERE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700205 MERR_BADCPU,
206 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800207 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700208 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700209 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800210 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700211 /*
212 * Matching success; the conditional ones first
213 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400214 MOK_JUMP, /* Matching OK but needs jmp_match() */
215 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700216};
217
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000218typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700219 enum ea_type type; /* what kind of EA is this? */
220 int sib_present; /* is a SIB byte necessary? */
221 int bytes; /* # of bytes of offset needed */
222 int size; /* lazy - this is sib+bytes+1 */
223 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700224 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000225} ea;
226
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400227#define GEN_SIB(scale, index, base) \
228 (((scale) << 6) | ((index) << 3) | ((base)))
229
230#define GEN_MODRM(mod, reg, rm) \
231 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
232
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800233static int64_t calcsize(int32_t, int64_t, int, insn *,
234 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700235static int emit_prefix(struct out_data *data, const int bits, insn *ins);
236static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700237static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400238 insn *instruction,
239 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700240static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700241static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000242static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700243static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000244static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700245static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700246static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000247
H. Peter Anvin8f622462017-04-02 19:02:29 -0700248static enum ea_type process_ea(operand *, ea *, int, int,
249 opflags_t, insn *, const char **);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700250
H. Peter Anvin164d2462017-02-20 02:39:56 -0800251static inline bool absolute_op(const struct operand *o)
252{
253 return o->segment == NO_SEG && o->wrt == NO_SEG &&
254 !(o->opflags & OPFLAG_RELATIVE);
255}
256
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400257static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000258{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700259 return ins->prefixes[pos] == prefix;
260}
261
262static void assert_no_prefix(insn * ins, enum prefix_pos pos)
263{
264 if (ins->prefixes[pos])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800265 nasm_error(ERR_NONFATAL, "invalid %s prefix",
266 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700267}
268
269static const char *size_name(int size)
270{
271 switch (size) {
272 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400273 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700274 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700276 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400277 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700278 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400279 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700280 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700282 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400283 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700284 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400285 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700286 case 64:
287 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700288 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400289 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000290 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700291}
292
H. Peter Anvin285222f2017-03-01 13:27:33 -0800293static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400294{
H. Peter Anvin285222f2017-03-01 13:27:33 -0800295 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400296 "%s data exceeds bounds", size_name(size));
297}
298
299static void warn_overflow_const(int64_t data, int size)
300{
301 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800302 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400303}
304
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800305static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
306{
307 bool err;
308
309 switch (sign) {
310 case OUT_WRAP:
311 err = overflow_general(data, size);
312 break;
313 case OUT_SIGNED:
314 err = overflow_signed(data, size);
315 break;
316 case OUT_UNSIGNED:
317 err = overflow_unsigned(data, size);
318 break;
319 default:
320 panic();
321 break;
322 }
323
324 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800325 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800326}
327
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000328/*
329 * This routine wrappers the real output format's output routine,
330 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800331 * generator at the same time, flatten unnecessary relocations,
332 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000333 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700334static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000335{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000336 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700337 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700338 union {
339 uint8_t b[8];
340 uint64_t q;
341 } xdata;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700342 size_t asize, amax;
343 uint64_t zeropad = 0;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800344 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800345 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000346
H. Peter Anvina77692b2016-09-20 14:04:33 -0700347 if (!data->size)
348 return; /* Nothing to do */
349
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700350 /*
351 * Convert addresses to RAWDATA if possible
352 * XXX: not all backends want this for global symbols!!!!
353 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700354 switch (data->type) {
355 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800356 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800357 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800358 goto address;
359
360 case OUT_RELADDR:
361 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800362 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800363 goto address;
364
365 address:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700366 nasm_assert(data->size <= 8);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700367 asize = data->size;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700368 amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
Chang S. Bae427d8e32018-05-02 08:07:52 -0700369 if ((ofmt->flags & OFMT_KEEP_ADDR) == 0 && data->tsegment == fixseg &&
370 data->twrt == NO_SEG) {
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800371 warn_overflow_out(addrval, asize, data->sign);
Martin Storsjö869087d2017-05-22 13:54:20 +0300372 xdata.q = cpu_to_le64(addrval);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700373 data->data = xdata.b;
374 data->type = OUT_RAWDATA;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700375 asize = amax = 0; /* No longer an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700376 }
377 break;
378
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700379 case OUT_SEGMENT:
380 nasm_assert(data->size <= 8);
381 asize = data->size;
382 amax = 2;
383 break;
384
H. Peter Anvina77692b2016-09-20 14:04:33 -0700385 default:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700386 asize = amax = 0; /* Not an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700387 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000388 }
389
Frank Kotlerabebb082003-09-06 04:45:37 +0000390 /*
391 * this call to src_get determines when we call the
392 * debug-format-specific "linenum" function
393 * it updates lineno and lnfname to the current values
394 * returning 0 if "same as last time", -2 if lnfname
395 * changed, and the amount by which lineno changed,
396 * if it did. thus, these variables must be static
397 */
398
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400399 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700400 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000401
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700402 if (asize > amax) {
403 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800404 nasm_error(ERR_NONFATAL,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700405 "%u-bit signed relocation unsupported by output format %s",
406 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800407 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800408 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700409 "%u-bit %s relocation zero-extended from %u bits",
410 (unsigned int)(asize << 3),
411 data->type == OUT_SEGMENT ? "segment" : "unsigned",
412 (unsigned int)(amax << 3));
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800413 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700414 zeropad = data->size - amax;
415 data->size = amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800416 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700417 lfmt->output(data);
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700418 ofmt->output(data);
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700419 data->offset += data->size;
420 data->insoffs += data->size;
421
422 if (zeropad) {
423 data->type = OUT_ZERODATA;
424 data->size = zeropad;
425 lfmt->output(data);
426 ofmt->output(data);
427 data->offset += zeropad;
428 data->insoffs += zeropad;
429 data->size += zeropad; /* Restore original size value */
430 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000431}
432
H. Peter Anvina77692b2016-09-20 14:04:33 -0700433static inline void out_rawdata(struct out_data *data, const void *rawdata,
434 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400435{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700436 data->type = OUT_RAWDATA;
437 data->data = rawdata;
438 data->size = size;
439 out(data);
440}
441
442static void out_rawbyte(struct out_data *data, uint8_t byte)
443{
444 data->type = OUT_RAWDATA;
445 data->data = &byte;
446 data->size = 1;
447 out(data);
448}
449
450static inline void out_reserve(struct out_data *data, uint64_t size)
451{
452 data->type = OUT_RESERVE;
453 data->size = size;
454 out(data);
455}
456
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700457static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700458{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700459 if (opx->opflags & OPFLAG_RELATIVE)
460 nasm_error(ERR_NONFATAL, "segment references cannot be relative");
461
462 data->type = OUT_SEGMENT;
463 data->sign = OUT_UNSIGNED;
464 data->size = 2;
465 data->toffset = opx->offset;
466 data->tsegment = ofmt->segbase(opx->segment | 1);
467 data->twrt = opx->wrt;
468 out(data);
469}
470
471static void out_imm(struct out_data *data, const struct operand *opx,
472 int size, enum out_sign sign)
473{
474 if (opx->segment != NO_SEG && (opx->segment & 1)) {
475 /*
476 * This is actually a segment reference, but eval() has
477 * already called ofmt->segbase() for us. Sigh.
478 */
479 if (size < 2)
480 nasm_error(ERR_NONFATAL, "segment reference must be 16 bits");
481
482 data->type = OUT_SEGMENT;
483 } else {
484 data->type = (opx->opflags & OPFLAG_RELATIVE)
485 ? OUT_RELADDR : OUT_ADDRESS;
486 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700487 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700488 data->toffset = opx->offset;
489 data->tsegment = opx->segment;
490 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800491 /*
492 * XXX: improve this if at some point in the future we can
493 * distinguish the subtrahend in expressions like [foo - bar]
494 * where bar is a symbol in the current segment. However, at the
495 * current point, if OPFLAG_RELATIVE is set that subtraction has
496 * already occurred.
497 */
498 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700499 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700500 out(data);
501}
502
H. Peter Anvin164d2462017-02-20 02:39:56 -0800503static void out_reladdr(struct out_data *data, const struct operand *opx,
504 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700505{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800506 if (opx->opflags & OPFLAG_RELATIVE)
507 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
508
H. Peter Anvina77692b2016-09-20 14:04:33 -0700509 data->type = OUT_RELADDR;
510 data->sign = OUT_SIGNED;
511 data->size = size;
512 data->toffset = opx->offset;
513 data->tsegment = opx->segment;
514 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800515 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700516 out(data);
517}
518
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700519static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800520 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000521{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800522 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800523 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000524 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800525 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000526
H. Peter Anvin755f5212012-02-25 11:41:34 -0800527 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700528 return false;
529 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400530 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700531 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400532 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700533
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800534 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100535
Victor van den Elzen154e5922009-02-25 17:32:00 +0100536 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100537 /* Be optimistic in pass 1 */
538 return true;
539
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700541 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000542
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700543 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800544 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
545
546 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
547 /* jmp short (opcode eb) cannot be used with bnd prefix. */
548 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800549 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800550 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800551 }
552
553 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000554}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000555
H. Peter Anvin04445362016-09-21 15:56:19 -0700556/* This is totally just a wild guess what is reasonable... */
557#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
558
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800559int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000560{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700561 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000562 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700563 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300564 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000565
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700566 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700567 data.offset = start;
568 data.segment = segment;
569 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700570 data.bits = bits;
571
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700572 wsize = db_bytes(instruction->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300573 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000574 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000575
H. Peter Anvineba20a72002-04-30 20:53:55 +0000576 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000577 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700578
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700579 list_for_each(e, instruction->eops) {
580 if (e->type == EOT_DB_NUMBER) {
581 if (wsize > 8) {
582 nasm_error(ERR_NONFATAL,
583 "integer supplied to a DT, DO, DY or DZ"
584 " instruction");
585 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700586 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700587 data.inslen = data.size = wsize;
588 data.toffset = e->offset;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700589 data.twrt = e->wrt;
590 data.relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700591 if (e->segment != NO_SEG && (e->segment & 1)) {
592 data.tsegment = e->segment;
593 data.type = OUT_SEGMENT;
594 data.sign = OUT_UNSIGNED;
595 } else {
596 data.tsegment = e->segment;
597 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
598 data.sign = OUT_WRAP;
599 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700600 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000601 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700602 } else if (e->type == EOT_DB_STRING ||
603 e->type == EOT_DB_STRING_FREE) {
604 int align = e->stringlen % wsize;
605 if (align)
606 align = wsize - align;
607
608 data.insoffs = 0;
609 data.inslen = e->stringlen + align;
610
611 out_rawdata(&data, e->stringval, e->stringlen);
612 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700613 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000614 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700615 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700616 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000617 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700618 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700619 off_t base = 0;
620 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700621 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700622 char *buf = NULL;
623 size_t blk = 0; /* Buffered I/O block size */
624 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000625
H. Peter Anvin94ead272017-09-27 15:22:23 -0700626 if (!t)
627 goto done;
628
H. Peter Anvind81a2352016-09-21 14:03:18 -0700629 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400630 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800631 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000632 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700633 goto done;
634 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000635
H. Peter Anvind81a2352016-09-21 14:03:18 -0700636 len = nasm_file_size(fp);
637
638 if (len == (off_t)-1) {
639 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700640 fname);
641 goto close_done;
642 }
643
H. Peter Anvina77692b2016-09-20 14:04:33 -0700644 if (instruction->eops->next) {
645 base = instruction->eops->next->offset;
646 if (base >= len) {
647 len = 0;
648 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000649 len -= base;
650 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700651 len > (off_t)instruction->eops->next->next->offset)
652 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000654 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700655
H. Peter Anvina77692b2016-09-20 14:04:33 -0700656 lfmt->set_offset(data.offset);
657 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000658
H. Peter Anvind81a2352016-09-21 14:03:18 -0700659 if (!len)
660 goto end_incbin;
661
662 /* Try to map file data */
663 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700664 if (!map) {
665 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
666 buf = nasm_malloc(blk);
667 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700668
669 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700670 /*
671 * Consider these irrelevant for INCBIN, since it is fully
672 * possible that these might be (way) bigger than an int
673 * can hold; there is, however, no reason to widen these
674 * types just for INCBIN. data.inslen == 0 signals to the
675 * backend that these fields are meaningless, if at all
676 * needed.
677 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700678 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700679 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700680
H. Peter Anvind81a2352016-09-21 14:03:18 -0700681 if (map) {
682 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700683 } else if ((off_t)m == len) {
684 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700685 } else {
686 off_t l = len;
687
688 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700689 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700690 "`incbin': unable to seek on file `%s'",
691 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700692 goto end_incbin;
693 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700694 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700695 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700696 if (!m || feof(fp)) {
697 /*
698 * This shouldn't happen unless the file
699 * actually changes while we are reading
700 * it.
701 */
702 nasm_error(ERR_NONFATAL,
703 "`incbin': unexpected EOF while"
704 " reading file `%s'", fname);
705 goto end_incbin;
706 }
707 out_rawdata(&data, buf, m);
708 l -= m;
709 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700710 }
711 }
712 end_incbin:
713 lfmt->downlevel(LIST_INCBIN);
714 if (instruction->times > 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700715 lfmt->uplevel(LIST_TIMES);
716 lfmt->downlevel(LIST_TIMES);
717 }
718 if (ferror(fp)) {
719 nasm_error(ERR_NONFATAL,
720 "`incbin': error while"
721 " reading file `%s'", fname);
722 }
723 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700724 if (buf)
725 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700726 if (map)
727 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700728 fclose(fp);
729 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700730 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700731 ;
732 } else {
733 /* "Real" instruction */
734
735 /* Check to see if we need an address-size prefix */
736 add_asp(instruction, bits);
737
738 m = find_match(&temp, instruction, data.segment, data.offset, bits);
739
740 if (m == MOK_GOOD) {
741 /* Matches! */
742 int64_t insn_size = calcsize(data.segment, data.offset,
743 bits, instruction, temp);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700744 nasm_assert(insn_size >= 0);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700745
746 data.itemp = temp;
747 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700748 data.insoffs = 0;
749 data.inslen = insn_size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700750
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700751 gencode(&data, instruction);
752 nasm_assert(data.insoffs == insn_size);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700753 } else {
754 /* No match */
755 switch (m) {
756 case MERR_OPSIZEMISSING:
757 nasm_error(ERR_NONFATAL, "operation size not specified");
758 break;
759 case MERR_OPSIZEMISMATCH:
760 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
761 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700762 case MERR_BRNOTHERE:
763 nasm_error(ERR_NONFATAL,
764 "broadcast not permitted on this operand");
765 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700766 case MERR_BRNUMMISMATCH:
767 nasm_error(ERR_NONFATAL,
768 "mismatch in the number of broadcasting elements");
769 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700770 case MERR_MASKNOTHERE:
771 nasm_error(ERR_NONFATAL,
772 "mask not permitted on this operand");
773 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700774 case MERR_DECONOTHERE:
775 nasm_error(ERR_NONFATAL, "unsupported mode decorator for instruction");
776 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700777 case MERR_BADCPU:
778 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
779 break;
780 case MERR_BADMODE:
781 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
782 bits);
783 break;
784 case MERR_ENCMISMATCH:
785 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
786 break;
787 case MERR_BADBND:
788 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
789 break;
790 case MERR_BADREPNE:
791 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
792 (has_prefix(instruction, PPS_REP, P_REPNE) ?
793 "repne" : "repnz"));
794 break;
795 default:
796 nasm_error(ERR_NONFATAL,
797 "invalid combination of opcode and operands");
798 break;
799 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700800
801 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400802 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000803 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700804 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000805}
806
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800807int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000808{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000809 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700810 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000811
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400812 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000813 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000814
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700815 if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000816 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300817 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000818
H. Peter Anvine2c80182005-01-15 22:15:51 +0000819 isize = 0;
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700820 wsize = db_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700821 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000822
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400823 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000824 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000825
H. Peter Anvine2c80182005-01-15 22:15:51 +0000826 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400827 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000828 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400829 warn_overflow_const(e->offset, wsize);
830 } else if (e->type == EOT_DB_STRING ||
831 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000832 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000833
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 align = (-osize) % wsize;
835 if (align < 0)
836 align += wsize;
837 isize += osize + align;
838 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700839 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000840 }
841
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400843 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700844 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000845
H. Peter Anvind81a2352016-09-21 14:03:18 -0700846 len = nasm_file_size_by_path(fname);
847 if (len == (off_t)-1) {
848 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
849 fname);
850 return 0;
851 }
852
853 if (instruction->eops->next) {
854 if (len <= (off_t)instruction->eops->next->offset) {
855 len = 0;
856 } else {
857 len -= instruction->eops->next->offset;
858 if (instruction->eops->next->next &&
859 len > (off_t)instruction->eops->next->next->offset) {
860 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000861 }
862 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000863 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700864
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700865 len *= instruction->times;
866 instruction->times = 1; /* Tell the upper layer to not iterate */
867
H. Peter Anvind81a2352016-09-21 14:03:18 -0700868 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000869 }
870
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700871 /* Check to see if we need an address-size prefix */
872 add_asp(instruction, bits);
873
H. Peter Anvin23595f52009-07-25 17:44:25 -0700874 m = find_match(&temp, instruction, segment, offset, bits);
875 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400876 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700877 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700878 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400879 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000880 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000881}
882
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800883static void bad_hle_warn(const insn * ins, uint8_t hleok)
884{
885 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800886 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800887 static const enum whatwarn warn[2][4] =
888 {
889 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
890 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
891 };
892 unsigned int n;
893
894 n = (unsigned int)rep_pfx - P_XACQUIRE;
895 if (n > 1)
896 return; /* Not XACQUIRE/XRELEASE */
897
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800898 ww = warn[n][hleok];
899 if (!is_class(MEMORY, ins->oprs[0].type))
900 ww = w_inval; /* HLE requires operand 0 to be memory */
901
902 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800903 case w_none:
904 break;
905
906 case w_lock:
907 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800908 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800909 "%s with this instruction requires lock",
910 prefix_name(rep_pfx));
911 }
912 break;
913
914 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800915 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800916 "%s invalid with this instruction",
917 prefix_name(rep_pfx));
918 break;
919 }
920}
921
H. Peter Anvin507ae032008-10-09 15:37:10 -0700922/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400923#define case3(x) case (x): case (x)+1: case (x)+2
924#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700925
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800926static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800927 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000928{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800929 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800930 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000931 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000932 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700933 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700934 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700935 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700936 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800937 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800938 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700939 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -0700940 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000941
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700942 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700943 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700944 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700945
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700946 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400947 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700948
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 (void)segment; /* Don't warn that this parameter is unused */
950 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000951
H. Peter Anvin839eca22007-10-29 23:12:47 -0700952 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400953 c = *codes++;
954 op1 = (c & 3) + ((opex & 1) << 2);
955 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
956 opx = &ins->oprs[op1];
957 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700958
H. Peter Anvin839eca22007-10-29 23:12:47 -0700959 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400960 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000961 codes += c, length += c;
962 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700963
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400964 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400965 opex = c;
966 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700967
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400968 case4(010):
969 ins->rex |=
970 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000971 codes++, length++;
972 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700973
Jin Kyu Song164d6072013-10-15 19:10:13 -0700974 case4(014):
975 /* this is an index reg of MIB operand */
976 mib_index = opx->basereg;
977 break;
978
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400979 case4(020):
980 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000981 length++;
982 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700983
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400984 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 length += 2;
986 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700987
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400988 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700989 if (opx->type & (BITS16 | BITS32 | BITS64))
990 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 else
992 length += (bits == 16) ? 2 : 4;
993 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700994
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400995 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 length += 4;
997 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700998
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400999 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001000 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001002
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001003 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 length++;
1005 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001006
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001007 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001008 length += 8; /* MOV reg64/imm */
1009 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001010
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001011 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 length += 2;
1013 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001014
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001015 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001016 if (opx->type & (BITS16 | BITS32 | BITS64))
1017 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001018 else
1019 length += (bits == 16) ? 2 : 4;
1020 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001021
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001022 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 length += 4;
1024 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001025
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001026 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001027 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001029
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001030 case 0172:
1031 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001032 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001033 length++;
1034 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001035
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001036 case4(0174):
1037 length++;
1038 break;
1039
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001040 case4(0240):
1041 ins->rex |= REX_EV;
1042 ins->vexreg = regval(opx);
1043 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1044 ins->vex_cm = *codes++;
1045 ins->vex_wlp = *codes++;
1046 ins->evex_tuple = (*codes++ - 0300);
1047 break;
1048
1049 case 0250:
1050 ins->rex |= REX_EV;
1051 ins->vexreg = 0;
1052 ins->vex_cm = *codes++;
1053 ins->vex_wlp = *codes++;
1054 ins->evex_tuple = (*codes++ - 0300);
1055 break;
1056
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001057 case4(0254):
1058 length += 4;
1059 break;
1060
1061 case4(0260):
1062 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001063 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001064 ins->vex_cm = *codes++;
1065 ins->vex_wlp = *codes++;
1066 break;
1067
1068 case 0270:
1069 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001070 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001071 ins->vex_cm = *codes++;
1072 ins->vex_wlp = *codes++;
1073 break;
1074
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001075 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001076 hleok = c & 3;
1077 break;
1078
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001079 case4(0274):
1080 length++;
1081 break;
1082
1083 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001084 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001085
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001087 if (bits == 64)
1088 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001089 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001091
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001093 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001095
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001097 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001098
Keith Kaniosb7a89542007-04-12 02:40:54 +00001099 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001100 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1101 has_prefix(ins, PPS_ASIZE, P_A32))
1102 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001103 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001104
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001105 case4(0314):
1106 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001107
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001109 {
1110 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1111 if (pfx == P_O16)
1112 break;
1113 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001114 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001115 else
1116 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001117 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001118 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001119
H. Peter Anvine2c80182005-01-15 22:15:51 +00001120 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001121 {
1122 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1123 if (pfx == P_O32)
1124 break;
1125 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001126 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001127 else
1128 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001129 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001130 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001131
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 case 0322:
1133 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001134
Keith Kaniosb7a89542007-04-12 02:40:54 +00001135 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001136 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001137 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001138
Keith Kaniosb7a89542007-04-12 02:40:54 +00001139 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001140 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001141 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001142
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001143 case 0325:
1144 ins->rex |= REX_NH;
1145 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001146
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001147 case 0326:
1148 break;
1149
H. Peter Anvine2c80182005-01-15 22:15:51 +00001150 case 0330:
1151 codes++, length++;
1152 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001153
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001156
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001157 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 case 0333:
1159 length++;
1160 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001161
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001162 case 0334:
1163 ins->rex |= REX_L;
1164 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001165
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001166 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001167 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001168
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001169 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001170 if (!ins->prefixes[PPS_REP])
1171 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001172 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001173
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001174 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001175 if (!ins->prefixes[PPS_REP])
1176 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001177 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001178
H. Peter Anvine2c80182005-01-15 22:15:51 +00001179 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001180 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001181 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001182 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001183 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1184 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001185 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001187 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001189
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001190 case 0341:
1191 if (!ins->prefixes[PPS_WAIT])
1192 ins->prefixes[PPS_WAIT] = P_WAIT;
1193 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001194
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001195 case 0360:
1196 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001197
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001198 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001199 length++;
1200 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001201
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001202 case 0364:
1203 case 0365:
1204 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001205
Keith Kanios48af1772007-08-17 07:37:52 +00001206 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001207 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001208 length++;
1209 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001210
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001211 case 0370:
1212 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001213 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001214
H. Peter Anvine2c80182005-01-15 22:15:51 +00001215 case 0373:
1216 length++;
1217 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001218
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001219 case 0374:
1220 eat = EA_XMMVSIB;
1221 break;
1222
1223 case 0375:
1224 eat = EA_YMMVSIB;
1225 break;
1226
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001227 case 0376:
1228 eat = EA_ZMMVSIB;
1229 break;
1230
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001231 case4(0100):
1232 case4(0110):
1233 case4(0120):
1234 case4(0130):
1235 case4(0200):
1236 case4(0204):
1237 case4(0210):
1238 case4(0214):
1239 case4(0220):
1240 case4(0224):
1241 case4(0230):
1242 case4(0234):
1243 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001244 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001245 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001246 opflags_t rflags;
1247 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001248 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001249
Keith Kaniosb7a89542007-04-12 02:40:54 +00001250 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001251
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001252 if (c <= 0177) {
1253 /* pick rfield from operand b (opx) */
1254 rflags = regflag(opx);
1255 rfield = nasm_regvals[opx->basereg];
1256 } else {
1257 rflags = 0;
1258 rfield = c & 7;
1259 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001260
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001261 /* EVEX.b1 : evex_brerop contains the operand position */
1262 op_er_sae = (ins->evex_brerop >= 0 ?
1263 &ins->oprs[ins->evex_brerop] : NULL);
1264
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001265 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1266 /* set EVEX.b */
1267 ins->evex_p[2] |= EVEX_P2B;
1268 if (op_er_sae->decoflags & ER) {
1269 /* set EVEX.RC (rounding control) */
1270 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1271 & EVEX_P2RC;
1272 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001273 } else {
1274 /* set EVEX.L'L (vector length) */
1275 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001276 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001277 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001278 /* set EVEX.b */
1279 ins->evex_p[2] |= EVEX_P2B;
1280 }
1281 }
1282
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001283 if (itemp_has(temp, IF_MIB)) {
1284 opy->eaflags |= EAF_MIB;
1285 /*
1286 * if a separate form of MIB (ICC style) is used,
1287 * the index reg info is merged into mem operand
1288 */
1289 if (mib_index != R_none) {
1290 opy->indexreg = mib_index;
1291 opy->scale = 1;
1292 opy->hintbase = mib_index;
1293 opy->hinttype = EAH_NOTBASE;
1294 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001295 }
1296
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001297 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001298 rfield, rflags, ins, &errmsg) != eat) {
1299 nasm_error(ERR_NONFATAL, "%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001301 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001302 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001303 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001304 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001305 }
1306 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001307
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001308 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001309 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001310 ": instruction code \\%o (0x%02X) given", c, c);
1311 break;
1312 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001313 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001314
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001315 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001316
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001317 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001318 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001319 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001320 return -1;
1321 }
1322 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001323 }
1324
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001325 switch (ins->prefixes[PPS_VEX]) {
1326 case P_EVEX:
1327 if (!(ins->rex & REX_EV))
1328 return -1;
1329 break;
1330 case P_VEX3:
1331 case P_VEX2:
1332 if (!(ins->rex & REX_V))
1333 return -1;
1334 break;
1335 default:
1336 break;
1337 }
1338
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001339 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001340 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001341
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001342 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001343 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001344 return -1;
1345 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001346 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001347 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001348 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001349 ins->rex &= ~REX_W;
1350 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001351 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001352 ins->rex |= REX_W;
1353 bad32 &= ~REX_W;
1354 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001355 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001356 /* Follow REX_W */
1357 break;
1358 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001359
H. Peter Anvinfc561202011-07-07 16:58:22 -07001360 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001361 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001362 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001363 } else if (!(ins->rex & REX_EV) &&
1364 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001365 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001366 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001367 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001368 if (ins->rex & REX_EV)
1369 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001370 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1371 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001372 length += 3;
1373 else
1374 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001375 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001376 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001377 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001378 return -1;
1379 } else if (bits == 64) {
1380 length++;
1381 } else if ((ins->rex & REX_L) &&
1382 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001383 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001384 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001385 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001386 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001387 length++;
1388 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001389 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001390 return -1;
1391 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001392 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001393
1394 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001395 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001396 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001397 "instruction is not lockable");
1398 }
1399
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001400 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001401
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001402 /*
1403 * when BND prefix is set by DEFAULT directive,
1404 * BND prefix is added to every appropriate instruction line
1405 * unless it is overridden by NOBND prefix.
1406 */
1407 if (globalbnd &&
1408 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1409 ins->prefixes[PPS_REP] = P_BND;
1410
H. Peter Anvina77692b2016-09-20 14:04:33 -07001411 /*
1412 * Add length of legacy prefixes
1413 */
1414 length += emit_prefix(NULL, bits, ins);
1415
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001416 return length;
1417}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001418
H. Peter Anvina77692b2016-09-20 14:04:33 -07001419static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001420{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001421 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001422 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001423 !(ins->rex & (REX_V | REX_EV)) &&
1424 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001425 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1426 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001427 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001428 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001429 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001430}
1431
H. Peter Anvina77692b2016-09-20 14:04:33 -07001432static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1433{
1434 int bytes = 0;
1435 int j;
1436
1437 for (j = 0; j < MAXPREFIX; j++) {
1438 uint8_t c = 0;
1439 switch (ins->prefixes[j]) {
1440 case P_WAIT:
1441 c = 0x9B;
1442 break;
1443 case P_LOCK:
1444 c = 0xF0;
1445 break;
1446 case P_REPNE:
1447 case P_REPNZ:
1448 case P_XACQUIRE:
1449 case P_BND:
1450 c = 0xF2;
1451 break;
1452 case P_REPE:
1453 case P_REPZ:
1454 case P_REP:
1455 case P_XRELEASE:
1456 c = 0xF3;
1457 break;
1458 case R_CS:
1459 if (bits == 64) {
1460 nasm_error(ERR_WARNING | ERR_PASS2,
1461 "cs segment base generated, but will be ignored in 64-bit mode");
1462 }
1463 c = 0x2E;
1464 break;
1465 case R_DS:
1466 if (bits == 64) {
1467 nasm_error(ERR_WARNING | ERR_PASS2,
1468 "ds segment base generated, but will be ignored in 64-bit mode");
1469 }
1470 c = 0x3E;
1471 break;
1472 case R_ES:
1473 if (bits == 64) {
1474 nasm_error(ERR_WARNING | ERR_PASS2,
1475 "es segment base generated, but will be ignored in 64-bit mode");
1476 }
1477 c = 0x26;
1478 break;
1479 case R_FS:
1480 c = 0x64;
1481 break;
1482 case R_GS:
1483 c = 0x65;
1484 break;
1485 case R_SS:
1486 if (bits == 64) {
1487 nasm_error(ERR_WARNING | ERR_PASS2,
1488 "ss segment base generated, but will be ignored in 64-bit mode");
1489 }
1490 c = 0x36;
1491 break;
1492 case R_SEGR6:
1493 case R_SEGR7:
1494 nasm_error(ERR_NONFATAL,
1495 "segr6 and segr7 cannot be used as prefixes");
1496 break;
1497 case P_A16:
1498 if (bits == 64) {
1499 nasm_error(ERR_NONFATAL,
1500 "16-bit addressing is not supported "
1501 "in 64-bit mode");
1502 } else if (bits != 16)
1503 c = 0x67;
1504 break;
1505 case P_A32:
1506 if (bits != 32)
1507 c = 0x67;
1508 break;
1509 case P_A64:
1510 if (bits != 64) {
1511 nasm_error(ERR_NONFATAL,
1512 "64-bit addressing is only supported "
1513 "in 64-bit mode");
1514 }
1515 break;
1516 case P_ASP:
1517 c = 0x67;
1518 break;
1519 case P_O16:
1520 if (bits != 16)
1521 c = 0x66;
1522 break;
1523 case P_O32:
1524 if (bits == 16)
1525 c = 0x66;
1526 break;
1527 case P_O64:
1528 /* REX.W */
1529 break;
1530 case P_OSP:
1531 c = 0x66;
1532 break;
1533 case P_EVEX:
1534 case P_VEX3:
1535 case P_VEX2:
1536 case P_NOBND:
1537 case P_none:
1538 break;
1539 default:
1540 nasm_panic(0, "invalid instruction prefix");
1541 }
1542 if (c) {
1543 if (data)
1544 out_rawbyte(data, c);
1545 bytes++;
1546 }
1547 }
1548 return bytes;
1549}
1550
1551static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001552{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001553 uint8_t c;
1554 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001555 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001556 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001557 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001558 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001559 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001560 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001561 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001562 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001563 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001564
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001565 ins->rex_done = false;
1566
H. Peter Anvina77692b2016-09-20 14:04:33 -07001567 emit_prefix(data, bits, ins);
1568
H. Peter Anvin839eca22007-10-29 23:12:47 -07001569 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001570 c = *codes++;
1571 op1 = (c & 3) + ((opex & 1) << 2);
1572 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1573 opx = &ins->oprs[op1];
1574 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001575
H. Peter Anvina77692b2016-09-20 14:04:33 -07001576
H. Peter Anvin839eca22007-10-29 23:12:47 -07001577 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001578 case 01:
1579 case 02:
1580 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001581 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001582 emit_rex(data, ins);
1583 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001585 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001586
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001587 case 05:
1588 case 06:
1589 case 07:
1590 opex = c;
1591 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001592
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001594 emit_rex(data, ins);
1595 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001597
Jin Kyu Song164d6072013-10-15 19:10:13 -07001598 case4(014):
1599 break;
1600
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001601 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001602 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001603 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001604
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001605 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001606 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001607 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001608
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001609 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001610 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001612
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001613 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001614 if (opx->type & (BITS16 | BITS32))
1615 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001616 else
1617 size = (bits == 16) ? 2 : 4;
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(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001622 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001623 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001624
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001625 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001626 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001627 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001629
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001630 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001631 if (opx->segment == data->segment) {
1632 int64_t delta = opx->offset - data->offset
1633 - (data->inslen - data->insoffs);
1634 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001635 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001636 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001637 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001638 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001639
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001640 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001641 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001642 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001643
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001644 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001645 out_reladdr(data, opx, 2);
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(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001649 if (opx->type & (BITS16 | BITS32 | BITS64))
1650 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001651 else
1652 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001653
1654 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001655 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001656
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001657 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001658 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001660
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001661 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001662 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001663 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001664 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001665 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001666 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001667
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001668 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001669 {
1670 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1671 const struct operand *opy;
1672
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001673 c = *codes++;
1674 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001675 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001676 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001677 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001678 "non-absolute expression not permitted as argument %d",
1679 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001680 } else if (opy->offset & ~mask) {
1681 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1682 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001683 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001684 c = opy->offset & mask;
1685 goto emit_is4;
1686 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001687
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001688 case 0173:
1689 c = *codes++;
1690 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001691 c &= 15;
1692 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001693
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001694 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001695 c = 0;
1696 emit_is4:
1697 r = nasm_regvals[opx->basereg];
1698 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001699 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001700
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001701 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001702 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001703 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001704 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001705 "signed dword immediate exceeds bounds");
1706 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001707 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001708 break;
1709
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001710 case4(0240):
1711 case 0250:
1712 codes += 3;
1713 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1714 EVEX_P2Z | EVEX_P2AAA, 2);
1715 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1716 bytes[0] = 0x62;
1717 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001718 bytes[1] = ((((ins->rex & 7) << 5) |
1719 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001720 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001721 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1722 ((~ins->vexreg & 15) << 3) |
1723 (1 << 2) | (ins->vex_wlp & 3);
1724 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001725 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001726 break;
1727
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001728 case4(0260):
1729 case 0270:
1730 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001731 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1732 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001733 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1734 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1735 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001736 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001737 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001738 } else {
1739 bytes[0] = 0xc5;
1740 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001741 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001742 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001743 }
1744 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001745
H. Peter Anvine014f352012-02-25 22:35:19 -08001746 case 0271:
1747 case 0272:
1748 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001749 break;
1750
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001751 case4(0274):
1752 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001753 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001754 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001755
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001756 if (absolute_op(opx)) {
1757 if (ins->rex & REX_W)
1758 s = 64;
1759 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1760 s = 16;
1761 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1762 s = 32;
1763 else
1764 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001765
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001766 um = (uint64_t)2 << (s-1);
1767 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001768
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001769 if (uv > 127 && uv < (uint64_t)-128 &&
1770 (uv < um-128 || uv > um-1)) {
1771 /* If this wasn't explicitly byte-sized, warn as though we
1772 * had fallen through to the imm16/32/64 case.
1773 */
1774 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1775 "%s value exceeds bounds",
1776 (opx->type & BITS8) ? "signed byte" :
1777 s == 16 ? "word" :
1778 s == 32 ? "dword" :
1779 "signed dword");
1780 }
1781
1782 /* Output as a raw byte to avoid byte overflow check */
1783 out_rawbyte(data, (uint8_t)uv);
1784 } else {
1785 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001786 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001787 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001788 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001789
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001790 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001792
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001794 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1795 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001797
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001799 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1800 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001802
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 case 0312:
1804 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001805
Keith Kaniosb7a89542007-04-12 02:40:54 +00001806 case 0313:
1807 ins->rex = 0;
1808 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001809
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001810 case4(0314):
1811 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001812
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001815 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001816
H. Peter Anvine2c80182005-01-15 22:15:51 +00001817 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001818 case 0323:
1819 break;
1820
Keith Kaniosb7a89542007-04-12 02:40:54 +00001821 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001822 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001824
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001825 case 0325:
1826 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001827
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001828 case 0326:
1829 break;
1830
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001832 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001833 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001834
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001837
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001838 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001840 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001842
Keith Kanios48af1772007-08-17 07:37:52 +00001843 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001844 if (ins->rex & REX_R)
1845 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001846 ins->rex &= ~(REX_L|REX_R);
1847 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001848
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001849 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001850 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001851
H. Peter Anvin962e3052008-08-28 17:47:16 -07001852 case 0336:
1853 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001854 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001855
H. Peter Anvine2c80182005-01-15 22:15:51 +00001856 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001857 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001858 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001859
1860 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001861 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001862
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001863 case 0341:
1864 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001865
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001866 case 0360:
1867 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001868
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001869 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001870 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001871 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001872
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001873 case 0364:
1874 case 0365:
1875 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001876
Keith Kanios48af1772007-08-17 07:37:52 +00001877 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001878 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001879 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001880 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001881
Jin Kyu Song03041092013-10-15 19:38:51 -07001882 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001883 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001884
H. Peter Anvine2c80182005-01-15 22:15:51 +00001885 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001886 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001887 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001888
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001889 case 0374:
1890 eat = EA_XMMVSIB;
1891 break;
1892
1893 case 0375:
1894 eat = EA_YMMVSIB;
1895 break;
1896
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001897 case 0376:
1898 eat = EA_ZMMVSIB;
1899 break;
1900
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001901 case4(0100):
1902 case4(0110):
1903 case4(0120):
1904 case4(0130):
1905 case4(0200):
1906 case4(0204):
1907 case4(0210):
1908 case4(0214):
1909 case4(0220):
1910 case4(0224):
1911 case4(0230):
1912 case4(0234):
1913 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914 ea ea_data;
1915 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001916 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001917 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001918 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001919
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001920 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001921 /* pick rfield from operand b (opx) */
1922 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001923 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001924 } else {
1925 /* rfield is constant */
1926 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001928 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001929
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001930 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001931 rfield, rflags, ins, &errmsg) != eat)
1932 nasm_error(ERR_NONFATAL, "%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07001933
H. Peter Anvine2c80182005-01-15 22:15:51 +00001934 p = bytes;
1935 *p++ = ea_data.modrm;
1936 if (ea_data.sib_present)
1937 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001938 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001939
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001940 /*
1941 * Make sure the address gets the right offset in case
1942 * the line breaks in the .lst file (BR 1197827)
1943 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001944
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001945 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001946 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001947 if (ea_data.disp8) {
1948 out_rawbyte(data, ea_data.disp8);
1949 } else if (ea_data.rip) {
1950 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001951 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001952 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001953
H. Peter Anvina77692b2016-09-20 14:04:33 -07001954 if (overflow_general(opy->offset, asize) ||
1955 signed_bits(opy->offset, ins->addr_size) !=
1956 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001957 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001958
H. Peter Anvina77692b2016-09-20 14:04:33 -07001959 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001960 (asize > ea_data.bytes)
1961 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001962 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001963 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001964 }
1965 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001966
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001967 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001968 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001969 ": instruction code \\%o (0x%02X) given", c, c);
1970 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001972 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001973}
1974
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001975static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001976{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001977 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001978 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001979 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001980}
1981
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001982static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001983{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001984 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001985 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001986 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001987}
1988
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001989static int op_rexflags(const operand * o, int mask)
1990{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001991 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001992 int val;
1993
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001994 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001995 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001996
H. Peter Anvina4835d42008-05-20 14:21:29 -07001997 flags = nasm_reg_flags[o->basereg];
1998 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001999
2000 return rexflags(val, flags, mask);
2001}
2002
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002003static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002004{
2005 int rex = 0;
2006
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002007 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002008 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002009 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002010 rex |= REX_W;
2011 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2012 rex |= REX_H;
2013 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2014 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002015
2016 return rex & mask;
2017}
2018
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002019static int evexflags(int val, decoflags_t deco,
2020 int mask, uint8_t byte)
2021{
2022 int evex = 0;
2023
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002024 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002025 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002026 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002027 evex |= (EVEX_P0RP | EVEX_P0X);
2028 break;
2029 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002030 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002031 evex |= EVEX_P2VP;
2032 if (deco & Z)
2033 evex |= EVEX_P2Z;
2034 if (deco & OPMASK_MASK)
2035 evex |= deco & EVEX_P2AAA;
2036 break;
2037 }
2038 return evex & mask;
2039}
2040
2041static int op_evexflags(const operand * o, int mask, uint8_t byte)
2042{
2043 int val;
2044
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002045 val = nasm_regvals[o->basereg];
2046
2047 return evexflags(val, o->decoflags, mask, byte);
2048}
2049
H. Peter Anvin23595f52009-07-25 17:44:25 -07002050static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002051 insn *instruction,
2052 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002053{
2054 const struct itemplate *temp;
2055 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002056 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002057 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002058 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002059 int i;
2060
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002061 /* broadcasting uses a different data element size */
2062 for (i = 0; i < instruction->operands; i++)
2063 if (i == broadcast)
2064 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2065 else
2066 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002067
2068 merr = MERR_INVALOP;
2069
2070 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002071 temp->opcode != I_none; temp++) {
2072 m = matches(temp, instruction, bits);
2073 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002074 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002075 m = MOK_GOOD;
2076 else
2077 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002078 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002079 /*
2080 * Missing operand size and a candidate for fuzzy matching...
2081 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002082 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002083 if (i == broadcast)
2084 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2085 else
2086 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002087 opsizemissing = true;
2088 }
2089 if (m > merr)
2090 merr = m;
2091 if (merr == MOK_GOOD)
2092 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002093 }
2094
2095 /* No match, but see if we can get a fuzzy operand size match... */
2096 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002097 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002098
2099 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002100 /*
2101 * We ignore extrinsic operand sizes on registers, so we should
2102 * never try to fuzzy-match on them. This also resolves the case
2103 * when we have e.g. "xmmrm128" in two different positions.
2104 */
2105 if (is_class(REGISTER, instruction->oprs[i].type))
2106 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002107
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002108 /* This tests if xsizeflags[i] has more than one bit set */
2109 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2110 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002111
Jin Kyu Song7903c072013-10-30 03:00:12 -07002112 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002113 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002114 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2115 BITS32 : BITS64);
2116 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002117 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002118 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002119 }
2120
2121 /* Try matching again... */
2122 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002123 temp->opcode != I_none; temp++) {
2124 m = matches(temp, instruction, bits);
2125 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002126 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002127 m = MOK_GOOD;
2128 else
2129 m = MERR_INVALOP;
2130 }
2131 if (m > merr)
2132 merr = m;
2133 if (merr == MOK_GOOD)
2134 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002135 }
2136
H. Peter Anvina81655b2009-07-25 18:15:28 -07002137done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002138 *tempp = temp;
2139 return merr;
2140}
2141
Mark Charneydcaef4b2014-10-09 13:45:17 -04002142static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2143{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002144 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002145 uint8_t brcast_num;
2146
Mark Charneydcaef4b2014-10-09 13:45:17 -04002147 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002148 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002149 "size of broadcasting element is greater than 64 bits");
2150
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002151 /*
2152 * The shift term is to take care of the extra BITS80 inserted
2153 * between BITS64 and BITS128.
2154 */
2155 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2156 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002157
2158 return brcast_num;
2159}
2160
H. Peter Anvin65289e82009-07-25 17:25:11 -07002161static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002162 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002163{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002164 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002165 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002166 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002167
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002168 /*
2169 * Check the opcode
2170 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002171 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002172 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002173
2174 /*
2175 * Count the operands
2176 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002177 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002178 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002179
2180 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002181 * Is it legal?
2182 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002183 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002184 return MERR_INVALOP;
2185
2186 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002187 * {evex} available?
2188 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002189 switch (instruction->prefixes[PPS_VEX]) {
2190 case P_EVEX:
2191 if (!itemp_has(itemp, IF_EVEX))
2192 return MERR_ENCMISMATCH;
2193 break;
2194 case P_VEX3:
2195 case P_VEX2:
2196 if (!itemp_has(itemp, IF_VEX))
2197 return MERR_ENCMISMATCH;
2198 break;
2199 default:
2200 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002201 }
2202
2203 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002204 * Check that no spurious colons or TOs are present
2205 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002206 for (i = 0; i < itemp->operands; i++)
2207 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002208 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002209
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002210 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002211 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002212 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002213 switch (itemp_smask(itemp)) {
2214 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002215 asize = BITS8;
2216 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002217 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002218 asize = BITS16;
2219 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002220 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002221 asize = BITS32;
2222 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002223 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002224 asize = BITS64;
2225 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002226 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002227 asize = BITS128;
2228 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002229 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002230 asize = BITS256;
2231 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002232 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002233 asize = BITS512;
2234 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002235 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002236 switch (bits) {
2237 case 16:
2238 asize = BITS16;
2239 break;
2240 case 32:
2241 asize = BITS32;
2242 break;
2243 case 64:
2244 asize = BITS64;
2245 break;
2246 default:
2247 asize = 0;
2248 break;
2249 }
2250 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002251 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002252 asize = 0;
2253 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002254 }
2255
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002256 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002257 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002258 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002259 memset(size, 0, sizeof size);
2260 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002261 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002262 /* S- flags apply to all operands */
2263 for (i = 0; i < MAX_OPERANDS; i++)
2264 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002265 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002266
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002267 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002268 * Check that the operand flags all match up,
2269 * it's a bit tricky so lets be verbose:
2270 *
2271 * 1) Find out the size of operand. If instruction
2272 * doesn't have one specified -- we're trying to
2273 * guess it either from template (IF_S* flag) or
2274 * from code bits.
2275 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002276 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002277 * template has an operand size specified AND this size differ
2278 * from which instruction has (perhaps we got it from code bits)
2279 * we are:
2280 * a) Check that only size of instruction and operand is differ
2281 * other characteristics do match
2282 * b) Perhaps it's a register specified in instruction so
2283 * for such a case we just mark that operand as "size
2284 * missing" and this will turn on fuzzy operand size
2285 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002286 */
2287 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002288 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002289 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002290 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002291 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002292 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002293 opflags_t template_opsize, insn_opsize;
2294
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002295 if (!(type & SIZE_MASK))
2296 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002297
Jin Kyu Song7903c072013-10-30 03:00:12 -07002298 insn_opsize = type & SIZE_MASK;
2299 if (!is_broadcast) {
2300 template_opsize = itemp->opd[i] & SIZE_MASK;
2301 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002302 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2303
2304 if (~ideco & BRDCAST_MASK)
2305 return MERR_BRNOTHERE;
2306
Jin Kyu Song7903c072013-10-30 03:00:12 -07002307 /*
2308 * when broadcasting, the element size depends on
2309 * the instruction type. decorator flag should match.
2310 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002311 if (deco_brsize) {
2312 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002313 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002314 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002315 } else {
2316 template_opsize = 0;
2317 }
2318 }
2319
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002320 if (~ideco & deco & OPMASK_MASK)
2321 return MERR_MASKNOTHERE;
2322
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002323 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2324 return MERR_DECONOTHERE;
2325
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002326 if (itemp->opd[i] & ~type & ~SIZE_MASK) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002327 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002328 } else if (template_opsize) {
2329 if (template_opsize != insn_opsize) {
2330 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002331 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002332 } else if (!is_class(REGISTER, type)) {
2333 /*
2334 * Note: we don't honor extrinsic operand sizes for registers,
2335 * so "missing operand size" for a register should be
2336 * considered a wildcard match rather than an error.
2337 */
2338 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002339 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002340 } else if (is_broadcast &&
2341 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002342 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002343 /*
2344 * broadcasting opsize matches but the number of repeated memory
2345 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002346 * if 64b double precision float is broadcasted to ymm (256b),
2347 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002348 */
2349 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002350 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002351 }
2352 }
2353
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002354 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002355 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002356
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002357 /*
2358 * Check operand sizes
2359 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002360 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2361 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002362 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002363 asize = itemp->opd[i] & SIZE_MASK;
2364 if (asize) {
2365 for (i = 0; i < oprs; i++)
2366 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002367 break;
2368 }
2369 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002370 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002371 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002372 }
2373
Keith Kaniosb7a89542007-04-12 02:40:54 +00002374 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 if (!(itemp->opd[i] & SIZE_MASK) &&
2376 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002377 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002378 }
2379
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002380 /*
2381 * Check template is okay at the set cpu level
2382 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002383 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002384 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002385
Keith Kaniosb7a89542007-04-12 02:40:54 +00002386 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002387 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002388 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002389 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002390 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002391
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002392 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002393 * If we have a HLE prefix, look for the NOHLE flag
2394 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002395 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002396 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2397 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2398 return MERR_BADHLE;
2399
2400 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002401 * Check if special handling needed for Jumps
2402 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002403 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002404 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002405
Jin Kyu Song03041092013-10-15 19:38:51 -07002406 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002407 * Check if BND prefix is allowed.
2408 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002409 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002410 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002411 (has_prefix(instruction, PPS_REP, P_BND) ||
2412 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002413 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002414 else if (itemp_has(itemp, IF_BND) &&
2415 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2416 has_prefix(instruction, PPS_REP, P_REPNZ)))
2417 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002418
H. Peter Anvin60926242009-07-26 16:25:38 -07002419 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002420}
2421
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002422/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002423 * Check if ModR/M.mod should/can be 01.
2424 * - EAF_BYTEOFFS is set
2425 * - offset can fit in a byte when EVEX is not used
2426 * - offset can be compressed when EVEX is used
2427 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002428#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2429 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2430 is_disp8n(input, ins, &output->disp8) : \
2431 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2432 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002433
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002434static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002435 int rfield, opflags_t rflags, insn *ins,
2436 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002437{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002438 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002439 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002440 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002441
H. Peter Anvin8f622462017-04-02 19:02:29 -07002442 *errmsg = "invalid effective address"; /* Default error message */
2443
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002444 output->type = EA_SCALAR;
2445 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002446 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002447
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002448 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002449 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002450 /* EVEX.R' flag for the REG operand */
2451 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002452
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002453 if (is_class(REGISTER, input->type)) {
2454 /*
2455 * It's a direct register.
2456 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002457 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002458 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002459
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002460 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002461 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002462
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002463 /* broadcasting is not available with a direct register operand. */
2464 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002465 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002466 goto err;
2467 }
2468
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002469 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002470 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002471 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002472 output->bytes = 0; /* no offset necessary either */
2473 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2474 } else {
2475 /*
2476 * It's a memory reference.
2477 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002478
2479 /* Embedded rounding or SAE is not available with a mem ref operand. */
2480 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002481 *errmsg = "embedded rounding is available only with "
2482 "register-register operations";
2483 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002484 }
2485
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002486 if (input->basereg == -1 &&
2487 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002488 /*
2489 * It's a pure offset.
2490 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002491 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002492 if (input->segment == NO_SEG ||
2493 (input->opflags & OPFLAG_RELATIVE)) {
2494 nasm_error(ERR_WARNING | ERR_PASS2,
2495 "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002496 input->type &= ~IP_REL;
2497 input->type |= MEMORY;
2498 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002499 }
2500
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002501 if (bits == 64 &&
2502 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002503 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002504 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002505 }
2506
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002507 if (eaflags & EAF_BYTEOFFS ||
2508 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002509 input->disp_size != (addrbits != 16 ? 32 : 16))) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002510 nasm_error(ERR_WARNING | ERR_PASS1,
2511 "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002512 }
2513
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002514 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002515 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002516 output->sib = GEN_SIB(0, 4, 5);
2517 output->bytes = 4;
2518 output->modrm = GEN_MODRM(0, rfield, 4);
2519 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002520 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002521 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002522 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002523 output->modrm = GEN_MODRM(0, rfield,
2524 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002525 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002526 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002527 } else {
2528 /*
2529 * It's an indirection.
2530 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002531 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002532 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002534 int t, it, bt; /* register numbers */
2535 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002536
H. Peter Anvine2c80182005-01-15 22:15:51 +00002537 if (s == 0)
2538 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002539
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002540 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002541 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002542 ix = nasm_reg_flags[i];
2543 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002544 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002545 ix = 0;
2546 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002547
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002548 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002549 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002550 bx = nasm_reg_flags[b];
2551 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002552 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002553 bx = 0;
2554 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002555
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002556 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002557 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002558 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002559 int32_t o = input->offset;
2560 int mod, scale, index, base;
2561
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002562 /*
2563 * For a vector SIB, one has to be a vector and the other,
2564 * if present, a GPR. The vector must be the index operand.
2565 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002566 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002567 if (s == 0)
2568 s = 1;
2569 else if (s != 1)
2570 goto err;
2571
2572 t = bt, bt = it, it = t;
2573 x = bx, bx = ix, ix = x;
2574 }
2575
2576 if (bt != -1) {
2577 if (REG_GPR & ~bx)
2578 goto err;
2579 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2580 sok &= bx;
2581 else
2582 goto err;
2583 }
2584
2585 /*
2586 * While we're here, ensure the user didn't specify
2587 * WORD or QWORD
2588 */
2589 if (input->disp_size == 16 || input->disp_size == 64)
2590 goto err;
2591
2592 if (addrbits == 16 ||
2593 (addrbits == 32 && !(sok & BITS32)) ||
2594 (addrbits == 64 && !(sok & BITS64)))
2595 goto err;
2596
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002597 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2598 : ((ix & YMMREG & ~REG_EA)
2599 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002600
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002601 output->rex |= rexflags(it, ix, REX_X);
2602 output->rex |= rexflags(bt, bx, REX_B);
2603 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002604
2605 index = it & 7; /* it is known to be != -1 */
2606
2607 switch (s) {
2608 case 1:
2609 scale = 0;
2610 break;
2611 case 2:
2612 scale = 1;
2613 break;
2614 case 4:
2615 scale = 2;
2616 break;
2617 case 8:
2618 scale = 3;
2619 break;
2620 default: /* then what the smeg is it? */
2621 goto err; /* panic */
2622 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002623
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002624 if (bt == -1) {
2625 base = 5;
2626 mod = 0;
2627 } else {
2628 base = (bt & 7);
2629 if (base != REG_NUM_EBP && o == 0 &&
2630 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002631 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002632 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002633 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002634 mod = 1;
2635 else
2636 mod = 2;
2637 }
2638
2639 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002640 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2641 output->modrm = GEN_MODRM(mod, rfield, 4);
2642 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002643 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002644 /*
2645 * it must be a 32/64-bit memory reference. Firstly we have
2646 * to check that all registers involved are type E/Rxx.
2647 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002648 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002649 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002650
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002651 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002652 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2653 sok &= ix;
2654 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002655 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002656 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002657
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002658 if (bt != -1) {
2659 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002660 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002661 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002662 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002663 sok &= bx;
2664 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002665
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002666 /*
2667 * While we're here, ensure the user didn't specify
2668 * WORD or QWORD
2669 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002670 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002671 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002672
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002673 if (addrbits == 16 ||
2674 (addrbits == 32 && !(sok & BITS32)) ||
2675 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002676 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002677
Keith Kaniosb7a89542007-04-12 02:40:54 +00002678 /* now reorganize base/index */
2679 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002680 ((hb == b && ht == EAH_NOTBASE) ||
2681 (hb == i && ht == EAH_MAKEBASE))) {
2682 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002683 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002684 x = bx, bx = ix, ix = x;
2685 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002686
Jin Kyu Song164d6072013-10-15 19:10:13 -07002687 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002688 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002689 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002690 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002691 if (eaflags & EAF_MIB) {
2692 /* only for mib operands */
2693 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2694 /*
2695 * make a single reg index [reg*1].
2696 * gas uses this form for an explicit index register.
2697 */
2698 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2699 }
2700 if ((ht == EAH_SUMMED) && bt == -1) {
2701 /* separate once summed index into [base, index] */
2702 bt = it, bx = ix, s--;
2703 }
2704 } else {
2705 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002706 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002707 s == 3 || s == 5 || s == 9) && bt == -1) {
2708 /* convert 3*EAX to EAX+2*EAX */
2709 bt = it, bx = ix, s--;
2710 }
2711 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002712 (eaflags & EAF_TIMESTWO) &&
2713 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002714 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002715 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002716 * to sib format with 0x0 displacement - [EAX*1+0].
2717 */
2718 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2719 }
2720 }
Keith Kanios48af1772007-08-17 07:37:52 +00002721 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002722 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002723 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002724 x = ix, ix = bx, bx = x;
2725 }
2726 if (it == REG_NUM_ESP ||
2727 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002728 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002729
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002730 output->rex |= rexflags(it, ix, REX_X);
2731 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002732
Keith Kanios48af1772007-08-17 07:37:52 +00002733 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002734 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002735 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002736
Keith Kaniosb7a89542007-04-12 02:40:54 +00002737 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002739 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002740 } else {
2741 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002742 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002743 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002744 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002745 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002746 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002747 mod = 1;
2748 else
2749 mod = 2;
2750 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002751
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002752 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002753 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2754 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002755 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002756 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002757 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002758
Keith Kaniosb7a89542007-04-12 02:40:54 +00002759 if (it == -1)
2760 index = 4, s = 1;
2761 else
2762 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002763
H. Peter Anvine2c80182005-01-15 22:15:51 +00002764 switch (s) {
2765 case 1:
2766 scale = 0;
2767 break;
2768 case 2:
2769 scale = 1;
2770 break;
2771 case 4:
2772 scale = 2;
2773 break;
2774 case 8:
2775 scale = 3;
2776 break;
2777 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002778 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002779 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002780
Keith Kaniosb7a89542007-04-12 02:40:54 +00002781 if (bt == -1) {
2782 base = 5;
2783 mod = 0;
2784 } else {
2785 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002786 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002787 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002788 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002789 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002790 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002791 mod = 1;
2792 else
2793 mod = 2;
2794 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002795
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002796 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002797 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2798 output->modrm = GEN_MODRM(mod, rfield, 4);
2799 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002800 }
2801 } else { /* it's 16-bit */
2802 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002803 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002804
Keith Kaniosb7a89542007-04-12 02:40:54 +00002805 /* check for 64-bit long mode */
2806 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002807 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002808
H. Peter Anvine2c80182005-01-15 22:15:51 +00002809 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002810 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2811 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002812 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002813
Keith Kaniosb7a89542007-04-12 02:40:54 +00002814 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002815 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002816 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002817
H. Peter Anvine2c80182005-01-15 22:15:51 +00002818 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002819 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002820 if (b == -1 && i != -1) {
2821 int tmp = b;
2822 b = i;
2823 i = tmp;
2824 } /* swap */
2825 if ((b == R_SI || b == R_DI) && i != -1) {
2826 int tmp = b;
2827 b = i;
2828 i = tmp;
2829 }
2830 /* have BX/BP as base, SI/DI index */
2831 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002832 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 if (i != -1 && b != -1 &&
2834 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002835 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002836 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002837 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002838
H. Peter Anvine2c80182005-01-15 22:15:51 +00002839 rm = -1;
2840 if (i != -1)
2841 switch (i * 256 + b) {
2842 case R_SI * 256 + R_BX:
2843 rm = 0;
2844 break;
2845 case R_DI * 256 + R_BX:
2846 rm = 1;
2847 break;
2848 case R_SI * 256 + R_BP:
2849 rm = 2;
2850 break;
2851 case R_DI * 256 + R_BP:
2852 rm = 3;
2853 break;
2854 } else
2855 switch (b) {
2856 case R_SI:
2857 rm = 4;
2858 break;
2859 case R_DI:
2860 rm = 5;
2861 break;
2862 case R_BP:
2863 rm = 6;
2864 break;
2865 case R_BX:
2866 rm = 7;
2867 break;
2868 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002869 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002870 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002871
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002872 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002873 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002874 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002875 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002876 mod = 1;
2877 else
2878 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002879
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002880 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002881 output->bytes = mod; /* bytes of offset needed */
2882 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002883 }
2884 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002885 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002886
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002887 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002888 return output->type;
2889
2890err:
2891 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002892}
2893
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002894static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002895{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002896 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002897 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002898
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002899 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002900
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002901 switch (ins->prefixes[PPS_ASIZE]) {
2902 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002903 valid &= 16;
2904 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002905 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002906 valid &= 32;
2907 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002908 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002909 valid &= 64;
2910 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002911 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002912 valid &= (addrbits == 32) ? 16 : 32;
2913 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002914 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002915 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002916 }
2917
2918 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002919 if (is_class(MEMORY, ins->oprs[j].type)) {
2920 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002921
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002922 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002923 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002924 i = 0;
2925 else
2926 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002927
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002928 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002929 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002930 b = 0;
2931 else
2932 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002933
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002934 if (ins->oprs[j].scale == 0)
2935 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002936
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002937 if (!i && !b) {
2938 int ds = ins->oprs[j].disp_size;
2939 if ((addrbits != 64 && ds > 8) ||
2940 (addrbits == 64 && ds == 16))
2941 valid &= ds;
2942 } else {
2943 if (!(REG16 & ~b))
2944 valid &= 16;
2945 if (!(REG32 & ~b))
2946 valid &= 32;
2947 if (!(REG64 & ~b))
2948 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002949
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002950 if (!(REG16 & ~i))
2951 valid &= 16;
2952 if (!(REG32 & ~i))
2953 valid &= 32;
2954 if (!(REG64 & ~i))
2955 valid &= 64;
2956 }
2957 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002958 }
2959
2960 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002961 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002962 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002963 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002964 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002965 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002966 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002967 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002968 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002969 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002970 }
2971
2972 defdisp = ins->addr_size == 16 ? 16 : 32;
2973
2974 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002975 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2976 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2977 /*
2978 * mem_offs sizes must match the address size; if not,
2979 * strip the MEM_OFFS bit and match only EA instructions
2980 */
2981 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2982 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002983 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002984}