blob: 30dc047e57992a49a9a7e760e300f292a93f3a23 [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])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800267 nasm_error(ERR_NONFATAL, "invalid %s prefix",
268 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700269}
270
271static const char *size_name(int size)
272{
273 switch (size) {
274 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700276 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400277 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700278 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400279 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700280 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700282 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400283 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700284 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400285 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700286 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400287 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700288 case 64:
289 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700290 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400291 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000292 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700293}
294
H. Peter Anvin285222f2017-03-01 13:27:33 -0800295static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400296{
H. Peter Anvin285222f2017-03-01 13:27:33 -0800297 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400298 "%s data exceeds bounds", size_name(size));
299}
300
301static void warn_overflow_const(int64_t data, int size)
302{
303 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800304 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400305}
306
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800307static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
308{
309 bool err;
310
311 switch (sign) {
312 case OUT_WRAP:
313 err = overflow_general(data, size);
314 break;
315 case OUT_SIGNED:
316 err = overflow_signed(data, size);
317 break;
318 case OUT_UNSIGNED:
319 err = overflow_unsigned(data, size);
320 break;
321 default:
322 panic();
323 break;
324 }
325
326 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800327 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800328}
329
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000330/*
331 * This routine wrappers the real output format's output routine,
332 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800333 * generator at the same time, flatten unnecessary relocations,
334 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000335 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700336static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000337{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000338 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700339 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700340 union {
341 uint8_t b[8];
342 uint64_t q;
343 } xdata;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700344 size_t asize, amax;
345 uint64_t zeropad = 0;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800346 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800347 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000348
H. Peter Anvina77692b2016-09-20 14:04:33 -0700349 if (!data->size)
350 return; /* Nothing to do */
351
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700352 /*
353 * Convert addresses to RAWDATA if possible
354 * XXX: not all backends want this for global symbols!!!!
355 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700356 switch (data->type) {
357 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800358 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800359 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800360 goto address;
361
362 case OUT_RELADDR:
363 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800364 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800365 goto address;
366
367 address:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700368 nasm_assert(data->size <= 8);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700369 asize = data->size;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700370 amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
Chang S. Bae427d8e32018-05-02 08:07:52 -0700371 if ((ofmt->flags & OFMT_KEEP_ADDR) == 0 && data->tsegment == fixseg &&
372 data->twrt == NO_SEG) {
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800373 warn_overflow_out(addrval, asize, data->sign);
Martin Storsjö869087d2017-05-22 13:54:20 +0300374 xdata.q = cpu_to_le64(addrval);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700375 data->data = xdata.b;
376 data->type = OUT_RAWDATA;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700377 asize = amax = 0; /* No longer an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700378 }
379 break;
380
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700381 case OUT_SEGMENT:
382 nasm_assert(data->size <= 8);
383 asize = data->size;
384 amax = 2;
385 break;
386
H. Peter Anvina77692b2016-09-20 14:04:33 -0700387 default:
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700388 asize = amax = 0; /* Not an address */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700389 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000390 }
391
Frank Kotlerabebb082003-09-06 04:45:37 +0000392 /*
393 * this call to src_get determines when we call the
394 * debug-format-specific "linenum" function
395 * it updates lineno and lnfname to the current values
396 * returning 0 if "same as last time", -2 if lnfname
397 * changed, and the amount by which lineno changed,
398 * if it did. thus, these variables must be static
399 */
400
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400401 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700402 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000403
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700404 if (asize > amax) {
405 if (data->type == OUT_RELADDR || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800406 nasm_error(ERR_NONFATAL,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700407 "%u-bit signed relocation unsupported by output format %s",
408 (unsigned int)(asize << 3), ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800409 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800410 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700411 "%u-bit %s relocation zero-extended from %u bits",
412 (unsigned int)(asize << 3),
413 data->type == OUT_SEGMENT ? "segment" : "unsigned",
414 (unsigned int)(amax << 3));
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800415 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700416 zeropad = data->size - amax;
417 data->size = amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800418 }
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700419 lfmt->output(data);
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700420 ofmt->output(data);
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700421 data->offset += data->size;
422 data->insoffs += data->size;
423
424 if (zeropad) {
425 data->type = OUT_ZERODATA;
426 data->size = zeropad;
427 lfmt->output(data);
428 ofmt->output(data);
429 data->offset += zeropad;
430 data->insoffs += zeropad;
431 data->size += zeropad; /* Restore original size value */
432 }
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000433}
434
H. Peter Anvina77692b2016-09-20 14:04:33 -0700435static inline void out_rawdata(struct out_data *data, const void *rawdata,
436 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400437{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700438 data->type = OUT_RAWDATA;
439 data->data = rawdata;
440 data->size = size;
441 out(data);
442}
443
444static void out_rawbyte(struct out_data *data, uint8_t byte)
445{
446 data->type = OUT_RAWDATA;
447 data->data = &byte;
448 data->size = 1;
449 out(data);
450}
451
452static inline void out_reserve(struct out_data *data, uint64_t size)
453{
454 data->type = OUT_RESERVE;
455 data->size = size;
456 out(data);
457}
458
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700459static void out_segment(struct out_data *data, const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700460{
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700461 if (opx->opflags & OPFLAG_RELATIVE)
462 nasm_error(ERR_NONFATAL, "segment references cannot be relative");
463
464 data->type = OUT_SEGMENT;
465 data->sign = OUT_UNSIGNED;
466 data->size = 2;
467 data->toffset = opx->offset;
468 data->tsegment = ofmt->segbase(opx->segment | 1);
469 data->twrt = opx->wrt;
470 out(data);
471}
472
473static void out_imm(struct out_data *data, const struct operand *opx,
474 int size, enum out_sign sign)
475{
476 if (opx->segment != NO_SEG && (opx->segment & 1)) {
477 /*
478 * This is actually a segment reference, but eval() has
479 * already called ofmt->segbase() for us. Sigh.
480 */
481 if (size < 2)
482 nasm_error(ERR_NONFATAL, "segment reference must be 16 bits");
483
484 data->type = OUT_SEGMENT;
485 } else {
486 data->type = (opx->opflags & OPFLAG_RELATIVE)
487 ? OUT_RELADDR : OUT_ADDRESS;
488 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700489 data->sign = sign;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700490 data->toffset = opx->offset;
491 data->tsegment = opx->segment;
492 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800493 /*
494 * XXX: improve this if at some point in the future we can
495 * distinguish the subtrahend in expressions like [foo - bar]
496 * where bar is a symbol in the current segment. However, at the
497 * current point, if OPFLAG_RELATIVE is set that subtraction has
498 * already occurred.
499 */
500 data->relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700501 data->size = size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700502 out(data);
503}
504
H. Peter Anvin164d2462017-02-20 02:39:56 -0800505static void out_reladdr(struct out_data *data, const struct operand *opx,
506 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700507{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800508 if (opx->opflags & OPFLAG_RELATIVE)
509 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
510
H. Peter Anvina77692b2016-09-20 14:04:33 -0700511 data->type = OUT_RELADDR;
512 data->sign = OUT_SIGNED;
513 data->size = size;
514 data->toffset = opx->offset;
515 data->tsegment = opx->segment;
516 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800517 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700518 out(data);
519}
520
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700521static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800522 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000523{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800524 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800525 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000526 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800527 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000528
H. Peter Anvin755f5212012-02-25 11:41:34 -0800529 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700530 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300531 if (!optimizing.level || (optimizing.flag & OPTIM_DISABLE_JMP_MATCH))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400532 return false;
Chang S. Baea5786342018-08-15 23:22:21 +0300533 if (optimizing.level < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400534 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700535
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800536 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100537
Victor van den Elzen154e5922009-02-25 17:32:00 +0100538 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100539 /* Be optimistic in pass 1 */
540 return true;
541
H. Peter Anvine2c80182005-01-15 22:15:51 +0000542 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700543 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000544
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700545 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800546 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
547
548 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
549 /* jmp short (opcode eb) cannot be used with bnd prefix. */
550 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800551 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800552 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800553 }
554
555 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000557
H. Peter Anvin04445362016-09-21 15:56:19 -0700558/* This is totally just a wild guess what is reasonable... */
559#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
560
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800561int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000562{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700563 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000564 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700565 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300566 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000567
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700568 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700569 data.offset = start;
570 data.segment = segment;
571 data.itemp = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700572 data.bits = bits;
573
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700574 wsize = db_bytes(instruction->opcode);
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300575 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000576 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000577
H. Peter Anvineba20a72002-04-30 20:53:55 +0000578 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000579 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700580
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700581 list_for_each(e, instruction->eops) {
582 if (e->type == EOT_DB_NUMBER) {
583 if (wsize > 8) {
584 nasm_error(ERR_NONFATAL,
585 "integer supplied to a DT, DO, DY or DZ"
586 " instruction");
587 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700588 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700589 data.inslen = data.size = wsize;
590 data.toffset = e->offset;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700591 data.twrt = e->wrt;
592 data.relbase = 0;
H. Peter Anvina7b6bfc2017-05-03 17:32:02 -0700593 if (e->segment != NO_SEG && (e->segment & 1)) {
594 data.tsegment = e->segment;
595 data.type = OUT_SEGMENT;
596 data.sign = OUT_UNSIGNED;
597 } else {
598 data.tsegment = e->segment;
599 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
600 data.sign = OUT_WRAP;
601 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700602 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000603 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700604 } else if (e->type == EOT_DB_STRING ||
605 e->type == EOT_DB_STRING_FREE) {
606 int align = e->stringlen % wsize;
607 if (align)
608 align = wsize - align;
609
610 data.insoffs = 0;
611 data.inslen = e->stringlen + align;
612
613 out_rawdata(&data, e->stringval, e->stringlen);
614 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700615 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000616 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700617 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700618 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000619 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700620 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700621 off_t base = 0;
622 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700623 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700624 char *buf = NULL;
625 size_t blk = 0; /* Buffered I/O block size */
626 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000627
H. Peter Anvin94ead272017-09-27 15:22:23 -0700628 if (!t)
629 goto done;
630
H. Peter Anvind81a2352016-09-21 14:03:18 -0700631 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400632 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800633 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000634 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700635 goto done;
636 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000637
H. Peter Anvind81a2352016-09-21 14:03:18 -0700638 len = nasm_file_size(fp);
639
640 if (len == (off_t)-1) {
641 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700642 fname);
643 goto close_done;
644 }
645
H. Peter Anvina77692b2016-09-20 14:04:33 -0700646 if (instruction->eops->next) {
647 base = instruction->eops->next->offset;
648 if (base >= len) {
649 len = 0;
650 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000651 len -= base;
652 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700653 len > (off_t)instruction->eops->next->next->offset)
654 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000655 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000656 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700657
H. Peter Anvina77692b2016-09-20 14:04:33 -0700658 lfmt->set_offset(data.offset);
659 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000660
H. Peter Anvind81a2352016-09-21 14:03:18 -0700661 if (!len)
662 goto end_incbin;
663
664 /* Try to map file data */
665 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700666 if (!map) {
667 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
668 buf = nasm_malloc(blk);
669 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700670
671 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700672 /*
673 * Consider these irrelevant for INCBIN, since it is fully
674 * possible that these might be (way) bigger than an int
675 * can hold; there is, however, no reason to widen these
676 * types just for INCBIN. data.inslen == 0 signals to the
677 * backend that these fields are meaningless, if at all
678 * needed.
679 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700680 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700681 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700682
H. Peter Anvind81a2352016-09-21 14:03:18 -0700683 if (map) {
684 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700685 } else if ((off_t)m == len) {
686 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700687 } else {
688 off_t l = len;
689
690 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700691 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700692 "`incbin': unable to seek on file `%s'",
693 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700694 goto end_incbin;
695 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700696 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700697 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700698 if (!m || feof(fp)) {
699 /*
700 * This shouldn't happen unless the file
701 * actually changes while we are reading
702 * it.
703 */
704 nasm_error(ERR_NONFATAL,
705 "`incbin': unexpected EOF while"
706 " reading file `%s'", fname);
707 goto end_incbin;
708 }
709 out_rawdata(&data, buf, m);
710 l -= m;
711 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700712 }
713 }
714 end_incbin:
715 lfmt->downlevel(LIST_INCBIN);
716 if (instruction->times > 1) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700717 lfmt->uplevel(LIST_TIMES);
718 lfmt->downlevel(LIST_TIMES);
719 }
720 if (ferror(fp)) {
721 nasm_error(ERR_NONFATAL,
722 "`incbin': error while"
723 " reading file `%s'", fname);
724 }
725 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700726 if (buf)
727 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700728 if (map)
729 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700730 fclose(fp);
731 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700732 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700733 ;
734 } else {
735 /* "Real" instruction */
736
737 /* Check to see if we need an address-size prefix */
738 add_asp(instruction, bits);
739
740 m = find_match(&temp, instruction, data.segment, data.offset, bits);
741
742 if (m == MOK_GOOD) {
743 /* Matches! */
744 int64_t insn_size = calcsize(data.segment, data.offset,
745 bits, instruction, temp);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700746 nasm_assert(insn_size >= 0);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700747
748 data.itemp = temp;
749 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700750 data.insoffs = 0;
751 data.inslen = insn_size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700752
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700753 gencode(&data, instruction);
754 nasm_assert(data.insoffs == insn_size);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700755 } else {
756 /* No match */
757 switch (m) {
758 case MERR_OPSIZEMISSING:
759 nasm_error(ERR_NONFATAL, "operation size not specified");
760 break;
761 case MERR_OPSIZEMISMATCH:
762 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
763 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700764 case MERR_BRNOTHERE:
765 nasm_error(ERR_NONFATAL,
766 "broadcast not permitted on this operand");
767 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700768 case MERR_BRNUMMISMATCH:
769 nasm_error(ERR_NONFATAL,
770 "mismatch in the number of broadcasting elements");
771 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700772 case MERR_MASKNOTHERE:
773 nasm_error(ERR_NONFATAL,
774 "mask not permitted on this operand");
775 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700776 case MERR_DECONOTHERE:
777 nasm_error(ERR_NONFATAL, "unsupported mode decorator for instruction");
778 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700779 case MERR_BADCPU:
780 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
781 break;
782 case MERR_BADMODE:
783 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
784 bits);
785 break;
786 case MERR_ENCMISMATCH:
787 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
788 break;
789 case MERR_BADBND:
790 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
791 break;
792 case MERR_BADREPNE:
793 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
794 (has_prefix(instruction, PPS_REP, P_REPNE) ?
795 "repne" : "repnz"));
796 break;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -0700797 case MERR_REGSETSIZE:
798 nasm_error(ERR_NONFATAL, "invalid register set size");
799 break;
800 case MERR_REGSET:
801 nasm_error(ERR_NONFATAL, "register set not valid for operand");
802 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700803 default:
804 nasm_error(ERR_NONFATAL,
805 "invalid combination of opcode and operands");
806 break;
807 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700808
809 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400810 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000811 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700812 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000813}
814
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800815int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000816{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000817 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700818 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000819
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400820 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000821 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000822
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700823 if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000824 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300825 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000826
H. Peter Anvine2c80182005-01-15 22:15:51 +0000827 isize = 0;
H. Peter Anvinaf9fe8f2017-05-01 21:44:24 -0700828 wsize = db_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700829 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000830
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400831 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000832 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000833
H. Peter Anvine2c80182005-01-15 22:15:51 +0000834 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400835 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000836 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400837 warn_overflow_const(e->offset, wsize);
838 } else if (e->type == EOT_DB_STRING ||
839 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000840 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000841
H. Peter Anvine2c80182005-01-15 22:15:51 +0000842 align = (-osize) % wsize;
843 if (align < 0)
844 align += wsize;
845 isize += osize + align;
846 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700847 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000848 }
849
H. Peter Anvine2c80182005-01-15 22:15:51 +0000850 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400851 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700852 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000853
H. Peter Anvind81a2352016-09-21 14:03:18 -0700854 len = nasm_file_size_by_path(fname);
855 if (len == (off_t)-1) {
856 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
857 fname);
858 return 0;
859 }
860
861 if (instruction->eops->next) {
862 if (len <= (off_t)instruction->eops->next->offset) {
863 len = 0;
864 } else {
865 len -= instruction->eops->next->offset;
866 if (instruction->eops->next->next &&
867 len > (off_t)instruction->eops->next->next->offset) {
868 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000869 }
870 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000871 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700872
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700873 len *= instruction->times;
874 instruction->times = 1; /* Tell the upper layer to not iterate */
875
H. Peter Anvind81a2352016-09-21 14:03:18 -0700876 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000877 }
878
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700879 /* Check to see if we need an address-size prefix */
880 add_asp(instruction, bits);
881
H. Peter Anvin23595f52009-07-25 17:44:25 -0700882 m = find_match(&temp, instruction, segment, offset, bits);
883 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400884 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700885 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700886 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400887 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000888 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000889}
890
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800891static void bad_hle_warn(const insn * ins, uint8_t hleok)
892{
893 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800894 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800895 static const enum whatwarn warn[2][4] =
896 {
897 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
898 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
899 };
900 unsigned int n;
901
902 n = (unsigned int)rep_pfx - P_XACQUIRE;
903 if (n > 1)
904 return; /* Not XACQUIRE/XRELEASE */
905
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800906 ww = warn[n][hleok];
907 if (!is_class(MEMORY, ins->oprs[0].type))
908 ww = w_inval; /* HLE requires operand 0 to be memory */
909
910 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800911 case w_none:
912 break;
913
914 case w_lock:
915 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800916 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800917 "%s with this instruction requires lock",
918 prefix_name(rep_pfx));
919 }
920 break;
921
922 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800923 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800924 "%s invalid with this instruction",
925 prefix_name(rep_pfx));
926 break;
927 }
928}
929
H. Peter Anvin507ae032008-10-09 15:37:10 -0700930/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400931#define case3(x) case (x): case (x)+1: case (x)+2
932#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700933
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800934static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800935 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000936{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800937 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800938 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000939 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000940 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700941 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700942 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700943 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700944 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800945 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800946 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700947 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -0700948 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000949
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700950 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700951 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700952 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700953
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700954 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400955 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700956
H. Peter Anvine2c80182005-01-15 22:15:51 +0000957 (void)segment; /* Don't warn that this parameter is unused */
958 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000959
H. Peter Anvin839eca22007-10-29 23:12:47 -0700960 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400961 c = *codes++;
962 op1 = (c & 3) + ((opex & 1) << 2);
963 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
964 opx = &ins->oprs[op1];
965 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700966
H. Peter Anvin839eca22007-10-29 23:12:47 -0700967 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400968 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 codes += c, length += c;
970 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700971
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400972 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400973 opex = c;
974 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700975
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400976 case4(010):
977 ins->rex |=
978 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 codes++, length++;
980 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700981
Jin Kyu Song164d6072013-10-15 19:10:13 -0700982 case4(014):
983 /* this is an index reg of MIB operand */
984 mib_index = opx->basereg;
985 break;
986
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400987 case4(020):
988 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 length++;
990 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700991
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400992 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000993 length += 2;
994 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700995
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400996 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700997 if (opx->type & (BITS16 | BITS32 | BITS64))
998 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000999 else
1000 length += (bits == 16) ? 2 : 4;
1001 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001002
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001003 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001004 length += 4;
1005 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001006
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001007 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001008 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001009 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001010
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001011 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001012 length++;
1013 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001014
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001015 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001016 length += 8; /* MOV reg64/imm */
1017 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001018
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001019 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001020 length += 2;
1021 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001022
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001023 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001024 if (opx->type & (BITS16 | BITS32 | BITS64))
1025 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001026 else
1027 length += (bits == 16) ? 2 : 4;
1028 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001029
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001030 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001031 length += 4;
1032 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001033
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001034 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001035 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001036 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001037
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001038 case 0172:
1039 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001040 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001041 length++;
1042 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001043
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001044 case4(0174):
1045 length++;
1046 break;
1047
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001048 case4(0240):
1049 ins->rex |= REX_EV;
1050 ins->vexreg = regval(opx);
1051 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1052 ins->vex_cm = *codes++;
1053 ins->vex_wlp = *codes++;
1054 ins->evex_tuple = (*codes++ - 0300);
1055 break;
1056
1057 case 0250:
1058 ins->rex |= REX_EV;
1059 ins->vexreg = 0;
1060 ins->vex_cm = *codes++;
1061 ins->vex_wlp = *codes++;
1062 ins->evex_tuple = (*codes++ - 0300);
1063 break;
1064
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001065 case4(0254):
1066 length += 4;
1067 break;
1068
1069 case4(0260):
1070 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001071 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001072 ins->vex_cm = *codes++;
1073 ins->vex_wlp = *codes++;
1074 break;
1075
1076 case 0270:
1077 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001078 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001079 ins->vex_cm = *codes++;
1080 ins->vex_wlp = *codes++;
1081 break;
1082
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001083 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001084 hleok = c & 3;
1085 break;
1086
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001087 case4(0274):
1088 length++;
1089 break;
1090
1091 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001092 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001093
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001095 if (bits == 64)
1096 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001097 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001099
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001101 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001102 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001103
H. Peter Anvine2c80182005-01-15 22:15:51 +00001104 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001105 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001106
Keith Kaniosb7a89542007-04-12 02:40:54 +00001107 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001108 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1109 has_prefix(ins, PPS_ASIZE, P_A32))
1110 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001111 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001112
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001113 case4(0314):
1114 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001115
H. Peter Anvine2c80182005-01-15 22:15:51 +00001116 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001117 {
1118 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1119 if (pfx == P_O16)
1120 break;
1121 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001122 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001123 else
1124 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001125 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001126 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001127
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001129 {
1130 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1131 if (pfx == P_O32)
1132 break;
1133 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001134 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001135 else
1136 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001137 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001138 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001139
H. Peter Anvine2c80182005-01-15 22:15:51 +00001140 case 0322:
1141 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001142
Keith Kaniosb7a89542007-04-12 02:40:54 +00001143 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001144 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001145 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001146
Keith Kaniosb7a89542007-04-12 02:40:54 +00001147 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001148 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001149 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001150
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001151 case 0325:
1152 ins->rex |= REX_NH;
1153 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001154
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001155 case 0326:
1156 break;
1157
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 case 0330:
1159 codes++, length++;
1160 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001161
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001163 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001164
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001165 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 case 0333:
1167 length++;
1168 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001169
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001170 case 0334:
1171 ins->rex |= REX_L;
1172 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001173
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001174 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001175 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001176
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001177 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001178 if (!ins->prefixes[PPS_REP])
1179 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001180 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001181
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001182 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001183 if (!ins->prefixes[PPS_REP])
1184 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001185 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001186
H. Peter Anvine2c80182005-01-15 22:15:51 +00001187 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001188 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001189 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001190 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001191 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1192 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001193 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001194 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001195 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001196 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001197
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001198 case 0341:
1199 if (!ins->prefixes[PPS_WAIT])
1200 ins->prefixes[PPS_WAIT] = P_WAIT;
1201 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001202
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001203 case 0360:
1204 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001205
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001206 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001207 length++;
1208 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001209
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001210 case 0364:
1211 case 0365:
1212 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001213
Keith Kanios48af1772007-08-17 07:37:52 +00001214 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001215 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001216 length++;
1217 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001218
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001219 case 0370:
1220 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001221 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001222
H. Peter Anvine2c80182005-01-15 22:15:51 +00001223 case 0373:
1224 length++;
1225 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001226
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001227 case 0374:
1228 eat = EA_XMMVSIB;
1229 break;
1230
1231 case 0375:
1232 eat = EA_YMMVSIB;
1233 break;
1234
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001235 case 0376:
1236 eat = EA_ZMMVSIB;
1237 break;
1238
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001239 case4(0100):
1240 case4(0110):
1241 case4(0120):
1242 case4(0130):
1243 case4(0200):
1244 case4(0204):
1245 case4(0210):
1246 case4(0214):
1247 case4(0220):
1248 case4(0224):
1249 case4(0230):
1250 case4(0234):
1251 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001252 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001253 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001254 opflags_t rflags;
1255 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001256 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001257
Keith Kaniosb7a89542007-04-12 02:40:54 +00001258 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001259
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001260 if (c <= 0177) {
1261 /* pick rfield from operand b (opx) */
1262 rflags = regflag(opx);
1263 rfield = nasm_regvals[opx->basereg];
1264 } else {
1265 rflags = 0;
1266 rfield = c & 7;
1267 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001268
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001269 /* EVEX.b1 : evex_brerop contains the operand position */
1270 op_er_sae = (ins->evex_brerop >= 0 ?
1271 &ins->oprs[ins->evex_brerop] : NULL);
1272
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001273 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1274 /* set EVEX.b */
1275 ins->evex_p[2] |= EVEX_P2B;
1276 if (op_er_sae->decoflags & ER) {
1277 /* set EVEX.RC (rounding control) */
1278 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1279 & EVEX_P2RC;
1280 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001281 } else {
1282 /* set EVEX.L'L (vector length) */
1283 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001284 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001285 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001286 /* set EVEX.b */
1287 ins->evex_p[2] |= EVEX_P2B;
1288 }
1289 }
1290
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001291 if (itemp_has(temp, IF_MIB)) {
1292 opy->eaflags |= EAF_MIB;
1293 /*
1294 * if a separate form of MIB (ICC style) is used,
1295 * the index reg info is merged into mem operand
1296 */
1297 if (mib_index != R_none) {
1298 opy->indexreg = mib_index;
1299 opy->scale = 1;
1300 opy->hintbase = mib_index;
1301 opy->hinttype = EAH_NOTBASE;
1302 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001303 }
1304
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001305 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001306 rfield, rflags, ins, &errmsg) != eat) {
1307 nasm_error(ERR_NONFATAL, "%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001308 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001309 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001310 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001311 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001312 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001313 }
1314 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001315
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001316 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001317 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001318 ": instruction code \\%o (0x%02X) given", c, c);
1319 break;
1320 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001321 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001322
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001323 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001324
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001325 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001326 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001327 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001328 return -1;
1329 }
1330 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001331 }
1332
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001333 switch (ins->prefixes[PPS_VEX]) {
1334 case P_EVEX:
1335 if (!(ins->rex & REX_EV))
1336 return -1;
1337 break;
1338 case P_VEX3:
1339 case P_VEX2:
1340 if (!(ins->rex & REX_V))
1341 return -1;
1342 break;
1343 default:
1344 break;
1345 }
1346
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001347 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001348 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001349
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001350 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001351 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001352 return -1;
1353 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001354 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001355 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001356 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001357 ins->rex &= ~REX_W;
1358 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001359 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001360 ins->rex |= REX_W;
1361 bad32 &= ~REX_W;
1362 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001363 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001364 /* Follow REX_W */
1365 break;
1366 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001367
H. Peter Anvinfc561202011-07-07 16:58:22 -07001368 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001369 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001370 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001371 } else if (!(ins->rex & REX_EV) &&
1372 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001373 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001374 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001375 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001376 if (ins->rex & REX_EV)
1377 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001378 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1379 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001380 length += 3;
1381 else
1382 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001383 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001384 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001385 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001386 return -1;
1387 } else if (bits == 64) {
1388 length++;
1389 } else if ((ins->rex & REX_L) &&
1390 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
H. Peter Anvina7ecf262018-02-06 14:43:07 -08001391 iflag_cpu_level_ok(&cpu, IF_X86_64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001392 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001393 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001394 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001395 length++;
1396 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001397 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001398 return -1;
1399 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001400 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001401
1402 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001403 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001404 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001405 "instruction is not lockable");
1406 }
1407
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001408 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001409
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001410 /*
1411 * when BND prefix is set by DEFAULT directive,
1412 * BND prefix is added to every appropriate instruction line
1413 * unless it is overridden by NOBND prefix.
1414 */
1415 if (globalbnd &&
1416 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1417 ins->prefixes[PPS_REP] = P_BND;
1418
H. Peter Anvina77692b2016-09-20 14:04:33 -07001419 /*
1420 * Add length of legacy prefixes
1421 */
1422 length += emit_prefix(NULL, bits, ins);
1423
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001424 return length;
1425}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001426
H. Peter Anvina77692b2016-09-20 14:04:33 -07001427static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001428{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001429 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001430 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001431 !(ins->rex & (REX_V | REX_EV)) &&
1432 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001433 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1434 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001435 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001436 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001437 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001438}
1439
H. Peter Anvina77692b2016-09-20 14:04:33 -07001440static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1441{
1442 int bytes = 0;
1443 int j;
1444
1445 for (j = 0; j < MAXPREFIX; j++) {
1446 uint8_t c = 0;
1447 switch (ins->prefixes[j]) {
1448 case P_WAIT:
1449 c = 0x9B;
1450 break;
1451 case P_LOCK:
1452 c = 0xF0;
1453 break;
1454 case P_REPNE:
1455 case P_REPNZ:
1456 case P_XACQUIRE:
1457 case P_BND:
1458 c = 0xF2;
1459 break;
1460 case P_REPE:
1461 case P_REPZ:
1462 case P_REP:
1463 case P_XRELEASE:
1464 c = 0xF3;
1465 break;
1466 case R_CS:
1467 if (bits == 64) {
1468 nasm_error(ERR_WARNING | ERR_PASS2,
1469 "cs segment base generated, but will be ignored in 64-bit mode");
1470 }
1471 c = 0x2E;
1472 break;
1473 case R_DS:
1474 if (bits == 64) {
1475 nasm_error(ERR_WARNING | ERR_PASS2,
1476 "ds segment base generated, but will be ignored in 64-bit mode");
1477 }
1478 c = 0x3E;
1479 break;
1480 case R_ES:
1481 if (bits == 64) {
1482 nasm_error(ERR_WARNING | ERR_PASS2,
1483 "es segment base generated, but will be ignored in 64-bit mode");
1484 }
1485 c = 0x26;
1486 break;
1487 case R_FS:
1488 c = 0x64;
1489 break;
1490 case R_GS:
1491 c = 0x65;
1492 break;
1493 case R_SS:
1494 if (bits == 64) {
1495 nasm_error(ERR_WARNING | ERR_PASS2,
1496 "ss segment base generated, but will be ignored in 64-bit mode");
1497 }
1498 c = 0x36;
1499 break;
1500 case R_SEGR6:
1501 case R_SEGR7:
1502 nasm_error(ERR_NONFATAL,
1503 "segr6 and segr7 cannot be used as prefixes");
1504 break;
1505 case P_A16:
1506 if (bits == 64) {
1507 nasm_error(ERR_NONFATAL,
1508 "16-bit addressing is not supported "
1509 "in 64-bit mode");
1510 } else if (bits != 16)
1511 c = 0x67;
1512 break;
1513 case P_A32:
1514 if (bits != 32)
1515 c = 0x67;
1516 break;
1517 case P_A64:
1518 if (bits != 64) {
1519 nasm_error(ERR_NONFATAL,
1520 "64-bit addressing is only supported "
1521 "in 64-bit mode");
1522 }
1523 break;
1524 case P_ASP:
1525 c = 0x67;
1526 break;
1527 case P_O16:
1528 if (bits != 16)
1529 c = 0x66;
1530 break;
1531 case P_O32:
1532 if (bits == 16)
1533 c = 0x66;
1534 break;
1535 case P_O64:
1536 /* REX.W */
1537 break;
1538 case P_OSP:
1539 c = 0x66;
1540 break;
1541 case P_EVEX:
1542 case P_VEX3:
1543 case P_VEX2:
1544 case P_NOBND:
1545 case P_none:
1546 break;
1547 default:
1548 nasm_panic(0, "invalid instruction prefix");
1549 }
1550 if (c) {
1551 if (data)
1552 out_rawbyte(data, c);
1553 bytes++;
1554 }
1555 }
1556 return bytes;
1557}
1558
1559static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001560{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001561 uint8_t c;
1562 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001563 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001564 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001565 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001566 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001567 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001568 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001569 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001570 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001571 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001572
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001573 ins->rex_done = false;
1574
H. Peter Anvina77692b2016-09-20 14:04:33 -07001575 emit_prefix(data, bits, ins);
1576
H. Peter Anvin839eca22007-10-29 23:12:47 -07001577 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001578 c = *codes++;
1579 op1 = (c & 3) + ((opex & 1) << 2);
1580 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1581 opx = &ins->oprs[op1];
1582 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001583
H. Peter Anvina77692b2016-09-20 14:04:33 -07001584
H. Peter Anvin839eca22007-10-29 23:12:47 -07001585 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 case 01:
1587 case 02:
1588 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001589 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001590 emit_rex(data, ins);
1591 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001592 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001594
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001595 case 05:
1596 case 06:
1597 case 07:
1598 opex = c;
1599 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001600
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001601 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001602 emit_rex(data, ins);
1603 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001604 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001605
Jin Kyu Song164d6072013-10-15 19:10:13 -07001606 case4(014):
1607 break;
1608
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001609 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001610 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001611 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001612
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001613 case4(024):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001614 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001615 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001616
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001617 case4(030):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001618 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001619 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001620
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001621 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001622 if (opx->type & (BITS16 | BITS32))
1623 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001624 else
1625 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001626 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001627 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001628
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001629 case4(040):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001630 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001631 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001632
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001633 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001634 size = ins->addr_size >> 3;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001635 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001636 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001637
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001638 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001639 if (opx->segment == data->segment) {
1640 int64_t delta = opx->offset - data->offset
1641 - (data->inslen - data->insoffs);
1642 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001643 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001644 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001645 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001646 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001647
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001648 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001649 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001650 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001651
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001653 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001654 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001655
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001656 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001657 if (opx->type & (BITS16 | BITS32 | BITS64))
1658 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001659 else
1660 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001661
1662 out_reladdr(data, opx, size);
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 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001666 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001667 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001668
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001669 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001670 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001671 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001672 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001673 out_segment(data, opx);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001674 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001675
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001676 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001677 {
1678 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1679 const struct operand *opy;
1680
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001681 c = *codes++;
1682 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001683 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001684 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001685 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001686 "non-absolute expression not permitted as argument %d",
1687 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001688 } else if (opy->offset & ~mask) {
1689 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1690 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001691 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001692 c = opy->offset & mask;
1693 goto emit_is4;
1694 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001695
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001696 case 0173:
1697 c = *codes++;
1698 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001699 c &= 15;
1700 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001701
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001702 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001703 c = 0;
1704 emit_is4:
1705 r = nasm_regvals[opx->basereg];
1706 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001707 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001708
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001709 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001710 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001711 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001712 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001713 "signed dword immediate exceeds bounds");
1714 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001715 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001716 break;
1717
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001718 case4(0240):
1719 case 0250:
1720 codes += 3;
1721 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1722 EVEX_P2Z | EVEX_P2AAA, 2);
1723 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1724 bytes[0] = 0x62;
1725 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001726 bytes[1] = ((((ins->rex & 7) << 5) |
1727 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001728 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001729 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1730 ((~ins->vexreg & 15) << 3) |
1731 (1 << 2) | (ins->vex_wlp & 3);
1732 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001733 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001734 break;
1735
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001736 case4(0260):
1737 case 0270:
1738 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001739 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1740 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001741 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1742 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1743 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001744 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001745 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001746 } else {
1747 bytes[0] = 0xc5;
1748 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001749 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001750 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001751 }
1752 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001753
H. Peter Anvine014f352012-02-25 22:35:19 -08001754 case 0271:
1755 case 0272:
1756 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001757 break;
1758
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001759 case4(0274):
1760 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001761 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001762 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001763
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001764 if (absolute_op(opx)) {
1765 if (ins->rex & REX_W)
1766 s = 64;
1767 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1768 s = 16;
1769 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1770 s = 32;
1771 else
1772 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001773
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001774 um = (uint64_t)2 << (s-1);
1775 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001776
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001777 if (uv > 127 && uv < (uint64_t)-128 &&
1778 (uv < um-128 || uv > um-1)) {
1779 /* If this wasn't explicitly byte-sized, warn as though we
1780 * had fallen through to the imm16/32/64 case.
1781 */
1782 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1783 "%s value exceeds bounds",
1784 (opx->type & BITS8) ? "signed byte" :
1785 s == 16 ? "word" :
1786 s == 32 ? "dword" :
1787 "signed dword");
1788 }
1789
1790 /* Output as a raw byte to avoid byte overflow check */
1791 out_rawbyte(data, (uint8_t)uv);
1792 } else {
1793 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001794 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001795 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001796 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001797
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001798 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001799 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001800
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001802 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1803 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001805
H. Peter Anvine2c80182005-01-15 22:15:51 +00001806 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001807 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1808 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001809 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001810
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 case 0312:
1812 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001813
Keith Kaniosb7a89542007-04-12 02:40:54 +00001814 case 0313:
1815 ins->rex = 0;
1816 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001817
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001818 case4(0314):
1819 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001820
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001822 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001824
H. Peter Anvine2c80182005-01-15 22:15:51 +00001825 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001826 case 0323:
1827 break;
1828
Keith Kaniosb7a89542007-04-12 02:40:54 +00001829 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001830 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001831 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001832
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001833 case 0325:
1834 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001835
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001836 case 0326:
1837 break;
1838
H. Peter Anvine2c80182005-01-15 22:15:51 +00001839 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001840 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001841 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001842
H. Peter Anvine2c80182005-01-15 22:15:51 +00001843 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001845
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001846 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001847 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001848 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001850
Keith Kanios48af1772007-08-17 07:37:52 +00001851 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001852 if (ins->rex & REX_R)
1853 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001854 ins->rex &= ~(REX_L|REX_R);
1855 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001856
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001857 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001858 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001859
H. Peter Anvin962e3052008-08-28 17:47:16 -07001860 case 0336:
1861 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001862 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001863
H. Peter Anvine2c80182005-01-15 22:15:51 +00001864 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001866 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001867
1868 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001869 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001870
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001871 case 0341:
1872 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001873
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001874 case 0360:
1875 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001876
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001877 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001878 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001879 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001880
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001881 case 0364:
1882 case 0365:
1883 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001884
Keith Kanios48af1772007-08-17 07:37:52 +00001885 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001886 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001887 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001888 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001889
Jin Kyu Song03041092013-10-15 19:38:51 -07001890 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001891 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001892
H. Peter Anvine2c80182005-01-15 22:15:51 +00001893 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001894 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001895 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001896
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001897 case 0374:
1898 eat = EA_XMMVSIB;
1899 break;
1900
1901 case 0375:
1902 eat = EA_YMMVSIB;
1903 break;
1904
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001905 case 0376:
1906 eat = EA_ZMMVSIB;
1907 break;
1908
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001909 case4(0100):
1910 case4(0110):
1911 case4(0120):
1912 case4(0130):
1913 case4(0200):
1914 case4(0204):
1915 case4(0210):
1916 case4(0214):
1917 case4(0220):
1918 case4(0224):
1919 case4(0230):
1920 case4(0234):
1921 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 ea ea_data;
1923 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001924 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001925 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001926 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001927
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001928 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001929 /* pick rfield from operand b (opx) */
1930 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001931 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001932 } else {
1933 /* rfield is constant */
1934 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001935 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001936 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001938 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001939 rfield, rflags, ins, &errmsg) != eat)
1940 nasm_error(ERR_NONFATAL, "%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07001941
H. Peter Anvine2c80182005-01-15 22:15:51 +00001942 p = bytes;
1943 *p++ = ea_data.modrm;
1944 if (ea_data.sib_present)
1945 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001946 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001947
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001948 /*
1949 * Make sure the address gets the right offset in case
1950 * the line breaks in the .lst file (BR 1197827)
1951 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001952
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001953 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001954 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001955 if (ea_data.disp8) {
1956 out_rawbyte(data, ea_data.disp8);
1957 } else if (ea_data.rip) {
1958 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001959 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001960 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001961
H. Peter Anvina77692b2016-09-20 14:04:33 -07001962 if (overflow_general(opy->offset, asize) ||
1963 signed_bits(opy->offset, ins->addr_size) !=
1964 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001965 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001966
H. Peter Anvina77692b2016-09-20 14:04:33 -07001967 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001968 (asize > ea_data.bytes)
1969 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001970 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001971 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001972 }
1973 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001974
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001975 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001976 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001977 ": instruction code \\%o (0x%02X) given", c, c);
1978 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001980 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001981}
1982
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001983static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001984{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001985 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001986 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001987 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001988}
1989
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001990static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001991{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001992 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001993 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001994 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001995}
1996
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001997static int op_rexflags(const operand * o, int mask)
1998{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001999 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002000 int val;
2001
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002002 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08002003 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002004
H. Peter Anvina4835d42008-05-20 14:21:29 -07002005 flags = nasm_reg_flags[o->basereg];
2006 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002007
2008 return rexflags(val, flags, mask);
2009}
2010
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002011static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002012{
2013 int rex = 0;
2014
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002015 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002016 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002017 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002018 rex |= REX_W;
2019 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2020 rex |= REX_H;
2021 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2022 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002023
2024 return rex & mask;
2025}
2026
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002027static int evexflags(int val, decoflags_t deco,
2028 int mask, uint8_t byte)
2029{
2030 int evex = 0;
2031
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002032 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002033 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002034 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002035 evex |= (EVEX_P0RP | EVEX_P0X);
2036 break;
2037 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002038 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002039 evex |= EVEX_P2VP;
2040 if (deco & Z)
2041 evex |= EVEX_P2Z;
2042 if (deco & OPMASK_MASK)
2043 evex |= deco & EVEX_P2AAA;
2044 break;
2045 }
2046 return evex & mask;
2047}
2048
2049static int op_evexflags(const operand * o, int mask, uint8_t byte)
2050{
2051 int val;
2052
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002053 val = nasm_regvals[o->basereg];
2054
2055 return evexflags(val, o->decoflags, mask, byte);
2056}
2057
H. Peter Anvin23595f52009-07-25 17:44:25 -07002058static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002059 insn *instruction,
2060 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002061{
2062 const struct itemplate *temp;
2063 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002064 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002065 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002066 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002067 int i;
2068
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002069 /* broadcasting uses a different data element size */
2070 for (i = 0; i < instruction->operands; i++)
2071 if (i == broadcast)
2072 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2073 else
2074 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002075
2076 merr = MERR_INVALOP;
2077
2078 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002079 temp->opcode != I_none; temp++) {
2080 m = matches(temp, instruction, bits);
2081 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002082 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002083 m = MOK_GOOD;
2084 else
2085 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002086 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002087 /*
2088 * Missing operand size and a candidate for fuzzy matching...
2089 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002090 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002091 if (i == broadcast)
2092 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2093 else
2094 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002095 opsizemissing = true;
2096 }
2097 if (m > merr)
2098 merr = m;
2099 if (merr == MOK_GOOD)
2100 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002101 }
2102
2103 /* No match, but see if we can get a fuzzy operand size match... */
2104 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002105 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002106
2107 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002108 /*
2109 * We ignore extrinsic operand sizes on registers, so we should
2110 * never try to fuzzy-match on them. This also resolves the case
2111 * when we have e.g. "xmmrm128" in two different positions.
2112 */
2113 if (is_class(REGISTER, instruction->oprs[i].type))
2114 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002115
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002116 /* This tests if xsizeflags[i] has more than one bit set */
2117 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2118 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002119
Jin Kyu Song7903c072013-10-30 03:00:12 -07002120 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002121 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002122 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2123 BITS32 : BITS64);
2124 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002125 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002126 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002127 }
2128
2129 /* Try matching again... */
2130 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002131 temp->opcode != I_none; temp++) {
2132 m = matches(temp, instruction, bits);
2133 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002134 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002135 m = MOK_GOOD;
2136 else
2137 m = MERR_INVALOP;
2138 }
2139 if (m > merr)
2140 merr = m;
2141 if (merr == MOK_GOOD)
2142 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002143 }
2144
H. Peter Anvina81655b2009-07-25 18:15:28 -07002145done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002146 *tempp = temp;
2147 return merr;
2148}
2149
Mark Charneydcaef4b2014-10-09 13:45:17 -04002150static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2151{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002152 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002153 uint8_t brcast_num;
2154
Mark Charneydcaef4b2014-10-09 13:45:17 -04002155 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002156 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002157 "size of broadcasting element is greater than 64 bits");
2158
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002159 /*
2160 * The shift term is to take care of the extra BITS80 inserted
2161 * between BITS64 and BITS128.
2162 */
2163 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2164 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002165
2166 return brcast_num;
2167}
2168
H. Peter Anvin65289e82009-07-25 17:25:11 -07002169static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002170 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002171{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002172 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin (Intel)e8ceba52018-06-28 02:20:11 -07002173 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002174 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002175
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002176 /*
2177 * Check the opcode
2178 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002179 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002180 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002181
2182 /*
2183 * Count the operands
2184 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002185 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002186 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002187
2188 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002189 * Is it legal?
2190 */
Chang S. Baea5786342018-08-15 23:22:21 +03002191 if (!(optimizing.level > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002192 return MERR_INVALOP;
2193
2194 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002195 * {evex} available?
2196 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002197 switch (instruction->prefixes[PPS_VEX]) {
2198 case P_EVEX:
2199 if (!itemp_has(itemp, IF_EVEX))
2200 return MERR_ENCMISMATCH;
2201 break;
2202 case P_VEX3:
2203 case P_VEX2:
2204 if (!itemp_has(itemp, IF_VEX))
2205 return MERR_ENCMISMATCH;
2206 break;
2207 default:
2208 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002209 }
2210
2211 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002212 * Check that no spurious colons or TOs are present
2213 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002214 for (i = 0; i < itemp->operands; i++)
2215 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002216 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002217
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002218 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002219 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002220 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002221 switch (itemp_smask(itemp)) {
2222 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002223 asize = BITS8;
2224 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002225 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002226 asize = BITS16;
2227 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002228 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002229 asize = BITS32;
2230 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002231 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002232 asize = BITS64;
2233 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002234 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002235 asize = BITS128;
2236 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002237 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002238 asize = BITS256;
2239 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002240 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002241 asize = BITS512;
2242 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002243 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002244 switch (bits) {
2245 case 16:
2246 asize = BITS16;
2247 break;
2248 case 32:
2249 asize = BITS32;
2250 break;
2251 case 64:
2252 asize = BITS64;
2253 break;
2254 default:
2255 asize = 0;
2256 break;
2257 }
2258 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002259 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002260 asize = 0;
2261 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002262 }
2263
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002264 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002265 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002266 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002267 memset(size, 0, sizeof size);
2268 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002269 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002270 /* S- flags apply to all operands */
2271 for (i = 0; i < MAX_OPERANDS; i++)
2272 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002273 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002274
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002275 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002276 * Check that the operand flags all match up,
2277 * it's a bit tricky so lets be verbose:
2278 *
2279 * 1) Find out the size of operand. If instruction
2280 * doesn't have one specified -- we're trying to
2281 * guess it either from template (IF_S* flag) or
2282 * from code bits.
2283 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002284 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002285 * template has an operand size specified AND this size differ
2286 * from which instruction has (perhaps we got it from code bits)
2287 * we are:
2288 * a) Check that only size of instruction and operand is differ
2289 * other characteristics do match
2290 * b) Perhaps it's a register specified in instruction so
2291 * for such a case we just mark that operand as "size
2292 * missing" and this will turn on fuzzy operand size
2293 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002294 */
2295 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002296 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002297 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002298 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002299 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002300 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002301 opflags_t template_opsize, insn_opsize;
2302
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002303 if (!(type & SIZE_MASK))
2304 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002305
Jin Kyu Song7903c072013-10-30 03:00:12 -07002306 insn_opsize = type & SIZE_MASK;
2307 if (!is_broadcast) {
2308 template_opsize = itemp->opd[i] & SIZE_MASK;
2309 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002310 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2311
2312 if (~ideco & BRDCAST_MASK)
2313 return MERR_BRNOTHERE;
2314
Jin Kyu Song7903c072013-10-30 03:00:12 -07002315 /*
2316 * when broadcasting, the element size depends on
2317 * the instruction type. decorator flag should match.
2318 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002319 if (deco_brsize) {
2320 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002321 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002322 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002323 } else {
2324 template_opsize = 0;
2325 }
2326 }
2327
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002328 if (~ideco & deco & OPMASK_MASK)
2329 return MERR_MASKNOTHERE;
2330
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002331 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2332 return MERR_DECONOTHERE;
2333
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002334 if (itemp->opd[i] & ~type & ~(SIZE_MASK|REGSET_MASK))
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002335 return MERR_INVALOP;
H. Peter Anvincd26fcc2018-06-25 17:15:08 -07002336
2337 if (~itemp->opd[i] & type & REGSET_MASK)
2338 return (itemp->opd[i] & REGSET_MASK)
2339 ? MERR_REGSETSIZE : MERR_REGSET;
2340
2341 if (template_opsize) {
Jin Kyu Song7903c072013-10-30 03:00:12 -07002342 if (template_opsize != insn_opsize) {
2343 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002344 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002345 } else if (!is_class(REGISTER, type)) {
2346 /*
2347 * Note: we don't honor extrinsic operand sizes for registers,
2348 * so "missing operand size" for a register should be
2349 * considered a wildcard match rather than an error.
2350 */
H. Peter Anvin (Intel)e8ceba52018-06-28 02:20:11 -07002351 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002352 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002353 } else if (is_broadcast &&
2354 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002355 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002356 /*
2357 * broadcasting opsize matches but the number of repeated memory
2358 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002359 * if 64b double precision float is broadcasted to ymm (256b),
2360 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002361 */
2362 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002363 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002364 }
2365 }
2366
H. Peter Anvin (Intel)e8ceba52018-06-28 02:20:11 -07002367 if (opsizemissing)
2368 return MERR_OPSIZEMISSING;
2369
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002370 /*
2371 * Check operand sizes
2372 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002373 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2374 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002375 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002376 asize = itemp->opd[i] & SIZE_MASK;
2377 if (asize) {
2378 for (i = 0; i < oprs; i++)
2379 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 break;
2381 }
2382 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002383 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002384 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002385 }
2386
Keith Kaniosb7a89542007-04-12 02:40:54 +00002387 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002388 if (!(itemp->opd[i] & SIZE_MASK) &&
2389 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002390 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002391 }
2392
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002393 /*
2394 * Check template is okay at the set cpu level
2395 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002396 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002397 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002398
Keith Kaniosb7a89542007-04-12 02:40:54 +00002399 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002400 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002401 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002402 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002403 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002404
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002405 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002406 * If we have a HLE prefix, look for the NOHLE flag
2407 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002408 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002409 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2410 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2411 return MERR_BADHLE;
2412
2413 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002414 * Check if special handling needed for Jumps
2415 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002416 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002417 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002418
Jin Kyu Song03041092013-10-15 19:38:51 -07002419 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002420 * Check if BND prefix is allowed.
2421 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002422 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002423 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002424 (has_prefix(instruction, PPS_REP, P_BND) ||
2425 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002426 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002427 else if (itemp_has(itemp, IF_BND) &&
2428 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2429 has_prefix(instruction, PPS_REP, P_REPNZ)))
2430 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002431
H. Peter Anvin60926242009-07-26 16:25:38 -07002432 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002433}
2434
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002435/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002436 * Check if ModR/M.mod should/can be 01.
2437 * - EAF_BYTEOFFS is set
2438 * - offset can fit in a byte when EVEX is not used
2439 * - offset can be compressed when EVEX is used
2440 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002441#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2442 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2443 is_disp8n(input, ins, &output->disp8) : \
2444 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2445 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002446
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002447static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002448 int rfield, opflags_t rflags, insn *ins,
2449 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002450{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002451 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002452 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002453 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002454
H. Peter Anvin8f622462017-04-02 19:02:29 -07002455 *errmsg = "invalid effective address"; /* Default error message */
2456
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002457 output->type = EA_SCALAR;
2458 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002459 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002460
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002461 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002462 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002463 /* EVEX.R' flag for the REG operand */
2464 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002465
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002466 if (is_class(REGISTER, input->type)) {
2467 /*
2468 * It's a direct register.
2469 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002470 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002471 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002472
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002473 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002474 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002475
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002476 /* broadcasting is not available with a direct register operand. */
2477 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002478 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002479 goto err;
2480 }
2481
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002482 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002483 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002484 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002485 output->bytes = 0; /* no offset necessary either */
2486 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2487 } else {
2488 /*
2489 * It's a memory reference.
2490 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002491
2492 /* Embedded rounding or SAE is not available with a mem ref operand. */
2493 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002494 *errmsg = "embedded rounding is available only with "
2495 "register-register operations";
2496 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002497 }
2498
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002499 if (input->basereg == -1 &&
2500 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002501 /*
2502 * It's a pure offset.
2503 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002504 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002505 if (input->segment == NO_SEG ||
2506 (input->opflags & OPFLAG_RELATIVE)) {
2507 nasm_error(ERR_WARNING | ERR_PASS2,
2508 "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002509 input->type &= ~IP_REL;
2510 input->type |= MEMORY;
2511 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002512 }
2513
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002514 if (bits == 64 &&
2515 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002516 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002517 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002518 }
2519
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002520 if (eaflags & EAF_BYTEOFFS ||
2521 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002522 input->disp_size != (addrbits != 16 ? 32 : 16))) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002523 nasm_error(ERR_WARNING | ERR_PASS1,
2524 "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002525 }
2526
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002527 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002528 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002529 output->sib = GEN_SIB(0, 4, 5);
2530 output->bytes = 4;
2531 output->modrm = GEN_MODRM(0, rfield, 4);
2532 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002533 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002534 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002535 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002536 output->modrm = GEN_MODRM(0, rfield,
2537 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002538 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002539 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002540 } else {
2541 /*
2542 * It's an indirection.
2543 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002544 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002545 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002546 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002547 int t, it, bt; /* register numbers */
2548 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002549
H. Peter Anvine2c80182005-01-15 22:15:51 +00002550 if (s == 0)
2551 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002552
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002553 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002554 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002555 ix = nasm_reg_flags[i];
2556 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002557 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002558 ix = 0;
2559 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002560
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002561 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002562 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002563 bx = nasm_reg_flags[b];
2564 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002565 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002566 bx = 0;
2567 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002568
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002569 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002570 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002571 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002572 int32_t o = input->offset;
2573 int mod, scale, index, base;
2574
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002575 /*
2576 * For a vector SIB, one has to be a vector and the other,
2577 * if present, a GPR. The vector must be the index operand.
2578 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002579 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002580 if (s == 0)
2581 s = 1;
2582 else if (s != 1)
2583 goto err;
2584
2585 t = bt, bt = it, it = t;
2586 x = bx, bx = ix, ix = x;
2587 }
2588
2589 if (bt != -1) {
2590 if (REG_GPR & ~bx)
2591 goto err;
2592 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2593 sok &= bx;
2594 else
2595 goto err;
2596 }
2597
2598 /*
2599 * While we're here, ensure the user didn't specify
2600 * WORD or QWORD
2601 */
2602 if (input->disp_size == 16 || input->disp_size == 64)
2603 goto err;
2604
2605 if (addrbits == 16 ||
2606 (addrbits == 32 && !(sok & BITS32)) ||
2607 (addrbits == 64 && !(sok & BITS64)))
2608 goto err;
2609
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002610 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2611 : ((ix & YMMREG & ~REG_EA)
2612 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002613
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002614 output->rex |= rexflags(it, ix, REX_X);
2615 output->rex |= rexflags(bt, bx, REX_B);
2616 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002617
2618 index = it & 7; /* it is known to be != -1 */
2619
2620 switch (s) {
2621 case 1:
2622 scale = 0;
2623 break;
2624 case 2:
2625 scale = 1;
2626 break;
2627 case 4:
2628 scale = 2;
2629 break;
2630 case 8:
2631 scale = 3;
2632 break;
2633 default: /* then what the smeg is it? */
2634 goto err; /* panic */
2635 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002636
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002637 if (bt == -1) {
2638 base = 5;
2639 mod = 0;
2640 } else {
2641 base = (bt & 7);
2642 if (base != REG_NUM_EBP && o == 0 &&
2643 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002644 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002645 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002646 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002647 mod = 1;
2648 else
2649 mod = 2;
2650 }
2651
2652 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002653 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2654 output->modrm = GEN_MODRM(mod, rfield, 4);
2655 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002656 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002657 /*
2658 * it must be a 32/64-bit memory reference. Firstly we have
2659 * to check that all registers involved are type E/Rxx.
2660 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002661 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002662 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002663
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002664 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002665 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2666 sok &= ix;
2667 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002668 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002669 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002670
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002671 if (bt != -1) {
2672 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002673 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002674 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002675 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002676 sok &= bx;
2677 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002678
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002679 /*
2680 * While we're here, ensure the user didn't specify
2681 * WORD or QWORD
2682 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002683 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002684 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002685
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002686 if (addrbits == 16 ||
2687 (addrbits == 32 && !(sok & BITS32)) ||
2688 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002689 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002690
Keith Kaniosb7a89542007-04-12 02:40:54 +00002691 /* now reorganize base/index */
2692 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002693 ((hb == b && ht == EAH_NOTBASE) ||
2694 (hb == i && ht == EAH_MAKEBASE))) {
2695 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002696 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002697 x = bx, bx = ix, ix = x;
2698 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002699
Jin Kyu Song164d6072013-10-15 19:10:13 -07002700 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002701 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002702 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002703 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002704 if (eaflags & EAF_MIB) {
2705 /* only for mib operands */
2706 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2707 /*
2708 * make a single reg index [reg*1].
2709 * gas uses this form for an explicit index register.
2710 */
2711 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2712 }
2713 if ((ht == EAH_SUMMED) && bt == -1) {
2714 /* separate once summed index into [base, index] */
2715 bt = it, bx = ix, s--;
2716 }
2717 } else {
2718 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002719 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002720 s == 3 || s == 5 || s == 9) && bt == -1) {
2721 /* convert 3*EAX to EAX+2*EAX */
2722 bt = it, bx = ix, s--;
2723 }
2724 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002725 (eaflags & EAF_TIMESTWO) &&
2726 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002727 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002728 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002729 * to sib format with 0x0 displacement - [EAX*1+0].
2730 */
2731 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2732 }
2733 }
Keith Kanios48af1772007-08-17 07:37:52 +00002734 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002735 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002736 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002737 x = ix, ix = bx, bx = x;
2738 }
2739 if (it == REG_NUM_ESP ||
2740 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002741 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002742
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002743 output->rex |= rexflags(it, ix, REX_X);
2744 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002745
Keith Kanios48af1772007-08-17 07:37:52 +00002746 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002747 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002748 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002749
Keith Kaniosb7a89542007-04-12 02:40:54 +00002750 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002751 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002752 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002753 } else {
2754 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002755 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002756 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002757 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002758 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002759 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002760 mod = 1;
2761 else
2762 mod = 2;
2763 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002764
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002765 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002766 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2767 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002768 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002769 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002770 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002771
Keith Kaniosb7a89542007-04-12 02:40:54 +00002772 if (it == -1)
2773 index = 4, s = 1;
2774 else
2775 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002776
H. Peter Anvine2c80182005-01-15 22:15:51 +00002777 switch (s) {
2778 case 1:
2779 scale = 0;
2780 break;
2781 case 2:
2782 scale = 1;
2783 break;
2784 case 4:
2785 scale = 2;
2786 break;
2787 case 8:
2788 scale = 3;
2789 break;
2790 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002791 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002792 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002793
Keith Kaniosb7a89542007-04-12 02:40:54 +00002794 if (bt == -1) {
2795 base = 5;
2796 mod = 0;
2797 } else {
2798 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002799 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002800 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002801 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002802 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002803 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002804 mod = 1;
2805 else
2806 mod = 2;
2807 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002808
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002809 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002810 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2811 output->modrm = GEN_MODRM(mod, rfield, 4);
2812 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002813 }
2814 } else { /* it's 16-bit */
2815 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002816 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002817
Keith Kaniosb7a89542007-04-12 02:40:54 +00002818 /* check for 64-bit long mode */
2819 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002820 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002821
H. Peter Anvine2c80182005-01-15 22:15:51 +00002822 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002823 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2824 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002825 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002826
Keith Kaniosb7a89542007-04-12 02:40:54 +00002827 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002828 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002829 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002830
H. Peter Anvine2c80182005-01-15 22:15:51 +00002831 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002832 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002833 if (b == -1 && i != -1) {
2834 int tmp = b;
2835 b = i;
2836 i = tmp;
2837 } /* swap */
2838 if ((b == R_SI || b == R_DI) && i != -1) {
2839 int tmp = b;
2840 b = i;
2841 i = tmp;
2842 }
2843 /* have BX/BP as base, SI/DI index */
2844 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002845 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002846 if (i != -1 && b != -1 &&
2847 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002848 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002849 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002850 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002851
H. Peter Anvine2c80182005-01-15 22:15:51 +00002852 rm = -1;
2853 if (i != -1)
2854 switch (i * 256 + b) {
2855 case R_SI * 256 + R_BX:
2856 rm = 0;
2857 break;
2858 case R_DI * 256 + R_BX:
2859 rm = 1;
2860 break;
2861 case R_SI * 256 + R_BP:
2862 rm = 2;
2863 break;
2864 case R_DI * 256 + R_BP:
2865 rm = 3;
2866 break;
2867 } else
2868 switch (b) {
2869 case R_SI:
2870 rm = 4;
2871 break;
2872 case R_DI:
2873 rm = 5;
2874 break;
2875 case R_BP:
2876 rm = 6;
2877 break;
2878 case R_BX:
2879 rm = 7;
2880 break;
2881 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002882 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002883 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002884
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002885 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002886 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002887 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002888 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002889 mod = 1;
2890 else
2891 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002892
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002893 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002894 output->bytes = mod; /* bytes of offset needed */
2895 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002896 }
2897 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002898 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002899
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002900 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002901 return output->type;
2902
2903err:
2904 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002905}
2906
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002907static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002908{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002909 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002910 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002911
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002912 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002913
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002914 switch (ins->prefixes[PPS_ASIZE]) {
2915 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002916 valid &= 16;
2917 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002918 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002919 valid &= 32;
2920 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002921 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002922 valid &= 64;
2923 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002924 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002925 valid &= (addrbits == 32) ? 16 : 32;
2926 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002927 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002928 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002929 }
2930
2931 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002932 if (is_class(MEMORY, ins->oprs[j].type)) {
2933 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002934
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002935 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002936 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002937 i = 0;
2938 else
2939 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002940
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002941 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002942 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002943 b = 0;
2944 else
2945 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002946
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002947 if (ins->oprs[j].scale == 0)
2948 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002949
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002950 if (!i && !b) {
2951 int ds = ins->oprs[j].disp_size;
2952 if ((addrbits != 64 && ds > 8) ||
2953 (addrbits == 64 && ds == 16))
2954 valid &= ds;
2955 } else {
2956 if (!(REG16 & ~b))
2957 valid &= 16;
2958 if (!(REG32 & ~b))
2959 valid &= 32;
2960 if (!(REG64 & ~b))
2961 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002962
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002963 if (!(REG16 & ~i))
2964 valid &= 16;
2965 if (!(REG32 & ~i))
2966 valid &= 32;
2967 if (!(REG64 & ~i))
2968 valid &= 64;
2969 }
2970 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002971 }
2972
2973 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002974 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002975 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002976 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002977 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002978 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002979 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002980 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002981 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002982 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002983 }
2984
2985 defdisp = ins->addr_size == 16 ? 16 : 32;
2986
2987 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002988 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2989 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2990 /*
2991 * mem_offs sizes must match the address size; if not,
2992 * strip the MEM_OFFS bit and match only EA instructions
2993 */
2994 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2995 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002996 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002997}