blob: 561bba558ad36ab922c853fa3d7a21cb7ddc0e6c [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 */
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800369 if (data->tsegment == fixseg && data->twrt == NO_SEG) {
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800370 warn_overflow_out(addrval, asize, data->sign);
Martin Storsjö869087d2017-05-22 13:54:20 +0300371 xdata.q = cpu_to_le64(addrval);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700372 data->data = xdata.b;
373 data->type = OUT_RAWDATA;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700374 asize = amax = 0; /* No longer an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700375 }
376 break;
377
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700378 case OUT_SEGMENT:
379 nasm_assert(data->size <= 8);
380 asize = data->size;
381 amax = 2;
382 break;
383
H. Peter Anvina77692b2016-09-20 14:04:33 -0700384 default:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700385 asize = amax = 0; /* Not an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700386 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000387 }
388
Frank Kotlerabebb082003-09-06 04:45:37 +0000389 /*
390 * this call to src_get determines when we call the
391 * debug-format-specific "linenum" function
392 * it updates lineno and lnfname to the current values
393 * returning 0 if "same as last time", -2 if lnfname
394 * changed, and the amount by which lineno changed,
395 * if it did. thus, these variables must be static
396 */
397
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400398 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700399 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000400
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700401 if (asize > amax) {
402 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800403 nasm_error(ERR_NONFATAL,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700404 "%u-bit signed relocation unsupported by output format %s",
405 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800406 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800407 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700408 "%u-bit %s relocation zero-extended from %u bits",
409 (unsigned int)(asize << 3),
410 data->type == OUT_SEGMENT ? "segment" : "unsigned",
411 (unsigned int)(amax << 3));
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800412 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700413 zeropad = data->size - amax;
414 data->size = amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800415 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700416 lfmt->output(data);
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700417 ofmt->output(data);
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700418 data->offset += data->size;
419 data->insoffs += data->size;
420
421 if (zeropad) {
422 data->type = OUT_ZERODATA;
423 data->size = zeropad;
424 lfmt->output(data);
425 ofmt->output(data);
426 data->offset += zeropad;
427 data->insoffs += zeropad;
428 data->size += zeropad; /* Restore original size value */
429 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000430}
431
H. Peter Anvina77692b2016-09-20 14:04:33 -0700432static inline void out_rawdata(struct out_data *data, const void *rawdata,
433 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400434{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700435 data->type = OUT_RAWDATA;
436 data->data = rawdata;
437 data->size = size;
438 out(data);
439}
440
441static void out_rawbyte(struct out_data *data, uint8_t byte)
442{
443 data->type = OUT_RAWDATA;
444 data->data = &byte;
445 data->size = 1;
446 out(data);
447}
448
449static inline void out_reserve(struct out_data *data, uint64_t size)
450{
451 data->type = OUT_RESERVE;
452 data->size = size;
453 out(data);
454}
455
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700456static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700457{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700458 if (opx->opflags & OPFLAG_RELATIVE)
459 nasm_error(ERR_NONFATAL, "segment references cannot be relative");
460
461 data->type = OUT_SEGMENT;
462 data->sign = OUT_UNSIGNED;
463 data->size = 2;
464 data->toffset = opx->offset;
465 data->tsegment = ofmt->segbase(opx->segment | 1);
466 data->twrt = opx->wrt;
467 out(data);
468}
469
470static void out_imm(struct out_data *data, const struct operand *opx,
471 int size, enum out_sign sign)
472{
473 if (opx->segment != NO_SEG && (opx->segment & 1)) {
474 /*
475 * This is actually a segment reference, but eval() has
476 * already called ofmt->segbase() for us. Sigh.
477 */
478 if (size < 2)
479 nasm_error(ERR_NONFATAL, "segment reference must be 16 bits");
480
481 data->type = OUT_SEGMENT;
482 } else {
483 data->type = (opx->opflags & OPFLAG_RELATIVE)
484 ? OUT_RELADDR : OUT_ADDRESS;
485 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700486 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700487 data->toffset = opx->offset;
488 data->tsegment = opx->segment;
489 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800490 /*
491 * XXX: improve this if at some point in the future we can
492 * distinguish the subtrahend in expressions like [foo - bar]
493 * where bar is a symbol in the current segment. However, at the
494 * current point, if OPFLAG_RELATIVE is set that subtraction has
495 * already occurred.
496 */
497 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700498 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700499 out(data);
500}
501
H. Peter Anvin164d2462017-02-20 02:39:56 -0800502static void out_reladdr(struct out_data *data, const struct operand *opx,
503 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700504{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800505 if (opx->opflags & OPFLAG_RELATIVE)
506 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
507
H. Peter Anvina77692b2016-09-20 14:04:33 -0700508 data->type = OUT_RELADDR;
509 data->sign = OUT_SIGNED;
510 data->size = size;
511 data->toffset = opx->offset;
512 data->tsegment = opx->segment;
513 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800514 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700515 out(data);
516}
517
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700518static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800519 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000520{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800521 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800522 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000523 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800524 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000525
H. Peter Anvin755f5212012-02-25 11:41:34 -0800526 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700527 return false;
528 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400529 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700530 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400531 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700532
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800533 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100534
Victor van den Elzen154e5922009-02-25 17:32:00 +0100535 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100536 /* Be optimistic in pass 1 */
537 return true;
538
H. Peter Anvine2c80182005-01-15 22:15:51 +0000539 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700540 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000541
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700542 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800543 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
544
545 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
546 /* jmp short (opcode eb) cannot be used with bnd prefix. */
547 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800548 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800549 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800550 }
551
552 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000554
H. Peter Anvin04445362016-09-21 15:56:19 -0700555/* This is totally just a wild guess what is reasonable... */
556#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
557
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800558int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000559{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700560 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000561 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700562 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300563 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000564
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700565 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700566 data.offset = start;
567 data.segment = segment;
568 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700569 data.bits = bits;
570
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700571 wsize = db_bytes(instruction->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300572 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000573 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000574
H. Peter Anvineba20a72002-04-30 20:53:55 +0000575 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000576 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700577
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700578 list_for_each(e, instruction->eops) {
579 if (e->type == EOT_DB_NUMBER) {
580 if (wsize > 8) {
581 nasm_error(ERR_NONFATAL,
582 "integer supplied to a DT, DO, DY or DZ"
583 " instruction");
584 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700585 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700586 data.inslen = data.size = wsize;
587 data.toffset = e->offset;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700588 data.twrt = e->wrt;
589 data.relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700590 if (e->segment != NO_SEG && (e->segment & 1)) {
591 data.tsegment = e->segment;
592 data.type = OUT_SEGMENT;
593 data.sign = OUT_UNSIGNED;
594 } else {
595 data.tsegment = e->segment;
596 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
597 data.sign = OUT_WRAP;
598 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700599 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000600 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700601 } else if (e->type == EOT_DB_STRING ||
602 e->type == EOT_DB_STRING_FREE) {
603 int align = e->stringlen % wsize;
604 if (align)
605 align = wsize - align;
606
607 data.insoffs = 0;
608 data.inslen = e->stringlen + align;
609
610 out_rawdata(&data, e->stringval, e->stringlen);
611 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700612 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000613 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700614 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700615 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700617 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700618 off_t base = 0;
619 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700620 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700621 char *buf = NULL;
622 size_t blk = 0; /* Buffered I/O block size */
623 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000624
H. Peter Anvin94ead272017-09-27 15:22:23 -0700625 if (!t)
626 goto done;
627
H. Peter Anvind81a2352016-09-21 14:03:18 -0700628 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400629 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800630 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700632 goto done;
633 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000634
H. Peter Anvind81a2352016-09-21 14:03:18 -0700635 len = nasm_file_size(fp);
636
637 if (len == (off_t)-1) {
638 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700639 fname);
640 goto close_done;
641 }
642
H. Peter Anvina77692b2016-09-20 14:04:33 -0700643 if (instruction->eops->next) {
644 base = instruction->eops->next->offset;
645 if (base >= len) {
646 len = 0;
647 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000648 len -= base;
649 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700650 len > (off_t)instruction->eops->next->next->offset)
651 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000652 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000653 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700654
H. Peter Anvina77692b2016-09-20 14:04:33 -0700655 lfmt->set_offset(data.offset);
656 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000657
H. Peter Anvind81a2352016-09-21 14:03:18 -0700658 if (!len)
659 goto end_incbin;
660
661 /* Try to map file data */
662 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700663 if (!map) {
664 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
665 buf = nasm_malloc(blk);
666 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700667
668 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700669 /*
670 * Consider these irrelevant for INCBIN, since it is fully
671 * possible that these might be (way) bigger than an int
672 * can hold; there is, however, no reason to widen these
673 * types just for INCBIN. data.inslen == 0 signals to the
674 * backend that these fields are meaningless, if at all
675 * needed.
676 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700677 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700678 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700679
H. Peter Anvind81a2352016-09-21 14:03:18 -0700680 if (map) {
681 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700682 } else if ((off_t)m == len) {
683 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700684 } else {
685 off_t l = len;
686
687 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700688 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700689 "`incbin': unable to seek on file `%s'",
690 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700691 goto end_incbin;
692 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700693 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700694 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700695 if (!m || feof(fp)) {
696 /*
697 * This shouldn't happen unless the file
698 * actually changes while we are reading
699 * it.
700 */
701 nasm_error(ERR_NONFATAL,
702 "`incbin': unexpected EOF while"
703 " reading file `%s'", fname);
704 goto end_incbin;
705 }
706 out_rawdata(&data, buf, m);
707 l -= m;
708 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700709 }
710 }
711 end_incbin:
712 lfmt->downlevel(LIST_INCBIN);
713 if (instruction->times > 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700714 lfmt->uplevel(LIST_TIMES);
715 lfmt->downlevel(LIST_TIMES);
716 }
717 if (ferror(fp)) {
718 nasm_error(ERR_NONFATAL,
719 "`incbin': error while"
720 " reading file `%s'", fname);
721 }
722 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700723 if (buf)
724 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700725 if (map)
726 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700727 fclose(fp);
728 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700729 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700730 ;
731 } else {
732 /* "Real" instruction */
733
734 /* Check to see if we need an address-size prefix */
735 add_asp(instruction, bits);
736
737 m = find_match(&temp, instruction, data.segment, data.offset, bits);
738
739 if (m == MOK_GOOD) {
740 /* Matches! */
741 int64_t insn_size = calcsize(data.segment, data.offset,
742 bits, instruction, temp);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700743 nasm_assert(insn_size >= 0);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700744
745 data.itemp = temp;
746 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700747 data.insoffs = 0;
748 data.inslen = insn_size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700749
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700750 gencode(&data, instruction);
751 nasm_assert(data.insoffs == insn_size);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700752 } else {
753 /* No match */
754 switch (m) {
755 case MERR_OPSIZEMISSING:
756 nasm_error(ERR_NONFATAL, "operation size not specified");
757 break;
758 case MERR_OPSIZEMISMATCH:
759 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
760 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700761 case MERR_BRNOTHERE:
762 nasm_error(ERR_NONFATAL,
763 "broadcast not permitted on this operand");
764 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700765 case MERR_BRNUMMISMATCH:
766 nasm_error(ERR_NONFATAL,
767 "mismatch in the number of broadcasting elements");
768 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700769 case MERR_MASKNOTHERE:
770 nasm_error(ERR_NONFATAL,
771 "mask not permitted on this operand");
772 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700773 case MERR_DECONOTHERE:
774 nasm_error(ERR_NONFATAL, "unsupported mode decorator for instruction");
775 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700776 case MERR_BADCPU:
777 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
778 break;
779 case MERR_BADMODE:
780 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
781 bits);
782 break;
783 case MERR_ENCMISMATCH:
784 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
785 break;
786 case MERR_BADBND:
787 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
788 break;
789 case MERR_BADREPNE:
790 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
791 (has_prefix(instruction, PPS_REP, P_REPNE) ?
792 "repne" : "repnz"));
793 break;
794 default:
795 nasm_error(ERR_NONFATAL,
796 "invalid combination of opcode and operands");
797 break;
798 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700799
800 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400801 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000802 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700803 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000804}
805
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800806int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000807{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000808 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700809 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000810
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400811 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000813
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700814 if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300816 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000817
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 isize = 0;
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700819 wsize = db_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700820 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000821
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400822 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000823 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000824
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400826 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400828 warn_overflow_const(e->offset, wsize);
829 } else if (e->type == EOT_DB_STRING ||
830 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000832
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 align = (-osize) % wsize;
834 if (align < 0)
835 align += wsize;
836 isize += osize + align;
837 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700838 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000839 }
840
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400842 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700843 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000844
H. Peter Anvind81a2352016-09-21 14:03:18 -0700845 len = nasm_file_size_by_path(fname);
846 if (len == (off_t)-1) {
847 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
848 fname);
849 return 0;
850 }
851
852 if (instruction->eops->next) {
853 if (len <= (off_t)instruction->eops->next->offset) {
854 len = 0;
855 } else {
856 len -= instruction->eops->next->offset;
857 if (instruction->eops->next->next &&
858 len > (off_t)instruction->eops->next->next->offset) {
859 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000860 }
861 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000862 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700863
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700864 len *= instruction->times;
865 instruction->times = 1; /* Tell the upper layer to not iterate */
866
H. Peter Anvind81a2352016-09-21 14:03:18 -0700867 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000868 }
869
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700870 /* Check to see if we need an address-size prefix */
871 add_asp(instruction, bits);
872
H. Peter Anvin23595f52009-07-25 17:44:25 -0700873 m = find_match(&temp, instruction, segment, offset, bits);
874 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400875 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700876 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700877 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400878 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000879 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000880}
881
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800882static void bad_hle_warn(const insn * ins, uint8_t hleok)
883{
884 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800885 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800886 static const enum whatwarn warn[2][4] =
887 {
888 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
889 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
890 };
891 unsigned int n;
892
893 n = (unsigned int)rep_pfx - P_XACQUIRE;
894 if (n > 1)
895 return; /* Not XACQUIRE/XRELEASE */
896
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800897 ww = warn[n][hleok];
898 if (!is_class(MEMORY, ins->oprs[0].type))
899 ww = w_inval; /* HLE requires operand 0 to be memory */
900
901 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800902 case w_none:
903 break;
904
905 case w_lock:
906 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800907 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800908 "%s with this instruction requires lock",
909 prefix_name(rep_pfx));
910 }
911 break;
912
913 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800914 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800915 "%s invalid with this instruction",
916 prefix_name(rep_pfx));
917 break;
918 }
919}
920
H. Peter Anvin507ae032008-10-09 15:37:10 -0700921/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400922#define case3(x) case (x): case (x)+1: case (x)+2
923#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700924
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800925static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800926 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000927{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800928 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800929 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000930 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000931 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700932 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700933 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700934 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700935 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800936 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800937 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700938 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -0700939 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000940
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700941 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700942 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700943 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700944
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700945 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400946 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700947
H. Peter Anvine2c80182005-01-15 22:15:51 +0000948 (void)segment; /* Don't warn that this parameter is unused */
949 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000950
H. Peter Anvin839eca22007-10-29 23:12:47 -0700951 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400952 c = *codes++;
953 op1 = (c & 3) + ((opex & 1) << 2);
954 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
955 opx = &ins->oprs[op1];
956 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700957
H. Peter Anvin839eca22007-10-29 23:12:47 -0700958 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400959 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000960 codes += c, length += c;
961 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700962
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400963 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400964 opex = c;
965 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700966
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400967 case4(010):
968 ins->rex |=
969 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000970 codes++, length++;
971 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700972
Jin Kyu Song164d6072013-10-15 19:10:13 -0700973 case4(014):
974 /* this is an index reg of MIB operand */
975 mib_index = opx->basereg;
976 break;
977
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400978 case4(020):
979 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000980 length++;
981 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700982
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400983 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000984 length += 2;
985 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700986
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400987 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700988 if (opx->type & (BITS16 | BITS32 | BITS64))
989 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 else
991 length += (bits == 16) ? 2 : 4;
992 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700993
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400994 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 length += 4;
996 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700997
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400998 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700999 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001001
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001002 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001003 length++;
1004 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001005
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001006 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001007 length += 8; /* MOV reg64/imm */
1008 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001009
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001010 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001011 length += 2;
1012 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001013
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001014 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001015 if (opx->type & (BITS16 | BITS32 | BITS64))
1016 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 else
1018 length += (bits == 16) ? 2 : 4;
1019 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001020
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001021 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 length += 4;
1023 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001024
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001025 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001026 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001028
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001029 case 0172:
1030 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001031 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001032 length++;
1033 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001034
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001035 case4(0174):
1036 length++;
1037 break;
1038
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001039 case4(0240):
1040 ins->rex |= REX_EV;
1041 ins->vexreg = regval(opx);
1042 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1043 ins->vex_cm = *codes++;
1044 ins->vex_wlp = *codes++;
1045 ins->evex_tuple = (*codes++ - 0300);
1046 break;
1047
1048 case 0250:
1049 ins->rex |= REX_EV;
1050 ins->vexreg = 0;
1051 ins->vex_cm = *codes++;
1052 ins->vex_wlp = *codes++;
1053 ins->evex_tuple = (*codes++ - 0300);
1054 break;
1055
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001056 case4(0254):
1057 length += 4;
1058 break;
1059
1060 case4(0260):
1061 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001062 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001063 ins->vex_cm = *codes++;
1064 ins->vex_wlp = *codes++;
1065 break;
1066
1067 case 0270:
1068 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001069 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001070 ins->vex_cm = *codes++;
1071 ins->vex_wlp = *codes++;
1072 break;
1073
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001074 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001075 hleok = c & 3;
1076 break;
1077
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001078 case4(0274):
1079 length++;
1080 break;
1081
1082 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001083 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001084
H. Peter Anvine2c80182005-01-15 22:15:51 +00001085 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001086 if (bits == 64)
1087 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001088 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001090
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001092 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001094
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001096 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001097
Keith Kaniosb7a89542007-04-12 02:40:54 +00001098 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001099 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1100 has_prefix(ins, PPS_ASIZE, P_A32))
1101 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001103
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001104 case4(0314):
1105 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001106
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001108 {
1109 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1110 if (pfx == P_O16)
1111 break;
1112 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001113 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001114 else
1115 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001117 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001118
H. Peter Anvine2c80182005-01-15 22:15:51 +00001119 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001120 {
1121 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1122 if (pfx == P_O32)
1123 break;
1124 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001125 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001126 else
1127 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001129 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001130
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 case 0322:
1132 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001133
Keith Kaniosb7a89542007-04-12 02:40:54 +00001134 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001135 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001136 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001137
Keith Kaniosb7a89542007-04-12 02:40:54 +00001138 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001139 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001140 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001141
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001142 case 0325:
1143 ins->rex |= REX_NH;
1144 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001145
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001146 case 0326:
1147 break;
1148
H. Peter Anvine2c80182005-01-15 22:15:51 +00001149 case 0330:
1150 codes++, length++;
1151 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001152
H. Peter Anvine2c80182005-01-15 22:15:51 +00001153 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001155
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001156 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 case 0333:
1158 length++;
1159 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001160
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001161 case 0334:
1162 ins->rex |= REX_L;
1163 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001164
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001165 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001166 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001167
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001168 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001169 if (!ins->prefixes[PPS_REP])
1170 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001171 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001172
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001173 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001174 if (!ins->prefixes[PPS_REP])
1175 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001176 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001177
H. Peter Anvine2c80182005-01-15 22:15:51 +00001178 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001179 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001180 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001181 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001182 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1183 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001184 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001185 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001186 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001188
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001189 case 0341:
1190 if (!ins->prefixes[PPS_WAIT])
1191 ins->prefixes[PPS_WAIT] = P_WAIT;
1192 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001193
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001194 case 0360:
1195 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001196
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001197 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001198 length++;
1199 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001200
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001201 case 0364:
1202 case 0365:
1203 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001204
Keith Kanios48af1772007-08-17 07:37:52 +00001205 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001206 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001207 length++;
1208 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001209
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001210 case 0370:
1211 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001212 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001213
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 case 0373:
1215 length++;
1216 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001217
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001218 case 0374:
1219 eat = EA_XMMVSIB;
1220 break;
1221
1222 case 0375:
1223 eat = EA_YMMVSIB;
1224 break;
1225
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001226 case 0376:
1227 eat = EA_ZMMVSIB;
1228 break;
1229
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001230 case4(0100):
1231 case4(0110):
1232 case4(0120):
1233 case4(0130):
1234 case4(0200):
1235 case4(0204):
1236 case4(0210):
1237 case4(0214):
1238 case4(0220):
1239 case4(0224):
1240 case4(0230):
1241 case4(0234):
1242 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001243 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001244 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001245 opflags_t rflags;
1246 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001247 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001248
Keith Kaniosb7a89542007-04-12 02:40:54 +00001249 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001250
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001251 if (c <= 0177) {
1252 /* pick rfield from operand b (opx) */
1253 rflags = regflag(opx);
1254 rfield = nasm_regvals[opx->basereg];
1255 } else {
1256 rflags = 0;
1257 rfield = c & 7;
1258 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001259
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001260 /* EVEX.b1 : evex_brerop contains the operand position */
1261 op_er_sae = (ins->evex_brerop >= 0 ?
1262 &ins->oprs[ins->evex_brerop] : NULL);
1263
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001264 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1265 /* set EVEX.b */
1266 ins->evex_p[2] |= EVEX_P2B;
1267 if (op_er_sae->decoflags & ER) {
1268 /* set EVEX.RC (rounding control) */
1269 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1270 & EVEX_P2RC;
1271 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001272 } else {
1273 /* set EVEX.L'L (vector length) */
1274 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001275 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001276 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001277 /* set EVEX.b */
1278 ins->evex_p[2] |= EVEX_P2B;
1279 }
1280 }
1281
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001282 if (itemp_has(temp, IF_MIB)) {
1283 opy->eaflags |= EAF_MIB;
1284 /*
1285 * if a separate form of MIB (ICC style) is used,
1286 * the index reg info is merged into mem operand
1287 */
1288 if (mib_index != R_none) {
1289 opy->indexreg = mib_index;
1290 opy->scale = 1;
1291 opy->hintbase = mib_index;
1292 opy->hinttype = EAH_NOTBASE;
1293 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001294 }
1295
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001296 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001297 rfield, rflags, ins, &errmsg) != eat) {
1298 nasm_error(ERR_NONFATAL, "%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001299 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001300 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001301 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001302 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001303 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001304 }
1305 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001306
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001307 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001308 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001309 ": instruction code \\%o (0x%02X) given", c, c);
1310 break;
1311 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001312 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001313
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001314 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001315
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001316 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001317 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001318 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001319 return -1;
1320 }
1321 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001322 }
1323
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001324 switch (ins->prefixes[PPS_VEX]) {
1325 case P_EVEX:
1326 if (!(ins->rex & REX_EV))
1327 return -1;
1328 break;
1329 case P_VEX3:
1330 case P_VEX2:
1331 if (!(ins->rex & REX_V))
1332 return -1;
1333 break;
1334 default:
1335 break;
1336 }
1337
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001338 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001339 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001340
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001341 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001342 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001343 return -1;
1344 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001345 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001346 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001347 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001348 ins->rex &= ~REX_W;
1349 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001350 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001351 ins->rex |= REX_W;
1352 bad32 &= ~REX_W;
1353 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001354 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001355 /* Follow REX_W */
1356 break;
1357 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001358
H. Peter Anvinfc561202011-07-07 16:58:22 -07001359 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001360 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001361 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001362 } else if (!(ins->rex & REX_EV) &&
1363 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001364 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001365 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001366 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001367 if (ins->rex & REX_EV)
1368 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001369 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1370 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001371 length += 3;
1372 else
1373 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001374 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001375 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001376 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001377 return -1;
1378 } else if (bits == 64) {
1379 length++;
1380 } else if ((ins->rex & REX_L) &&
1381 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001382 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001383 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001384 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001385 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001386 length++;
1387 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001388 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001389 return -1;
1390 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001391 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001392
1393 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001394 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001395 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001396 "instruction is not lockable");
1397 }
1398
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001399 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001400
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001401 /*
1402 * when BND prefix is set by DEFAULT directive,
1403 * BND prefix is added to every appropriate instruction line
1404 * unless it is overridden by NOBND prefix.
1405 */
1406 if (globalbnd &&
1407 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1408 ins->prefixes[PPS_REP] = P_BND;
1409
H. Peter Anvina77692b2016-09-20 14:04:33 -07001410 /*
1411 * Add length of legacy prefixes
1412 */
1413 length += emit_prefix(NULL, bits, ins);
1414
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001415 return length;
1416}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001417
H. Peter Anvina77692b2016-09-20 14:04:33 -07001418static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001419{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001420 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001421 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001422 !(ins->rex & (REX_V | REX_EV)) &&
1423 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001424 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1425 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001426 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001427 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001428 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001429}
1430
H. Peter Anvina77692b2016-09-20 14:04:33 -07001431static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1432{
1433 int bytes = 0;
1434 int j;
1435
1436 for (j = 0; j < MAXPREFIX; j++) {
1437 uint8_t c = 0;
1438 switch (ins->prefixes[j]) {
1439 case P_WAIT:
1440 c = 0x9B;
1441 break;
1442 case P_LOCK:
1443 c = 0xF0;
1444 break;
1445 case P_REPNE:
1446 case P_REPNZ:
1447 case P_XACQUIRE:
1448 case P_BND:
1449 c = 0xF2;
1450 break;
1451 case P_REPE:
1452 case P_REPZ:
1453 case P_REP:
1454 case P_XRELEASE:
1455 c = 0xF3;
1456 break;
1457 case R_CS:
1458 if (bits == 64) {
1459 nasm_error(ERR_WARNING | ERR_PASS2,
1460 "cs segment base generated, but will be ignored in 64-bit mode");
1461 }
1462 c = 0x2E;
1463 break;
1464 case R_DS:
1465 if (bits == 64) {
1466 nasm_error(ERR_WARNING | ERR_PASS2,
1467 "ds segment base generated, but will be ignored in 64-bit mode");
1468 }
1469 c = 0x3E;
1470 break;
1471 case R_ES:
1472 if (bits == 64) {
1473 nasm_error(ERR_WARNING | ERR_PASS2,
1474 "es segment base generated, but will be ignored in 64-bit mode");
1475 }
1476 c = 0x26;
1477 break;
1478 case R_FS:
1479 c = 0x64;
1480 break;
1481 case R_GS:
1482 c = 0x65;
1483 break;
1484 case R_SS:
1485 if (bits == 64) {
1486 nasm_error(ERR_WARNING | ERR_PASS2,
1487 "ss segment base generated, but will be ignored in 64-bit mode");
1488 }
1489 c = 0x36;
1490 break;
1491 case R_SEGR6:
1492 case R_SEGR7:
1493 nasm_error(ERR_NONFATAL,
1494 "segr6 and segr7 cannot be used as prefixes");
1495 break;
1496 case P_A16:
1497 if (bits == 64) {
1498 nasm_error(ERR_NONFATAL,
1499 "16-bit addressing is not supported "
1500 "in 64-bit mode");
1501 } else if (bits != 16)
1502 c = 0x67;
1503 break;
1504 case P_A32:
1505 if (bits != 32)
1506 c = 0x67;
1507 break;
1508 case P_A64:
1509 if (bits != 64) {
1510 nasm_error(ERR_NONFATAL,
1511 "64-bit addressing is only supported "
1512 "in 64-bit mode");
1513 }
1514 break;
1515 case P_ASP:
1516 c = 0x67;
1517 break;
1518 case P_O16:
1519 if (bits != 16)
1520 c = 0x66;
1521 break;
1522 case P_O32:
1523 if (bits == 16)
1524 c = 0x66;
1525 break;
1526 case P_O64:
1527 /* REX.W */
1528 break;
1529 case P_OSP:
1530 c = 0x66;
1531 break;
1532 case P_EVEX:
1533 case P_VEX3:
1534 case P_VEX2:
1535 case P_NOBND:
1536 case P_none:
1537 break;
1538 default:
1539 nasm_panic(0, "invalid instruction prefix");
1540 }
1541 if (c) {
1542 if (data)
1543 out_rawbyte(data, c);
1544 bytes++;
1545 }
1546 }
1547 return bytes;
1548}
1549
1550static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001551{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001552 uint8_t c;
1553 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001554 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001555 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001556 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001557 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001558 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001559 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001560 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001561 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001562 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001563
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001564 ins->rex_done = false;
1565
H. Peter Anvina77692b2016-09-20 14:04:33 -07001566 emit_prefix(data, bits, ins);
1567
H. Peter Anvin839eca22007-10-29 23:12:47 -07001568 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001569 c = *codes++;
1570 op1 = (c & 3) + ((opex & 1) << 2);
1571 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1572 opx = &ins->oprs[op1];
1573 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001574
H. Peter Anvina77692b2016-09-20 14:04:33 -07001575
H. Peter Anvin839eca22007-10-29 23:12:47 -07001576 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001577 case 01:
1578 case 02:
1579 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001580 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001581 emit_rex(data, ins);
1582 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001585
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001586 case 05:
1587 case 06:
1588 case 07:
1589 opex = c;
1590 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001591
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001592 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001593 emit_rex(data, ins);
1594 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001595 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001596
Jin Kyu Song164d6072013-10-15 19:10:13 -07001597 case4(014):
1598 break;
1599
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001600 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001601 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001602 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001603
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001604 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001605 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001606 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001607
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001608 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001609 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001611
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001612 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001613 if (opx->type & (BITS16 | BITS32))
1614 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 else
1616 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001617 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001618 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001619
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001620 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001621 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001622 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001623
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001624 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001625 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001626 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001628
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001629 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001630 if (opx->segment == data->segment) {
1631 int64_t delta = opx->offset - data->offset
1632 - (data->inslen - data->insoffs);
1633 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001634 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001635 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001636 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001637 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001638
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001639 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001640 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001641 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001642
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001643 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001644 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001645 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001646
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001647 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001648 if (opx->type & (BITS16 | BITS32 | BITS64))
1649 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001650 else
1651 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001652
1653 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001655
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001656 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001657 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001658 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001659
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001660 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001661 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001662 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001663 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001664 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001665 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001666
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001667 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001668 {
1669 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1670 const struct operand *opy;
1671
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001672 c = *codes++;
1673 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001674 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001675 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001676 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001677 "non-absolute expression not permitted as argument %d",
1678 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001679 } else if (opy->offset & ~mask) {
1680 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1681 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001682 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001683 c = opy->offset & mask;
1684 goto emit_is4;
1685 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001686
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001687 case 0173:
1688 c = *codes++;
1689 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001690 c &= 15;
1691 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001692
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001693 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001694 c = 0;
1695 emit_is4:
1696 r = nasm_regvals[opx->basereg];
1697 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001698 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001699
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001700 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001701 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001702 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001703 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001704 "signed dword immediate exceeds bounds");
1705 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001706 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001707 break;
1708
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001709 case4(0240):
1710 case 0250:
1711 codes += 3;
1712 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1713 EVEX_P2Z | EVEX_P2AAA, 2);
1714 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1715 bytes[0] = 0x62;
1716 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001717 bytes[1] = ((((ins->rex & 7) << 5) |
1718 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001719 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001720 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1721 ((~ins->vexreg & 15) << 3) |
1722 (1 << 2) | (ins->vex_wlp & 3);
1723 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001724 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001725 break;
1726
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001727 case4(0260):
1728 case 0270:
1729 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001730 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1731 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001732 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1733 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1734 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001735 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001736 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001737 } else {
1738 bytes[0] = 0xc5;
1739 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001740 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001741 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001742 }
1743 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001744
H. Peter Anvine014f352012-02-25 22:35:19 -08001745 case 0271:
1746 case 0272:
1747 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001748 break;
1749
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001750 case4(0274):
1751 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001752 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001753 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001754
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001755 if (absolute_op(opx)) {
1756 if (ins->rex & REX_W)
1757 s = 64;
1758 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1759 s = 16;
1760 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1761 s = 32;
1762 else
1763 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001764
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001765 um = (uint64_t)2 << (s-1);
1766 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001767
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001768 if (uv > 127 && uv < (uint64_t)-128 &&
1769 (uv < um-128 || uv > um-1)) {
1770 /* If this wasn't explicitly byte-sized, warn as though we
1771 * had fallen through to the imm16/32/64 case.
1772 */
1773 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1774 "%s value exceeds bounds",
1775 (opx->type & BITS8) ? "signed byte" :
1776 s == 16 ? "word" :
1777 s == 32 ? "dword" :
1778 "signed dword");
1779 }
1780
1781 /* Output as a raw byte to avoid byte overflow check */
1782 out_rawbyte(data, (uint8_t)uv);
1783 } else {
1784 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001785 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001786 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001787 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001788
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001789 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001791
H. Peter Anvine2c80182005-01-15 22:15:51 +00001792 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001793 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1794 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001795 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001796
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001798 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1799 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001801
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 case 0312:
1803 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001804
Keith Kaniosb7a89542007-04-12 02:40:54 +00001805 case 0313:
1806 ins->rex = 0;
1807 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001808
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001809 case4(0314):
1810 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001811
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001813 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001815
H. Peter Anvine2c80182005-01-15 22:15:51 +00001816 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001817 case 0323:
1818 break;
1819
Keith Kaniosb7a89542007-04-12 02:40:54 +00001820 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001821 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001823
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001824 case 0325:
1825 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001826
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001827 case 0326:
1828 break;
1829
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001831 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001832 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001833
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001835 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001836
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001837 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001838 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001839 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001840 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001841
Keith Kanios48af1772007-08-17 07:37:52 +00001842 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001843 if (ins->rex & REX_R)
1844 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001845 ins->rex &= ~(REX_L|REX_R);
1846 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001847
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001848 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001849 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001850
H. Peter Anvin962e3052008-08-28 17:47:16 -07001851 case 0336:
1852 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001853 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001854
H. Peter Anvine2c80182005-01-15 22:15:51 +00001855 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001856 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001857 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001858
1859 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001861
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001862 case 0341:
1863 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001864
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001865 case 0360:
1866 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001867
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001868 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001869 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001870 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001871
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001872 case 0364:
1873 case 0365:
1874 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001875
Keith Kanios48af1772007-08-17 07:37:52 +00001876 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001877 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001878 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001879 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001880
Jin Kyu Song03041092013-10-15 19:38:51 -07001881 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001882 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001883
H. Peter Anvine2c80182005-01-15 22:15:51 +00001884 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001885 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001886 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001887
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001888 case 0374:
1889 eat = EA_XMMVSIB;
1890 break;
1891
1892 case 0375:
1893 eat = EA_YMMVSIB;
1894 break;
1895
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001896 case 0376:
1897 eat = EA_ZMMVSIB;
1898 break;
1899
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001900 case4(0100):
1901 case4(0110):
1902 case4(0120):
1903 case4(0130):
1904 case4(0200):
1905 case4(0204):
1906 case4(0210):
1907 case4(0214):
1908 case4(0220):
1909 case4(0224):
1910 case4(0230):
1911 case4(0234):
1912 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001913 ea ea_data;
1914 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001915 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001916 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001917 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001918
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001919 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001920 /* pick rfield from operand b (opx) */
1921 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001922 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001923 } else {
1924 /* rfield is constant */
1925 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001926 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001927 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001928
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001929 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001930 rfield, rflags, ins, &errmsg) != eat)
1931 nasm_error(ERR_NONFATAL, "%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07001932
H. Peter Anvine2c80182005-01-15 22:15:51 +00001933 p = bytes;
1934 *p++ = ea_data.modrm;
1935 if (ea_data.sib_present)
1936 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001937 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001938
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001939 /*
1940 * Make sure the address gets the right offset in case
1941 * the line breaks in the .lst file (BR 1197827)
1942 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001943
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001944 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001945 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001946 if (ea_data.disp8) {
1947 out_rawbyte(data, ea_data.disp8);
1948 } else if (ea_data.rip) {
1949 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001950 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001951 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001952
H. Peter Anvina77692b2016-09-20 14:04:33 -07001953 if (overflow_general(opy->offset, asize) ||
1954 signed_bits(opy->offset, ins->addr_size) !=
1955 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001956 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001957
H. Peter Anvina77692b2016-09-20 14:04:33 -07001958 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001959 (asize > ea_data.bytes)
1960 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001961 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001962 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001963 }
1964 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001965
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001966 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001967 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001968 ": instruction code \\%o (0x%02X) given", c, c);
1969 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001971 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001972}
1973
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001974static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001975{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001976 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001977 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001978 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001979}
1980
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001981static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001982{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001983 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001984 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001985 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001986}
1987
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001988static int op_rexflags(const operand * o, int mask)
1989{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001990 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001991 int val;
1992
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001993 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001994 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001995
H. Peter Anvina4835d42008-05-20 14:21:29 -07001996 flags = nasm_reg_flags[o->basereg];
1997 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001998
1999 return rexflags(val, flags, mask);
2000}
2001
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002002static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002003{
2004 int rex = 0;
2005
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002006 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002007 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002008 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002009 rex |= REX_W;
2010 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2011 rex |= REX_H;
2012 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2013 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002014
2015 return rex & mask;
2016}
2017
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002018static int evexflags(int val, decoflags_t deco,
2019 int mask, uint8_t byte)
2020{
2021 int evex = 0;
2022
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002023 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002024 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002025 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002026 evex |= (EVEX_P0RP | EVEX_P0X);
2027 break;
2028 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002029 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002030 evex |= EVEX_P2VP;
2031 if (deco & Z)
2032 evex |= EVEX_P2Z;
2033 if (deco & OPMASK_MASK)
2034 evex |= deco & EVEX_P2AAA;
2035 break;
2036 }
2037 return evex & mask;
2038}
2039
2040static int op_evexflags(const operand * o, int mask, uint8_t byte)
2041{
2042 int val;
2043
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002044 val = nasm_regvals[o->basereg];
2045
2046 return evexflags(val, o->decoflags, mask, byte);
2047}
2048
H. Peter Anvin23595f52009-07-25 17:44:25 -07002049static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002050 insn *instruction,
2051 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002052{
2053 const struct itemplate *temp;
2054 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002055 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002056 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002057 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002058 int i;
2059
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002060 /* broadcasting uses a different data element size */
2061 for (i = 0; i < instruction->operands; i++)
2062 if (i == broadcast)
2063 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2064 else
2065 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002066
2067 merr = MERR_INVALOP;
2068
2069 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002070 temp->opcode != I_none; temp++) {
2071 m = matches(temp, instruction, bits);
2072 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002073 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002074 m = MOK_GOOD;
2075 else
2076 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002077 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002078 /*
2079 * Missing operand size and a candidate for fuzzy matching...
2080 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002081 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002082 if (i == broadcast)
2083 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2084 else
2085 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002086 opsizemissing = true;
2087 }
2088 if (m > merr)
2089 merr = m;
2090 if (merr == MOK_GOOD)
2091 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002092 }
2093
2094 /* No match, but see if we can get a fuzzy operand size match... */
2095 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002096 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002097
2098 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002099 /*
2100 * We ignore extrinsic operand sizes on registers, so we should
2101 * never try to fuzzy-match on them. This also resolves the case
2102 * when we have e.g. "xmmrm128" in two different positions.
2103 */
2104 if (is_class(REGISTER, instruction->oprs[i].type))
2105 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002106
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002107 /* This tests if xsizeflags[i] has more than one bit set */
2108 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2109 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002110
Jin Kyu Song7903c072013-10-30 03:00:12 -07002111 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002112 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002113 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2114 BITS32 : BITS64);
2115 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002116 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002117 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002118 }
2119
2120 /* Try matching again... */
2121 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002122 temp->opcode != I_none; temp++) {
2123 m = matches(temp, instruction, bits);
2124 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002125 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002126 m = MOK_GOOD;
2127 else
2128 m = MERR_INVALOP;
2129 }
2130 if (m > merr)
2131 merr = m;
2132 if (merr == MOK_GOOD)
2133 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002134 }
2135
H. Peter Anvina81655b2009-07-25 18:15:28 -07002136done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002137 *tempp = temp;
2138 return merr;
2139}
2140
Mark Charneydcaef4b2014-10-09 13:45:17 -04002141static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2142{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002143 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002144 uint8_t brcast_num;
2145
Mark Charneydcaef4b2014-10-09 13:45:17 -04002146 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002147 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002148 "size of broadcasting element is greater than 64 bits");
2149
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002150 /*
2151 * The shift term is to take care of the extra BITS80 inserted
2152 * between BITS64 and BITS128.
2153 */
2154 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2155 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002156
2157 return brcast_num;
2158}
2159
H. Peter Anvin65289e82009-07-25 17:25:11 -07002160static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002161 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002162{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002163 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002164 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002165 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002166
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002167 /*
2168 * Check the opcode
2169 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002170 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002171 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002172
2173 /*
2174 * Count the operands
2175 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002176 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002177 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002178
2179 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002180 * Is it legal?
2181 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002182 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002183 return MERR_INVALOP;
2184
2185 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002186 * {evex} available?
2187 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002188 switch (instruction->prefixes[PPS_VEX]) {
2189 case P_EVEX:
2190 if (!itemp_has(itemp, IF_EVEX))
2191 return MERR_ENCMISMATCH;
2192 break;
2193 case P_VEX3:
2194 case P_VEX2:
2195 if (!itemp_has(itemp, IF_VEX))
2196 return MERR_ENCMISMATCH;
2197 break;
2198 default:
2199 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002200 }
2201
2202 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002203 * Check that no spurious colons or TOs are present
2204 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002205 for (i = 0; i < itemp->operands; i++)
2206 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002207 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002208
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002209 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002210 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002211 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002212 switch (itemp_smask(itemp)) {
2213 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002214 asize = BITS8;
2215 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002216 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002217 asize = BITS16;
2218 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002219 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002220 asize = BITS32;
2221 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002222 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002223 asize = BITS64;
2224 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002225 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002226 asize = BITS128;
2227 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002228 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002229 asize = BITS256;
2230 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002231 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002232 asize = BITS512;
2233 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002234 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002235 switch (bits) {
2236 case 16:
2237 asize = BITS16;
2238 break;
2239 case 32:
2240 asize = BITS32;
2241 break;
2242 case 64:
2243 asize = BITS64;
2244 break;
2245 default:
2246 asize = 0;
2247 break;
2248 }
2249 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002250 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002251 asize = 0;
2252 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002253 }
2254
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002255 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002256 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002257 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002258 memset(size, 0, sizeof size);
2259 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002260 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002261 /* S- flags apply to all operands */
2262 for (i = 0; i < MAX_OPERANDS; i++)
2263 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002264 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002265
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002266 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002267 * Check that the operand flags all match up,
2268 * it's a bit tricky so lets be verbose:
2269 *
2270 * 1) Find out the size of operand. If instruction
2271 * doesn't have one specified -- we're trying to
2272 * guess it either from template (IF_S* flag) or
2273 * from code bits.
2274 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002275 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002276 * template has an operand size specified AND this size differ
2277 * from which instruction has (perhaps we got it from code bits)
2278 * we are:
2279 * a) Check that only size of instruction and operand is differ
2280 * other characteristics do match
2281 * b) Perhaps it's a register specified in instruction so
2282 * for such a case we just mark that operand as "size
2283 * missing" and this will turn on fuzzy operand size
2284 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002285 */
2286 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002287 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002288 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002289 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002290 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002291 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002292 opflags_t template_opsize, insn_opsize;
2293
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002294 if (!(type & SIZE_MASK))
2295 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002296
Jin Kyu Song7903c072013-10-30 03:00:12 -07002297 insn_opsize = type & SIZE_MASK;
2298 if (!is_broadcast) {
2299 template_opsize = itemp->opd[i] & SIZE_MASK;
2300 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002301 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2302
2303 if (~ideco & BRDCAST_MASK)
2304 return MERR_BRNOTHERE;
2305
Jin Kyu Song7903c072013-10-30 03:00:12 -07002306 /*
2307 * when broadcasting, the element size depends on
2308 * the instruction type. decorator flag should match.
2309 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002310 if (deco_brsize) {
2311 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002312 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002313 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002314 } else {
2315 template_opsize = 0;
2316 }
2317 }
2318
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002319 if (~ideco & deco & OPMASK_MASK)
2320 return MERR_MASKNOTHERE;
2321
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002322 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2323 return MERR_DECONOTHERE;
2324
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002325 if (itemp->opd[i] & ~type & ~SIZE_MASK) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002326 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002327 } else if (template_opsize) {
2328 if (template_opsize != insn_opsize) {
2329 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002330 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002331 } else if (!is_class(REGISTER, type)) {
2332 /*
2333 * Note: we don't honor extrinsic operand sizes for registers,
2334 * so "missing operand size" for a register should be
2335 * considered a wildcard match rather than an error.
2336 */
2337 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002338 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002339 } else if (is_broadcast &&
2340 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002341 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002342 /*
2343 * broadcasting opsize matches but the number of repeated memory
2344 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002345 * if 64b double precision float is broadcasted to ymm (256b),
2346 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002347 */
2348 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002349 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002350 }
2351 }
2352
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002353 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002354 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002355
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002356 /*
2357 * Check operand sizes
2358 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002359 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2360 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002361 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002362 asize = itemp->opd[i] & SIZE_MASK;
2363 if (asize) {
2364 for (i = 0; i < oprs; i++)
2365 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 break;
2367 }
2368 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002369 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002371 }
2372
Keith Kaniosb7a89542007-04-12 02:40:54 +00002373 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 if (!(itemp->opd[i] & SIZE_MASK) &&
2375 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002376 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002377 }
2378
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002379 /*
2380 * Check template is okay at the set cpu level
2381 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002382 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002383 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002384
Keith Kaniosb7a89542007-04-12 02:40:54 +00002385 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002386 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002387 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002388 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002389 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002391 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002392 * If we have a HLE prefix, look for the NOHLE flag
2393 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002394 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002395 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2396 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2397 return MERR_BADHLE;
2398
2399 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002400 * Check if special handling needed for Jumps
2401 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002402 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002403 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404
Jin Kyu Song03041092013-10-15 19:38:51 -07002405 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002406 * Check if BND prefix is allowed.
2407 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002408 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002409 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002410 (has_prefix(instruction, PPS_REP, P_BND) ||
2411 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002412 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002413 else if (itemp_has(itemp, IF_BND) &&
2414 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2415 has_prefix(instruction, PPS_REP, P_REPNZ)))
2416 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002417
H. Peter Anvin60926242009-07-26 16:25:38 -07002418 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002419}
2420
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002421/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002422 * Check if ModR/M.mod should/can be 01.
2423 * - EAF_BYTEOFFS is set
2424 * - offset can fit in a byte when EVEX is not used
2425 * - offset can be compressed when EVEX is used
2426 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002427#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2428 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2429 is_disp8n(input, ins, &output->disp8) : \
2430 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2431 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002432
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002433static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002434 int rfield, opflags_t rflags, insn *ins,
2435 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002436{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002437 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002438 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002439 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002440
H. Peter Anvin8f622462017-04-02 19:02:29 -07002441 *errmsg = "invalid effective address"; /* Default error message */
2442
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002443 output->type = EA_SCALAR;
2444 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002445 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002446
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002447 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002448 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002449 /* EVEX.R' flag for the REG operand */
2450 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002451
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002452 if (is_class(REGISTER, input->type)) {
2453 /*
2454 * It's a direct register.
2455 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002456 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002457 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002458
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002459 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002460 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002461
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002462 /* broadcasting is not available with a direct register operand. */
2463 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002464 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002465 goto err;
2466 }
2467
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002468 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002469 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002470 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002471 output->bytes = 0; /* no offset necessary either */
2472 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2473 } else {
2474 /*
2475 * It's a memory reference.
2476 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002477
2478 /* Embedded rounding or SAE is not available with a mem ref operand. */
2479 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002480 *errmsg = "embedded rounding is available only with "
2481 "register-register operations";
2482 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002483 }
2484
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002485 if (input->basereg == -1 &&
2486 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002487 /*
2488 * It's a pure offset.
2489 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002490 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002491 if (input->segment == NO_SEG ||
2492 (input->opflags & OPFLAG_RELATIVE)) {
2493 nasm_error(ERR_WARNING | ERR_PASS2,
2494 "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002495 input->type &= ~IP_REL;
2496 input->type |= MEMORY;
2497 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002498 }
2499
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002500 if (bits == 64 &&
2501 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002502 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002503 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002504 }
2505
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002506 if (eaflags & EAF_BYTEOFFS ||
2507 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002508 input->disp_size != (addrbits != 16 ? 32 : 16))) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002509 nasm_error(ERR_WARNING | ERR_PASS1,
2510 "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002511 }
2512
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002513 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002514 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002515 output->sib = GEN_SIB(0, 4, 5);
2516 output->bytes = 4;
2517 output->modrm = GEN_MODRM(0, rfield, 4);
2518 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002519 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002520 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002521 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002522 output->modrm = GEN_MODRM(0, rfield,
2523 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002524 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002525 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002526 } else {
2527 /*
2528 * It's an indirection.
2529 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002531 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002533 int t, it, bt; /* register numbers */
2534 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002535
H. Peter Anvine2c80182005-01-15 22:15:51 +00002536 if (s == 0)
2537 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002538
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002539 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002540 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002541 ix = nasm_reg_flags[i];
2542 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002543 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002544 ix = 0;
2545 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002546
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002547 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002548 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002549 bx = nasm_reg_flags[b];
2550 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002551 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002552 bx = 0;
2553 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002554
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002555 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002556 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002557 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002558 int32_t o = input->offset;
2559 int mod, scale, index, base;
2560
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002561 /*
2562 * For a vector SIB, one has to be a vector and the other,
2563 * if present, a GPR. The vector must be the index operand.
2564 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002565 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002566 if (s == 0)
2567 s = 1;
2568 else if (s != 1)
2569 goto err;
2570
2571 t = bt, bt = it, it = t;
2572 x = bx, bx = ix, ix = x;
2573 }
2574
2575 if (bt != -1) {
2576 if (REG_GPR & ~bx)
2577 goto err;
2578 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2579 sok &= bx;
2580 else
2581 goto err;
2582 }
2583
2584 /*
2585 * While we're here, ensure the user didn't specify
2586 * WORD or QWORD
2587 */
2588 if (input->disp_size == 16 || input->disp_size == 64)
2589 goto err;
2590
2591 if (addrbits == 16 ||
2592 (addrbits == 32 && !(sok & BITS32)) ||
2593 (addrbits == 64 && !(sok & BITS64)))
2594 goto err;
2595
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002596 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2597 : ((ix & YMMREG & ~REG_EA)
2598 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002599
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002600 output->rex |= rexflags(it, ix, REX_X);
2601 output->rex |= rexflags(bt, bx, REX_B);
2602 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002603
2604 index = it & 7; /* it is known to be != -1 */
2605
2606 switch (s) {
2607 case 1:
2608 scale = 0;
2609 break;
2610 case 2:
2611 scale = 1;
2612 break;
2613 case 4:
2614 scale = 2;
2615 break;
2616 case 8:
2617 scale = 3;
2618 break;
2619 default: /* then what the smeg is it? */
2620 goto err; /* panic */
2621 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002622
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002623 if (bt == -1) {
2624 base = 5;
2625 mod = 0;
2626 } else {
2627 base = (bt & 7);
2628 if (base != REG_NUM_EBP && o == 0 &&
2629 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002630 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002631 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002632 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002633 mod = 1;
2634 else
2635 mod = 2;
2636 }
2637
2638 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002639 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2640 output->modrm = GEN_MODRM(mod, rfield, 4);
2641 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002642 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002643 /*
2644 * it must be a 32/64-bit memory reference. Firstly we have
2645 * to check that all registers involved are type E/Rxx.
2646 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002647 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002648 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002649
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002650 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002651 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2652 sok &= ix;
2653 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002654 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002655 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002656
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002657 if (bt != -1) {
2658 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002659 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002660 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002661 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002662 sok &= bx;
2663 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002664
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002665 /*
2666 * While we're here, ensure the user didn't specify
2667 * WORD or QWORD
2668 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002669 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002670 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002671
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002672 if (addrbits == 16 ||
2673 (addrbits == 32 && !(sok & BITS32)) ||
2674 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002675 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002676
Keith Kaniosb7a89542007-04-12 02:40:54 +00002677 /* now reorganize base/index */
2678 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002679 ((hb == b && ht == EAH_NOTBASE) ||
2680 (hb == i && ht == EAH_MAKEBASE))) {
2681 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002682 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002683 x = bx, bx = ix, ix = x;
2684 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002685
Jin Kyu Song164d6072013-10-15 19:10:13 -07002686 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002687 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002688 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002689 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002690 if (eaflags & EAF_MIB) {
2691 /* only for mib operands */
2692 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2693 /*
2694 * make a single reg index [reg*1].
2695 * gas uses this form for an explicit index register.
2696 */
2697 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2698 }
2699 if ((ht == EAH_SUMMED) && bt == -1) {
2700 /* separate once summed index into [base, index] */
2701 bt = it, bx = ix, s--;
2702 }
2703 } else {
2704 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002705 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002706 s == 3 || s == 5 || s == 9) && bt == -1) {
2707 /* convert 3*EAX to EAX+2*EAX */
2708 bt = it, bx = ix, s--;
2709 }
2710 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002711 (eaflags & EAF_TIMESTWO) &&
2712 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002713 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002714 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002715 * to sib format with 0x0 displacement - [EAX*1+0].
2716 */
2717 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2718 }
2719 }
Keith Kanios48af1772007-08-17 07:37:52 +00002720 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002721 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002722 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002723 x = ix, ix = bx, bx = x;
2724 }
2725 if (it == REG_NUM_ESP ||
2726 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002727 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002728
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002729 output->rex |= rexflags(it, ix, REX_X);
2730 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002731
Keith Kanios48af1772007-08-17 07:37:52 +00002732 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002733 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002735
Keith Kaniosb7a89542007-04-12 02:40:54 +00002736 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002739 } else {
2740 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002741 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002742 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002743 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002744 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002745 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002746 mod = 1;
2747 else
2748 mod = 2;
2749 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002750
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002751 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002752 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2753 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002754 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002755 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002757
Keith Kaniosb7a89542007-04-12 02:40:54 +00002758 if (it == -1)
2759 index = 4, s = 1;
2760 else
2761 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002762
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 switch (s) {
2764 case 1:
2765 scale = 0;
2766 break;
2767 case 2:
2768 scale = 1;
2769 break;
2770 case 4:
2771 scale = 2;
2772 break;
2773 case 8:
2774 scale = 3;
2775 break;
2776 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002777 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002778 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002779
Keith Kaniosb7a89542007-04-12 02:40:54 +00002780 if (bt == -1) {
2781 base = 5;
2782 mod = 0;
2783 } else {
2784 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002785 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002786 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002787 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002788 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002789 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002790 mod = 1;
2791 else
2792 mod = 2;
2793 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002794
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002795 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002796 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2797 output->modrm = GEN_MODRM(mod, rfield, 4);
2798 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 }
2800 } else { /* it's 16-bit */
2801 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002802 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002803
Keith Kaniosb7a89542007-04-12 02:40:54 +00002804 /* check for 64-bit long mode */
2805 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002806 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002807
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002809 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2810 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002811 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002812
Keith Kaniosb7a89542007-04-12 02:40:54 +00002813 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002814 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002815 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002816
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002818 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002819 if (b == -1 && i != -1) {
2820 int tmp = b;
2821 b = i;
2822 i = tmp;
2823 } /* swap */
2824 if ((b == R_SI || b == R_DI) && i != -1) {
2825 int tmp = b;
2826 b = i;
2827 i = tmp;
2828 }
2829 /* have BX/BP as base, SI/DI index */
2830 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002831 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002832 if (i != -1 && b != -1 &&
2833 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002834 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002835 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002836 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002837
H. Peter Anvine2c80182005-01-15 22:15:51 +00002838 rm = -1;
2839 if (i != -1)
2840 switch (i * 256 + b) {
2841 case R_SI * 256 + R_BX:
2842 rm = 0;
2843 break;
2844 case R_DI * 256 + R_BX:
2845 rm = 1;
2846 break;
2847 case R_SI * 256 + R_BP:
2848 rm = 2;
2849 break;
2850 case R_DI * 256 + R_BP:
2851 rm = 3;
2852 break;
2853 } else
2854 switch (b) {
2855 case R_SI:
2856 rm = 4;
2857 break;
2858 case R_DI:
2859 rm = 5;
2860 break;
2861 case R_BP:
2862 rm = 6;
2863 break;
2864 case R_BX:
2865 rm = 7;
2866 break;
2867 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002868 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002869 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002870
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002871 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002872 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002874 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 mod = 1;
2876 else
2877 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002878
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002879 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002880 output->bytes = mod; /* bytes of offset needed */
2881 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002882 }
2883 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002884 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002885
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002886 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002887 return output->type;
2888
2889err:
2890 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002891}
2892
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002893static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002894{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002895 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002896 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002897
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002898 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002899
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002900 switch (ins->prefixes[PPS_ASIZE]) {
2901 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002902 valid &= 16;
2903 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002904 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 valid &= 32;
2906 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002907 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002908 valid &= 64;
2909 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002910 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 valid &= (addrbits == 32) ? 16 : 32;
2912 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002913 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002914 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002915 }
2916
2917 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002918 if (is_class(MEMORY, ins->oprs[j].type)) {
2919 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002920
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002921 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002922 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002923 i = 0;
2924 else
2925 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002926
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002927 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002928 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002929 b = 0;
2930 else
2931 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002932
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002933 if (ins->oprs[j].scale == 0)
2934 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002935
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002936 if (!i && !b) {
2937 int ds = ins->oprs[j].disp_size;
2938 if ((addrbits != 64 && ds > 8) ||
2939 (addrbits == 64 && ds == 16))
2940 valid &= ds;
2941 } else {
2942 if (!(REG16 & ~b))
2943 valid &= 16;
2944 if (!(REG32 & ~b))
2945 valid &= 32;
2946 if (!(REG64 & ~b))
2947 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002948
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002949 if (!(REG16 & ~i))
2950 valid &= 16;
2951 if (!(REG32 & ~i))
2952 valid &= 32;
2953 if (!(REG64 & ~i))
2954 valid &= 64;
2955 }
2956 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002957 }
2958
2959 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002960 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002961 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002962 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002963 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002964 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002965 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002966 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002967 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002968 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002969 }
2970
2971 defdisp = ins->addr_size == 16 ? 16 : 32;
2972
2973 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002974 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2975 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2976 /*
2977 * mem_offs sizes must match the address size; if not,
2978 * strip the MEM_OFFS bit and match only EA instructions
2979 */
2980 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2981 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002982 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002983}