blob: 10d9ec91dd635d81b32e7a948cf448b777158590 [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 Anvincd26fcc2018-06-25 17:15:08 -0700211 MERR_REGSETSIZE,
212 MERR_REGSET,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700213 /*
214 * Matching success; the conditional ones first
215 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400216 MOK_JUMP, /* Matching OK but needs jmp_match() */
217 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700218};
219
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000220typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700221 enum ea_type type; /* what kind of EA is this? */
222 int sib_present; /* is a SIB byte necessary? */
223 int bytes; /* # of bytes of offset needed */
224 int size; /* lazy - this is sib+bytes+1 */
225 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700226 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000227} ea;
228
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400229#define GEN_SIB(scale, index, base) \
230 (((scale) << 6) | ((index) << 3) | ((base)))
231
232#define GEN_MODRM(mod, reg, rm) \
233 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
234
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800235static int64_t calcsize(int32_t, int64_t, int, insn *,
236 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700237static int emit_prefix(struct out_data *data, const int bits, insn *ins);
238static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700239static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400240 insn *instruction,
241 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700242static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700243static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000244static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700245static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000246static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700247static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700248static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000249
H. Peter Anvin8f622462017-04-02 19:02:29 -0700250static enum ea_type process_ea(operand *, ea *, int, int,
251 opflags_t, insn *, const char **);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700252
H. Peter Anvin164d2462017-02-20 02:39:56 -0800253static inline bool absolute_op(const struct operand *o)
254{
255 return o->segment == NO_SEG && o->wrt == NO_SEG &&
256 !(o->opflags & OPFLAG_RELATIVE);
257}
258
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400259static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000260{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700261 return ins->prefixes[pos] == prefix;
262}
263
264static void assert_no_prefix(insn * ins, enum prefix_pos pos)
265{
266 if (ins->prefixes[pos])
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300267 nasm_nonfatal("invalid %s prefix", prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700268}
269
270static const char *size_name(int size)
271{
272 switch (size) {
273 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700275 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400276 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700277 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400278 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700279 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400280 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700281 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400282 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700283 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400284 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700285 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400286 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700287 case 64:
288 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700289 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400290 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000291 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700292}
293
H. Peter Anvin285222f2017-03-01 13:27:33 -0800294static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400295{
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -0800296 nasm_warnf(ERR_PASS2 | WARN_NOV, "%s data exceeds bounds",
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300297 size_name(size));
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400298}
299
300static void warn_overflow_const(int64_t data, int size)
301{
302 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800303 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400304}
305
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800306static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
307{
308 bool err;
309
310 switch (sign) {
311 case OUT_WRAP:
312 err = overflow_general(data, size);
313 break;
314 case OUT_SIGNED:
315 err = overflow_signed(data, size);
316 break;
317 case OUT_UNSIGNED:
318 err = overflow_unsigned(data, size);
319 break;
320 default:
321 panic();
322 break;
323 }
324
325 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800326 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800327}
328
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000329/*
330 * This routine wrappers the real output format's output routine,
331 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800332 * generator at the same time, flatten unnecessary relocations,
333 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000334 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700335static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000336{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000337 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700338 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700339 union {
340 uint8_t b[8];
341 uint64_t q;
342 } xdata;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700343 size_t asize, amax;
344 uint64_t zeropad = 0;
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 Anvina7b6bfc2017-05-03 17:32:02 -0700367 nasm_assert(data->size <= 8);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700368 asize = data->size;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700369 amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
Chang S. Bae427d8e32018-05-02 08:07:52 -0700370 if ((ofmt->flags & OFMT_KEEP_ADDR) == 0 && data->tsegment == fixseg &&
371 data->twrt == NO_SEG) {
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800372 warn_overflow_out(addrval, asize, data->sign);
Martin Storsjö869087d2017-05-22 13:54:20 +0300373 xdata.q = cpu_to_le64(addrval);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700374 data->data = xdata.b;
375 data->type = OUT_RAWDATA;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700376 asize = amax = 0; /* No longer an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700377 }
378 break;
379
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700380 case OUT_SEGMENT:
381 nasm_assert(data->size <= 8);
382 asize = data->size;
383 amax = 2;
384 break;
385
H. Peter Anvina77692b2016-09-20 14:04:33 -0700386 default:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700387 asize = amax = 0; /* Not an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700388 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000389 }
390
Frank Kotlerabebb082003-09-06 04:45:37 +0000391 /*
392 * this call to src_get determines when we call the
393 * debug-format-specific "linenum" function
394 * it updates lineno and lnfname to the current values
395 * returning 0 if "same as last time", -2 if lnfname
396 * changed, and the amount by which lineno changed,
397 * if it did. thus, these variables must be static
398 */
399
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400400 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700401 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000402
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700403 if (asize > amax) {
404 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300405 nasm_nonfatal("%u-bit signed relocation unsupported by output format %s",
406 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800407 } else {
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -0800408 nasm_warnf(WARN_ZEXTRELOC,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700409 "%u-bit %s relocation zero-extended from %u bits",
410 (unsigned int)(asize << 3),
411 data->type == OUT_SEGMENT ? "segment" : "unsigned",
412 (unsigned int)(amax << 3));
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800413 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700414 zeropad = data->size - amax;
415 data->size = amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800416 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700417 lfmt->output(data);
H. Peter Anvined859f72018-06-15 00:03:53 -0700418
419 if (likely(data->segment != NO_SEG)) {
420 ofmt->output(data);
421 } else {
422 /* Outputting to ABSOLUTE section - only reserve is permitted */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300423 if (data->type != OUT_RESERVE)
424 nasm_nonfatal("attempt to assemble code in [ABSOLUTE] space");
H. Peter Anvined859f72018-06-15 00:03:53 -0700425 /* No need to push to the backend */
426 }
427
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700428 data->offset += data->size;
429 data->insoffs += data->size;
430
431 if (zeropad) {
432 data->type = OUT_ZERODATA;
433 data->size = zeropad;
434 lfmt->output(data);
435 ofmt->output(data);
436 data->offset += zeropad;
437 data->insoffs += zeropad;
438 data->size += zeropad; /* Restore original size value */
439 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000440}
441
H. Peter Anvina77692b2016-09-20 14:04:33 -0700442static inline void out_rawdata(struct out_data *data, const void *rawdata,
443 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400444{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700445 data->type = OUT_RAWDATA;
446 data->data = rawdata;
447 data->size = size;
448 out(data);
449}
450
451static void out_rawbyte(struct out_data *data, uint8_t byte)
452{
453 data->type = OUT_RAWDATA;
454 data->data = &byte;
455 data->size = 1;
456 out(data);
457}
458
459static inline void out_reserve(struct out_data *data, uint64_t size)
460{
461 data->type = OUT_RESERVE;
462 data->size = size;
463 out(data);
464}
465
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700466static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700467{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700468 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300469 nasm_nonfatal("segment references cannot be relative");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700470
471 data->type = OUT_SEGMENT;
472 data->sign = OUT_UNSIGNED;
473 data->size = 2;
474 data->toffset = opx->offset;
475 data->tsegment = ofmt->segbase(opx->segment | 1);
476 data->twrt = opx->wrt;
477 out(data);
478}
479
480static void out_imm(struct out_data *data, const struct operand *opx,
481 int size, enum out_sign sign)
482{
483 if (opx->segment != NO_SEG && (opx->segment & 1)) {
484 /*
485 * This is actually a segment reference, but eval() has
486 * already called ofmt->segbase() for us. Sigh.
487 */
488 if (size < 2)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300489 nasm_nonfatal("segment reference must be 16 bits");
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700490
491 data->type = OUT_SEGMENT;
492 } else {
493 data->type = (opx->opflags & OPFLAG_RELATIVE)
494 ? OUT_RELADDR : OUT_ADDRESS;
495 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700496 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700497 data->toffset = opx->offset;
498 data->tsegment = opx->segment;
499 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800500 /*
501 * XXX: improve this if at some point in the future we can
502 * distinguish the subtrahend in expressions like [foo - bar]
503 * where bar is a symbol in the current segment. However, at the
504 * current point, if OPFLAG_RELATIVE is set that subtraction has
505 * already occurred.
506 */
507 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700508 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700509 out(data);
510}
511
H. Peter Anvin164d2462017-02-20 02:39:56 -0800512static void out_reladdr(struct out_data *data, const struct operand *opx,
513 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700514{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800515 if (opx->opflags & OPFLAG_RELATIVE)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300516 nasm_nonfatal("invalid use of self-relative expression");
H. Peter Anvin164d2462017-02-20 02:39:56 -0800517
H. Peter Anvina77692b2016-09-20 14:04:33 -0700518 data->type = OUT_RELADDR;
519 data->sign = OUT_SIGNED;
520 data->size = size;
521 data->toffset = opx->offset;
522 data->tsegment = opx->segment;
523 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800524 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700525 out(data);
526}
527
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700528static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800529 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000530{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800531 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800532 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000533 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800534 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000535
H. Peter Anvin755f5212012-02-25 11:41:34 -0800536 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700537 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300538 if (!optimizing.level || (optimizing.flag & OPTIM_DISABLE_JMP_MATCH))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400539 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300540 if (optimizing.level < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400541 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700542
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800543 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100544
Victor van den Elzen154e5922009-02-25 17:32:00 +0100545 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100546 /* Be optimistic in pass 1 */
547 return true;
548
H. Peter Anvine2c80182005-01-15 22:15:51 +0000549 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700550 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000551
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700552 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800553 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
554
555 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
556 /* jmp short (opcode eb) cannot be used with bnd prefix. */
557 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -0800558 nasm_warnf(WARN_BND | ERR_PASS2 ,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300559 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800560 }
561
562 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000563}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000564
H. Peter Anvin04445362016-09-21 15:56:19 -0700565/* This is totally just a wild guess what is reasonable... */
566#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
567
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800568int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000569{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700570 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000571 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700572 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300573 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000574
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700575 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700576 data.offset = start;
577 data.segment = segment;
578 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700579 data.bits = bits;
580
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700581 wsize = db_bytes(instruction->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300582 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000583 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000584
H. Peter Anvineba20a72002-04-30 20:53:55 +0000585 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000586 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700587
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700588 list_for_each(e, instruction->eops) {
589 if (e->type == EOT_DB_NUMBER) {
590 if (wsize > 8) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300591 nasm_nonfatal("integer supplied to a DT,DO,DY or DZ");
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700592 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700593 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700594 data.inslen = data.size = wsize;
595 data.toffset = e->offset;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700596 data.twrt = e->wrt;
597 data.relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700598 if (e->segment != NO_SEG && (e->segment & 1)) {
599 data.tsegment = e->segment;
600 data.type = OUT_SEGMENT;
601 data.sign = OUT_UNSIGNED;
602 } else {
603 data.tsegment = e->segment;
604 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
605 data.sign = OUT_WRAP;
606 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700607 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000608 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700609 } else if (e->type == EOT_DB_STRING ||
610 e->type == EOT_DB_STRING_FREE) {
611 int align = e->stringlen % wsize;
612 if (align)
613 align = wsize - align;
614
615 data.insoffs = 0;
616 data.inslen = e->stringlen + align;
617
618 out_rawdata(&data, e->stringval, e->stringlen);
619 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700620 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000621 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700622 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700623 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000624 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700625 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700626 off_t base = 0;
627 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700628 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700629 char *buf = NULL;
630 size_t blk = 0; /* Buffered I/O block size */
631 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000632
H. Peter Anvin94ead272017-09-27 15:22:23 -0700633 if (!t)
634 goto done;
635
H. Peter Anvind81a2352016-09-21 14:03:18 -0700636 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400637 if (!fp) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300638 nasm_nonfatal("`incbin': unable to open file `%s'",
639 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700640 goto done;
641 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000642
H. Peter Anvind81a2352016-09-21 14:03:18 -0700643 len = nasm_file_size(fp);
644
645 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300646 nasm_nonfatal("`incbin': unable to get length of file `%s'",
647 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700648 goto close_done;
649 }
650
H. Peter Anvina77692b2016-09-20 14:04:33 -0700651 if (instruction->eops->next) {
652 base = instruction->eops->next->offset;
653 if (base >= len) {
654 len = 0;
655 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 len -= base;
657 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700658 len > (off_t)instruction->eops->next->next->offset)
659 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000660 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000661 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700662
H. Peter Anvina77692b2016-09-20 14:04:33 -0700663 lfmt->set_offset(data.offset);
664 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000665
H. Peter Anvind81a2352016-09-21 14:03:18 -0700666 if (!len)
667 goto end_incbin;
668
669 /* Try to map file data */
670 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700671 if (!map) {
672 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
673 buf = nasm_malloc(blk);
674 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700675
676 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700677 /*
678 * Consider these irrelevant for INCBIN, since it is fully
679 * possible that these might be (way) bigger than an int
680 * can hold; there is, however, no reason to widen these
681 * types just for INCBIN. data.inslen == 0 signals to the
682 * backend that these fields are meaningless, if at all
683 * needed.
684 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700685 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700686 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700687
H. Peter Anvind81a2352016-09-21 14:03:18 -0700688 if (map) {
689 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700690 } else if ((off_t)m == len) {
691 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700692 } else {
693 off_t l = len;
694
695 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300696 nasm_nonfatal("`incbin': unable to seek on file `%s'",
697 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700698 goto end_incbin;
699 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700700 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700701 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700702 if (!m || feof(fp)) {
703 /*
704 * This shouldn't happen unless the file
705 * actually changes while we are reading
706 * it.
707 */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300708 nasm_nonfatal("`incbin': unexpected EOF while"
709 " reading file `%s'", fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700710 goto end_incbin;
711 }
712 out_rawdata(&data, buf, m);
713 l -= m;
714 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700715 }
716 }
717 end_incbin:
718 lfmt->downlevel(LIST_INCBIN);
719 if (instruction->times > 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700720 lfmt->uplevel(LIST_TIMES);
721 lfmt->downlevel(LIST_TIMES);
722 }
723 if (ferror(fp)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300724 nasm_nonfatal("`incbin': error while"
725 " reading file `%s'", fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700726 }
727 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700728 if (buf)
729 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700730 if (map)
731 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700732 fclose(fp);
733 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700734 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700735 ;
736 } else {
737 /* "Real" instruction */
738
739 /* Check to see if we need an address-size prefix */
740 add_asp(instruction, bits);
741
742 m = find_match(&temp, instruction, data.segment, data.offset, bits);
743
744 if (m == MOK_GOOD) {
745 /* Matches! */
746 int64_t insn_size = calcsize(data.segment, data.offset,
747 bits, instruction, temp);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700748 nasm_assert(insn_size >= 0);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700749
750 data.itemp = temp;
751 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700752 data.insoffs = 0;
753 data.inslen = insn_size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700754
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700755 gencode(&data, instruction);
756 nasm_assert(data.insoffs == insn_size);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700757 } else {
758 /* No match */
759 switch (m) {
760 case MERR_OPSIZEMISSING:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300761 nasm_nonfatal("operation size not specified");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700762 break;
763 case MERR_OPSIZEMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300764 nasm_nonfatal("mismatch in operand sizes");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700765 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700766 case MERR_BRNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300767 nasm_nonfatal("broadcast not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700768 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700769 case MERR_BRNUMMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300770 nasm_nonfatal("mismatch in the number of broadcasting elements");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700771 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700772 case MERR_MASKNOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300773 nasm_nonfatal("mask not permitted on this operand");
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700774 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700775 case MERR_DECONOTHERE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300776 nasm_nonfatal("unsupported mode decorator for instruction");
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700777 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700778 case MERR_BADCPU:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300779 nasm_nonfatal("no instruction for this cpu level");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700780 break;
781 case MERR_BADMODE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300782 nasm_nonfatal("instruction not supported in %d-bit mode", bits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700783 break;
784 case MERR_ENCMISMATCH:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300785 nasm_nonfatal("specific encoding scheme not available");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700786 break;
787 case MERR_BADBND:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300788 nasm_nonfatal("bnd prefix is not allowed");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700789 break;
790 case MERR_BADREPNE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300791 nasm_nonfatal("%s prefix is not allowed",
792 (has_prefix(instruction, PPS_REP, P_REPNE) ?
793 "repne" : "repnz"));
H. Peter Anvina77692b2016-09-20 14:04:33 -0700794 break;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700795 case MERR_REGSETSIZE:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300796 nasm_nonfatal("invalid register set size");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700797 break;
798 case MERR_REGSET:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300799 nasm_nonfatal("register set not valid for operand");
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700800 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700801 default:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300802 nasm_nonfatal("invalid combination of opcode and operands");
H. Peter Anvina77692b2016-09-20 14:04:33 -0700803 break;
804 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700805
806 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400807 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000808 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700809 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000810}
811
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800812int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000813{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000814 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700815 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000816
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400817 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000819
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700820 if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300822 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000823
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 isize = 0;
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700825 wsize = db_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700826 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000827
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400828 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000829 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000830
H. Peter Anvine2c80182005-01-15 22:15:51 +0000831 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400832 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400834 warn_overflow_const(e->offset, wsize);
835 } else if (e->type == EOT_DB_STRING ||
836 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000837 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000838
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 align = (-osize) % wsize;
840 if (align < 0)
841 align += wsize;
842 isize += osize + align;
843 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700844 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000845 }
846
H. Peter Anvine2c80182005-01-15 22:15:51 +0000847 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400848 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700849 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000850
H. Peter Anvind81a2352016-09-21 14:03:18 -0700851 len = nasm_file_size_by_path(fname);
852 if (len == (off_t)-1) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300853 nasm_nonfatal("`incbin': unable to get length of file `%s'",
854 fname);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700855 return 0;
856 }
857
858 if (instruction->eops->next) {
859 if (len <= (off_t)instruction->eops->next->offset) {
860 len = 0;
861 } else {
862 len -= instruction->eops->next->offset;
863 if (instruction->eops->next->next &&
864 len > (off_t)instruction->eops->next->next->offset) {
865 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000866 }
867 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000868 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700869
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700870 len *= instruction->times;
871 instruction->times = 1; /* Tell the upper layer to not iterate */
872
H. Peter Anvind81a2352016-09-21 14:03:18 -0700873 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000874 }
875
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700876 /* Check to see if we need an address-size prefix */
877 add_asp(instruction, bits);
878
H. Peter Anvin23595f52009-07-25 17:44:25 -0700879 m = find_match(&temp, instruction, segment, offset, bits);
880 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400881 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700882 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700883 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400884 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000885 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000886}
887
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800888static void bad_hle_warn(const insn * ins, uint8_t hleok)
889{
890 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800891 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800892 static const enum whatwarn warn[2][4] =
893 {
894 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
895 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
896 };
897 unsigned int n;
898
899 n = (unsigned int)rep_pfx - P_XACQUIRE;
900 if (n > 1)
901 return; /* Not XACQUIRE/XRELEASE */
902
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800903 ww = warn[n][hleok];
904 if (!is_class(MEMORY, ins->oprs[0].type))
905 ww = w_inval; /* HLE requires operand 0 to be memory */
906
907 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800908 case w_none:
909 break;
910
911 case w_lock:
912 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -0800913 nasm_warnf(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300914 "%s with this instruction requires lock",
915 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800916 }
917 break;
918
919 case w_inval:
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -0800920 nasm_warnf(WARN_HLE | ERR_PASS2,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +0300921 "%s invalid with this instruction",
922 prefix_name(rep_pfx));
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800923 break;
924 }
925}
926
H. Peter Anvin507ae032008-10-09 15:37:10 -0700927/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400928#define case3(x) case (x): case (x)+1: case (x)+2
929#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700930
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800931static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800932 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000933{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800934 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800935 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000936 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000937 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700938 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700939 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700940 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700941 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800942 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800943 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700944 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -0700945 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000946
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700947 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700948 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700949 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700950
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700951 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400952 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700953
H. Peter Anvine2c80182005-01-15 22:15:51 +0000954 (void)segment; /* Don't warn that this parameter is unused */
955 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000956
H. Peter Anvin839eca22007-10-29 23:12:47 -0700957 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400958 c = *codes++;
959 op1 = (c & 3) + ((opex & 1) << 2);
960 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
961 opx = &ins->oprs[op1];
962 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700963
H. Peter Anvin839eca22007-10-29 23:12:47 -0700964 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400965 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000966 codes += c, length += c;
967 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700968
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400969 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400970 opex = c;
971 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700972
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400973 case4(010):
974 ins->rex |=
975 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000976 codes++, length++;
977 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700978
Jin Kyu Song164d6072013-10-15 19:10:13 -0700979 case4(014):
980 /* this is an index reg of MIB operand */
981 mib_index = opx->basereg;
982 break;
983
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400984 case4(020):
985 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000986 length++;
987 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700988
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400989 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 length += 2;
991 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700992
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400993 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700994 if (opx->type & (BITS16 | BITS32 | BITS64))
995 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 else
997 length += (bits == 16) ? 2 : 4;
998 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700999
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001000 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 length += 4;
1002 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001003
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001004 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001005 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001007
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001008 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 length++;
1010 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001011
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001012 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001013 length += 8; /* MOV reg64/imm */
1014 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001015
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001016 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001017 length += 2;
1018 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001019
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001020 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001021 if (opx->type & (BITS16 | BITS32 | BITS64))
1022 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001023 else
1024 length += (bits == 16) ? 2 : 4;
1025 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001026
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001027 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001028 length += 4;
1029 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001030
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001031 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001032 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001033 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001034
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001035 case 0172:
1036 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001037 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001038 length++;
1039 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001040
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001041 case4(0174):
1042 length++;
1043 break;
1044
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001045 case4(0240):
1046 ins->rex |= REX_EV;
1047 ins->vexreg = regval(opx);
1048 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1049 ins->vex_cm = *codes++;
1050 ins->vex_wlp = *codes++;
1051 ins->evex_tuple = (*codes++ - 0300);
1052 break;
1053
1054 case 0250:
1055 ins->rex |= REX_EV;
1056 ins->vexreg = 0;
1057 ins->vex_cm = *codes++;
1058 ins->vex_wlp = *codes++;
1059 ins->evex_tuple = (*codes++ - 0300);
1060 break;
1061
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001062 case4(0254):
1063 length += 4;
1064 break;
1065
1066 case4(0260):
1067 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001068 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001069 ins->vex_cm = *codes++;
1070 ins->vex_wlp = *codes++;
1071 break;
1072
1073 case 0270:
1074 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001075 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001076 ins->vex_cm = *codes++;
1077 ins->vex_wlp = *codes++;
1078 break;
1079
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001080 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001081 hleok = c & 3;
1082 break;
1083
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001084 case4(0274):
1085 length++;
1086 break;
1087
1088 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001089 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001090
H. Peter Anvine2c80182005-01-15 22:15:51 +00001091 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001092 if (bits == 64)
1093 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001094 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001096
H. Peter Anvine2c80182005-01-15 22:15:51 +00001097 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001098 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001099 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001100
H. Peter Anvine2c80182005-01-15 22:15:51 +00001101 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001102 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001103
Keith Kaniosb7a89542007-04-12 02:40:54 +00001104 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001105 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1106 has_prefix(ins, PPS_ASIZE, P_A32))
1107 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001108 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001109
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001110 case4(0314):
1111 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001112
H. Peter Anvine2c80182005-01-15 22:15:51 +00001113 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001114 {
1115 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1116 if (pfx == P_O16)
1117 break;
1118 if (pfx != P_none)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001119 nasm_warnf(ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001120 else
1121 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001122 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001123 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001124
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001126 {
1127 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1128 if (pfx == P_O32)
1129 break;
1130 if (pfx != P_none)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001131 nasm_warnf(ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001132 else
1133 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001134 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001135 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001136
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 case 0322:
1138 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001139
Keith Kaniosb7a89542007-04-12 02:40:54 +00001140 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001141 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001142 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001143
Keith Kaniosb7a89542007-04-12 02:40:54 +00001144 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001145 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001146 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001147
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001148 case 0325:
1149 ins->rex |= REX_NH;
1150 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001151
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001152 case 0326:
1153 break;
1154
H. Peter Anvine2c80182005-01-15 22:15:51 +00001155 case 0330:
1156 codes++, length++;
1157 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001158
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001161
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001162 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 case 0333:
1164 length++;
1165 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001166
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001167 case 0334:
1168 ins->rex |= REX_L;
1169 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001170
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001171 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001172 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001173
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001174 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001175 if (!ins->prefixes[PPS_REP])
1176 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001177 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001178
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001179 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001180 if (!ins->prefixes[PPS_REP])
1181 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001182 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001183
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001185 if (!absolute_op(&ins->oprs[0]))
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001186 nasm_nonfatal("attempt to reserve non-constant"
1187 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001188 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001189 nasm_warnf(ERR_PASS1, "forward reference in RESx "
1190 "can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001192 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001194
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001195 case 0341:
1196 if (!ins->prefixes[PPS_WAIT])
1197 ins->prefixes[PPS_WAIT] = P_WAIT;
1198 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001199
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001200 case 0360:
1201 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001202
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001203 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001204 length++;
1205 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001206
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001207 case 0364:
1208 case 0365:
1209 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001210
Keith Kanios48af1772007-08-17 07:37:52 +00001211 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001212 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001213 length++;
1214 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001215
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001216 case 0370:
1217 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001218 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001219
H. Peter Anvine2c80182005-01-15 22:15:51 +00001220 case 0373:
1221 length++;
1222 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001223
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001224 case 0374:
1225 eat = EA_XMMVSIB;
1226 break;
1227
1228 case 0375:
1229 eat = EA_YMMVSIB;
1230 break;
1231
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001232 case 0376:
1233 eat = EA_ZMMVSIB;
1234 break;
1235
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001236 case4(0100):
1237 case4(0110):
1238 case4(0120):
1239 case4(0130):
1240 case4(0200):
1241 case4(0204):
1242 case4(0210):
1243 case4(0214):
1244 case4(0220):
1245 case4(0224):
1246 case4(0230):
1247 case4(0234):
1248 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001249 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001250 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001251 opflags_t rflags;
1252 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001253 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001254
Keith Kaniosb7a89542007-04-12 02:40:54 +00001255 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001256
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001257 if (c <= 0177) {
1258 /* pick rfield from operand b (opx) */
1259 rflags = regflag(opx);
1260 rfield = nasm_regvals[opx->basereg];
1261 } else {
1262 rflags = 0;
1263 rfield = c & 7;
1264 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001265
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001266 /* EVEX.b1 : evex_brerop contains the operand position */
1267 op_er_sae = (ins->evex_brerop >= 0 ?
1268 &ins->oprs[ins->evex_brerop] : NULL);
1269
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001270 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1271 /* set EVEX.b */
1272 ins->evex_p[2] |= EVEX_P2B;
1273 if (op_er_sae->decoflags & ER) {
1274 /* set EVEX.RC (rounding control) */
1275 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1276 & EVEX_P2RC;
1277 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001278 } else {
1279 /* set EVEX.L'L (vector length) */
1280 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001281 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001282 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001283 /* set EVEX.b */
1284 ins->evex_p[2] |= EVEX_P2B;
1285 }
1286 }
1287
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001288 if (itemp_has(temp, IF_MIB)) {
1289 opy->eaflags |= EAF_MIB;
1290 /*
1291 * if a separate form of MIB (ICC style) is used,
1292 * the index reg info is merged into mem operand
1293 */
1294 if (mib_index != R_none) {
1295 opy->indexreg = mib_index;
1296 opy->scale = 1;
1297 opy->hintbase = mib_index;
1298 opy->hinttype = EAH_NOTBASE;
1299 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001300 }
1301
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001302 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001303 rfield, rflags, ins, &errmsg) != eat) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001304 nasm_nonfatal("%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001305 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001306 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001307 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001309 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001310 }
1311 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001312
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001313 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001314 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001315 ": instruction code \\%o (0x%02X) given", c, c);
1316 break;
1317 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001318 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001319
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001320 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001321
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001322 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001323 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001324 nasm_nonfatal("instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001325 return -1;
1326 }
1327 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001328 }
1329
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001330 switch (ins->prefixes[PPS_VEX]) {
1331 case P_EVEX:
1332 if (!(ins->rex & REX_EV))
1333 return -1;
1334 break;
1335 case P_VEX3:
1336 case P_VEX2:
1337 if (!(ins->rex & REX_V))
1338 return -1;
1339 break;
1340 default:
1341 break;
1342 }
1343
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001344 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001345 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001346
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001347 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001348 nasm_nonfatal("cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001349 return -1;
1350 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001351 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001352 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001353 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001354 ins->rex &= ~REX_W;
1355 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001356 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001357 ins->rex |= REX_W;
1358 bad32 &= ~REX_W;
1359 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001360 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001361 /* Follow REX_W */
1362 break;
1363 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001364
H. Peter Anvinfc561202011-07-07 16:58:22 -07001365 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001366 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001367 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001368 } else if (!(ins->rex & REX_EV) &&
1369 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001370 nasm_nonfatal("invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001371 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001372 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001373 if (ins->rex & REX_EV)
1374 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001375 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1376 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001377 length += 3;
1378 else
1379 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001380 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001381 if (ins->rex & REX_H) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001382 nasm_nonfatal("cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001383 return -1;
1384 } else if (bits == 64) {
1385 length++;
1386 } else if ((ins->rex & REX_L) &&
1387 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001388 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001389 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001390 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001391 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001392 length++;
1393 } else {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001394 nasm_nonfatal("invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001395 return -1;
1396 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001397 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001398
1399 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001400 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -08001401 nasm_warnf(WARN_LOCK | ERR_PASS2 , "instruction is not lockable");
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001402 }
1403
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001404 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001405
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001406 /*
1407 * when BND prefix is set by DEFAULT directive,
1408 * BND prefix is added to every appropriate instruction line
1409 * unless it is overridden by NOBND prefix.
1410 */
1411 if (globalbnd &&
1412 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1413 ins->prefixes[PPS_REP] = P_BND;
1414
H. Peter Anvina77692b2016-09-20 14:04:33 -07001415 /*
1416 * Add length of legacy prefixes
1417 */
1418 length += emit_prefix(NULL, bits, ins);
1419
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001420 return length;
1421}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001422
H. Peter Anvina77692b2016-09-20 14:04:33 -07001423static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001424{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001425 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001426 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001427 !(ins->rex & (REX_V | REX_EV)) &&
1428 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001429 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1430 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001431 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001432 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001433 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001434}
1435
H. Peter Anvina77692b2016-09-20 14:04:33 -07001436static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1437{
1438 int bytes = 0;
1439 int j;
1440
1441 for (j = 0; j < MAXPREFIX; j++) {
1442 uint8_t c = 0;
1443 switch (ins->prefixes[j]) {
1444 case P_WAIT:
1445 c = 0x9B;
1446 break;
1447 case P_LOCK:
1448 c = 0xF0;
1449 break;
1450 case P_REPNE:
1451 case P_REPNZ:
1452 case P_XACQUIRE:
1453 case P_BND:
1454 c = 0xF2;
1455 break;
1456 case P_REPE:
1457 case P_REPZ:
1458 case P_REP:
1459 case P_XRELEASE:
1460 c = 0xF3;
1461 break;
1462 case R_CS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001463 if (bits == 64)
1464 nasm_warnf(ERR_PASS2, "cs segment base generated, "
1465 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001466 c = 0x2E;
1467 break;
1468 case R_DS:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001469 if (bits == 64)
1470 nasm_warnf(ERR_PASS2, "ds segment base generated, "
1471 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001472 c = 0x3E;
1473 break;
1474 case R_ES:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001475 if (bits == 64)
1476 nasm_warnf(ERR_PASS2, "es segment base generated, "
1477 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001478 c = 0x26;
1479 break;
1480 case R_FS:
1481 c = 0x64;
1482 break;
1483 case R_GS:
1484 c = 0x65;
1485 break;
1486 case R_SS:
1487 if (bits == 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001488 nasm_warnf(ERR_PASS2, "ss segment base generated, "
1489 "but will be ignored in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001490 }
1491 c = 0x36;
1492 break;
1493 case R_SEGR6:
1494 case R_SEGR7:
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001495 nasm_nonfatal("segr6 and segr7 cannot be used as prefixes");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001496 break;
1497 case P_A16:
1498 if (bits == 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001499 nasm_nonfatal("16-bit addressing is not supported "
1500 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001501 } else if (bits != 16)
1502 c = 0x67;
1503 break;
1504 case P_A32:
1505 if (bits != 32)
1506 c = 0x67;
1507 break;
1508 case P_A64:
1509 if (bits != 64) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001510 nasm_nonfatal("64-bit addressing is only supported "
1511 "in 64-bit mode");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001512 }
1513 break;
1514 case P_ASP:
1515 c = 0x67;
1516 break;
1517 case P_O16:
1518 if (bits != 16)
1519 c = 0x66;
1520 break;
1521 case P_O32:
1522 if (bits == 16)
1523 c = 0x66;
1524 break;
1525 case P_O64:
1526 /* REX.W */
1527 break;
1528 case P_OSP:
1529 c = 0x66;
1530 break;
1531 case P_EVEX:
1532 case P_VEX3:
1533 case P_VEX2:
1534 case P_NOBND:
1535 case P_none:
1536 break;
1537 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001538 nasm_panic("invalid instruction prefix");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001539 }
1540 if (c) {
1541 if (data)
1542 out_rawbyte(data, c);
1543 bytes++;
1544 }
1545 }
1546 return bytes;
1547}
1548
1549static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001550{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001551 uint8_t c;
1552 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001553 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001554 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001555 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001556 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001557 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001558 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001559 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001560 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001561 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001562
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001563 ins->rex_done = false;
1564
H. Peter Anvina77692b2016-09-20 14:04:33 -07001565 emit_prefix(data, bits, ins);
1566
H. Peter Anvin839eca22007-10-29 23:12:47 -07001567 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001568 c = *codes++;
1569 op1 = (c & 3) + ((opex & 1) << 2);
1570 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1571 opx = &ins->oprs[op1];
1572 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001573
H. Peter Anvina77692b2016-09-20 14:04:33 -07001574
H. Peter Anvin839eca22007-10-29 23:12:47 -07001575 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001576 case 01:
1577 case 02:
1578 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001579 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001580 emit_rex(data, ins);
1581 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001583 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001584
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001585 case 05:
1586 case 06:
1587 case 07:
1588 opex = c;
1589 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001590
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001591 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001592 emit_rex(data, ins);
1593 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001594 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001595
Jin Kyu Song164d6072013-10-15 19:10:13 -07001596 case4(014):
1597 break;
1598
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001599 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001600 out_imm(data, opx, 1, 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(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001604 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001605 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001606
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001607 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001608 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001609 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001610
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001611 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001612 if (opx->type & (BITS16 | BITS32))
1613 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001614 else
1615 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001616 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001617 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001618
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001619 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001620 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001621 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001622
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001623 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001624 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001625 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001627
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001628 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001629 if (opx->segment == data->segment) {
1630 int64_t delta = opx->offset - data->offset
1631 - (data->inslen - data->insoffs);
1632 if (delta > 127 || delta < -128)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001633 nasm_nonfatal("short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001634 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001635 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001637
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001638 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001639 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001640 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001641
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001642 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001643 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001644 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001645
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001646 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001647 if (opx->type & (BITS16 | BITS32 | BITS64))
1648 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001649 else
1650 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001651
1652 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001653 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001654
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001655 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001656 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001657 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001658
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001659 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001660 if (opx->segment == NO_SEG)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001661 nasm_nonfatal("value referenced by FAR is not relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001662 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001663 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001664
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001665 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001666 {
1667 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1668 const struct operand *opy;
1669
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001670 c = *codes++;
1671 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001672 opy = &ins->oprs[c & 7];
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001673 if (!absolute_op(opy))
1674 nasm_nonfatal("non-absolute expression not permitted "
1675 "as argument %d", c & 7);
1676 else if (opy->offset & ~mask)
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -08001677 nasm_warnf(ERR_PASS2 | WARN_NOV,
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001678 "is4 argument exceeds bounds");
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001679 c = opy->offset & mask;
1680 goto emit_is4;
1681 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001682
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001683 case 0173:
1684 c = *codes++;
1685 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001686 c &= 15;
1687 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001688
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001689 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001690 c = 0;
1691 emit_is4:
1692 r = nasm_regvals[opx->basereg];
1693 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001694 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001695
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001696 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001697 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001698 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -08001699 nasm_warnf(ERR_PASS2 | WARN_NOV,
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001700 "signed dword immediate exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001701 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001702 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001703 break;
1704
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001705 case4(0240):
1706 case 0250:
1707 codes += 3;
1708 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1709 EVEX_P2Z | EVEX_P2AAA, 2);
1710 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1711 bytes[0] = 0x62;
1712 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001713 bytes[1] = ((((ins->rex & 7) << 5) |
1714 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001715 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001716 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1717 ((~ins->vexreg & 15) << 3) |
1718 (1 << 2) | (ins->vex_wlp & 3);
1719 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001720 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001721 break;
1722
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001723 case4(0260):
1724 case 0270:
1725 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001726 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1727 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001728 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1729 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1730 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001731 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001732 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001733 } else {
1734 bytes[0] = 0xc5;
1735 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001736 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001737 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001738 }
1739 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001740
H. Peter Anvine014f352012-02-25 22:35:19 -08001741 case 0271:
1742 case 0272:
1743 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001744 break;
1745
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001746 case4(0274):
1747 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001748 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001749 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001750
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001751 if (absolute_op(opx)) {
1752 if (ins->rex & REX_W)
1753 s = 64;
1754 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1755 s = 16;
1756 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1757 s = 32;
1758 else
1759 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001760
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001761 um = (uint64_t)2 << (s-1);
1762 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001763
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001764 if (uv > 127 && uv < (uint64_t)-128 &&
1765 (uv < um-128 || uv > um-1)) {
1766 /* If this wasn't explicitly byte-sized, warn as though we
1767 * had fallen through to the imm16/32/64 case.
1768 */
H. Peter Anvin (Intel)df4d3422018-12-12 17:48:38 -08001769 nasm_warnf(ERR_PASS2 | WARN_NOV,
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001770 "%s value exceeds bounds",
1771 (opx->type & BITS8) ? "signed byte" :
1772 s == 16 ? "word" :
1773 s == 32 ? "dword" :
1774 "signed dword");
1775 }
1776
1777 /* Output as a raw byte to avoid byte overflow check */
1778 out_rawbyte(data, (uint8_t)uv);
1779 } else {
1780 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001781 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001782 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001783 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001784
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001785 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001787
H. Peter Anvine2c80182005-01-15 22:15:51 +00001788 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001789 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1790 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001792
H. Peter Anvine2c80182005-01-15 22:15:51 +00001793 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001794 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1795 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001796 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001797
H. Peter Anvine2c80182005-01-15 22:15:51 +00001798 case 0312:
1799 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001800
Keith Kaniosb7a89542007-04-12 02:40:54 +00001801 case 0313:
1802 ins->rex = 0;
1803 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001804
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001805 case4(0314):
1806 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001807
H. Peter Anvine2c80182005-01-15 22:15:51 +00001808 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001811
H. Peter Anvine2c80182005-01-15 22:15:51 +00001812 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001813 case 0323:
1814 break;
1815
Keith Kaniosb7a89542007-04-12 02:40:54 +00001816 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001817 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001818 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001819
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001820 case 0325:
1821 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001822
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001823 case 0326:
1824 break;
1825
H. Peter Anvine2c80182005-01-15 22:15:51 +00001826 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001827 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001828 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001829
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001832
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001833 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001835 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001836 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001837
Keith Kanios48af1772007-08-17 07:37:52 +00001838 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001839 if (ins->rex & REX_R)
1840 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001841 ins->rex &= ~(REX_L|REX_R);
1842 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001843
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001844 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001845 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001846
H. Peter Anvin962e3052008-08-28 17:47:16 -07001847 case 0336:
1848 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001849 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001850
H. Peter Anvine2c80182005-01-15 22:15:51 +00001851 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001852 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvinc5136902018-06-15 18:20:17 -07001853 nasm_panic("non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001854
1855 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001856 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001857
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001858 case 0341:
1859 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001860
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001861 case 0360:
1862 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001863
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001864 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001865 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001866 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001867
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001868 case 0364:
1869 case 0365:
1870 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001871
Keith Kanios48af1772007-08-17 07:37:52 +00001872 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001873 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001874 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001875 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001876
Jin Kyu Song03041092013-10-15 19:38:51 -07001877 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001878 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001879
H. Peter Anvine2c80182005-01-15 22:15:51 +00001880 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001881 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001882 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001883
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001884 case 0374:
1885 eat = EA_XMMVSIB;
1886 break;
1887
1888 case 0375:
1889 eat = EA_YMMVSIB;
1890 break;
1891
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001892 case 0376:
1893 eat = EA_ZMMVSIB;
1894 break;
1895
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001896 case4(0100):
1897 case4(0110):
1898 case4(0120):
1899 case4(0130):
1900 case4(0200):
1901 case4(0204):
1902 case4(0210):
1903 case4(0214):
1904 case4(0220):
1905 case4(0224):
1906 case4(0230):
1907 case4(0234):
1908 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001909 ea ea_data;
1910 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001911 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001912 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001913 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001914
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001915 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001916 /* pick rfield from operand b (opx) */
1917 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001918 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001919 } else {
1920 /* rfield is constant */
1921 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001923 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001924
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001925 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001926 rfield, rflags, ins, &errmsg) != eat)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03001927 nasm_nonfatal("%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07001928
H. Peter Anvine2c80182005-01-15 22:15:51 +00001929 p = bytes;
1930 *p++ = ea_data.modrm;
1931 if (ea_data.sib_present)
1932 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001933 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001934
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001935 /*
1936 * Make sure the address gets the right offset in case
1937 * the line breaks in the .lst file (BR 1197827)
1938 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001939
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001940 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001941 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001942 if (ea_data.disp8) {
1943 out_rawbyte(data, ea_data.disp8);
1944 } else if (ea_data.rip) {
1945 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001946 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001947 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001948
H. Peter Anvina77692b2016-09-20 14:04:33 -07001949 if (overflow_general(opy->offset, asize) ||
1950 signed_bits(opy->offset, ins->addr_size) !=
1951 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001952 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001953
H. Peter Anvina77692b2016-09-20 14:04:33 -07001954 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001955 (asize > ea_data.bytes)
1956 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001957 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001958 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001959 }
1960 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001961
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001962 default:
H. Peter Anvinc5136902018-06-15 18:20:17 -07001963 nasm_panic("internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001964 ": instruction code \\%o (0x%02X) given", c, c);
1965 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001966 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001967 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001968}
1969
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001970static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001971{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001972 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001973 nasm_panic("invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001974 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001975}
1976
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001977static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001978{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001979 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001980 nasm_panic("invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001981 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001982}
1983
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001984static int op_rexflags(const operand * o, int mask)
1985{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001986 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001987 int val;
1988
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001989 if (!is_register(o->basereg))
H. Peter Anvinc5136902018-06-15 18:20:17 -07001990 nasm_panic("invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001991
H. Peter Anvina4835d42008-05-20 14:21:29 -07001992 flags = nasm_reg_flags[o->basereg];
1993 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001994
1995 return rexflags(val, flags, mask);
1996}
1997
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001998static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001999{
2000 int rex = 0;
2001
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002002 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002003 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002004 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002005 rex |= REX_W;
2006 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2007 rex |= REX_H;
2008 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2009 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002010
2011 return rex & mask;
2012}
2013
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002014static int evexflags(int val, decoflags_t deco,
2015 int mask, uint8_t byte)
2016{
2017 int evex = 0;
2018
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002019 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002020 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002021 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002022 evex |= (EVEX_P0RP | EVEX_P0X);
2023 break;
2024 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002025 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002026 evex |= EVEX_P2VP;
2027 if (deco & Z)
2028 evex |= EVEX_P2Z;
2029 if (deco & OPMASK_MASK)
2030 evex |= deco & EVEX_P2AAA;
2031 break;
2032 }
2033 return evex & mask;
2034}
2035
2036static int op_evexflags(const operand * o, int mask, uint8_t byte)
2037{
2038 int val;
2039
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002040 val = nasm_regvals[o->basereg];
2041
2042 return evexflags(val, o->decoflags, mask, byte);
2043}
2044
H. Peter Anvin23595f52009-07-25 17:44:25 -07002045static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002046 insn *instruction,
2047 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002048{
2049 const struct itemplate *temp;
2050 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002051 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002052 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002053 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002054 int i;
2055
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002056 /* broadcasting uses a different data element size */
2057 for (i = 0; i < instruction->operands; i++)
2058 if (i == broadcast)
2059 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2060 else
2061 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002062
2063 merr = MERR_INVALOP;
2064
2065 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002066 temp->opcode != I_none; temp++) {
2067 m = matches(temp, instruction, bits);
2068 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002069 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002070 m = MOK_GOOD;
2071 else
2072 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002073 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002074 /*
2075 * Missing operand size and a candidate for fuzzy matching...
2076 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002077 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002078 if (i == broadcast)
2079 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2080 else
2081 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002082 opsizemissing = true;
2083 }
2084 if (m > merr)
2085 merr = m;
2086 if (merr == MOK_GOOD)
2087 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002088 }
2089
2090 /* No match, but see if we can get a fuzzy operand size match... */
2091 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002092 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002093
2094 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002095 /*
2096 * We ignore extrinsic operand sizes on registers, so we should
2097 * never try to fuzzy-match on them. This also resolves the case
2098 * when we have e.g. "xmmrm128" in two different positions.
2099 */
2100 if (is_class(REGISTER, instruction->oprs[i].type))
2101 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002102
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002103 /* This tests if xsizeflags[i] has more than one bit set */
2104 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2105 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002106
Jin Kyu Song7903c072013-10-30 03:00:12 -07002107 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002108 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002109 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2110 BITS32 : BITS64);
2111 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002112 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002113 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002114 }
2115
2116 /* Try matching again... */
2117 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002118 temp->opcode != I_none; temp++) {
2119 m = matches(temp, instruction, bits);
2120 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002121 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002122 m = MOK_GOOD;
2123 else
2124 m = MERR_INVALOP;
2125 }
2126 if (m > merr)
2127 merr = m;
2128 if (merr == MOK_GOOD)
2129 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002130 }
2131
H. Peter Anvina81655b2009-07-25 18:15:28 -07002132done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002133 *tempp = temp;
2134 return merr;
2135}
2136
Mark Charneydcaef4b2014-10-09 13:45:17 -04002137static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2138{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002139 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002140 uint8_t brcast_num;
2141
Mark Charneydcaef4b2014-10-09 13:45:17 -04002142 if (brsize > BITS64)
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002143 nasm_fatal("size of broadcasting element is greater than 64 bits");
Mark Charneydcaef4b2014-10-09 13:45:17 -04002144
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002145 /*
2146 * The shift term is to take care of the extra BITS80 inserted
2147 * between BITS64 and BITS128.
2148 */
2149 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2150 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002151
2152 return brcast_num;
2153}
2154
H. Peter Anvin65289e82009-07-25 17:25:11 -07002155static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002156 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002157{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002158 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002159 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002160 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002161
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002162 /*
2163 * Check the opcode
2164 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002165 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002166 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002167
2168 /*
2169 * Count the operands
2170 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002171 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002172 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002173
2174 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002175 * Is it legal?
2176 */
Chang S. Baea5786342018-08-15 23:22:21 +03002177 if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002178 return MERR_INVALOP;
2179
2180 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002181 * {evex} available?
2182 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002183 switch (instruction->prefixes[PPS_VEX]) {
2184 case P_EVEX:
2185 if (!itemp_has(itemp, IF_EVEX))
2186 return MERR_ENCMISMATCH;
2187 break;
2188 case P_VEX3:
2189 case P_VEX2:
2190 if (!itemp_has(itemp, IF_VEX))
2191 return MERR_ENCMISMATCH;
2192 break;
2193 default:
2194 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002195 }
2196
2197 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002198 * Check that no spurious colons or TOs are present
2199 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002200 for (i = 0; i < itemp->operands; i++)
2201 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002202 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002203
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002204 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002205 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002206 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002207 switch (itemp_smask(itemp)) {
2208 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002209 asize = BITS8;
2210 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002211 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002212 asize = BITS16;
2213 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002214 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002215 asize = BITS32;
2216 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002217 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002218 asize = BITS64;
2219 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002220 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002221 asize = BITS128;
2222 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002223 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002224 asize = BITS256;
2225 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002226 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002227 asize = BITS512;
2228 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002229 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002230 switch (bits) {
2231 case 16:
2232 asize = BITS16;
2233 break;
2234 case 32:
2235 asize = BITS32;
2236 break;
2237 case 64:
2238 asize = BITS64;
2239 break;
2240 default:
2241 asize = 0;
2242 break;
2243 }
2244 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002245 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002246 asize = 0;
2247 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002248 }
2249
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002250 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002251 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002252 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002253 memset(size, 0, sizeof size);
2254 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002255 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002256 /* S- flags apply to all operands */
2257 for (i = 0; i < MAX_OPERANDS; i++)
2258 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002259 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002260
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002261 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002262 * Check that the operand flags all match up,
2263 * it's a bit tricky so lets be verbose:
2264 *
2265 * 1) Find out the size of operand. If instruction
2266 * doesn't have one specified -- we're trying to
2267 * guess it either from template (IF_S* flag) or
2268 * from code bits.
2269 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002270 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002271 * template has an operand size specified AND this size differ
2272 * from which instruction has (perhaps we got it from code bits)
2273 * we are:
2274 * a) Check that only size of instruction and operand is differ
2275 * other characteristics do match
2276 * b) Perhaps it's a register specified in instruction so
2277 * for such a case we just mark that operand as "size
2278 * missing" and this will turn on fuzzy operand size
2279 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002280 */
2281 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002282 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002283 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002284 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002285 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002286 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002287 opflags_t template_opsize, insn_opsize;
2288
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002289 if (!(type & SIZE_MASK))
2290 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002291
Jin Kyu Song7903c072013-10-30 03:00:12 -07002292 insn_opsize = type & SIZE_MASK;
2293 if (!is_broadcast) {
2294 template_opsize = itemp->opd[i] & SIZE_MASK;
2295 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002296 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2297
2298 if (~ideco & BRDCAST_MASK)
2299 return MERR_BRNOTHERE;
2300
Jin Kyu Song7903c072013-10-30 03:00:12 -07002301 /*
2302 * when broadcasting, the element size depends on
2303 * the instruction type. decorator flag should match.
2304 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002305 if (deco_brsize) {
2306 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002307 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002308 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002309 } else {
2310 template_opsize = 0;
2311 }
2312 }
2313
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002314 if (~ideco & deco & OPMASK_MASK)
2315 return MERR_MASKNOTHERE;
2316
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002317 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2318 return MERR_DECONOTHERE;
2319
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002320 if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002321 return MERR_INVALOP;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002322
2323 if (~itemp->opd[i] & type & REGSET_MASK)
2324 return (itemp->opd[i] & REGSET_MASK)
2325 ? MERR_REGSETSIZE : MERR_REGSET;
2326
2327 if (template_opsize) {
Jin Kyu Song7903c072013-10-30 03:00:12 -07002328 if (template_opsize != insn_opsize) {
2329 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002330 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002331 } else if (!is_class(REGISTER, type)) {
2332 /*
2333 * Note: we don't honor extrinsic operand sizes for registers,
2334 * so "missing operand size" for a register should be
2335 * considered a wildcard match rather than an error.
2336 */
2337 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002338 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002339 } else if (is_broadcast &&
2340 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002341 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002342 /*
2343 * broadcasting opsize matches but the number of repeated memory
2344 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002345 * if 64b double precision float is broadcasted to ymm (256b),
2346 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002347 */
2348 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002349 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002350 }
2351 }
2352
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002353 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002354 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002355
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002356 /*
2357 * Check operand sizes
2358 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002359 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2360 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002361 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002362 asize = itemp->opd[i] & SIZE_MASK;
2363 if (asize) {
2364 for (i = 0; i < oprs; i++)
2365 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002366 break;
2367 }
2368 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002369 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002370 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002371 }
2372
Keith Kaniosb7a89542007-04-12 02:40:54 +00002373 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002374 if (!(itemp->opd[i] & SIZE_MASK) &&
2375 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002376 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002377 }
2378
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002379 /*
2380 * Check template is okay at the set cpu level
2381 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002382 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002383 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002384
Keith Kaniosb7a89542007-04-12 02:40:54 +00002385 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002386 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002387 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002388 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002389 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002390
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002391 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002392 * If we have a HLE prefix, look for the NOHLE flag
2393 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002394 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002395 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2396 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2397 return MERR_BADHLE;
2398
2399 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002400 * Check if special handling needed for Jumps
2401 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002402 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002403 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404
Jin Kyu Song03041092013-10-15 19:38:51 -07002405 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002406 * Check if BND prefix is allowed.
2407 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002408 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002409 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002410 (has_prefix(instruction, PPS_REP, P_BND) ||
2411 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002412 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002413 else if (itemp_has(itemp, IF_BND) &&
2414 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2415 has_prefix(instruction, PPS_REP, P_REPNZ)))
2416 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002417
H. Peter Anvin60926242009-07-26 16:25:38 -07002418 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002419}
2420
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002421/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002422 * Check if ModR/M.mod should/can be 01.
2423 * - EAF_BYTEOFFS is set
2424 * - offset can fit in a byte when EVEX is not used
2425 * - offset can be compressed when EVEX is used
2426 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002427#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2428 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2429 is_disp8n(input, ins, &output->disp8) : \
2430 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2431 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002432
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002433static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002434 int rfield, opflags_t rflags, insn *ins,
2435 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002436{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002437 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002438 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002439 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002440
H. Peter Anvin8f622462017-04-02 19:02:29 -07002441 *errmsg = "invalid effective address"; /* Default error message */
2442
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002443 output->type = EA_SCALAR;
2444 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002445 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002446
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002447 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002448 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002449 /* EVEX.R' flag for the REG operand */
2450 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002451
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002452 if (is_class(REGISTER, input->type)) {
2453 /*
2454 * It's a direct register.
2455 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002456 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002457 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002458
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002459 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002460 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002461
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002462 /* broadcasting is not available with a direct register operand. */
2463 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002464 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002465 goto err;
2466 }
2467
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002468 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002469 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002470 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002471 output->bytes = 0; /* no offset necessary either */
2472 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2473 } else {
2474 /*
2475 * It's a memory reference.
2476 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002477
2478 /* Embedded rounding or SAE is not available with a mem ref operand. */
2479 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002480 *errmsg = "embedded rounding is available only with "
2481 "register-register operations";
2482 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002483 }
2484
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002485 if (input->basereg == -1 &&
2486 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002487 /*
2488 * It's a pure offset.
2489 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002490 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002491 if (input->segment == NO_SEG ||
2492 (input->opflags & OPFLAG_RELATIVE)) {
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002493 nasm_warnf(ERR_PASS2, "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002494 input->type &= ~IP_REL;
2495 input->type |= MEMORY;
2496 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002497 }
2498
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002499 if (bits == 64 &&
2500 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002501 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002502 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002503 }
2504
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002505 if (eaflags & EAF_BYTEOFFS ||
2506 (eaflags & EAF_WORDOFFS &&
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002507 input->disp_size != (addrbits != 16 ? 32 : 16)))
2508 nasm_warnf(ERR_PASS1, "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002509
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002510 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002511 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002512 output->sib = GEN_SIB(0, 4, 5);
2513 output->bytes = 4;
2514 output->modrm = GEN_MODRM(0, rfield, 4);
2515 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002516 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002517 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002518 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002519 output->modrm = GEN_MODRM(0, rfield,
2520 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002521 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002522 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002523 } else {
2524 /*
2525 * It's an indirection.
2526 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002527 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002528 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002529 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002530 int t, it, bt; /* register numbers */
2531 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002532
H. Peter Anvine2c80182005-01-15 22:15:51 +00002533 if (s == 0)
2534 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002535
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002536 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002537 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002538 ix = nasm_reg_flags[i];
2539 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002540 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002541 ix = 0;
2542 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002543
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002544 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002545 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002546 bx = nasm_reg_flags[b];
2547 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002548 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002549 bx = 0;
2550 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002551
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002552 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002553 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002554 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002555 int32_t o = input->offset;
2556 int mod, scale, index, base;
2557
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002558 /*
2559 * For a vector SIB, one has to be a vector and the other,
2560 * if present, a GPR. The vector must be the index operand.
2561 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002562 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002563 if (s == 0)
2564 s = 1;
2565 else if (s != 1)
2566 goto err;
2567
2568 t = bt, bt = it, it = t;
2569 x = bx, bx = ix, ix = x;
2570 }
2571
2572 if (bt != -1) {
2573 if (REG_GPR & ~bx)
2574 goto err;
2575 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2576 sok &= bx;
2577 else
2578 goto err;
2579 }
2580
2581 /*
2582 * While we're here, ensure the user didn't specify
2583 * WORD or QWORD
2584 */
2585 if (input->disp_size == 16 || input->disp_size == 64)
2586 goto err;
2587
2588 if (addrbits == 16 ||
2589 (addrbits == 32 && !(sok & BITS32)) ||
2590 (addrbits == 64 && !(sok & BITS64)))
2591 goto err;
2592
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002593 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2594 : ((ix & YMMREG & ~REG_EA)
2595 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002596
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002597 output->rex |= rexflags(it, ix, REX_X);
2598 output->rex |= rexflags(bt, bx, REX_B);
2599 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002600
2601 index = it & 7; /* it is known to be != -1 */
2602
2603 switch (s) {
2604 case 1:
2605 scale = 0;
2606 break;
2607 case 2:
2608 scale = 1;
2609 break;
2610 case 4:
2611 scale = 2;
2612 break;
2613 case 8:
2614 scale = 3;
2615 break;
2616 default: /* then what the smeg is it? */
2617 goto err; /* panic */
2618 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002619
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002620 if (bt == -1) {
2621 base = 5;
2622 mod = 0;
2623 } else {
2624 base = (bt & 7);
2625 if (base != REG_NUM_EBP && o == 0 &&
2626 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002627 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002628 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002629 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002630 mod = 1;
2631 else
2632 mod = 2;
2633 }
2634
2635 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002636 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2637 output->modrm = GEN_MODRM(mod, rfield, 4);
2638 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002639 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002640 /*
2641 * it must be a 32/64-bit memory reference. Firstly we have
2642 * to check that all registers involved are type E/Rxx.
2643 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002644 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002645 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002646
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002647 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002648 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2649 sok &= ix;
2650 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002651 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002652 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002653
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002654 if (bt != -1) {
2655 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002656 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002657 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002658 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002659 sok &= bx;
2660 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002661
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002662 /*
2663 * While we're here, ensure the user didn't specify
2664 * WORD or QWORD
2665 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002666 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002667 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002668
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002669 if (addrbits == 16 ||
2670 (addrbits == 32 && !(sok & BITS32)) ||
2671 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002672 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002673
Keith Kaniosb7a89542007-04-12 02:40:54 +00002674 /* now reorganize base/index */
2675 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002676 ((hb == b && ht == EAH_NOTBASE) ||
2677 (hb == i && ht == EAH_MAKEBASE))) {
2678 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002679 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002680 x = bx, bx = ix, ix = x;
2681 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002682
Jin Kyu Song164d6072013-10-15 19:10:13 -07002683 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002684 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002685 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002686 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002687 if (eaflags & EAF_MIB) {
2688 /* only for mib operands */
2689 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2690 /*
2691 * make a single reg index [reg*1].
2692 * gas uses this form for an explicit index register.
2693 */
2694 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2695 }
2696 if ((ht == EAH_SUMMED) && bt == -1) {
2697 /* separate once summed index into [base, index] */
2698 bt = it, bx = ix, s--;
2699 }
2700 } else {
2701 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002702 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002703 s == 3 || s == 5 || s == 9) && bt == -1) {
2704 /* convert 3*EAX to EAX+2*EAX */
2705 bt = it, bx = ix, s--;
2706 }
2707 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002708 (eaflags & EAF_TIMESTWO) &&
2709 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002710 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002711 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002712 * to sib format with 0x0 displacement - [EAX*1+0].
2713 */
2714 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2715 }
2716 }
Keith Kanios48af1772007-08-17 07:37:52 +00002717 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002718 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002719 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002720 x = ix, ix = bx, bx = x;
2721 }
2722 if (it == REG_NUM_ESP ||
2723 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002724 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002725
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002726 output->rex |= rexflags(it, ix, REX_X);
2727 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002728
Keith Kanios48af1772007-08-17 07:37:52 +00002729 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002730 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002731 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002732
Keith Kaniosb7a89542007-04-12 02:40:54 +00002733 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002735 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002736 } else {
2737 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002738 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002739 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002740 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002741 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002742 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002743 mod = 1;
2744 else
2745 mod = 2;
2746 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002747
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002748 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002749 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2750 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002751 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002752 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002753 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002754
Keith Kaniosb7a89542007-04-12 02:40:54 +00002755 if (it == -1)
2756 index = 4, s = 1;
2757 else
2758 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002759
H. Peter Anvine2c80182005-01-15 22:15:51 +00002760 switch (s) {
2761 case 1:
2762 scale = 0;
2763 break;
2764 case 2:
2765 scale = 1;
2766 break;
2767 case 4:
2768 scale = 2;
2769 break;
2770 case 8:
2771 scale = 3;
2772 break;
2773 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002774 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002775 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002776
Keith Kaniosb7a89542007-04-12 02:40:54 +00002777 if (bt == -1) {
2778 base = 5;
2779 mod = 0;
2780 } else {
2781 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002782 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002783 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002784 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002785 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002786 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002787 mod = 1;
2788 else
2789 mod = 2;
2790 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002791
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002792 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002793 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2794 output->modrm = GEN_MODRM(mod, rfield, 4);
2795 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002796 }
2797 } else { /* it's 16-bit */
2798 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002799 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002800
Keith Kaniosb7a89542007-04-12 02:40:54 +00002801 /* check for 64-bit long mode */
2802 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002803 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002804
H. Peter Anvine2c80182005-01-15 22:15:51 +00002805 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002806 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2807 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002808 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002809
Keith Kaniosb7a89542007-04-12 02:40:54 +00002810 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002811 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002812 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002813
H. Peter Anvine2c80182005-01-15 22:15:51 +00002814 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002815 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002816 if (b == -1 && i != -1) {
2817 int tmp = b;
2818 b = i;
2819 i = tmp;
2820 } /* swap */
2821 if ((b == R_SI || b == R_DI) && i != -1) {
2822 int tmp = b;
2823 b = i;
2824 i = tmp;
2825 }
2826 /* have BX/BP as base, SI/DI index */
2827 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002828 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002829 if (i != -1 && b != -1 &&
2830 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002831 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002832 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002833 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002834
H. Peter Anvine2c80182005-01-15 22:15:51 +00002835 rm = -1;
2836 if (i != -1)
2837 switch (i * 256 + b) {
2838 case R_SI * 256 + R_BX:
2839 rm = 0;
2840 break;
2841 case R_DI * 256 + R_BX:
2842 rm = 1;
2843 break;
2844 case R_SI * 256 + R_BP:
2845 rm = 2;
2846 break;
2847 case R_DI * 256 + R_BP:
2848 rm = 3;
2849 break;
2850 } else
2851 switch (b) {
2852 case R_SI:
2853 rm = 4;
2854 break;
2855 case R_DI:
2856 rm = 5;
2857 break;
2858 case R_BP:
2859 rm = 6;
2860 break;
2861 case R_BX:
2862 rm = 7;
2863 break;
2864 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002865 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002866 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002867
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002868 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002869 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002870 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002871 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002872 mod = 1;
2873 else
2874 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002875
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002876 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002877 output->bytes = mod; /* bytes of offset needed */
2878 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002879 }
2880 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002881 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002882
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002883 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002884 return output->type;
2885
2886err:
2887 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002888}
2889
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002890static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002891{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002892 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002893 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002894
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002895 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002896
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002897 switch (ins->prefixes[PPS_ASIZE]) {
2898 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002899 valid &= 16;
2900 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002901 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002902 valid &= 32;
2903 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002904 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 valid &= 64;
2906 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002907 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002908 valid &= (addrbits == 32) ? 16 : 32;
2909 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002910 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002912 }
2913
2914 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002915 if (is_class(MEMORY, ins->oprs[j].type)) {
2916 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002917
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002918 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002919 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002920 i = 0;
2921 else
2922 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002923
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002924 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002925 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002926 b = 0;
2927 else
2928 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002929
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002930 if (ins->oprs[j].scale == 0)
2931 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002932
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002933 if (!i && !b) {
2934 int ds = ins->oprs[j].disp_size;
2935 if ((addrbits != 64 && ds > 8) ||
2936 (addrbits == 64 && ds == 16))
2937 valid &= ds;
2938 } else {
2939 if (!(REG16 & ~b))
2940 valid &= 16;
2941 if (!(REG32 & ~b))
2942 valid &= 32;
2943 if (!(REG64 & ~b))
2944 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002945
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002946 if (!(REG16 & ~i))
2947 valid &= 16;
2948 if (!(REG32 & ~i))
2949 valid &= 32;
2950 if (!(REG64 & ~i))
2951 valid &= 64;
2952 }
2953 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002954 }
2955
2956 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002957 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002958 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002959 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002960 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002961 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002962 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002963 /* Impossible... */
Cyrill Gorcunov00526d92018-11-25 01:32:22 +03002964 nasm_nonfatal("impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002965 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002966 }
2967
2968 defdisp = ins->addr_size == 16 ? 16 : 32;
2969
2970 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002971 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2972 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2973 /*
2974 * mem_offs sizes must match the address size; if not,
2975 * strip the MEM_OFFS bit and match only EA instructions
2976 */
2977 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2978 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002979 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002980}