blob: 3bfccec0915c8a1f70aff4beeef08534f105a98a [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 int asize;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800339 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700340 union {
341 uint8_t b[8];
342 uint64_t q;
343 } xdata;
344 uint64_t size = data->size;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800345 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800346 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000347
H. Peter Anvina77692b2016-09-20 14:04:33 -0700348 if (!data->size)
349 return; /* Nothing to do */
350
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700351 /*
352 * Convert addresses to RAWDATA if possible
353 * XXX: not all backends want this for global symbols!!!!
354 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700355 switch (data->type) {
356 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800357 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800358 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800359 goto address;
360
361 case OUT_RELADDR:
362 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800363 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800364 goto address;
365
366 address:
H. Peter Anvina77692b2016-09-20 14:04:33 -0700367 asize = data->size;
368 nasm_assert(asize <= 8);
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800369 if (data->tsegment == fixseg && data->twrt == NO_SEG) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700370 uint8_t *q = xdata.b;
371
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800372 warn_overflow_out(addrval, asize, data->sign);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700373
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800374 WRITEADDR(q, addrval, asize);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700375 data->data = xdata.b;
376 data->type = OUT_RAWDATA;
377 asize = 0; /* No longer an address */
378 }
379 break;
380
381 default:
382 asize = 0; /* Not an address */
383 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000384 }
385
H. Peter Anvina77692b2016-09-20 14:04:33 -0700386 lfmt->output(data);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800387
Frank Kotlerabebb082003-09-06 04:45:37 +0000388 /*
389 * this call to src_get determines when we call the
390 * debug-format-specific "linenum" function
391 * it updates lineno and lnfname to the current values
392 * returning 0 if "same as last time", -2 if lnfname
393 * changed, and the amount by which lineno changed,
394 * if it did. thus, these variables must be static
395 */
396
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400397 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700398 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000399
H. Peter Anvinb6412502016-02-11 21:07:40 -0800400 if (asize && asize > amax) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700401 if (data->type != OUT_ADDRESS || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800402 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800403 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800404 asize << 3, ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800405 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800406 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800407 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800408 asize << 3, ofmt->maxbits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700409 data->size = amax;
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700410 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700411 data->insoffs += amax;
412 data->offset += amax;
413 data->size = size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800414 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700415 data->data = zero_buffer;
416 data->type = OUT_RAWDATA;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800417 }
418
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700419 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700420 data->offset += size;
421 data->insoffs += size;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000422}
423
H. Peter Anvina77692b2016-09-20 14:04:33 -0700424static inline void out_rawdata(struct out_data *data, const void *rawdata,
425 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400426{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700427 data->type = OUT_RAWDATA;
428 data->data = rawdata;
429 data->size = size;
430 out(data);
431}
432
433static void out_rawbyte(struct out_data *data, uint8_t byte)
434{
435 data->type = OUT_RAWDATA;
436 data->data = &byte;
437 data->size = 1;
438 out(data);
439}
440
441static inline void out_reserve(struct out_data *data, uint64_t size)
442{
443 data->type = OUT_RESERVE;
444 data->size = size;
445 out(data);
446}
447
H. Peter Anvin164d2462017-02-20 02:39:56 -0800448static inline void out_imm(struct out_data *data, const struct operand *opx,
H. Peter Anvina77692b2016-09-20 14:04:33 -0700449 int size, enum out_sign sign)
450{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800451 data->type =
452 (opx->opflags & OPFLAG_RELATIVE) ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700453 data->sign = sign;
454 data->size = size;
455 data->toffset = opx->offset;
456 data->tsegment = opx->segment;
457 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800458 /*
459 * XXX: improve this if at some point in the future we can
460 * distinguish the subtrahend in expressions like [foo - bar]
461 * where bar is a symbol in the current segment. However, at the
462 * current point, if OPFLAG_RELATIVE is set that subtraction has
463 * already occurred.
464 */
465 data->relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700466 out(data);
467}
468
H. Peter Anvin164d2462017-02-20 02:39:56 -0800469static void out_reladdr(struct out_data *data, const struct operand *opx,
470 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700471{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800472 if (opx->opflags & OPFLAG_RELATIVE)
473 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
474
H. Peter Anvina77692b2016-09-20 14:04:33 -0700475 data->type = OUT_RELADDR;
476 data->sign = OUT_SIGNED;
477 data->size = size;
478 data->toffset = opx->offset;
479 data->tsegment = opx->segment;
480 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800481 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700482 out(data);
483}
484
H. Peter Anvin164d2462017-02-20 02:39:56 -0800485static inline void out_segment(struct out_data *data,
486 const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700487{
488 data->type = OUT_SEGMENT;
489 data->sign = OUT_UNSIGNED;
490 data->size = 2;
H. Peter Anvin217e7142017-05-01 15:10:47 -0700491 data->toffset = opx->offset; /* Is this really needed/wanted? */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700492 data->tsegment = ofmt->segbase(opx->segment + 1);
493 data->twrt = opx->wrt;
494 out(data);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400495}
496
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700497static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800498 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000499{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800500 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800501 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000502 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800503 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000504
H. Peter Anvin755f5212012-02-25 11:41:34 -0800505 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700506 return false;
507 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400508 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700509 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400510 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700511
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800512 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100513
Victor van den Elzen154e5922009-02-25 17:32:00 +0100514 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100515 /* Be optimistic in pass 1 */
516 return true;
517
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700519 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000520
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700521 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800522 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
523
524 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
525 /* jmp short (opcode eb) cannot be used with bnd prefix. */
526 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800527 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800528 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800529 }
530
531 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000532}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000533
H. Peter Anvin04445362016-09-21 15:56:19 -0700534/* This is totally just a wild guess what is reasonable... */
535#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
536
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800537int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000538{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700539 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000540 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700541 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300542 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000543
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700544 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700545 data.offset = start;
546 data.segment = segment;
547 data.itemp = NULL;
548 data.sign = OUT_WRAP;
549 data.bits = bits;
550
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300551 wsize = idata_bytes(instruction->opcode);
552 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000553 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000554
H. Peter Anvineba20a72002-04-30 20:53:55 +0000555 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700557
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700558 list_for_each(e, instruction->eops) {
559 if (e->type == EOT_DB_NUMBER) {
560 if (wsize > 8) {
561 nasm_error(ERR_NONFATAL,
562 "integer supplied to a DT, DO, DY or DZ"
563 " instruction");
564 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700565 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700566 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
567 data.inslen = data.size = wsize;
568 data.toffset = e->offset;
569 data.tsegment = e->segment;
570 data.twrt = e->wrt;
571 data.relbase = 0;
572 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000573 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700574 } else if (e->type == EOT_DB_STRING ||
575 e->type == EOT_DB_STRING_FREE) {
576 int align = e->stringlen % wsize;
577 if (align)
578 align = wsize - align;
579
580 data.insoffs = 0;
581 data.inslen = e->stringlen + align;
582
583 out_rawdata(&data, e->stringval, e->stringlen);
584 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700585 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700587 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700588 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000589 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700590 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700591 off_t base = 0;
592 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700593 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700594 char *buf = NULL;
595 size_t blk = 0; /* Buffered I/O block size */
596 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000597
H. Peter Anvin94ead272017-09-27 15:22:23 -0700598 if (!t)
599 goto done;
600
H. Peter Anvind81a2352016-09-21 14:03:18 -0700601 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400602 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800603 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000604 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700605 goto done;
606 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000607
H. Peter Anvind81a2352016-09-21 14:03:18 -0700608 len = nasm_file_size(fp);
609
610 if (len == (off_t)-1) {
611 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700612 fname);
613 goto close_done;
614 }
615
H. Peter Anvina77692b2016-09-20 14:04:33 -0700616 if (instruction->eops->next) {
617 base = instruction->eops->next->offset;
618 if (base >= len) {
619 len = 0;
620 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 len -= base;
622 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700623 len > (off_t)instruction->eops->next->next->offset)
624 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000625 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700627
H. Peter Anvina77692b2016-09-20 14:04:33 -0700628 lfmt->set_offset(data.offset);
629 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000630
H. Peter Anvind81a2352016-09-21 14:03:18 -0700631 if (!len)
632 goto end_incbin;
633
634 /* Try to map file data */
635 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700636 if (!map) {
637 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
638 buf = nasm_malloc(blk);
639 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700640
641 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700642 /*
643 * Consider these irrelevant for INCBIN, since it is fully
644 * possible that these might be (way) bigger than an int
645 * can hold; there is, however, no reason to widen these
646 * types just for INCBIN. data.inslen == 0 signals to the
647 * backend that these fields are meaningless, if at all
648 * needed.
649 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700650 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700651 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700652
H. Peter Anvind81a2352016-09-21 14:03:18 -0700653 if (map) {
654 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700655 } else if ((off_t)m == len) {
656 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700657 } else {
658 off_t l = len;
659
660 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700661 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700662 "`incbin': unable to seek on file `%s'",
663 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700664 goto end_incbin;
665 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700666 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700667 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700668 if (!m || feof(fp)) {
669 /*
670 * This shouldn't happen unless the file
671 * actually changes while we are reading
672 * it.
673 */
674 nasm_error(ERR_NONFATAL,
675 "`incbin': unexpected EOF while"
676 " reading file `%s'", fname);
677 goto end_incbin;
678 }
679 out_rawdata(&data, buf, m);
680 l -= m;
681 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700682 }
683 }
684 end_incbin:
685 lfmt->downlevel(LIST_INCBIN);
686 if (instruction->times > 1) {
H. Peter Anvin5810c592017-05-01 19:51:09 -0700687 lfmt->set_offset(start);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700688 lfmt->uplevel(LIST_TIMES);
689 lfmt->downlevel(LIST_TIMES);
690 }
691 if (ferror(fp)) {
692 nasm_error(ERR_NONFATAL,
693 "`incbin': error while"
694 " reading file `%s'", fname);
695 }
696 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700697 if (buf)
698 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700699 if (map)
700 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700701 fclose(fp);
702 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700703 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700704 ;
705 } else {
706 /* "Real" instruction */
707
708 /* Check to see if we need an address-size prefix */
709 add_asp(instruction, bits);
710
711 m = find_match(&temp, instruction, data.segment, data.offset, bits);
712
713 if (m == MOK_GOOD) {
714 /* Matches! */
715 int64_t insn_size = calcsize(data.segment, data.offset,
716 bits, instruction, temp);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700717 nasm_assert(insn_size >= 0);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700718
719 data.itemp = temp;
720 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700721 data.insoffs = 0;
722 data.inslen = insn_size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700723
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700724 gencode(&data, instruction);
725 nasm_assert(data.insoffs == insn_size);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700726 } else {
727 /* No match */
728 switch (m) {
729 case MERR_OPSIZEMISSING:
730 nasm_error(ERR_NONFATAL, "operation size not specified");
731 break;
732 case MERR_OPSIZEMISMATCH:
733 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
734 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700735 case MERR_BRNOTHERE:
736 nasm_error(ERR_NONFATAL,
737 "broadcast not permitted on this operand");
738 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700739 case MERR_BRNUMMISMATCH:
740 nasm_error(ERR_NONFATAL,
741 "mismatch in the number of broadcasting elements");
742 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700743 case MERR_MASKNOTHERE:
744 nasm_error(ERR_NONFATAL,
745 "mask not permitted on this operand");
746 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700747 case MERR_DECONOTHERE:
748 nasm_error(ERR_NONFATAL, "unsupported mode decorator for instruction");
749 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700750 case MERR_BADCPU:
751 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
752 break;
753 case MERR_BADMODE:
754 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
755 bits);
756 break;
757 case MERR_ENCMISMATCH:
758 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
759 break;
760 case MERR_BADBND:
761 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
762 break;
763 case MERR_BADREPNE:
764 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
765 (has_prefix(instruction, PPS_REP, P_REPNE) ?
766 "repne" : "repnz"));
767 break;
768 default:
769 nasm_error(ERR_NONFATAL,
770 "invalid combination of opcode and operands");
771 break;
772 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700773
774 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400775 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000776 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700777 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000778}
779
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800780int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000781{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000782 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700783 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000784
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400785 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000786 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000787
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700788 if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000789 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300790 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000791
H. Peter Anvine2c80182005-01-15 22:15:51 +0000792 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300793 wsize = idata_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700794 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000795
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400796 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000797 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000798
H. Peter Anvine2c80182005-01-15 22:15:51 +0000799 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400800 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000801 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400802 warn_overflow_const(e->offset, wsize);
803 } else if (e->type == EOT_DB_STRING ||
804 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000805 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000806
H. Peter Anvine2c80182005-01-15 22:15:51 +0000807 align = (-osize) % wsize;
808 if (align < 0)
809 align += wsize;
810 isize += osize + align;
811 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700812 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000813 }
814
H. Peter Anvine2c80182005-01-15 22:15:51 +0000815 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400816 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700817 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000818
H. Peter Anvind81a2352016-09-21 14:03:18 -0700819 len = nasm_file_size_by_path(fname);
820 if (len == (off_t)-1) {
821 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
822 fname);
823 return 0;
824 }
825
826 if (instruction->eops->next) {
827 if (len <= (off_t)instruction->eops->next->offset) {
828 len = 0;
829 } else {
830 len -= instruction->eops->next->offset;
831 if (instruction->eops->next->next &&
832 len > (off_t)instruction->eops->next->next->offset) {
833 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 }
835 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700837
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700838 len *= instruction->times;
839 instruction->times = 1; /* Tell the upper layer to not iterate */
840
H. Peter Anvind81a2352016-09-21 14:03:18 -0700841 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000842 }
843
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700844 /* Check to see if we need an address-size prefix */
845 add_asp(instruction, bits);
846
H. Peter Anvin23595f52009-07-25 17:44:25 -0700847 m = find_match(&temp, instruction, segment, offset, bits);
848 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400849 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700850 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700851 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400852 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000853 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000854}
855
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800856static void bad_hle_warn(const insn * ins, uint8_t hleok)
857{
858 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800859 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800860 static const enum whatwarn warn[2][4] =
861 {
862 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
863 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
864 };
865 unsigned int n;
866
867 n = (unsigned int)rep_pfx - P_XACQUIRE;
868 if (n > 1)
869 return; /* Not XACQUIRE/XRELEASE */
870
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800871 ww = warn[n][hleok];
872 if (!is_class(MEMORY, ins->oprs[0].type))
873 ww = w_inval; /* HLE requires operand 0 to be memory */
874
875 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800876 case w_none:
877 break;
878
879 case w_lock:
880 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800881 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800882 "%s with this instruction requires lock",
883 prefix_name(rep_pfx));
884 }
885 break;
886
887 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800888 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800889 "%s invalid with this instruction",
890 prefix_name(rep_pfx));
891 break;
892 }
893}
894
H. Peter Anvin507ae032008-10-09 15:37:10 -0700895/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400896#define case3(x) case (x): case (x)+1: case (x)+2
897#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700898
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800899static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800900 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000901{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800902 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800903 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000904 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000905 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700906 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700907 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700908 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700909 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800910 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800911 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700912 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -0700913 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000914
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700915 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700916 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700917 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700918
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700919 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400920 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700921
H. Peter Anvine2c80182005-01-15 22:15:51 +0000922 (void)segment; /* Don't warn that this parameter is unused */
923 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000924
H. Peter Anvin839eca22007-10-29 23:12:47 -0700925 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400926 c = *codes++;
927 op1 = (c & 3) + ((opex & 1) << 2);
928 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
929 opx = &ins->oprs[op1];
930 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700931
H. Peter Anvin839eca22007-10-29 23:12:47 -0700932 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400933 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000934 codes += c, length += c;
935 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700936
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400937 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400938 opex = c;
939 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700940
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400941 case4(010):
942 ins->rex |=
943 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000944 codes++, length++;
945 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700946
Jin Kyu Song164d6072013-10-15 19:10:13 -0700947 case4(014):
948 /* this is an index reg of MIB operand */
949 mib_index = opx->basereg;
950 break;
951
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400952 case4(020):
953 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 length++;
955 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700956
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400957 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000958 length += 2;
959 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700960
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400961 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700962 if (opx->type & (BITS16 | BITS32 | BITS64))
963 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000964 else
965 length += (bits == 16) ? 2 : 4;
966 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700967
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400968 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 length += 4;
970 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700971
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400972 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700973 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700975
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400976 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000977 length++;
978 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700979
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400980 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000981 length += 8; /* MOV reg64/imm */
982 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700983
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400984 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 length += 2;
986 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700987
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400988 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700989 if (opx->type & (BITS16 | BITS32 | BITS64))
990 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000991 else
992 length += (bits == 16) ? 2 : 4;
993 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700994
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400995 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 length += 4;
997 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700998
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400999 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001000 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001002
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001003 case 0172:
1004 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001005 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001006 length++;
1007 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001008
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001009 case4(0174):
1010 length++;
1011 break;
1012
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001013 case4(0240):
1014 ins->rex |= REX_EV;
1015 ins->vexreg = regval(opx);
1016 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1017 ins->vex_cm = *codes++;
1018 ins->vex_wlp = *codes++;
1019 ins->evex_tuple = (*codes++ - 0300);
1020 break;
1021
1022 case 0250:
1023 ins->rex |= REX_EV;
1024 ins->vexreg = 0;
1025 ins->vex_cm = *codes++;
1026 ins->vex_wlp = *codes++;
1027 ins->evex_tuple = (*codes++ - 0300);
1028 break;
1029
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001030 case4(0254):
1031 length += 4;
1032 break;
1033
1034 case4(0260):
1035 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001036 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001037 ins->vex_cm = *codes++;
1038 ins->vex_wlp = *codes++;
1039 break;
1040
1041 case 0270:
1042 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001043 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001044 ins->vex_cm = *codes++;
1045 ins->vex_wlp = *codes++;
1046 break;
1047
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001048 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001049 hleok = c & 3;
1050 break;
1051
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001052 case4(0274):
1053 length++;
1054 break;
1055
1056 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001057 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001058
H. Peter Anvine2c80182005-01-15 22:15:51 +00001059 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001060 if (bits == 64)
1061 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001062 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001063 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001064
H. Peter Anvine2c80182005-01-15 22:15:51 +00001065 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001066 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001067 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001068
H. Peter Anvine2c80182005-01-15 22:15:51 +00001069 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001070 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001071
Keith Kaniosb7a89542007-04-12 02:40:54 +00001072 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001073 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1074 has_prefix(ins, PPS_ASIZE, P_A32))
1075 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001076 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001077
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001078 case4(0314):
1079 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001080
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001082 {
1083 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1084 if (pfx == P_O16)
1085 break;
1086 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001087 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001088 else
1089 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001091 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001092
H. Peter Anvine2c80182005-01-15 22:15:51 +00001093 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001094 {
1095 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1096 if (pfx == P_O32)
1097 break;
1098 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001099 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001100 else
1101 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001103 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001104
H. Peter Anvine2c80182005-01-15 22:15:51 +00001105 case 0322:
1106 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001107
Keith Kaniosb7a89542007-04-12 02:40:54 +00001108 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001109 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001110 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001111
Keith Kaniosb7a89542007-04-12 02:40:54 +00001112 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001113 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001114 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001115
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001116 case 0325:
1117 ins->rex |= REX_NH;
1118 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001119
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001120 case 0326:
1121 break;
1122
H. Peter Anvine2c80182005-01-15 22:15:51 +00001123 case 0330:
1124 codes++, length++;
1125 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001126
H. Peter Anvine2c80182005-01-15 22:15:51 +00001127 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001129
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001130 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001131 case 0333:
1132 length++;
1133 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001134
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001135 case 0334:
1136 ins->rex |= REX_L;
1137 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001138
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001139 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001140 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001141
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001142 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001143 if (!ins->prefixes[PPS_REP])
1144 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001145 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001146
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001147 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001148 if (!ins->prefixes[PPS_REP])
1149 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001150 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001151
H. Peter Anvine2c80182005-01-15 22:15:51 +00001152 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001153 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001154 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001156 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1157 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001158 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001160 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001161 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001162
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001163 case 0341:
1164 if (!ins->prefixes[PPS_WAIT])
1165 ins->prefixes[PPS_WAIT] = P_WAIT;
1166 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001167
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001168 case 0360:
1169 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001170
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001171 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001172 length++;
1173 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001174
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001175 case 0364:
1176 case 0365:
1177 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001178
Keith Kanios48af1772007-08-17 07:37:52 +00001179 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001180 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001181 length++;
1182 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001183
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001184 case 0370:
1185 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001186 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001187
H. Peter Anvine2c80182005-01-15 22:15:51 +00001188 case 0373:
1189 length++;
1190 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001191
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001192 case 0374:
1193 eat = EA_XMMVSIB;
1194 break;
1195
1196 case 0375:
1197 eat = EA_YMMVSIB;
1198 break;
1199
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001200 case 0376:
1201 eat = EA_ZMMVSIB;
1202 break;
1203
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001204 case4(0100):
1205 case4(0110):
1206 case4(0120):
1207 case4(0130):
1208 case4(0200):
1209 case4(0204):
1210 case4(0210):
1211 case4(0214):
1212 case4(0220):
1213 case4(0224):
1214 case4(0230):
1215 case4(0234):
1216 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001217 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001218 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001219 opflags_t rflags;
1220 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001221 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001222
Keith Kaniosb7a89542007-04-12 02:40:54 +00001223 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001224
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001225 if (c <= 0177) {
1226 /* pick rfield from operand b (opx) */
1227 rflags = regflag(opx);
1228 rfield = nasm_regvals[opx->basereg];
1229 } else {
1230 rflags = 0;
1231 rfield = c & 7;
1232 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001233
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001234 /* EVEX.b1 : evex_brerop contains the operand position */
1235 op_er_sae = (ins->evex_brerop >= 0 ?
1236 &ins->oprs[ins->evex_brerop] : NULL);
1237
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001238 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1239 /* set EVEX.b */
1240 ins->evex_p[2] |= EVEX_P2B;
1241 if (op_er_sae->decoflags & ER) {
1242 /* set EVEX.RC (rounding control) */
1243 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1244 & EVEX_P2RC;
1245 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001246 } else {
1247 /* set EVEX.L'L (vector length) */
1248 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001249 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001250 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001251 /* set EVEX.b */
1252 ins->evex_p[2] |= EVEX_P2B;
1253 }
1254 }
1255
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001256 if (itemp_has(temp, IF_MIB)) {
1257 opy->eaflags |= EAF_MIB;
1258 /*
1259 * if a separate form of MIB (ICC style) is used,
1260 * the index reg info is merged into mem operand
1261 */
1262 if (mib_index != R_none) {
1263 opy->indexreg = mib_index;
1264 opy->scale = 1;
1265 opy->hintbase = mib_index;
1266 opy->hinttype = EAH_NOTBASE;
1267 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001268 }
1269
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001270 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001271 rfield, rflags, ins, &errmsg) != eat) {
1272 nasm_error(ERR_NONFATAL, "%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001273 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001274 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001275 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001276 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001277 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001278 }
1279 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001280
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001281 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001282 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001283 ": instruction code \\%o (0x%02X) given", c, c);
1284 break;
1285 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001286 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001287
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001288 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001289
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001290 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001291 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001292 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001293 return -1;
1294 }
1295 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001296 }
1297
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001298 switch (ins->prefixes[PPS_VEX]) {
1299 case P_EVEX:
1300 if (!(ins->rex & REX_EV))
1301 return -1;
1302 break;
1303 case P_VEX3:
1304 case P_VEX2:
1305 if (!(ins->rex & REX_V))
1306 return -1;
1307 break;
1308 default:
1309 break;
1310 }
1311
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001312 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001313 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001314
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001315 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001316 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001317 return -1;
1318 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001319 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001320 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001321 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001322 ins->rex &= ~REX_W;
1323 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001324 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001325 ins->rex |= REX_W;
1326 bad32 &= ~REX_W;
1327 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001328 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001329 /* Follow REX_W */
1330 break;
1331 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001332
H. Peter Anvinfc561202011-07-07 16:58:22 -07001333 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001334 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001335 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001336 } else if (!(ins->rex & REX_EV) &&
1337 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001338 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001339 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001340 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001341 if (ins->rex & REX_EV)
1342 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001343 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1344 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001345 length += 3;
1346 else
1347 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001348 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001349 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001350 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001351 return -1;
1352 } else if (bits == 64) {
1353 length++;
1354 } else if ((ins->rex & REX_L) &&
1355 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001356 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001357 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001358 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001359 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001360 length++;
1361 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001362 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001363 return -1;
1364 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001365 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001366
1367 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001368 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001369 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001370 "instruction is not lockable");
1371 }
1372
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001373 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001374
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001375 /*
1376 * when BND prefix is set by DEFAULT directive,
1377 * BND prefix is added to every appropriate instruction line
1378 * unless it is overridden by NOBND prefix.
1379 */
1380 if (globalbnd &&
1381 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1382 ins->prefixes[PPS_REP] = P_BND;
1383
H. Peter Anvina77692b2016-09-20 14:04:33 -07001384 /*
1385 * Add length of legacy prefixes
1386 */
1387 length += emit_prefix(NULL, bits, ins);
1388
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001389 return length;
1390}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001391
H. Peter Anvina77692b2016-09-20 14:04:33 -07001392static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001393{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001394 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001395 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001396 !(ins->rex & (REX_V | REX_EV)) &&
1397 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001398 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1399 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001400 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001401 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001402 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001403}
1404
H. Peter Anvina77692b2016-09-20 14:04:33 -07001405static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1406{
1407 int bytes = 0;
1408 int j;
1409
1410 for (j = 0; j < MAXPREFIX; j++) {
1411 uint8_t c = 0;
1412 switch (ins->prefixes[j]) {
1413 case P_WAIT:
1414 c = 0x9B;
1415 break;
1416 case P_LOCK:
1417 c = 0xF0;
1418 break;
1419 case P_REPNE:
1420 case P_REPNZ:
1421 case P_XACQUIRE:
1422 case P_BND:
1423 c = 0xF2;
1424 break;
1425 case P_REPE:
1426 case P_REPZ:
1427 case P_REP:
1428 case P_XRELEASE:
1429 c = 0xF3;
1430 break;
1431 case R_CS:
1432 if (bits == 64) {
1433 nasm_error(ERR_WARNING | ERR_PASS2,
1434 "cs segment base generated, but will be ignored in 64-bit mode");
1435 }
1436 c = 0x2E;
1437 break;
1438 case R_DS:
1439 if (bits == 64) {
1440 nasm_error(ERR_WARNING | ERR_PASS2,
1441 "ds segment base generated, but will be ignored in 64-bit mode");
1442 }
1443 c = 0x3E;
1444 break;
1445 case R_ES:
1446 if (bits == 64) {
1447 nasm_error(ERR_WARNING | ERR_PASS2,
1448 "es segment base generated, but will be ignored in 64-bit mode");
1449 }
1450 c = 0x26;
1451 break;
1452 case R_FS:
1453 c = 0x64;
1454 break;
1455 case R_GS:
1456 c = 0x65;
1457 break;
1458 case R_SS:
1459 if (bits == 64) {
1460 nasm_error(ERR_WARNING | ERR_PASS2,
1461 "ss segment base generated, but will be ignored in 64-bit mode");
1462 }
1463 c = 0x36;
1464 break;
1465 case R_SEGR6:
1466 case R_SEGR7:
1467 nasm_error(ERR_NONFATAL,
1468 "segr6 and segr7 cannot be used as prefixes");
1469 break;
1470 case P_A16:
1471 if (bits == 64) {
1472 nasm_error(ERR_NONFATAL,
1473 "16-bit addressing is not supported "
1474 "in 64-bit mode");
1475 } else if (bits != 16)
1476 c = 0x67;
1477 break;
1478 case P_A32:
1479 if (bits != 32)
1480 c = 0x67;
1481 break;
1482 case P_A64:
1483 if (bits != 64) {
1484 nasm_error(ERR_NONFATAL,
1485 "64-bit addressing is only supported "
1486 "in 64-bit mode");
1487 }
1488 break;
1489 case P_ASP:
1490 c = 0x67;
1491 break;
1492 case P_O16:
1493 if (bits != 16)
1494 c = 0x66;
1495 break;
1496 case P_O32:
1497 if (bits == 16)
1498 c = 0x66;
1499 break;
1500 case P_O64:
1501 /* REX.W */
1502 break;
1503 case P_OSP:
1504 c = 0x66;
1505 break;
1506 case P_EVEX:
1507 case P_VEX3:
1508 case P_VEX2:
1509 case P_NOBND:
1510 case P_none:
1511 break;
1512 default:
1513 nasm_panic(0, "invalid instruction prefix");
1514 }
1515 if (c) {
1516 if (data)
1517 out_rawbyte(data, c);
1518 bytes++;
1519 }
1520 }
1521 return bytes;
1522}
1523
1524static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001525{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001526 uint8_t c;
1527 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001528 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001529 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001530 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001531 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001532 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001533 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001534 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001535 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001536 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001537
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001538 ins->rex_done = false;
1539
H. Peter Anvina77692b2016-09-20 14:04:33 -07001540 emit_prefix(data, bits, ins);
1541
H. Peter Anvin839eca22007-10-29 23:12:47 -07001542 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001543 c = *codes++;
1544 op1 = (c & 3) + ((opex & 1) << 2);
1545 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1546 opx = &ins->oprs[op1];
1547 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001548
H. Peter Anvina77692b2016-09-20 14:04:33 -07001549
H. Peter Anvin839eca22007-10-29 23:12:47 -07001550 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001551 case 01:
1552 case 02:
1553 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001554 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001555 emit_rex(data, ins);
1556 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001557 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001558 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001559
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001560 case 05:
1561 case 06:
1562 case 07:
1563 opex = c;
1564 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001565
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001566 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001567 emit_rex(data, ins);
1568 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001569 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001570
Jin Kyu Song164d6072013-10-15 19:10:13 -07001571 case4(014):
1572 break;
1573
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001574 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001575 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001577
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001578 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001579 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001580 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001581
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001582 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001583 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001585
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001586 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001587 if (opx->type & (BITS16 | BITS32))
1588 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001589 else
1590 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001591 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001592 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001593
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001594 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001595 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001597
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001598 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001599 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001600 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001602
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001603 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001604 if (opx->segment == data->segment) {
1605 int64_t delta = opx->offset - data->offset
1606 - (data->inslen - data->insoffs);
1607 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001608 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001609 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001610 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001612
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001613 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001614 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001615 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001616
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001617 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001618 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001620
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001621 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001622 if (opx->type & (BITS16 | BITS32 | BITS64))
1623 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 else
1625 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001626
1627 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001628 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001629
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001630 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001631 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001632 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001633
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001634 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001635 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001636 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001637 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001638 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001640
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001641 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001642 {
1643 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1644 const struct operand *opy;
1645
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001646 c = *codes++;
1647 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001648 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001649 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001650 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001651 "non-absolute expression not permitted as argument %d",
1652 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001653 } else if (opy->offset & ~mask) {
1654 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1655 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001656 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001657 c = opy->offset & mask;
1658 goto emit_is4;
1659 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001660
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001661 case 0173:
1662 c = *codes++;
1663 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001664 c &= 15;
1665 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001666
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001667 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001668 c = 0;
1669 emit_is4:
1670 r = nasm_regvals[opx->basereg];
1671 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001672 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001673
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001674 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001675 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001676 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001677 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001678 "signed dword immediate exceeds bounds");
1679 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001680 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001681 break;
1682
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001683 case4(0240):
1684 case 0250:
1685 codes += 3;
1686 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1687 EVEX_P2Z | EVEX_P2AAA, 2);
1688 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1689 bytes[0] = 0x62;
1690 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001691 bytes[1] = ((((ins->rex & 7) << 5) |
1692 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001693 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001694 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1695 ((~ins->vexreg & 15) << 3) |
1696 (1 << 2) | (ins->vex_wlp & 3);
1697 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001698 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001699 break;
1700
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001701 case4(0260):
1702 case 0270:
1703 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001704 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1705 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001706 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1707 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1708 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001709 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001710 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001711 } else {
1712 bytes[0] = 0xc5;
1713 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001714 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001715 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001716 }
1717 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001718
H. Peter Anvine014f352012-02-25 22:35:19 -08001719 case 0271:
1720 case 0272:
1721 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001722 break;
1723
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001724 case4(0274):
1725 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001726 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001727 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001728
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001729 if (absolute_op(opx)) {
1730 if (ins->rex & REX_W)
1731 s = 64;
1732 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1733 s = 16;
1734 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1735 s = 32;
1736 else
1737 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001738
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001739 um = (uint64_t)2 << (s-1);
1740 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001741
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001742 if (uv > 127 && uv < (uint64_t)-128 &&
1743 (uv < um-128 || uv > um-1)) {
1744 /* If this wasn't explicitly byte-sized, warn as though we
1745 * had fallen through to the imm16/32/64 case.
1746 */
1747 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1748 "%s value exceeds bounds",
1749 (opx->type & BITS8) ? "signed byte" :
1750 s == 16 ? "word" :
1751 s == 32 ? "dword" :
1752 "signed dword");
1753 }
1754
1755 /* Output as a raw byte to avoid byte overflow check */
1756 out_rawbyte(data, (uint8_t)uv);
1757 } else {
1758 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001759 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001760 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001761 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001762
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001763 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001765
H. Peter Anvine2c80182005-01-15 22:15:51 +00001766 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001767 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1768 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001770
H. Peter Anvine2c80182005-01-15 22:15:51 +00001771 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001772 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1773 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001774 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001775
H. Peter Anvine2c80182005-01-15 22:15:51 +00001776 case 0312:
1777 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001778
Keith Kaniosb7a89542007-04-12 02:40:54 +00001779 case 0313:
1780 ins->rex = 0;
1781 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001782
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001783 case4(0314):
1784 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001785
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001787 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001789
H. Peter Anvine2c80182005-01-15 22:15:51 +00001790 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001791 case 0323:
1792 break;
1793
Keith Kaniosb7a89542007-04-12 02:40:54 +00001794 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001795 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001797
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001798 case 0325:
1799 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001800
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001801 case 0326:
1802 break;
1803
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001805 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001807
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001810
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001811 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001813 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001814 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001815
Keith Kanios48af1772007-08-17 07:37:52 +00001816 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001817 if (ins->rex & REX_R)
1818 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001819 ins->rex &= ~(REX_L|REX_R);
1820 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001821
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001822 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001823 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001824
H. Peter Anvin962e3052008-08-28 17:47:16 -07001825 case 0336:
1826 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001827 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001828
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001831 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001832
1833 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001835
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001836 case 0341:
1837 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001838
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001839 case 0360:
1840 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001841
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001842 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001843 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001844 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001845
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001846 case 0364:
1847 case 0365:
1848 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001849
Keith Kanios48af1772007-08-17 07:37:52 +00001850 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001851 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001852 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001853 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001854
Jin Kyu Song03041092013-10-15 19:38:51 -07001855 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001856 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001857
H. Peter Anvine2c80182005-01-15 22:15:51 +00001858 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001859 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001860 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001861
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001862 case 0374:
1863 eat = EA_XMMVSIB;
1864 break;
1865
1866 case 0375:
1867 eat = EA_YMMVSIB;
1868 break;
1869
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001870 case 0376:
1871 eat = EA_ZMMVSIB;
1872 break;
1873
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001874 case4(0100):
1875 case4(0110):
1876 case4(0120):
1877 case4(0130):
1878 case4(0200):
1879 case4(0204):
1880 case4(0210):
1881 case4(0214):
1882 case4(0220):
1883 case4(0224):
1884 case4(0230):
1885 case4(0234):
1886 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001887 ea ea_data;
1888 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001889 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001890 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001891 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001892
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001893 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001894 /* pick rfield from operand b (opx) */
1895 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001896 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001897 } else {
1898 /* rfield is constant */
1899 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001900 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001901 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001902
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001903 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001904 rfield, rflags, ins, &errmsg) != eat)
1905 nasm_error(ERR_NONFATAL, "%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07001906
H. Peter Anvine2c80182005-01-15 22:15:51 +00001907 p = bytes;
1908 *p++ = ea_data.modrm;
1909 if (ea_data.sib_present)
1910 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001911 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001913 /*
1914 * Make sure the address gets the right offset in case
1915 * the line breaks in the .lst file (BR 1197827)
1916 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001917
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001918 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001919 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001920 if (ea_data.disp8) {
1921 out_rawbyte(data, ea_data.disp8);
1922 } else if (ea_data.rip) {
1923 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001924 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001925 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001926
H. Peter Anvina77692b2016-09-20 14:04:33 -07001927 if (overflow_general(opy->offset, asize) ||
1928 signed_bits(opy->offset, ins->addr_size) !=
1929 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001930 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001931
H. Peter Anvina77692b2016-09-20 14:04:33 -07001932 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001933 (asize > ea_data.bytes)
1934 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001935 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001936 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001937 }
1938 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001939
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001940 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001941 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001942 ": instruction code \\%o (0x%02X) given", c, c);
1943 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001944 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001945 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001946}
1947
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001948static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001949{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001950 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001951 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001952 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001953}
1954
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001955static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001956{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001957 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001958 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001959 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001960}
1961
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001962static int op_rexflags(const operand * o, int mask)
1963{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001964 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001965 int val;
1966
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001967 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001968 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001969
H. Peter Anvina4835d42008-05-20 14:21:29 -07001970 flags = nasm_reg_flags[o->basereg];
1971 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001972
1973 return rexflags(val, flags, mask);
1974}
1975
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001976static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001977{
1978 int rex = 0;
1979
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001980 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001981 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001982 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001983 rex |= REX_W;
1984 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
1985 rex |= REX_H;
1986 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
1987 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001988
1989 return rex & mask;
1990}
1991
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001992static int evexflags(int val, decoflags_t deco,
1993 int mask, uint8_t byte)
1994{
1995 int evex = 0;
1996
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001997 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001998 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001999 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002000 evex |= (EVEX_P0RP | EVEX_P0X);
2001 break;
2002 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002003 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002004 evex |= EVEX_P2VP;
2005 if (deco & Z)
2006 evex |= EVEX_P2Z;
2007 if (deco & OPMASK_MASK)
2008 evex |= deco & EVEX_P2AAA;
2009 break;
2010 }
2011 return evex & mask;
2012}
2013
2014static int op_evexflags(const operand * o, int mask, uint8_t byte)
2015{
2016 int val;
2017
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002018 val = nasm_regvals[o->basereg];
2019
2020 return evexflags(val, o->decoflags, mask, byte);
2021}
2022
H. Peter Anvin23595f52009-07-25 17:44:25 -07002023static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002024 insn *instruction,
2025 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002026{
2027 const struct itemplate *temp;
2028 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002029 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002030 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002031 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002032 int i;
2033
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002034 /* broadcasting uses a different data element size */
2035 for (i = 0; i < instruction->operands; i++)
2036 if (i == broadcast)
2037 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2038 else
2039 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002040
2041 merr = MERR_INVALOP;
2042
2043 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002044 temp->opcode != I_none; temp++) {
2045 m = matches(temp, instruction, bits);
2046 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002047 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002048 m = MOK_GOOD;
2049 else
2050 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002051 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002052 /*
2053 * Missing operand size and a candidate for fuzzy matching...
2054 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002055 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002056 if (i == broadcast)
2057 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2058 else
2059 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002060 opsizemissing = true;
2061 }
2062 if (m > merr)
2063 merr = m;
2064 if (merr == MOK_GOOD)
2065 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002066 }
2067
2068 /* No match, but see if we can get a fuzzy operand size match... */
2069 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002070 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002071
2072 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002073 /*
2074 * We ignore extrinsic operand sizes on registers, so we should
2075 * never try to fuzzy-match on them. This also resolves the case
2076 * when we have e.g. "xmmrm128" in two different positions.
2077 */
2078 if (is_class(REGISTER, instruction->oprs[i].type))
2079 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002080
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002081 /* This tests if xsizeflags[i] has more than one bit set */
2082 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2083 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002084
Jin Kyu Song7903c072013-10-30 03:00:12 -07002085 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002086 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002087 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2088 BITS32 : BITS64);
2089 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002090 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002091 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002092 }
2093
2094 /* Try matching again... */
2095 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002096 temp->opcode != I_none; temp++) {
2097 m = matches(temp, instruction, bits);
2098 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002099 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002100 m = MOK_GOOD;
2101 else
2102 m = MERR_INVALOP;
2103 }
2104 if (m > merr)
2105 merr = m;
2106 if (merr == MOK_GOOD)
2107 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002108 }
2109
H. Peter Anvina81655b2009-07-25 18:15:28 -07002110done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002111 *tempp = temp;
2112 return merr;
2113}
2114
Mark Charneydcaef4b2014-10-09 13:45:17 -04002115static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2116{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002117 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002118 uint8_t brcast_num;
2119
Mark Charneydcaef4b2014-10-09 13:45:17 -04002120 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002121 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002122 "size of broadcasting element is greater than 64 bits");
2123
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002124 /*
2125 * The shift term is to take care of the extra BITS80 inserted
2126 * between BITS64 and BITS128.
2127 */
2128 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2129 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002130
2131 return brcast_num;
2132}
2133
H. Peter Anvin65289e82009-07-25 17:25:11 -07002134static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002135 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002136{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002137 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002138 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002139 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002140
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002141 /*
2142 * Check the opcode
2143 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002144 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002145 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002146
2147 /*
2148 * Count the operands
2149 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002150 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002151 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002152
2153 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002154 * Is it legal?
2155 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002156 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002157 return MERR_INVALOP;
2158
2159 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002160 * {evex} available?
2161 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002162 switch (instruction->prefixes[PPS_VEX]) {
2163 case P_EVEX:
2164 if (!itemp_has(itemp, IF_EVEX))
2165 return MERR_ENCMISMATCH;
2166 break;
2167 case P_VEX3:
2168 case P_VEX2:
2169 if (!itemp_has(itemp, IF_VEX))
2170 return MERR_ENCMISMATCH;
2171 break;
2172 default:
2173 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002174 }
2175
2176 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002177 * Check that no spurious colons or TOs are present
2178 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002179 for (i = 0; i < itemp->operands; i++)
2180 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002181 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002182
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002183 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002184 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002185 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002186 switch (itemp_smask(itemp)) {
2187 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002188 asize = BITS8;
2189 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002190 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002191 asize = BITS16;
2192 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002193 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002194 asize = BITS32;
2195 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002196 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002197 asize = BITS64;
2198 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002199 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002200 asize = BITS128;
2201 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002202 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002203 asize = BITS256;
2204 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002205 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002206 asize = BITS512;
2207 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002208 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002209 switch (bits) {
2210 case 16:
2211 asize = BITS16;
2212 break;
2213 case 32:
2214 asize = BITS32;
2215 break;
2216 case 64:
2217 asize = BITS64;
2218 break;
2219 default:
2220 asize = 0;
2221 break;
2222 }
2223 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002224 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002225 asize = 0;
2226 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002227 }
2228
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002229 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002230 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002231 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002232 memset(size, 0, sizeof size);
2233 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002234 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002235 /* S- flags apply to all operands */
2236 for (i = 0; i < MAX_OPERANDS; i++)
2237 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002238 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002239
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002240 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002241 * Check that the operand flags all match up,
2242 * it's a bit tricky so lets be verbose:
2243 *
2244 * 1) Find out the size of operand. If instruction
2245 * doesn't have one specified -- we're trying to
2246 * guess it either from template (IF_S* flag) or
2247 * from code bits.
2248 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002249 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002250 * template has an operand size specified AND this size differ
2251 * from which instruction has (perhaps we got it from code bits)
2252 * we are:
2253 * a) Check that only size of instruction and operand is differ
2254 * other characteristics do match
2255 * b) Perhaps it's a register specified in instruction so
2256 * for such a case we just mark that operand as "size
2257 * missing" and this will turn on fuzzy operand size
2258 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002259 */
2260 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002261 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002262 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002263 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002264 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002265 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002266 opflags_t template_opsize, insn_opsize;
2267
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002268 if (!(type & SIZE_MASK))
2269 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002270
Jin Kyu Song7903c072013-10-30 03:00:12 -07002271 insn_opsize = type & SIZE_MASK;
2272 if (!is_broadcast) {
2273 template_opsize = itemp->opd[i] & SIZE_MASK;
2274 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002275 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2276
2277 if (~ideco & BRDCAST_MASK)
2278 return MERR_BRNOTHERE;
2279
Jin Kyu Song7903c072013-10-30 03:00:12 -07002280 /*
2281 * when broadcasting, the element size depends on
2282 * the instruction type. decorator flag should match.
2283 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002284 if (deco_brsize) {
2285 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002286 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002287 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002288 } else {
2289 template_opsize = 0;
2290 }
2291 }
2292
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002293 if (~ideco & deco & OPMASK_MASK)
2294 return MERR_MASKNOTHERE;
2295
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002296 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2297 return MERR_DECONOTHERE;
2298
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002299 if (itemp->opd[i] & ~type & ~SIZE_MASK) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002300 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002301 } else if (template_opsize) {
2302 if (template_opsize != insn_opsize) {
2303 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002304 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002305 } else if (!is_class(REGISTER, type)) {
2306 /*
2307 * Note: we don't honor extrinsic operand sizes for registers,
2308 * so "missing operand size" for a register should be
2309 * considered a wildcard match rather than an error.
2310 */
2311 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002312 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002313 } else if (is_broadcast &&
2314 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002315 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002316 /*
2317 * broadcasting opsize matches but the number of repeated memory
2318 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002319 * if 64b double precision float is broadcasted to ymm (256b),
2320 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002321 */
2322 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002323 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002324 }
2325 }
2326
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002327 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002328 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002329
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002330 /*
2331 * Check operand sizes
2332 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002333 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2334 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002335 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002336 asize = itemp->opd[i] & SIZE_MASK;
2337 if (asize) {
2338 for (i = 0; i < oprs; i++)
2339 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002340 break;
2341 }
2342 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002343 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002344 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002345 }
2346
Keith Kaniosb7a89542007-04-12 02:40:54 +00002347 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002348 if (!(itemp->opd[i] & SIZE_MASK) &&
2349 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002350 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002351 }
2352
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002353 /*
2354 * Check template is okay at the set cpu level
2355 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002356 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002357 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002358
Keith Kaniosb7a89542007-04-12 02:40:54 +00002359 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002360 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002361 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002362 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002363 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002364
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002365 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002366 * If we have a HLE prefix, look for the NOHLE flag
2367 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002368 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002369 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2370 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2371 return MERR_BADHLE;
2372
2373 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002374 * Check if special handling needed for Jumps
2375 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002376 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002377 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002378
Jin Kyu Song03041092013-10-15 19:38:51 -07002379 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002380 * Check if BND prefix is allowed.
2381 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002382 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002383 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002384 (has_prefix(instruction, PPS_REP, P_BND) ||
2385 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002386 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002387 else if (itemp_has(itemp, IF_BND) &&
2388 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2389 has_prefix(instruction, PPS_REP, P_REPNZ)))
2390 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002391
H. Peter Anvin60926242009-07-26 16:25:38 -07002392 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002393}
2394
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002395/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002396 * Check if ModR/M.mod should/can be 01.
2397 * - EAF_BYTEOFFS is set
2398 * - offset can fit in a byte when EVEX is not used
2399 * - offset can be compressed when EVEX is used
2400 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002401#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2402 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2403 is_disp8n(input, ins, &output->disp8) : \
2404 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2405 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002406
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002407static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002408 int rfield, opflags_t rflags, insn *ins,
2409 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002410{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002411 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002412 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002413 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002414
H. Peter Anvin8f622462017-04-02 19:02:29 -07002415 *errmsg = "invalid effective address"; /* Default error message */
2416
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002417 output->type = EA_SCALAR;
2418 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002419 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002420
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002421 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002422 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002423 /* EVEX.R' flag for the REG operand */
2424 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002425
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002426 if (is_class(REGISTER, input->type)) {
2427 /*
2428 * It's a direct register.
2429 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002430 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002431 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002432
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002433 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002434 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002435
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002436 /* broadcasting is not available with a direct register operand. */
2437 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002438 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002439 goto err;
2440 }
2441
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002442 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002443 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002444 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002445 output->bytes = 0; /* no offset necessary either */
2446 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2447 } else {
2448 /*
2449 * It's a memory reference.
2450 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002451
2452 /* Embedded rounding or SAE is not available with a mem ref operand. */
2453 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002454 *errmsg = "embedded rounding is available only with "
2455 "register-register operations";
2456 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002457 }
2458
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002459 if (input->basereg == -1 &&
2460 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002461 /*
2462 * It's a pure offset.
2463 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002464 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002465 if (input->segment == NO_SEG ||
2466 (input->opflags & OPFLAG_RELATIVE)) {
2467 nasm_error(ERR_WARNING | ERR_PASS2,
2468 "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002469 input->type &= ~IP_REL;
2470 input->type |= MEMORY;
2471 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002472 }
2473
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002474 if (bits == 64 &&
2475 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002476 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002477 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002478 }
2479
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002480 if (eaflags & EAF_BYTEOFFS ||
2481 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002482 input->disp_size != (addrbits != 16 ? 32 : 16))) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002483 nasm_error(ERR_WARNING | ERR_PASS1,
2484 "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002485 }
2486
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002487 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002488 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002489 output->sib = GEN_SIB(0, 4, 5);
2490 output->bytes = 4;
2491 output->modrm = GEN_MODRM(0, rfield, 4);
2492 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002493 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002494 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002495 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002496 output->modrm = GEN_MODRM(0, rfield,
2497 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002498 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002499 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002500 } else {
2501 /*
2502 * It's an indirection.
2503 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002504 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002505 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002506 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002507 int t, it, bt; /* register numbers */
2508 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002509
H. Peter Anvine2c80182005-01-15 22:15:51 +00002510 if (s == 0)
2511 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002512
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002513 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002514 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002515 ix = nasm_reg_flags[i];
2516 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002517 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002518 ix = 0;
2519 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002520
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002521 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002522 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002523 bx = nasm_reg_flags[b];
2524 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002525 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002526 bx = 0;
2527 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002528
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002529 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002530 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002531 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002532 int32_t o = input->offset;
2533 int mod, scale, index, base;
2534
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002535 /*
2536 * For a vector SIB, one has to be a vector and the other,
2537 * if present, a GPR. The vector must be the index operand.
2538 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002539 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002540 if (s == 0)
2541 s = 1;
2542 else if (s != 1)
2543 goto err;
2544
2545 t = bt, bt = it, it = t;
2546 x = bx, bx = ix, ix = x;
2547 }
2548
2549 if (bt != -1) {
2550 if (REG_GPR & ~bx)
2551 goto err;
2552 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2553 sok &= bx;
2554 else
2555 goto err;
2556 }
2557
2558 /*
2559 * While we're here, ensure the user didn't specify
2560 * WORD or QWORD
2561 */
2562 if (input->disp_size == 16 || input->disp_size == 64)
2563 goto err;
2564
2565 if (addrbits == 16 ||
2566 (addrbits == 32 && !(sok & BITS32)) ||
2567 (addrbits == 64 && !(sok & BITS64)))
2568 goto err;
2569
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002570 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2571 : ((ix & YMMREG & ~REG_EA)
2572 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002573
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002574 output->rex |= rexflags(it, ix, REX_X);
2575 output->rex |= rexflags(bt, bx, REX_B);
2576 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002577
2578 index = it & 7; /* it is known to be != -1 */
2579
2580 switch (s) {
2581 case 1:
2582 scale = 0;
2583 break;
2584 case 2:
2585 scale = 1;
2586 break;
2587 case 4:
2588 scale = 2;
2589 break;
2590 case 8:
2591 scale = 3;
2592 break;
2593 default: /* then what the smeg is it? */
2594 goto err; /* panic */
2595 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002596
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002597 if (bt == -1) {
2598 base = 5;
2599 mod = 0;
2600 } else {
2601 base = (bt & 7);
2602 if (base != REG_NUM_EBP && o == 0 &&
2603 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002604 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002605 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002606 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002607 mod = 1;
2608 else
2609 mod = 2;
2610 }
2611
2612 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002613 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2614 output->modrm = GEN_MODRM(mod, rfield, 4);
2615 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002616 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002617 /*
2618 * it must be a 32/64-bit memory reference. Firstly we have
2619 * to check that all registers involved are type E/Rxx.
2620 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002621 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002622 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002623
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002624 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002625 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2626 sok &= ix;
2627 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002628 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002629 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002630
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002631 if (bt != -1) {
2632 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002633 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002634 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002635 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002636 sok &= bx;
2637 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002638
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002639 /*
2640 * While we're here, ensure the user didn't specify
2641 * WORD or QWORD
2642 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002643 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002644 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002645
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002646 if (addrbits == 16 ||
2647 (addrbits == 32 && !(sok & BITS32)) ||
2648 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002649 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002650
Keith Kaniosb7a89542007-04-12 02:40:54 +00002651 /* now reorganize base/index */
2652 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002653 ((hb == b && ht == EAH_NOTBASE) ||
2654 (hb == i && ht == EAH_MAKEBASE))) {
2655 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002656 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002657 x = bx, bx = ix, ix = x;
2658 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002659
Jin Kyu Song164d6072013-10-15 19:10:13 -07002660 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002661 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002662 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002663 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002664 if (eaflags & EAF_MIB) {
2665 /* only for mib operands */
2666 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2667 /*
2668 * make a single reg index [reg*1].
2669 * gas uses this form for an explicit index register.
2670 */
2671 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2672 }
2673 if ((ht == EAH_SUMMED) && bt == -1) {
2674 /* separate once summed index into [base, index] */
2675 bt = it, bx = ix, s--;
2676 }
2677 } else {
2678 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002679 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002680 s == 3 || s == 5 || s == 9) && bt == -1) {
2681 /* convert 3*EAX to EAX+2*EAX */
2682 bt = it, bx = ix, s--;
2683 }
2684 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002685 (eaflags & EAF_TIMESTWO) &&
2686 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002687 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002688 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002689 * to sib format with 0x0 displacement - [EAX*1+0].
2690 */
2691 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2692 }
2693 }
Keith Kanios48af1772007-08-17 07:37:52 +00002694 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002695 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002696 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002697 x = ix, ix = bx, bx = x;
2698 }
2699 if (it == REG_NUM_ESP ||
2700 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002701 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002702
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002703 output->rex |= rexflags(it, ix, REX_X);
2704 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002705
Keith Kanios48af1772007-08-17 07:37:52 +00002706 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002707 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002708 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002709
Keith Kaniosb7a89542007-04-12 02:40:54 +00002710 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002711 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002712 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002713 } else {
2714 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002715 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002716 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002717 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002718 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002719 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002720 mod = 1;
2721 else
2722 mod = 2;
2723 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002724
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002725 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002726 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2727 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002728 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002729 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002730 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002731
Keith Kaniosb7a89542007-04-12 02:40:54 +00002732 if (it == -1)
2733 index = 4, s = 1;
2734 else
2735 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002736
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 switch (s) {
2738 case 1:
2739 scale = 0;
2740 break;
2741 case 2:
2742 scale = 1;
2743 break;
2744 case 4:
2745 scale = 2;
2746 break;
2747 case 8:
2748 scale = 3;
2749 break;
2750 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002751 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002752 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002753
Keith Kaniosb7a89542007-04-12 02:40:54 +00002754 if (bt == -1) {
2755 base = 5;
2756 mod = 0;
2757 } else {
2758 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002759 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002760 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002761 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002762 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002763 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002764 mod = 1;
2765 else
2766 mod = 2;
2767 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002768
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002769 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002770 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2771 output->modrm = GEN_MODRM(mod, rfield, 4);
2772 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002773 }
2774 } else { /* it's 16-bit */
2775 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002776 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002777
Keith Kaniosb7a89542007-04-12 02:40:54 +00002778 /* check for 64-bit long mode */
2779 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002780 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002781
H. Peter Anvine2c80182005-01-15 22:15:51 +00002782 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002783 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2784 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002785 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002786
Keith Kaniosb7a89542007-04-12 02:40:54 +00002787 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002788 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002789 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002790
H. Peter Anvine2c80182005-01-15 22:15:51 +00002791 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002792 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002793 if (b == -1 && i != -1) {
2794 int tmp = b;
2795 b = i;
2796 i = tmp;
2797 } /* swap */
2798 if ((b == R_SI || b == R_DI) && i != -1) {
2799 int tmp = b;
2800 b = i;
2801 i = tmp;
2802 }
2803 /* have BX/BP as base, SI/DI index */
2804 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002805 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 if (i != -1 && b != -1 &&
2807 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002808 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002809 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002810 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002811
H. Peter Anvine2c80182005-01-15 22:15:51 +00002812 rm = -1;
2813 if (i != -1)
2814 switch (i * 256 + b) {
2815 case R_SI * 256 + R_BX:
2816 rm = 0;
2817 break;
2818 case R_DI * 256 + R_BX:
2819 rm = 1;
2820 break;
2821 case R_SI * 256 + R_BP:
2822 rm = 2;
2823 break;
2824 case R_DI * 256 + R_BP:
2825 rm = 3;
2826 break;
2827 } else
2828 switch (b) {
2829 case R_SI:
2830 rm = 4;
2831 break;
2832 case R_DI:
2833 rm = 5;
2834 break;
2835 case R_BP:
2836 rm = 6;
2837 break;
2838 case R_BX:
2839 rm = 7;
2840 break;
2841 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002842 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002843 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002844
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002845 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002846 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002847 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002848 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002849 mod = 1;
2850 else
2851 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002852
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002853 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002854 output->bytes = mod; /* bytes of offset needed */
2855 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002856 }
2857 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002858 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002859
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002860 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002861 return output->type;
2862
2863err:
2864 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002865}
2866
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002867static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002868{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002869 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002870 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002871
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002872 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002873
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002874 switch (ins->prefixes[PPS_ASIZE]) {
2875 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002876 valid &= 16;
2877 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002878 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002879 valid &= 32;
2880 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002881 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002882 valid &= 64;
2883 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002884 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002885 valid &= (addrbits == 32) ? 16 : 32;
2886 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002887 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002888 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002889 }
2890
2891 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002892 if (is_class(MEMORY, ins->oprs[j].type)) {
2893 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002894
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002895 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002896 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002897 i = 0;
2898 else
2899 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002900
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002901 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002902 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002903 b = 0;
2904 else
2905 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002906
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002907 if (ins->oprs[j].scale == 0)
2908 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002909
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002910 if (!i && !b) {
2911 int ds = ins->oprs[j].disp_size;
2912 if ((addrbits != 64 && ds > 8) ||
2913 (addrbits == 64 && ds == 16))
2914 valid &= ds;
2915 } else {
2916 if (!(REG16 & ~b))
2917 valid &= 16;
2918 if (!(REG32 & ~b))
2919 valid &= 32;
2920 if (!(REG64 & ~b))
2921 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002922
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002923 if (!(REG16 & ~i))
2924 valid &= 16;
2925 if (!(REG32 & ~i))
2926 valid &= 32;
2927 if (!(REG64 & ~i))
2928 valid &= 64;
2929 }
2930 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002931 }
2932
2933 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002934 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002935 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002936 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002937 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002938 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002939 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002940 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002941 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002942 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002943 }
2944
2945 defdisp = ins->addr_size == 16 ? 16 : 32;
2946
2947 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002948 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2949 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2950 /*
2951 * mem_offs sizes must match the address size; if not,
2952 * strip the MEM_OFFS bit and match only EA instructions
2953 */
2954 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2955 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002956 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002957}