blob: 851ea8228eb51bfb95c826f02a725352cf620a95 [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvin164d2462017-02-20 02:39:56 -08003 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07004 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00006 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07007 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
9 * conditions are met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
Cyrill Gorcunov1de95002009-11-06 00:08:38 +030017 *
H. Peter Anvin9e6747c2009-06-28 17:13:04 -070018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 * ----------------------------------------------------------------------- */
33
34/*
35 * assemble.c code generation for the Netwide Assembler
H. Peter Anvinea6e34d2002-04-30 20:51:32 +000036 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040037 * Bytecode specification
38 * ----------------------
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070039 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +040040 *
41 * Codes Mnemonic Explanation
42 *
43 * \0 terminates the code. (Unless it's a literal of course.)
44 * \1..\4 that many literal bytes follow in the code stream
45 * \5 add 4 to the primary operand number (b, low octdigit)
46 * \6 add 4 to the secondary operand number (a, middle octdigit)
47 * \7 add 4 to both the primary and the secondary operand number
48 * \10..\13 a literal byte follows in the code stream, to be added
49 * to the register value of operand 0..3
50 * \14..\17 the position of index register operand in MIB (BND insns)
51 * \20..\23 ib a byte immediate operand, from operand 0..3
52 * \24..\27 ib,u a zero-extended byte immediate operand, from operand 0..3
53 * \30..\33 iw a word immediate operand, from operand 0..3
54 * \34..\37 iwd select between \3[0-3] and \4[0-3] depending on 16/32 bit
55 * assembly mode or the operand-size override on the operand
56 * \40..\43 id a long immediate operand, from operand 0..3
57 * \44..\47 iwdq select between \3[0-3], \4[0-3] and \5[4-7]
58 * depending on the address size of the instruction.
59 * \50..\53 rel8 a byte relative operand, from operand 0..3
60 * \54..\57 iq a qword immediate operand, from operand 0..3
61 * \60..\63 rel16 a word relative operand, from operand 0..3
62 * \64..\67 rel select between \6[0-3] and \7[0-3] depending on 16/32 bit
63 * assembly mode or the operand-size override on the operand
64 * \70..\73 rel32 a long relative operand, from operand 0..3
65 * \74..\77 seg a word constant, from the _segment_ part of operand 0..3
66 * \1ab a ModRM, calculated on EA in operand a, with the spare
67 * field the register value of operand b.
68 * \172\ab the register number from operand a in bits 7..4, with
69 * the 4-bit immediate from operand b in bits 3..0.
70 * \173\xab the register number from operand a in bits 7..4, with
71 * the value b in bits 3..0.
72 * \174..\177 the register number from operand 0..3 in bits 7..4, and
73 * an arbitrary value in bits 3..0 (assembled as zero.)
74 * \2ab a ModRM, calculated on EA in operand a, with the spare
75 * field equal to digit b.
76 *
77 * \240..\243 this instruction uses EVEX rather than REX or VEX/XOP, with the
78 * V field taken from operand 0..3.
79 * \250 this instruction uses EVEX rather than REX or VEX/XOP, with the
80 * V field set to 1111b.
81 *
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070082 * EVEX prefixes are followed by the sequence:
83 * \cm\wlp\tup where cm is:
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -070084 * cc 00m mmm
85 * c = 2 for EVEX and mmmm is the M field (EVEX.P0[3:0])
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -070086 * and wlp is:
87 * 00 wwl lpp
88 * [l0] ll = 0 (.128, .lz)
89 * [l1] ll = 1 (.256)
90 * [l2] ll = 2 (.512)
91 * [lig] ll = 3 for EVEX.L'L don't care (always assembled as 0)
92 *
93 * [w0] ww = 0 for W = 0
94 * [w1] ww = 1 for W = 1
95 * [wig] ww = 2 for W don't care (always assembled as 0)
96 * [ww] ww = 3 for W used as REX.W
97 *
98 * [p0] pp = 0 for no prefix
99 * [60] pp = 1 for legacy prefix 60
100 * [f3] pp = 2
101 * [f2] pp = 3
102 *
103 * tup is tuple type for Disp8*N from %tuple_codes in insns.pl
104 * (compressed displacement encoding)
105 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400106 * \254..\257 id,s a signed 32-bit operand to be extended to 64 bits.
107 * \260..\263 this instruction uses VEX/XOP rather than REX, with the
108 * V field taken from operand 0..3.
109 * \270 this instruction uses VEX/XOP rather than REX, with the
110 * V field set to 1111b.
H. Peter Anvind85d2502008-05-04 17:53:31 -0700111 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700112 * VEX/XOP prefixes are followed by the sequence:
113 * \tmm\wlp where mm is the M field; and wlp is:
H. Peter Anvin421059c2010-08-16 14:56:33 -0700114 * 00 wwl lpp
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700115 * [l0] ll = 0 for L = 0 (.128, .lz)
116 * [l1] ll = 1 for L = 1 (.256)
117 * [lig] ll = 2 for L don't care (always assembled as 0)
H. Peter Anvin421059c2010-08-16 14:56:33 -0700118 *
H. Peter Anvin978c2172010-08-16 13:48:43 -0700119 * [w0] ww = 0 for W = 0
120 * [w1 ] ww = 1 for W = 1
121 * [wig] ww = 2 for W don't care (always assembled as 0)
122 * [ww] ww = 3 for W used as REX.W
H. Peter Anvinbd420c72008-05-22 11:24:35 -0700123 *
H. Peter Anvina04019c2009-05-03 21:42:34 -0700124 * t = 0 for VEX (C4/C5), t = 1 for XOP (8F).
H. Peter Anvind85d2502008-05-04 17:53:31 -0700125 *
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400126 * \271 hlexr instruction takes XRELEASE (F3) with or without lock
127 * \272 hlenl instruction takes XACQUIRE/XRELEASE with or without lock
128 * \273 hle instruction takes XACQUIRE/XRELEASE with lock only
129 * \274..\277 ib,s a byte immediate operand, from operand 0..3, sign-extended
130 * to the operand size (if o16/o32/o64 present) or the bit size
131 * \310 a16 indicates fixed 16-bit address size, i.e. optional 0x67.
132 * \311 a32 indicates fixed 32-bit address size, i.e. optional 0x67.
133 * \312 adf (disassembler only) invalid with non-default address size.
134 * \313 a64 indicates fixed 64-bit address size, 0x67 invalid.
135 * \314 norexb (disassembler only) invalid with REX.B
136 * \315 norexx (disassembler only) invalid with REX.X
137 * \316 norexr (disassembler only) invalid with REX.R
138 * \317 norexw (disassembler only) invalid with REX.W
139 * \320 o16 indicates fixed 16-bit operand size, i.e. optional 0x66.
140 * \321 o32 indicates fixed 32-bit operand size, i.e. optional 0x66.
141 * \322 odf indicates that this instruction is only valid when the
142 * operand size is the default (instruction to disassembler,
143 * generates no code in the assembler)
144 * \323 o64nw indicates fixed 64-bit operand size, REX on extensions only.
145 * \324 o64 indicates 64-bit operand size requiring REX prefix.
146 * \325 nohi instruction which always uses spl/bpl/sil/dil
147 * \326 nof3 instruction not valid with 0xF3 REP prefix. Hint for
148 disassembler only; for SSE instructions.
149 * \330 a literal byte follows in the code stream, to be added
150 * to the condition code value of the instruction.
151 * \331 norep instruction not valid with REP prefix. Hint for
152 * disassembler only; for SSE instructions.
153 * \332 f2i REP prefix (0xF2 byte) used as opcode extension.
154 * \333 f3i REP prefix (0xF3 byte) used as opcode extension.
155 * \334 rex.l LOCK prefix used as REX.R (used in non-64-bit mode)
156 * \335 repe disassemble a rep (0xF3 byte) prefix as repe not rep.
157 * \336 mustrep force a REP(E) prefix (0xF3) even if not specified.
158 * \337 mustrepne force a REPNE prefix (0xF2) even if not specified.
159 * \336-\337 are still listed as prefixes in the disassembler.
160 * \340 resb reserve <operand 0> bytes of uninitialized storage.
161 * Operand 0 had better be a segmentless constant.
162 * \341 wait this instruction needs a WAIT "prefix"
Cyrill Gorcunov8a5d3e62014-08-25 20:04:30 +0400163 * \360 np no SSE prefix (== \364\331)
Cyrill Gorcunov5d488a32014-08-25 17:50:53 +0400164 * \361 66 SSE prefix (== \366\331)
165 * \364 !osp operand-size prefix (0x66) not permitted
166 * \365 !asp address-size prefix (0x67) not permitted
167 * \366 operand-size prefix (0x66) used as opcode extension
168 * \367 address-size prefix (0x67) used as opcode extension
169 * \370,\371 jcc8 match only if operand 0 meets byte jump criteria.
170 * jmp8 370 is used for Jcc, 371 is used for JMP.
171 * \373 jlen assemble 0x03 if bits==16, 0x05 if bits==32;
172 * used for conditional jump over longer jump
173 * \374 vsibx|vm32x|vm64x this instruction takes an XMM VSIB memory EA
174 * \375 vsiby|vm32y|vm64y this instruction takes an YMM VSIB memory EA
175 * \376 vsibz|vm32z|vm64z this instruction takes an ZMM VSIB memory EA
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000176 */
177
H. Peter Anvinfe501952007-10-02 21:53:51 -0700178#include "compiler.h"
179
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000180#include <stdio.h>
181#include <string.h>
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800182#include <stdlib.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000183
184#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000185#include "nasmlib.h"
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800186#include "error.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000187#include "assemble.h"
188#include "insns.h"
H. Peter Anvina4835d42008-05-20 14:21:29 -0700189#include "tables.h"
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -0800190#include "disp8.h"
H. Peter Anvin172b8402016-02-18 01:16:18 -0800191#include "listing.h"
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000192
H. Peter Anvin65289e82009-07-25 17:25:11 -0700193enum match_result {
194 /*
195 * Matching errors. These should be sorted so that more specific
196 * errors come later in the sequence.
197 */
198 MERR_INVALOP,
199 MERR_OPSIZEMISSING,
200 MERR_OPSIZEMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700201 MERR_BRNOTHERE,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700202 MERR_BRNUMMISMATCH,
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700203 MERR_MASKNOTHERE,
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700204 MERR_DECONOTHERE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700205 MERR_BADCPU,
206 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800207 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700208 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700209 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800210 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700211 /*
212 * Matching success; the conditional ones first
213 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400214 MOK_JUMP, /* Matching OK but needs jmp_match() */
215 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700216};
217
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000218typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700219 enum ea_type type; /* what kind of EA is this? */
220 int sib_present; /* is a SIB byte necessary? */
221 int bytes; /* # of bytes of offset needed */
222 int size; /* lazy - this is sib+bytes+1 */
223 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700224 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000225} ea;
226
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400227#define GEN_SIB(scale, index, base) \
228 (((scale) << 6) | ((index) << 3) | ((base)))
229
230#define GEN_MODRM(mod, reg, rm) \
231 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
232
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800233static int64_t calcsize(int32_t, int64_t, int, insn *,
234 const struct itemplate *);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700235static int emit_prefix(struct out_data *data, const int bits, insn *ins);
236static void gencode(struct out_data *data, insn *ins);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700237static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400238 insn *instruction,
239 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700240static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700241static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000242static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700243static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000244static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700245static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700246static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000247
H. Peter Anvin8f622462017-04-02 19:02:29 -0700248static enum ea_type process_ea(operand *, ea *, int, int,
249 opflags_t, insn *, const char **);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700250
H. Peter Anvin164d2462017-02-20 02:39:56 -0800251static inline bool absolute_op(const struct operand *o)
252{
253 return o->segment == NO_SEG && o->wrt == NO_SEG &&
254 !(o->opflags & OPFLAG_RELATIVE);
255}
256
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400257static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000258{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700259 return ins->prefixes[pos] == prefix;
260}
261
262static void assert_no_prefix(insn * ins, enum prefix_pos pos)
263{
264 if (ins->prefixes[pos])
H. Peter Anvin215186f2016-02-17 20:27:41 -0800265 nasm_error(ERR_NONFATAL, "invalid %s prefix",
266 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700267}
268
269static const char *size_name(int size)
270{
271 switch (size) {
272 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400273 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700274 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400275 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700276 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400277 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700278 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400279 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700280 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400281 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700282 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400283 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700284 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400285 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700286 case 64:
287 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700288 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400289 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000290 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700291}
292
H. Peter Anvin285222f2017-03-01 13:27:33 -0800293static void warn_overflow(int size)
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400294{
H. Peter Anvin285222f2017-03-01 13:27:33 -0800295 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400296 "%s data exceeds bounds", size_name(size));
297}
298
299static void warn_overflow_const(int64_t data, int size)
300{
301 if (overflow_general(data, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800302 warn_overflow(size);
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400303}
304
305static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700306{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800307 if (absolute_op(o)) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400308 if (overflow_general(o->offset, size))
H. Peter Anvin285222f2017-03-01 13:27:33 -0800309 warn_overflow(size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700310 }
311}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400312
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800313static void warn_overflow_out(int64_t data, int size, enum out_sign sign)
314{
315 bool err;
316
317 switch (sign) {
318 case OUT_WRAP:
319 err = overflow_general(data, size);
320 break;
321 case OUT_SIGNED:
322 err = overflow_signed(data, size);
323 break;
324 case OUT_UNSIGNED:
325 err = overflow_unsigned(data, size);
326 break;
327 default:
328 panic();
329 break;
330 }
331
332 if (err)
H. Peter Anvin285222f2017-03-01 13:27:33 -0800333 warn_overflow(size);
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800334}
335
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000336/*
337 * This routine wrappers the real output format's output routine,
338 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800339 * generator at the same time, flatten unnecessary relocations,
340 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000341 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700342static void out(struct out_data *data)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000343{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000344 static int32_t lineno = 0; /* static!!! */
H. Peter Anvin274cda82016-05-10 02:56:29 -0700345 static const char *lnfname = NULL;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700346 int asize;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800347 const int amax = ofmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700348 union {
349 uint8_t b[8];
350 uint64_t q;
351 } xdata;
352 uint64_t size = data->size;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800353 int64_t addrval;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800354 int32_t fixseg; /* Segment for which to produce fixed data */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000355
H. Peter Anvina77692b2016-09-20 14:04:33 -0700356 if (!data->size)
357 return; /* Nothing to do */
358
H. Peter Anvin472a7c12016-10-31 08:44:25 -0700359 /*
360 * Convert addresses to RAWDATA if possible
361 * XXX: not all backends want this for global symbols!!!!
362 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700363 switch (data->type) {
364 case OUT_ADDRESS:
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800365 addrval = data->toffset;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800366 fixseg = NO_SEG; /* Absolute address is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800367 goto address;
368
369 case OUT_RELADDR:
370 addrval = data->toffset - data->relbase;
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800371 fixseg = data->segment; /* Our own segment is fixed data */
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800372 goto address;
373
374 address:
H. Peter Anvina77692b2016-09-20 14:04:33 -0700375 asize = data->size;
376 nasm_assert(asize <= 8);
H. Peter Anvinc5cbb972017-02-21 11:53:15 -0800377 if (data->tsegment == fixseg && data->twrt == NO_SEG) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700378 uint8_t *q = xdata.b;
379
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800380 warn_overflow_out(addrval, asize, data->sign);
H. Peter Anvind85d2502008-05-04 17:53:31 -0700381
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800382 WRITEADDR(q, addrval, asize);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700383 data->data = xdata.b;
384 data->type = OUT_RAWDATA;
385 asize = 0; /* No longer an address */
386 }
387 break;
388
389 default:
390 asize = 0; /* Not an address */
391 break;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000392 }
393
H. Peter Anvina77692b2016-09-20 14:04:33 -0700394 lfmt->output(data);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800395
Frank Kotlerabebb082003-09-06 04:45:37 +0000396 /*
397 * this call to src_get determines when we call the
398 * debug-format-specific "linenum" function
399 * it updates lineno and lnfname to the current values
400 * returning 0 if "same as last time", -2 if lnfname
401 * changed, and the amount by which lineno changed,
402 * if it did. thus, these variables must be static
403 */
404
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400405 if (src_get(&lineno, &lnfname))
H. Peter Anvina77692b2016-09-20 14:04:33 -0700406 dfmt->linenum(lnfname, lineno, data->segment);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000407
H. Peter Anvinb6412502016-02-11 21:07:40 -0800408 if (asize && asize > amax) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700409 if (data->type != OUT_ADDRESS || data->sign == OUT_SIGNED) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800410 nasm_error(ERR_NONFATAL,
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800411 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800412 asize << 3, ofmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800413 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800414 nasm_error(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800415 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin215186f2016-02-17 20:27:41 -0800416 asize << 3, ofmt->maxbits);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700417 data->size = amax;
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700418 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700419 data->insoffs += amax;
420 data->offset += amax;
421 data->size = size = asize - amax;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800422 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700423 data->data = zero_buffer;
424 data->type = OUT_RAWDATA;
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800425 }
426
H. Peter Anvinfa803ab2016-09-24 09:46:47 -0700427 ofmt->output(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700428 data->offset += size;
429 data->insoffs += size;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000430}
431
H. Peter Anvina77692b2016-09-20 14:04:33 -0700432static inline void out_rawdata(struct out_data *data, const void *rawdata,
433 size_t size)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400434{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700435 data->type = OUT_RAWDATA;
436 data->data = rawdata;
437 data->size = size;
438 out(data);
439}
440
441static void out_rawbyte(struct out_data *data, uint8_t byte)
442{
443 data->type = OUT_RAWDATA;
444 data->data = &byte;
445 data->size = 1;
446 out(data);
447}
448
449static inline void out_reserve(struct out_data *data, uint64_t size)
450{
451 data->type = OUT_RESERVE;
452 data->size = size;
453 out(data);
454}
455
H. Peter Anvin164d2462017-02-20 02:39:56 -0800456static inline void out_imm(struct out_data *data, const struct operand *opx,
H. Peter Anvina77692b2016-09-20 14:04:33 -0700457 int size, enum out_sign sign)
458{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800459 data->type =
460 (opx->opflags & OPFLAG_RELATIVE) ? OUT_RELADDR : OUT_ADDRESS;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700461 data->sign = sign;
462 data->size = size;
463 data->toffset = opx->offset;
464 data->tsegment = opx->segment;
465 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800466 /*
467 * XXX: improve this if at some point in the future we can
468 * distinguish the subtrahend in expressions like [foo - bar]
469 * where bar is a symbol in the current segment. However, at the
470 * current point, if OPFLAG_RELATIVE is set that subtraction has
471 * already occurred.
472 */
473 data->relbase = 0;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700474 out(data);
475}
476
H. Peter Anvin164d2462017-02-20 02:39:56 -0800477static void out_reladdr(struct out_data *data, const struct operand *opx,
478 int size)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700479{
H. Peter Anvin164d2462017-02-20 02:39:56 -0800480 if (opx->opflags & OPFLAG_RELATIVE)
481 nasm_error(ERR_NONFATAL, "invalid use of self-relative expression");
482
H. Peter Anvina77692b2016-09-20 14:04:33 -0700483 data->type = OUT_RELADDR;
484 data->sign = OUT_SIGNED;
485 data->size = size;
486 data->toffset = opx->offset;
487 data->tsegment = opx->segment;
488 data->twrt = opx->wrt;
H. Peter Anvin8930a8f2017-02-21 11:30:22 -0800489 data->relbase = data->offset + (data->inslen - data->insoffs);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700490 out(data);
491}
492
H. Peter Anvin164d2462017-02-20 02:39:56 -0800493static inline void out_segment(struct out_data *data,
494 const struct operand *opx)
H. Peter Anvina77692b2016-09-20 14:04:33 -0700495{
496 data->type = OUT_SEGMENT;
497 data->sign = OUT_UNSIGNED;
498 data->size = 2;
H. Peter Anvin217e7142017-05-01 15:10:47 -0700499 data->toffset = opx->offset; /* Is this really needed/wanted? */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700500 data->tsegment = ofmt->segbase(opx->segment + 1);
501 data->twrt = opx->wrt;
502 out(data);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400503}
504
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700505static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800506 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000507{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800508 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800509 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000510 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800511 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000512
H. Peter Anvin755f5212012-02-25 11:41:34 -0800513 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700514 return false;
515 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400516 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700517 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400518 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700519
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800520 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100521
Victor van den Elzen154e5922009-02-25 17:32:00 +0100522 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100523 /* Be optimistic in pass 1 */
524 return true;
525
H. Peter Anvine2c80182005-01-15 22:15:51 +0000526 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700527 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000528
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700529 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800530 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
531
532 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
533 /* jmp short (opcode eb) cannot be used with bnd prefix. */
534 ins->prefixes[PPS_REP] = P_none;
H. Peter Anvin215186f2016-02-17 20:27:41 -0800535 nasm_error(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800536 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800537 }
538
539 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000540}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000541
H. Peter Anvin04445362016-09-21 15:56:19 -0700542/* This is totally just a wild guess what is reasonable... */
543#define INCBIN_MAX_BUF (ZERO_BUF_SIZE * 16)
544
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800545int64_t assemble(int32_t segment, int64_t start, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000546{
H. Peter Anvina77692b2016-09-20 14:04:33 -0700547 struct out_data data;
H. Peter Anvin3360d792007-09-11 04:16:57 +0000548 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700549 enum match_result m;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300550 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000551
H. Peter Anvine886c0e2017-03-31 14:56:17 -0700552 nasm_zero(data);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700553 data.offset = start;
554 data.segment = segment;
555 data.itemp = NULL;
556 data.sign = OUT_WRAP;
557 data.bits = bits;
558
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300559 wsize = idata_bytes(instruction->opcode);
560 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000561 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000562
H. Peter Anvineba20a72002-04-30 20:53:55 +0000563 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000564 extop *e;
H. Peter Anvin5810c592017-05-01 19:51:09 -0700565
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700566 list_for_each(e, instruction->eops) {
567 if (e->type == EOT_DB_NUMBER) {
568 if (wsize > 8) {
569 nasm_error(ERR_NONFATAL,
570 "integer supplied to a DT, DO, DY or DZ"
571 " instruction");
572 } else {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700573 data.insoffs = 0;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700574 data.type = e->relative ? OUT_RELADDR : OUT_ADDRESS;
575 data.inslen = data.size = wsize;
576 data.toffset = e->offset;
577 data.tsegment = e->segment;
578 data.twrt = e->wrt;
579 data.relbase = 0;
580 out(&data);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000581 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700582 } else if (e->type == EOT_DB_STRING ||
583 e->type == EOT_DB_STRING_FREE) {
584 int align = e->stringlen % wsize;
585 if (align)
586 align = wsize - align;
587
588 data.insoffs = 0;
589 data.inslen = e->stringlen + align;
590
591 out_rawdata(&data, e->stringval, e->stringlen);
592 out_rawdata(&data, zero_buffer, align);
H. Peter Anvin5f93c952017-05-01 19:44:34 -0700593 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000594 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700595 } else if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700596 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000597 FILE *fp;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700598 size_t t = instruction->times; /* INCBIN handles TIMES by itself */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700599 off_t base = 0;
600 off_t len;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700601 const void *map = NULL;
H. Peter Anvin04445362016-09-21 15:56:19 -0700602 char *buf = NULL;
603 size_t blk = 0; /* Buffered I/O block size */
604 size_t m = 0; /* Bytes last read */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000605
H. Peter Anvind81a2352016-09-21 14:03:18 -0700606 fp = nasm_open_read(fname, NF_BINARY|NF_FORMAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400607 if (!fp) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800608 nasm_error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
H. Peter Anvine2c80182005-01-15 22:15:51 +0000609 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700610 goto done;
611 }
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000612
H. Peter Anvind81a2352016-09-21 14:03:18 -0700613 len = nasm_file_size(fp);
614
615 if (len == (off_t)-1) {
616 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
H. Peter Anvina77692b2016-09-20 14:04:33 -0700617 fname);
618 goto close_done;
619 }
620
H. Peter Anvina77692b2016-09-20 14:04:33 -0700621 if (instruction->eops->next) {
622 base = instruction->eops->next->offset;
623 if (base >= len) {
624 len = 0;
625 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000626 len -= base;
627 if (instruction->eops->next->next &&
H. Peter Anvina77692b2016-09-20 14:04:33 -0700628 len > (off_t)instruction->eops->next->next->offset)
629 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000630 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000631 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700632
H. Peter Anvina77692b2016-09-20 14:04:33 -0700633 lfmt->set_offset(data.offset);
634 lfmt->uplevel(LIST_INCBIN);
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000635
H. Peter Anvind81a2352016-09-21 14:03:18 -0700636 if (!len)
637 goto end_incbin;
638
639 /* Try to map file data */
640 map = nasm_map_file(fp, base, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700641 if (!map) {
642 blk = len < (off_t)INCBIN_MAX_BUF ? (size_t)len : INCBIN_MAX_BUF;
643 buf = nasm_malloc(blk);
644 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700645
646 while (t--) {
H. Peter Anvin96921a52016-09-24 09:53:03 -0700647 /*
648 * Consider these irrelevant for INCBIN, since it is fully
649 * possible that these might be (way) bigger than an int
650 * can hold; there is, however, no reason to widen these
651 * types just for INCBIN. data.inslen == 0 signals to the
652 * backend that these fields are meaningless, if at all
653 * needed.
654 */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700655 data.insoffs = 0;
H. Peter Anvin96921a52016-09-24 09:53:03 -0700656 data.inslen = 0;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700657
H. Peter Anvind81a2352016-09-21 14:03:18 -0700658 if (map) {
659 out_rawdata(&data, map, len);
H. Peter Anvin04445362016-09-21 15:56:19 -0700660 } else if ((off_t)m == len) {
661 out_rawdata(&data, buf, len);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700662 } else {
663 off_t l = len;
664
665 if (fseeko(fp, base, SEEK_SET) < 0 || ferror(fp)) {
H. Peter Anvina77692b2016-09-20 14:04:33 -0700666 nasm_error(ERR_NONFATAL,
H. Peter Anvind81a2352016-09-21 14:03:18 -0700667 "`incbin': unable to seek on file `%s'",
668 fname);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700669 goto end_incbin;
670 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700671 while (l > 0) {
H. Peter Anvin04445362016-09-21 15:56:19 -0700672 m = fread(buf, 1, l < (off_t)blk ? (size_t)l : blk, fp);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700673 if (!m || feof(fp)) {
674 /*
675 * This shouldn't happen unless the file
676 * actually changes while we are reading
677 * it.
678 */
679 nasm_error(ERR_NONFATAL,
680 "`incbin': unexpected EOF while"
681 " reading file `%s'", fname);
682 goto end_incbin;
683 }
684 out_rawdata(&data, buf, m);
685 l -= m;
686 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700687 }
688 }
689 end_incbin:
690 lfmt->downlevel(LIST_INCBIN);
691 if (instruction->times > 1) {
H. Peter Anvin5810c592017-05-01 19:51:09 -0700692 lfmt->set_offset(start);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700693 lfmt->uplevel(LIST_TIMES);
694 lfmt->downlevel(LIST_TIMES);
695 }
696 if (ferror(fp)) {
697 nasm_error(ERR_NONFATAL,
698 "`incbin': error while"
699 " reading file `%s'", fname);
700 }
701 close_done:
H. Peter Anvin04445362016-09-21 15:56:19 -0700702 if (buf)
703 nasm_free(buf);
H. Peter Anvind81a2352016-09-21 14:03:18 -0700704 if (map)
705 nasm_unmap_file(map, len);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700706 fclose(fp);
707 done:
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700708 instruction->times = 1; /* Tell the upper layer not to iterate */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700709 ;
710 } else {
711 /* "Real" instruction */
712
713 /* Check to see if we need an address-size prefix */
714 add_asp(instruction, bits);
715
716 m = find_match(&temp, instruction, data.segment, data.offset, bits);
717
718 if (m == MOK_GOOD) {
719 /* Matches! */
720 int64_t insn_size = calcsize(data.segment, data.offset,
721 bits, instruction, temp);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700722 nasm_assert(insn_size >= 0);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700723
724 data.itemp = temp;
725 data.bits = bits;
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700726 data.insoffs = 0;
727 data.inslen = insn_size;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700728
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700729 gencode(&data, instruction);
730 nasm_assert(data.insoffs == insn_size);
H. Peter Anvina77692b2016-09-20 14:04:33 -0700731 } else {
732 /* No match */
733 switch (m) {
734 case MERR_OPSIZEMISSING:
735 nasm_error(ERR_NONFATAL, "operation size not specified");
736 break;
737 case MERR_OPSIZEMISMATCH:
738 nasm_error(ERR_NONFATAL, "mismatch in operand sizes");
739 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700740 case MERR_BRNOTHERE:
741 nasm_error(ERR_NONFATAL,
742 "broadcast not permitted on this operand");
743 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700744 case MERR_BRNUMMISMATCH:
745 nasm_error(ERR_NONFATAL,
746 "mismatch in the number of broadcasting elements");
747 break;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -0700748 case MERR_MASKNOTHERE:
749 nasm_error(ERR_NONFATAL,
750 "mask not permitted on this operand");
751 break;
H. Peter Anvinff04a9f2017-08-16 21:48:52 -0700752 case MERR_DECONOTHERE:
753 nasm_error(ERR_NONFATAL, "unsupported mode decorator for instruction");
754 break;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700755 case MERR_BADCPU:
756 nasm_error(ERR_NONFATAL, "no instruction for this cpu level");
757 break;
758 case MERR_BADMODE:
759 nasm_error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
760 bits);
761 break;
762 case MERR_ENCMISMATCH:
763 nasm_error(ERR_NONFATAL, "specific encoding scheme not available");
764 break;
765 case MERR_BADBND:
766 nasm_error(ERR_NONFATAL, "bnd prefix is not allowed");
767 break;
768 case MERR_BADREPNE:
769 nasm_error(ERR_NONFATAL, "%s prefix is not allowed",
770 (has_prefix(instruction, PPS_REP, P_REPNE) ?
771 "repne" : "repnz"));
772 break;
773 default:
774 nasm_error(ERR_NONFATAL,
775 "invalid combination of opcode and operands");
776 break;
777 }
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700778
779 instruction->times = 1; /* Avoid repeated error messages */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400780 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000781 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700782 return data.offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000783}
784
H. Peter Anvinb20bc732017-03-07 19:23:03 -0800785int64_t insn_size(int32_t segment, int64_t offset, int bits, insn *instruction)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000786{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000787 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700788 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000789
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400790 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000791 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000792
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700793 if (opcode_is_db(instruction->opcode)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000794 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300795 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000796
H. Peter Anvine2c80182005-01-15 22:15:51 +0000797 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300798 wsize = idata_bytes(instruction->opcode);
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700799 nasm_assert(wsize > 0);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000800
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400801 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000802 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000803
H. Peter Anvine2c80182005-01-15 22:15:51 +0000804 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400805 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000806 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400807 warn_overflow_const(e->offset, wsize);
808 } else if (e->type == EOT_DB_STRING ||
809 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000811
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 align = (-osize) % wsize;
813 if (align < 0)
814 align += wsize;
815 isize += osize + align;
816 }
H. Peter Anvina77692b2016-09-20 14:04:33 -0700817 return isize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000818 }
819
H. Peter Anvine2c80182005-01-15 22:15:51 +0000820 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400821 const char *fname = instruction->eops->stringval;
H. Peter Anvina77692b2016-09-20 14:04:33 -0700822 off_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000823
H. Peter Anvind81a2352016-09-21 14:03:18 -0700824 len = nasm_file_size_by_path(fname);
825 if (len == (off_t)-1) {
826 nasm_error(ERR_NONFATAL, "`incbin': unable to get length of file `%s'",
827 fname);
828 return 0;
829 }
830
831 if (instruction->eops->next) {
832 if (len <= (off_t)instruction->eops->next->offset) {
833 len = 0;
834 } else {
835 len -= instruction->eops->next->offset;
836 if (instruction->eops->next->next &&
837 len > (off_t)instruction->eops->next->next->offset) {
838 len = (off_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000839 }
840 }
H. Peter Anvine2c80182005-01-15 22:15:51 +0000841 }
H. Peter Anvind81a2352016-09-21 14:03:18 -0700842
H. Peter Anvin3e458a82017-05-01 20:28:29 -0700843 len *= instruction->times;
844 instruction->times = 1; /* Tell the upper layer to not iterate */
845
H. Peter Anvind81a2352016-09-21 14:03:18 -0700846 return len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000847 }
848
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700849 /* Check to see if we need an address-size prefix */
850 add_asp(instruction, bits);
851
H. Peter Anvin23595f52009-07-25 17:44:25 -0700852 m = find_match(&temp, instruction, segment, offset, bits);
853 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400854 /* we've matched an instruction. */
H. Peter Anvina77692b2016-09-20 14:04:33 -0700855 return calcsize(segment, offset, bits, instruction, temp);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700856 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400857 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000858 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000859}
860
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800861static void bad_hle_warn(const insn * ins, uint8_t hleok)
862{
863 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800864 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800865 static const enum whatwarn warn[2][4] =
866 {
867 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
868 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
869 };
870 unsigned int n;
871
872 n = (unsigned int)rep_pfx - P_XACQUIRE;
873 if (n > 1)
874 return; /* Not XACQUIRE/XRELEASE */
875
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800876 ww = warn[n][hleok];
877 if (!is_class(MEMORY, ins->oprs[0].type))
878 ww = w_inval; /* HLE requires operand 0 to be memory */
879
880 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800881 case w_none:
882 break;
883
884 case w_lock:
885 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin215186f2016-02-17 20:27:41 -0800886 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800887 "%s with this instruction requires lock",
888 prefix_name(rep_pfx));
889 }
890 break;
891
892 case w_inval:
H. Peter Anvin215186f2016-02-17 20:27:41 -0800893 nasm_error(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800894 "%s invalid with this instruction",
895 prefix_name(rep_pfx));
896 break;
897 }
898}
899
H. Peter Anvin507ae032008-10-09 15:37:10 -0700900/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400901#define case3(x) case (x): case (x)+1: case (x)+2
902#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700903
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800904static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800905 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000906{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800907 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800908 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000909 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000910 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700911 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700912 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700913 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700914 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800915 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800916 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700917 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvin8f622462017-04-02 19:02:29 -0700918 const char *errmsg;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000919
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700920 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700921 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700922 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700923
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700924 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400925 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700926
H. Peter Anvine2c80182005-01-15 22:15:51 +0000927 (void)segment; /* Don't warn that this parameter is unused */
928 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000929
H. Peter Anvin839eca22007-10-29 23:12:47 -0700930 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400931 c = *codes++;
932 op1 = (c & 3) + ((opex & 1) << 2);
933 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
934 opx = &ins->oprs[op1];
935 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700936
H. Peter Anvin839eca22007-10-29 23:12:47 -0700937 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400938 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000939 codes += c, length += c;
940 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700941
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400942 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400943 opex = c;
944 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700945
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400946 case4(010):
947 ins->rex |=
948 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000949 codes++, length++;
950 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700951
Jin Kyu Song164d6072013-10-15 19:10:13 -0700952 case4(014):
953 /* this is an index reg of MIB operand */
954 mib_index = opx->basereg;
955 break;
956
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400957 case4(020):
958 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000959 length++;
960 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700961
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400962 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000963 length += 2;
964 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700965
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400966 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700967 if (opx->type & (BITS16 | BITS32 | BITS64))
968 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000969 else
970 length += (bits == 16) ? 2 : 4;
971 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700972
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400973 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000974 length += 4;
975 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700976
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400977 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700978 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000979 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700980
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400981 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000982 length++;
983 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700984
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400985 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +0000986 length += 8; /* MOV reg64/imm */
987 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700988
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400989 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000990 length += 2;
991 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700992
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400993 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700994 if (opx->type & (BITS16 | BITS32 | BITS64))
995 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000996 else
997 length += (bits == 16) ? 2 : 4;
998 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700999
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001000 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001001 length += 4;
1002 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001003
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001004 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001005 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001006 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001007
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001008 case 0172:
1009 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001010 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001011 length++;
1012 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001013
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001014 case4(0174):
1015 length++;
1016 break;
1017
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001018 case4(0240):
1019 ins->rex |= REX_EV;
1020 ins->vexreg = regval(opx);
1021 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1022 ins->vex_cm = *codes++;
1023 ins->vex_wlp = *codes++;
1024 ins->evex_tuple = (*codes++ - 0300);
1025 break;
1026
1027 case 0250:
1028 ins->rex |= REX_EV;
1029 ins->vexreg = 0;
1030 ins->vex_cm = *codes++;
1031 ins->vex_wlp = *codes++;
1032 ins->evex_tuple = (*codes++ - 0300);
1033 break;
1034
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001035 case4(0254):
1036 length += 4;
1037 break;
1038
1039 case4(0260):
1040 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001041 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001042 ins->vex_cm = *codes++;
1043 ins->vex_wlp = *codes++;
1044 break;
1045
1046 case 0270:
1047 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001048 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001049 ins->vex_cm = *codes++;
1050 ins->vex_wlp = *codes++;
1051 break;
1052
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001053 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001054 hleok = c & 3;
1055 break;
1056
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001057 case4(0274):
1058 length++;
1059 break;
1060
1061 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001062 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001063
H. Peter Anvine2c80182005-01-15 22:15:51 +00001064 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001065 if (bits == 64)
1066 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001067 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001068 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001069
H. Peter Anvine2c80182005-01-15 22:15:51 +00001070 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001071 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001072 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001073
H. Peter Anvine2c80182005-01-15 22:15:51 +00001074 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001075 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001076
Keith Kaniosb7a89542007-04-12 02:40:54 +00001077 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001078 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1079 has_prefix(ins, PPS_ASIZE, P_A32))
1080 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001081 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001082
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001083 case4(0314):
1084 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001085
H. Peter Anvine2c80182005-01-15 22:15:51 +00001086 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001087 {
1088 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1089 if (pfx == P_O16)
1090 break;
1091 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001092 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001093 else
1094 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001095 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001096 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001097
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001099 {
1100 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1101 if (pfx == P_O32)
1102 break;
1103 if (pfx != P_none)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001104 nasm_error(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001105 else
1106 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001108 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001109
H. Peter Anvine2c80182005-01-15 22:15:51 +00001110 case 0322:
1111 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001112
Keith Kaniosb7a89542007-04-12 02:40:54 +00001113 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001114 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001115 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001116
Keith Kaniosb7a89542007-04-12 02:40:54 +00001117 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001118 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001119 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001120
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001121 case 0325:
1122 ins->rex |= REX_NH;
1123 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001124
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001125 case 0326:
1126 break;
1127
H. Peter Anvine2c80182005-01-15 22:15:51 +00001128 case 0330:
1129 codes++, length++;
1130 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001131
H. Peter Anvine2c80182005-01-15 22:15:51 +00001132 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001134
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001135 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 case 0333:
1137 length++;
1138 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001139
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001140 case 0334:
1141 ins->rex |= REX_L;
1142 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001143
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001144 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001145 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001146
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001147 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001148 if (!ins->prefixes[PPS_REP])
1149 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001150 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001151
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001152 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001153 if (!ins->prefixes[PPS_REP])
1154 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001155 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001156
H. Peter Anvine2c80182005-01-15 22:15:51 +00001157 case 0340:
H. Peter Anvin164d2462017-02-20 02:39:56 -08001158 if (!absolute_op(&ins->oprs[0]))
H. Peter Anvin215186f2016-02-17 20:27:41 -08001159 nasm_error(ERR_NONFATAL, "attempt to reserve non-constant"
H. Peter Anvine2c80182005-01-15 22:15:51 +00001160 " quantity of BSS space");
H. Peter Anvinc5d40b32016-10-03 22:18:31 -07001161 else if (ins->oprs[0].opflags & OPFLAG_FORWARD)
1162 nasm_error(ERR_WARNING | ERR_PASS1,
H. Peter Anvine346b3b2016-10-03 22:45:23 -07001163 "forward reference in RESx can have unpredictable results");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001164 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001165 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001166 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001167
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001168 case 0341:
1169 if (!ins->prefixes[PPS_WAIT])
1170 ins->prefixes[PPS_WAIT] = P_WAIT;
1171 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001172
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001173 case 0360:
1174 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001175
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001176 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001177 length++;
1178 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001179
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001180 case 0364:
1181 case 0365:
1182 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001183
Keith Kanios48af1772007-08-17 07:37:52 +00001184 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001185 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001186 length++;
1187 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001188
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001189 case 0370:
1190 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001191 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001192
H. Peter Anvine2c80182005-01-15 22:15:51 +00001193 case 0373:
1194 length++;
1195 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001196
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001197 case 0374:
1198 eat = EA_XMMVSIB;
1199 break;
1200
1201 case 0375:
1202 eat = EA_YMMVSIB;
1203 break;
1204
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001205 case 0376:
1206 eat = EA_ZMMVSIB;
1207 break;
1208
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001209 case4(0100):
1210 case4(0110):
1211 case4(0120):
1212 case4(0130):
1213 case4(0200):
1214 case4(0204):
1215 case4(0210):
1216 case4(0214):
1217 case4(0220):
1218 case4(0224):
1219 case4(0230):
1220 case4(0234):
1221 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001222 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001223 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001224 opflags_t rflags;
1225 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001226 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001227
Keith Kaniosb7a89542007-04-12 02:40:54 +00001228 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001229
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001230 if (c <= 0177) {
1231 /* pick rfield from operand b (opx) */
1232 rflags = regflag(opx);
1233 rfield = nasm_regvals[opx->basereg];
1234 } else {
1235 rflags = 0;
1236 rfield = c & 7;
1237 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001238
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001239 /* EVEX.b1 : evex_brerop contains the operand position */
1240 op_er_sae = (ins->evex_brerop >= 0 ?
1241 &ins->oprs[ins->evex_brerop] : NULL);
1242
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001243 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1244 /* set EVEX.b */
1245 ins->evex_p[2] |= EVEX_P2B;
1246 if (op_er_sae->decoflags & ER) {
1247 /* set EVEX.RC (rounding control) */
1248 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1249 & EVEX_P2RC;
1250 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001251 } else {
1252 /* set EVEX.L'L (vector length) */
1253 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001254 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001255 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001256 /* set EVEX.b */
1257 ins->evex_p[2] |= EVEX_P2B;
1258 }
1259 }
1260
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001261 if (itemp_has(temp, IF_MIB)) {
1262 opy->eaflags |= EAF_MIB;
1263 /*
1264 * if a separate form of MIB (ICC style) is used,
1265 * the index reg info is merged into mem operand
1266 */
1267 if (mib_index != R_none) {
1268 opy->indexreg = mib_index;
1269 opy->scale = 1;
1270 opy->hintbase = mib_index;
1271 opy->hinttype = EAH_NOTBASE;
1272 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001273 }
1274
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001275 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001276 rfield, rflags, ins, &errmsg) != eat) {
1277 nasm_error(ERR_NONFATAL, "%s", errmsg);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001278 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001279 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001280 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001281 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001282 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001283 }
1284 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001285
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001286 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001287 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001288 ": instruction code \\%o (0x%02X) given", c, c);
1289 break;
1290 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001291 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001292
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001293 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001294
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001295 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001296 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001297 nasm_error(ERR_NONFATAL, "instruction cannot use high registers");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001298 return -1;
1299 }
1300 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001301 }
1302
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001303 switch (ins->prefixes[PPS_VEX]) {
1304 case P_EVEX:
1305 if (!(ins->rex & REX_EV))
1306 return -1;
1307 break;
1308 case P_VEX3:
1309 case P_VEX2:
1310 if (!(ins->rex & REX_V))
1311 return -1;
1312 break;
1313 default:
1314 break;
1315 }
1316
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001317 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001318 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001319
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001320 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001321 nasm_error(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001322 return -1;
1323 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001324 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001325 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001326 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001327 ins->rex &= ~REX_W;
1328 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001329 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001330 ins->rex |= REX_W;
1331 bad32 &= ~REX_W;
1332 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001333 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001334 /* Follow REX_W */
1335 break;
1336 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001337
H. Peter Anvinfc561202011-07-07 16:58:22 -07001338 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001339 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001340 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001341 } else if (!(ins->rex & REX_EV) &&
1342 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001343 nasm_error(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
Jin Kyu Song66c61922013-08-26 20:28:43 -07001344 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001345 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001346 if (ins->rex & REX_EV)
1347 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001348 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1349 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001350 length += 3;
1351 else
1352 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001353 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001354 if (ins->rex & REX_H) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001355 nasm_error(ERR_NONFATAL, "cannot use high register in rex instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001356 return -1;
1357 } else if (bits == 64) {
1358 length++;
1359 } else if ((ins->rex & REX_L) &&
1360 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001361 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001362 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001363 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001364 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001365 length++;
1366 } else {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001367 nasm_error(ERR_NONFATAL, "invalid operands in non-64-bit mode");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001368 return -1;
1369 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001370 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001371
1372 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001373 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001374 nasm_error(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001375 "instruction is not lockable");
1376 }
1377
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001378 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001379
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001380 /*
1381 * when BND prefix is set by DEFAULT directive,
1382 * BND prefix is added to every appropriate instruction line
1383 * unless it is overridden by NOBND prefix.
1384 */
1385 if (globalbnd &&
1386 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1387 ins->prefixes[PPS_REP] = P_BND;
1388
H. Peter Anvina77692b2016-09-20 14:04:33 -07001389 /*
1390 * Add length of legacy prefixes
1391 */
1392 length += emit_prefix(NULL, bits, ins);
1393
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001394 return length;
1395}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001396
H. Peter Anvina77692b2016-09-20 14:04:33 -07001397static inline void emit_rex(struct out_data *data, insn *ins)
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001398{
H. Peter Anvina77692b2016-09-20 14:04:33 -07001399 if (data->bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001400 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001401 !(ins->rex & (REX_V | REX_EV)) &&
1402 !ins->rex_done) {
H. Peter Anvina77692b2016-09-20 14:04:33 -07001403 uint8_t rex = (ins->rex & REX_MASK) | REX_P;
1404 out_rawbyte(data, rex);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001405 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001406 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001407 }
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001408}
1409
H. Peter Anvina77692b2016-09-20 14:04:33 -07001410static int emit_prefix(struct out_data *data, const int bits, insn *ins)
1411{
1412 int bytes = 0;
1413 int j;
1414
1415 for (j = 0; j < MAXPREFIX; j++) {
1416 uint8_t c = 0;
1417 switch (ins->prefixes[j]) {
1418 case P_WAIT:
1419 c = 0x9B;
1420 break;
1421 case P_LOCK:
1422 c = 0xF0;
1423 break;
1424 case P_REPNE:
1425 case P_REPNZ:
1426 case P_XACQUIRE:
1427 case P_BND:
1428 c = 0xF2;
1429 break;
1430 case P_REPE:
1431 case P_REPZ:
1432 case P_REP:
1433 case P_XRELEASE:
1434 c = 0xF3;
1435 break;
1436 case R_CS:
1437 if (bits == 64) {
1438 nasm_error(ERR_WARNING | ERR_PASS2,
1439 "cs segment base generated, but will be ignored in 64-bit mode");
1440 }
1441 c = 0x2E;
1442 break;
1443 case R_DS:
1444 if (bits == 64) {
1445 nasm_error(ERR_WARNING | ERR_PASS2,
1446 "ds segment base generated, but will be ignored in 64-bit mode");
1447 }
1448 c = 0x3E;
1449 break;
1450 case R_ES:
1451 if (bits == 64) {
1452 nasm_error(ERR_WARNING | ERR_PASS2,
1453 "es segment base generated, but will be ignored in 64-bit mode");
1454 }
1455 c = 0x26;
1456 break;
1457 case R_FS:
1458 c = 0x64;
1459 break;
1460 case R_GS:
1461 c = 0x65;
1462 break;
1463 case R_SS:
1464 if (bits == 64) {
1465 nasm_error(ERR_WARNING | ERR_PASS2,
1466 "ss segment base generated, but will be ignored in 64-bit mode");
1467 }
1468 c = 0x36;
1469 break;
1470 case R_SEGR6:
1471 case R_SEGR7:
1472 nasm_error(ERR_NONFATAL,
1473 "segr6 and segr7 cannot be used as prefixes");
1474 break;
1475 case P_A16:
1476 if (bits == 64) {
1477 nasm_error(ERR_NONFATAL,
1478 "16-bit addressing is not supported "
1479 "in 64-bit mode");
1480 } else if (bits != 16)
1481 c = 0x67;
1482 break;
1483 case P_A32:
1484 if (bits != 32)
1485 c = 0x67;
1486 break;
1487 case P_A64:
1488 if (bits != 64) {
1489 nasm_error(ERR_NONFATAL,
1490 "64-bit addressing is only supported "
1491 "in 64-bit mode");
1492 }
1493 break;
1494 case P_ASP:
1495 c = 0x67;
1496 break;
1497 case P_O16:
1498 if (bits != 16)
1499 c = 0x66;
1500 break;
1501 case P_O32:
1502 if (bits == 16)
1503 c = 0x66;
1504 break;
1505 case P_O64:
1506 /* REX.W */
1507 break;
1508 case P_OSP:
1509 c = 0x66;
1510 break;
1511 case P_EVEX:
1512 case P_VEX3:
1513 case P_VEX2:
1514 case P_NOBND:
1515 case P_none:
1516 break;
1517 default:
1518 nasm_panic(0, "invalid instruction prefix");
1519 }
1520 if (c) {
1521 if (data)
1522 out_rawbyte(data, c);
1523 bytes++;
1524 }
1525 }
1526 return bytes;
1527}
1528
1529static void gencode(struct out_data *data, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001530{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001531 uint8_t c;
1532 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001533 int64_t size;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001534 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001535 struct operand *opx;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001536 const uint8_t *codes = data->itemp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001537 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001538 enum ea_type eat = EA_SCALAR;
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001539 int r;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001540 const int bits = data->bits;
H. Peter Anvin8f622462017-04-02 19:02:29 -07001541 const char *errmsg;
H. Peter Anvin70653092007-10-19 14:42:29 -07001542
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001543 ins->rex_done = false;
1544
H. Peter Anvina77692b2016-09-20 14:04:33 -07001545 emit_prefix(data, bits, ins);
1546
H. Peter Anvin839eca22007-10-29 23:12:47 -07001547 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001548 c = *codes++;
1549 op1 = (c & 3) + ((opex & 1) << 2);
1550 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1551 opx = &ins->oprs[op1];
1552 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001553
H. Peter Anvina77692b2016-09-20 14:04:33 -07001554
H. Peter Anvin839eca22007-10-29 23:12:47 -07001555 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001556 case 01:
1557 case 02:
1558 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001559 case 04:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001560 emit_rex(data, ins);
1561 out_rawdata(data, codes, c);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001562 codes += c;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001563 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001564
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001565 case 05:
1566 case 06:
1567 case 07:
1568 opex = c;
1569 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001570
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001571 case4(010):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001572 emit_rex(data, ins);
1573 out_rawbyte(data, *codes++ + (regval(opx) & 7));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001574 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001575
Jin Kyu Song164d6072013-10-15 19:10:13 -07001576 case4(014):
1577 break;
1578
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001579 case4(020):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001580 if (opx->offset < -256 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001581 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001582 "byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001583 out_imm(data, opx, 1, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001585
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001586 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001587 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001588 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001589 "unsigned byte value exceeds bounds");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001590 out_imm(data, opx, 1, OUT_UNSIGNED);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001591 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001592
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001593 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001594 warn_overflow_opd(opx, 2);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001595 out_imm(data, opx, 2, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001597
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001598 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001599 if (opx->type & (BITS16 | BITS32))
1600 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001601 else
1602 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001603 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001604 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001605 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001606
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001607 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001608 warn_overflow_opd(opx, 4);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001609 out_imm(data, opx, 4, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001610 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001611
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001612 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001613 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001614 warn_overflow_opd(opx, size);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001615 out_imm(data, opx, size, OUT_WRAP);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001616 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001617
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001618 case4(050):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001619 if (opx->segment == data->segment) {
1620 int64_t delta = opx->offset - data->offset
1621 - (data->inslen - data->insoffs);
1622 if (delta > 127 || delta < -128)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001623 nasm_error(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001624 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001625 out_reladdr(data, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001626 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001627
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001628 case4(054):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001629 out_imm(data, opx, 8, OUT_WRAP);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001630 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001631
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001632 case4(060):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001633 out_reladdr(data, opx, 2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001634 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001635
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001636 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001637 if (opx->type & (BITS16 | BITS32 | BITS64))
1638 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001639 else
1640 size = (bits == 16) ? 2 : 4;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001641
1642 out_reladdr(data, opx, size);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001643 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001644
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001645 case4(070):
H. Peter Anvina77692b2016-09-20 14:04:33 -07001646 out_reladdr(data, opx, 4);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001647 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001648
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001649 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001650 if (opx->segment == NO_SEG)
H. Peter Anvin215186f2016-02-17 20:27:41 -08001651 nasm_error(ERR_NONFATAL, "value referenced by FAR is not"
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001652 " relocatable");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001653 out_segment(data, opx);
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 case 0172:
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001657 {
1658 int mask = ins->prefixes[PPS_VEX] == P_EVEX ? 7 : 15;
1659 const struct operand *opy;
1660
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001661 c = *codes++;
1662 opx = &ins->oprs[c >> 3];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001663 opy = &ins->oprs[c & 7];
H. Peter Anvin164d2462017-02-20 02:39:56 -08001664 if (!absolute_op(opy)) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001665 nasm_error(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001666 "non-absolute expression not permitted as argument %d",
1667 c & 7);
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001668 } else if (opy->offset & ~mask) {
1669 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1670 "is4 argument exceeds bounds");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001671 }
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001672 c = opy->offset & mask;
1673 goto emit_is4;
1674 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001675
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001676 case 0173:
1677 c = *codes++;
1678 opx = &ins->oprs[c >> 4];
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001679 c &= 15;
1680 goto emit_is4;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001681
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001682 case4(0174):
H. Peter Anvin637b9cc2016-09-20 16:39:46 -07001683 c = 0;
1684 emit_is4:
1685 r = nasm_regvals[opx->basereg];
1686 out_rawbyte(data, (r << 4) | ((r & 0x10) >> 1) | c);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001687 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001688
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001689 case4(0254):
H. Peter Anvin164d2462017-02-20 02:39:56 -08001690 if (absolute_op(opx) &&
H. Peter Anvina77692b2016-09-20 14:04:33 -07001691 (int32_t)opx->offset != (int64_t)opx->offset) {
H. Peter Anvin215186f2016-02-17 20:27:41 -08001692 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001693 "signed dword immediate exceeds bounds");
1694 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07001695 out_imm(data, opx, 4, OUT_SIGNED);
H. Peter Anvin588df782008-10-07 10:05:10 -07001696 break;
1697
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001698 case4(0240):
1699 case 0250:
1700 codes += 3;
1701 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1702 EVEX_P2Z | EVEX_P2AAA, 2);
1703 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1704 bytes[0] = 0x62;
1705 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001706 bytes[1] = ((((ins->rex & 7) << 5) |
1707 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
H. Peter Anvin2c9b6ad2016-05-13 14:42:55 -07001708 (ins->vex_cm & EVEX_P0MM);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001709 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1710 ((~ins->vexreg & 15) << 3) |
1711 (1 << 2) | (ins->vex_wlp & 3);
1712 bytes[3] = ins->evex_p[2];
H. Peter Anvina77692b2016-09-20 14:04:33 -07001713 out_rawdata(data, bytes, 4);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001714 break;
1715
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001716 case4(0260):
1717 case 0270:
1718 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001719 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1720 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001721 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1722 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1723 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001724 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001725 out_rawdata(data, bytes, 3);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001726 } else {
1727 bytes[0] = 0xc5;
1728 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001729 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
H. Peter Anvina77692b2016-09-20 14:04:33 -07001730 out_rawdata(data, bytes, 2);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001731 }
1732 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001733
H. Peter Anvine014f352012-02-25 22:35:19 -08001734 case 0271:
1735 case 0272:
1736 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001737 break;
1738
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001739 case4(0274):
1740 {
H. Peter Anvin02788e12017-03-01 13:39:10 -08001741 uint64_t uv, um;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001742 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001743
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001744 if (absolute_op(opx)) {
1745 if (ins->rex & REX_W)
1746 s = 64;
1747 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1748 s = 16;
1749 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1750 s = 32;
1751 else
1752 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001753
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001754 um = (uint64_t)2 << (s-1);
1755 uv = opx->offset;
H. Peter Anvin02788e12017-03-01 13:39:10 -08001756
H. Peter Anvin64e87d02017-03-01 13:45:02 -08001757 if (uv > 127 && uv < (uint64_t)-128 &&
1758 (uv < um-128 || uv > um-1)) {
1759 /* If this wasn't explicitly byte-sized, warn as though we
1760 * had fallen through to the imm16/32/64 case.
1761 */
1762 nasm_error(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1763 "%s value exceeds bounds",
1764 (opx->type & BITS8) ? "signed byte" :
1765 s == 16 ? "word" :
1766 s == 32 ? "dword" :
1767 "signed dword");
1768 }
1769
1770 /* Output as a raw byte to avoid byte overflow check */
1771 out_rawbyte(data, (uint8_t)uv);
1772 } else {
1773 out_imm(data, opx, 1, OUT_WRAP); /* XXX: OUT_SIGNED? */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001774 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001775 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001776 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001777
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001778 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001780
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 case 0310:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001782 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16))
1783 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001784 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001785
H. Peter Anvine2c80182005-01-15 22:15:51 +00001786 case 0311:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001787 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32))
1788 out_rawbyte(data, 0x67);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001790
H. Peter Anvine2c80182005-01-15 22:15:51 +00001791 case 0312:
1792 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001793
Keith Kaniosb7a89542007-04-12 02:40:54 +00001794 case 0313:
1795 ins->rex = 0;
1796 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001797
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001798 case4(0314):
1799 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001800
H. Peter Anvine2c80182005-01-15 22:15:51 +00001801 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001802 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001804
H. Peter Anvine2c80182005-01-15 22:15:51 +00001805 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001806 case 0323:
1807 break;
1808
Keith Kaniosb7a89542007-04-12 02:40:54 +00001809 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001810 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001811 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001812
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001813 case 0325:
1814 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001815
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001816 case 0326:
1817 break;
1818
H. Peter Anvine2c80182005-01-15 22:15:51 +00001819 case 0330:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001820 out_rawbyte(data, *codes++ ^ get_cond_opcode(ins->condition));
H. Peter Anvine2c80182005-01-15 22:15:51 +00001821 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001822
H. Peter Anvine2c80182005-01-15 22:15:51 +00001823 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001824 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001825
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001826 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001827 case 0333:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001828 out_rawbyte(data, c - 0332 + 0xF2);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001830
Keith Kanios48af1772007-08-17 07:37:52 +00001831 case 0334:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001832 if (ins->rex & REX_R)
1833 out_rawbyte(data, 0xF0);
Keith Kanios48af1772007-08-17 07:37:52 +00001834 ins->rex &= ~(REX_L|REX_R);
1835 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001836
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001837 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001838 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001839
H. Peter Anvin962e3052008-08-28 17:47:16 -07001840 case 0336:
1841 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001842 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001843
H. Peter Anvine2c80182005-01-15 22:15:51 +00001844 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001845 if (ins->oprs[0].segment != NO_SEG)
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001846 nasm_panic(0, "non-constant BSS size in pass two");
H. Peter Anvina77692b2016-09-20 14:04:33 -07001847
1848 out_reserve(data, ins->oprs[0].offset);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001849 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001850
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001851 case 0341:
1852 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001853
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001854 case 0360:
1855 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001856
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001857 case 0361:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001858 out_rawbyte(data, 0x66);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001859 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001860
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001861 case 0364:
1862 case 0365:
1863 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001864
Keith Kanios48af1772007-08-17 07:37:52 +00001865 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001866 case 0367:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001867 out_rawbyte(data, c - 0366 + 0x66);
Keith Kanios48af1772007-08-17 07:37:52 +00001868 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001869
Jin Kyu Song03041092013-10-15 19:38:51 -07001870 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001871 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001872
H. Peter Anvine2c80182005-01-15 22:15:51 +00001873 case 0373:
H. Peter Anvina77692b2016-09-20 14:04:33 -07001874 out_rawbyte(data, bits == 16 ? 3 : 5);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001875 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001876
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001877 case 0374:
1878 eat = EA_XMMVSIB;
1879 break;
1880
1881 case 0375:
1882 eat = EA_YMMVSIB;
1883 break;
1884
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001885 case 0376:
1886 eat = EA_ZMMVSIB;
1887 break;
1888
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001889 case4(0100):
1890 case4(0110):
1891 case4(0120):
1892 case4(0130):
1893 case4(0200):
1894 case4(0204):
1895 case4(0210):
1896 case4(0214):
1897 case4(0220):
1898 case4(0224):
1899 case4(0230):
1900 case4(0234):
1901 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001902 ea ea_data;
1903 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001904 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001905 uint8_t *p;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001906 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001907
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001908 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001909 /* pick rfield from operand b (opx) */
1910 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001911 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001912 } else {
1913 /* rfield is constant */
1914 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001915 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001916 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001918 if (process_ea(opy, &ea_data, bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07001919 rfield, rflags, ins, &errmsg) != eat)
1920 nasm_error(ERR_NONFATAL, "%s", errmsg);
Charles Crayne7e975552007-11-03 22:06:13 -07001921
H. Peter Anvine2c80182005-01-15 22:15:51 +00001922 p = bytes;
1923 *p++ = ea_data.modrm;
1924 if (ea_data.sib_present)
1925 *p++ = ea_data.sib;
H. Peter Anvina77692b2016-09-20 14:04:33 -07001926 out_rawdata(data, bytes, p - bytes);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001927
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001928 /*
1929 * Make sure the address gets the right offset in case
1930 * the line breaks in the .lst file (BR 1197827)
1931 */
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001932
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001933 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001934 /* use compressed displacement, if available */
H. Peter Anvina77692b2016-09-20 14:04:33 -07001935 if (ea_data.disp8) {
1936 out_rawbyte(data, ea_data.disp8);
1937 } else if (ea_data.rip) {
1938 out_reladdr(data, opy, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001939 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001940 int asize = ins->addr_size >> 3;
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001941
H. Peter Anvina77692b2016-09-20 14:04:33 -07001942 if (overflow_general(opy->offset, asize) ||
1943 signed_bits(opy->offset, ins->addr_size) !=
1944 signed_bits(opy->offset, ea_data.bytes << 3))
H. Peter Anvin285222f2017-03-01 13:27:33 -08001945 warn_overflow(ea_data.bytes);
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001946
H. Peter Anvina77692b2016-09-20 14:04:33 -07001947 out_imm(data, opy, ea_data.bytes,
H. Peter Anvind9bc2442017-03-28 15:52:58 -07001948 (asize > ea_data.bytes)
1949 ? OUT_SIGNED : OUT_WRAP);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001950 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001951 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001952 }
1953 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001954
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001955 default:
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001956 nasm_panic(0, "internal instruction table corrupt"
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001957 ": instruction code \\%o (0x%02X) given", c, c);
1958 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001959 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001960 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001961}
1962
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001963static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001964{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001965 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001966 nasm_panic(0, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001967 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001968}
1969
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001970static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001971{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001972 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001973 nasm_panic(0, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001974 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001975}
1976
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001977static int op_rexflags(const operand * o, int mask)
1978{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001979 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001980 int val;
1981
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001982 if (!is_register(o->basereg))
H. Peter Anvind6d1b652016-03-03 14:36:01 -08001983 nasm_panic(0, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001984
H. Peter Anvina4835d42008-05-20 14:21:29 -07001985 flags = nasm_reg_flags[o->basereg];
1986 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001987
1988 return rexflags(val, flags, mask);
1989}
1990
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001991static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001992{
1993 int rex = 0;
1994
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08001995 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001996 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001997 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001998 rex |= REX_W;
1999 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2000 rex |= REX_H;
2001 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2002 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002003
2004 return rex & mask;
2005}
2006
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002007static int evexflags(int val, decoflags_t deco,
2008 int mask, uint8_t byte)
2009{
2010 int evex = 0;
2011
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002012 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002013 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002014 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002015 evex |= (EVEX_P0RP | EVEX_P0X);
2016 break;
2017 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002018 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002019 evex |= EVEX_P2VP;
2020 if (deco & Z)
2021 evex |= EVEX_P2Z;
2022 if (deco & OPMASK_MASK)
2023 evex |= deco & EVEX_P2AAA;
2024 break;
2025 }
2026 return evex & mask;
2027}
2028
2029static int op_evexflags(const operand * o, int mask, uint8_t byte)
2030{
2031 int val;
2032
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002033 val = nasm_regvals[o->basereg];
2034
2035 return evexflags(val, o->decoflags, mask, byte);
2036}
2037
H. Peter Anvin23595f52009-07-25 17:44:25 -07002038static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002039 insn *instruction,
2040 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002041{
2042 const struct itemplate *temp;
2043 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002044 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002045 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002046 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002047 int i;
2048
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002049 /* broadcasting uses a different data element size */
2050 for (i = 0; i < instruction->operands; i++)
2051 if (i == broadcast)
2052 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2053 else
2054 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002055
2056 merr = MERR_INVALOP;
2057
2058 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002059 temp->opcode != I_none; temp++) {
2060 m = matches(temp, instruction, bits);
2061 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002062 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002063 m = MOK_GOOD;
2064 else
2065 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002066 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002067 /*
2068 * Missing operand size and a candidate for fuzzy matching...
2069 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002070 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002071 if (i == broadcast)
2072 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2073 else
2074 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002075 opsizemissing = true;
2076 }
2077 if (m > merr)
2078 merr = m;
2079 if (merr == MOK_GOOD)
2080 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002081 }
2082
2083 /* No match, but see if we can get a fuzzy operand size match... */
2084 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002085 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002086
2087 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002088 /*
2089 * We ignore extrinsic operand sizes on registers, so we should
2090 * never try to fuzzy-match on them. This also resolves the case
2091 * when we have e.g. "xmmrm128" in two different positions.
2092 */
2093 if (is_class(REGISTER, instruction->oprs[i].type))
2094 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002095
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002096 /* This tests if xsizeflags[i] has more than one bit set */
2097 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2098 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002099
Jin Kyu Song7903c072013-10-30 03:00:12 -07002100 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002101 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002102 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2103 BITS32 : BITS64);
2104 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002105 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002106 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002107 }
2108
2109 /* Try matching again... */
2110 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002111 temp->opcode != I_none; temp++) {
2112 m = matches(temp, instruction, bits);
2113 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002114 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002115 m = MOK_GOOD;
2116 else
2117 m = MERR_INVALOP;
2118 }
2119 if (m > merr)
2120 merr = m;
2121 if (merr == MOK_GOOD)
2122 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002123 }
2124
H. Peter Anvina81655b2009-07-25 18:15:28 -07002125done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002126 *tempp = temp;
2127 return merr;
2128}
2129
Mark Charneydcaef4b2014-10-09 13:45:17 -04002130static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2131{
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002132 unsigned int opsize = (opflags & SIZE_MASK) >> SIZE_SHIFT;
Mark Charneydcaef4b2014-10-09 13:45:17 -04002133 uint8_t brcast_num;
2134
Mark Charneydcaef4b2014-10-09 13:45:17 -04002135 if (brsize > BITS64)
H. Peter Anvin215186f2016-02-17 20:27:41 -08002136 nasm_error(ERR_FATAL,
Mark Charneydcaef4b2014-10-09 13:45:17 -04002137 "size of broadcasting element is greater than 64 bits");
2138
H. Peter Anvin2902fbc2017-02-20 00:35:58 -08002139 /*
2140 * The shift term is to take care of the extra BITS80 inserted
2141 * between BITS64 and BITS128.
2142 */
2143 brcast_num = ((opsize / (BITS64 >> SIZE_SHIFT)) * (BITS64 / brsize))
2144 >> (opsize > (BITS64 >> SIZE_SHIFT));
Mark Charneydcaef4b2014-10-09 13:45:17 -04002145
2146 return brcast_num;
2147}
2148
H. Peter Anvin65289e82009-07-25 17:25:11 -07002149static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002150 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002151{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002152 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002153 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002154 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002155
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002156 /*
2157 * Check the opcode
2158 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002159 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002160 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002161
2162 /*
2163 * Count the operands
2164 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002165 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002166 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002167
2168 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002169 * Is it legal?
2170 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002171 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002172 return MERR_INVALOP;
2173
2174 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002175 * {evex} available?
2176 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002177 switch (instruction->prefixes[PPS_VEX]) {
2178 case P_EVEX:
2179 if (!itemp_has(itemp, IF_EVEX))
2180 return MERR_ENCMISMATCH;
2181 break;
2182 case P_VEX3:
2183 case P_VEX2:
2184 if (!itemp_has(itemp, IF_VEX))
2185 return MERR_ENCMISMATCH;
2186 break;
2187 default:
2188 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002189 }
2190
2191 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002192 * Check that no spurious colons or TOs are present
2193 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002194 for (i = 0; i < itemp->operands; i++)
2195 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002196 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002197
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002198 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002199 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002200 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002201 switch (itemp_smask(itemp)) {
2202 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002203 asize = BITS8;
2204 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002205 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002206 asize = BITS16;
2207 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002208 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002209 asize = BITS32;
2210 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002211 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002212 asize = BITS64;
2213 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002214 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002215 asize = BITS128;
2216 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002217 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002218 asize = BITS256;
2219 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002220 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002221 asize = BITS512;
2222 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002223 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002224 switch (bits) {
2225 case 16:
2226 asize = BITS16;
2227 break;
2228 case 32:
2229 asize = BITS32;
2230 break;
2231 case 64:
2232 asize = BITS64;
2233 break;
2234 default:
2235 asize = 0;
2236 break;
2237 }
2238 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002239 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002240 asize = 0;
2241 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002242 }
2243
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002244 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002245 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002246 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002247 memset(size, 0, sizeof size);
2248 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002249 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002250 /* S- flags apply to all operands */
2251 for (i = 0; i < MAX_OPERANDS; i++)
2252 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002253 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002254
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002255 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002256 * Check that the operand flags all match up,
2257 * it's a bit tricky so lets be verbose:
2258 *
2259 * 1) Find out the size of operand. If instruction
2260 * doesn't have one specified -- we're trying to
2261 * guess it either from template (IF_S* flag) or
2262 * from code bits.
2263 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002264 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002265 * template has an operand size specified AND this size differ
2266 * from which instruction has (perhaps we got it from code bits)
2267 * we are:
2268 * a) Check that only size of instruction and operand is differ
2269 * other characteristics do match
2270 * b) Perhaps it's a register specified in instruction so
2271 * for such a case we just mark that operand as "size
2272 * missing" and this will turn on fuzzy operand size
2273 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002274 */
2275 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002276 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002277 decoflags_t deco = instruction->oprs[i].decoflags;
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002278 decoflags_t ideco = itemp->deco[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002279 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002280 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002281 opflags_t template_opsize, insn_opsize;
2282
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002283 if (!(type & SIZE_MASK))
2284 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002285
Jin Kyu Song7903c072013-10-30 03:00:12 -07002286 insn_opsize = type & SIZE_MASK;
2287 if (!is_broadcast) {
2288 template_opsize = itemp->opd[i] & SIZE_MASK;
2289 } else {
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002290 decoflags_t deco_brsize = ideco & BRSIZE_MASK;
2291
2292 if (~ideco & BRDCAST_MASK)
2293 return MERR_BRNOTHERE;
2294
Jin Kyu Song7903c072013-10-30 03:00:12 -07002295 /*
2296 * when broadcasting, the element size depends on
2297 * the instruction type. decorator flag should match.
2298 */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002299 if (deco_brsize) {
2300 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002301 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002302 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002303 } else {
2304 template_opsize = 0;
2305 }
2306 }
2307
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002308 if (~ideco & deco & OPMASK_MASK)
2309 return MERR_MASKNOTHERE;
2310
H. Peter Anvinff04a9f2017-08-16 21:48:52 -07002311 if (~ideco & deco & (Z_MASK|STATICRND_MASK|SAE_MASK))
2312 return MERR_DECONOTHERE;
2313
H. Peter Anvin8e37ff42017-04-02 18:38:58 -07002314 if (itemp->opd[i] & ~type & ~SIZE_MASK) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002315 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002316 } else if (template_opsize) {
2317 if (template_opsize != insn_opsize) {
2318 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002319 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002320 } else if (!is_class(REGISTER, type)) {
2321 /*
2322 * Note: we don't honor extrinsic operand sizes for registers,
2323 * so "missing operand size" for a register should be
2324 * considered a wildcard match rather than an error.
2325 */
2326 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002327 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002328 } else if (is_broadcast &&
2329 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002330 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002331 /*
2332 * broadcasting opsize matches but the number of repeated memory
2333 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002334 * if 64b double precision float is broadcasted to ymm (256b),
2335 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002336 */
2337 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002338 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002339 }
2340 }
2341
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002342 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002343 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002344
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002345 /*
2346 * Check operand sizes
2347 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002348 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2349 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002350 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002351 asize = itemp->opd[i] & SIZE_MASK;
2352 if (asize) {
2353 for (i = 0; i < oprs; i++)
2354 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002355 break;
2356 }
2357 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002358 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002359 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002360 }
2361
Keith Kaniosb7a89542007-04-12 02:40:54 +00002362 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002363 if (!(itemp->opd[i] & SIZE_MASK) &&
2364 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002365 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002366 }
2367
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002368 /*
2369 * Check template is okay at the set cpu level
2370 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002371 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002372 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002373
Keith Kaniosb7a89542007-04-12 02:40:54 +00002374 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002375 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002376 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002377 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002378 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002379
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002380 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002381 * If we have a HLE prefix, look for the NOHLE flag
2382 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002383 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002384 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2385 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2386 return MERR_BADHLE;
2387
2388 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002389 * Check if special handling needed for Jumps
2390 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002391 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002392 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002393
Jin Kyu Song03041092013-10-15 19:38:51 -07002394 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002395 * Check if BND prefix is allowed.
2396 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002397 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002398 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002399 (has_prefix(instruction, PPS_REP, P_BND) ||
2400 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002401 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002402 else if (itemp_has(itemp, IF_BND) &&
2403 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2404 has_prefix(instruction, PPS_REP, P_REPNZ)))
2405 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002406
H. Peter Anvin60926242009-07-26 16:25:38 -07002407 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002408}
2409
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002410/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002411 * Check if ModR/M.mod should/can be 01.
2412 * - EAF_BYTEOFFS is set
2413 * - offset can fit in a byte when EVEX is not used
2414 * - offset can be compressed when EVEX is used
2415 */
Henrik Gramner16d4db32017-04-20 16:02:19 +02002416#define IS_MOD_01() (!(input->eaflags & EAF_WORDOFFS) && \
2417 (ins->rex & REX_EV ? seg == NO_SEG && !forw_ref && \
2418 is_disp8n(input, ins, &output->disp8) : \
2419 input->eaflags & EAF_BYTEOFFS || (o >= -128 && \
2420 o <= 127 && seg == NO_SEG && !forw_ref)))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002421
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002422static enum ea_type process_ea(operand *input, ea *output, int bits,
H. Peter Anvin8f622462017-04-02 19:02:29 -07002423 int rfield, opflags_t rflags, insn *ins,
2424 const char **errmsg)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002425{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002426 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002427 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002428 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002429
H. Peter Anvin8f622462017-04-02 19:02:29 -07002430 *errmsg = "invalid effective address"; /* Default error message */
2431
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002432 output->type = EA_SCALAR;
2433 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002434 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002435
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002436 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002437 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002438 /* EVEX.R' flag for the REG operand */
2439 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002440
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002441 if (is_class(REGISTER, input->type)) {
2442 /*
2443 * It's a direct register.
2444 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002445 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002446 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002447
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002448 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002449 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002450
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002451 /* broadcasting is not available with a direct register operand. */
2452 if (input->decoflags & BRDCAST_MASK) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002453 *errmsg = "broadcast not allowed with register operand";
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002454 goto err;
2455 }
2456
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002457 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002458 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002459 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002460 output->bytes = 0; /* no offset necessary either */
2461 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2462 } else {
2463 /*
2464 * It's a memory reference.
2465 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002466
2467 /* Embedded rounding or SAE is not available with a mem ref operand. */
2468 if (input->decoflags & (ER | SAE)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002469 *errmsg = "embedded rounding is available only with "
2470 "register-register operations";
2471 goto err;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002472 }
2473
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002474 if (input->basereg == -1 &&
2475 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002476 /*
2477 * It's a pure offset.
2478 */
H. Peter Anvin164d2462017-02-20 02:39:56 -08002479 if (bits == 64 && ((input->type & IP_REL) == IP_REL)) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002480 if (input->segment == NO_SEG ||
2481 (input->opflags & OPFLAG_RELATIVE)) {
2482 nasm_error(ERR_WARNING | ERR_PASS2,
2483 "absolute address can not be RIP-relative");
H. Peter Anvin164d2462017-02-20 02:39:56 -08002484 input->type &= ~IP_REL;
2485 input->type |= MEMORY;
2486 }
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002487 }
2488
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002489 if (bits == 64 &&
2490 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
H. Peter Anvine83311c2017-04-06 18:50:28 -07002491 *errmsg = "RIP-relative addressing is prohibited for MIB";
H. Peter Anvin8f622462017-04-02 19:02:29 -07002492 goto err;
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002493 }
2494
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002495 if (eaflags & EAF_BYTEOFFS ||
2496 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002497 input->disp_size != (addrbits != 16 ? 32 : 16))) {
H. Peter Anvin8f622462017-04-02 19:02:29 -07002498 nasm_error(ERR_WARNING | ERR_PASS1,
2499 "displacement size ignored on absolute address");
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002500 }
2501
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002502 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002503 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002504 output->sib = GEN_SIB(0, 4, 5);
2505 output->bytes = 4;
2506 output->modrm = GEN_MODRM(0, rfield, 4);
2507 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002508 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002509 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002510 output->bytes = (addrbits != 16 ? 4 : 2);
H. Peter Anvin8f622462017-04-02 19:02:29 -07002511 output->modrm = GEN_MODRM(0, rfield,
2512 (addrbits != 16 ? 5 : 6));
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002513 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002514 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002515 } else {
2516 /*
2517 * It's an indirection.
2518 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002519 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002520 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002521 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002522 int t, it, bt; /* register numbers */
2523 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002524
H. Peter Anvine2c80182005-01-15 22:15:51 +00002525 if (s == 0)
2526 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002527
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002528 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002529 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002530 ix = nasm_reg_flags[i];
2531 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002532 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002533 ix = 0;
2534 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002535
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002536 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002537 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002538 bx = nasm_reg_flags[b];
2539 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002540 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002541 bx = 0;
2542 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002543
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002544 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002545 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002546 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002547 int32_t o = input->offset;
2548 int mod, scale, index, base;
2549
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002550 /*
2551 * For a vector SIB, one has to be a vector and the other,
2552 * if present, a GPR. The vector must be the index operand.
2553 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002554 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002555 if (s == 0)
2556 s = 1;
2557 else if (s != 1)
2558 goto err;
2559
2560 t = bt, bt = it, it = t;
2561 x = bx, bx = ix, ix = x;
2562 }
2563
2564 if (bt != -1) {
2565 if (REG_GPR & ~bx)
2566 goto err;
2567 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2568 sok &= bx;
2569 else
2570 goto err;
2571 }
2572
2573 /*
2574 * While we're here, ensure the user didn't specify
2575 * WORD or QWORD
2576 */
2577 if (input->disp_size == 16 || input->disp_size == 64)
2578 goto err;
2579
2580 if (addrbits == 16 ||
2581 (addrbits == 32 && !(sok & BITS32)) ||
2582 (addrbits == 64 && !(sok & BITS64)))
2583 goto err;
2584
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002585 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2586 : ((ix & YMMREG & ~REG_EA)
2587 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002588
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002589 output->rex |= rexflags(it, ix, REX_X);
2590 output->rex |= rexflags(bt, bx, REX_B);
2591 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002592
2593 index = it & 7; /* it is known to be != -1 */
2594
2595 switch (s) {
2596 case 1:
2597 scale = 0;
2598 break;
2599 case 2:
2600 scale = 1;
2601 break;
2602 case 4:
2603 scale = 2;
2604 break;
2605 case 8:
2606 scale = 3;
2607 break;
2608 default: /* then what the smeg is it? */
2609 goto err; /* panic */
2610 }
H. Peter Anvina77692b2016-09-20 14:04:33 -07002611
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002612 if (bt == -1) {
2613 base = 5;
2614 mod = 0;
2615 } else {
2616 base = (bt & 7);
2617 if (base != REG_NUM_EBP && o == 0 &&
2618 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002619 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002620 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002621 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002622 mod = 1;
2623 else
2624 mod = 2;
2625 }
2626
2627 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002628 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2629 output->modrm = GEN_MODRM(mod, rfield, 4);
2630 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002631 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002632 /*
2633 * it must be a 32/64-bit memory reference. Firstly we have
2634 * to check that all registers involved are type E/Rxx.
2635 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002636 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002637 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002638
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002639 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002640 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2641 sok &= ix;
2642 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002643 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002644 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002645
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002646 if (bt != -1) {
2647 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002648 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002649 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002650 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002651 sok &= bx;
2652 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002653
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002654 /*
2655 * While we're here, ensure the user didn't specify
2656 * WORD or QWORD
2657 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002658 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002659 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002660
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002661 if (addrbits == 16 ||
2662 (addrbits == 32 && !(sok & BITS32)) ||
2663 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002664 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002665
Keith Kaniosb7a89542007-04-12 02:40:54 +00002666 /* now reorganize base/index */
2667 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002668 ((hb == b && ht == EAH_NOTBASE) ||
2669 (hb == i && ht == EAH_MAKEBASE))) {
2670 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002671 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002672 x = bx, bx = ix, ix = x;
2673 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002674
Jin Kyu Song164d6072013-10-15 19:10:13 -07002675 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002676 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002677 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002678 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002679 if (eaflags & EAF_MIB) {
2680 /* only for mib operands */
2681 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2682 /*
2683 * make a single reg index [reg*1].
2684 * gas uses this form for an explicit index register.
2685 */
2686 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2687 }
2688 if ((ht == EAH_SUMMED) && bt == -1) {
2689 /* separate once summed index into [base, index] */
2690 bt = it, bx = ix, s--;
2691 }
2692 } else {
2693 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002694 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002695 s == 3 || s == 5 || s == 9) && bt == -1) {
2696 /* convert 3*EAX to EAX+2*EAX */
2697 bt = it, bx = ix, s--;
2698 }
2699 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002700 (eaflags & EAF_TIMESTWO) &&
2701 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002702 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002703 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002704 * to sib format with 0x0 displacement - [EAX*1+0].
2705 */
2706 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2707 }
2708 }
Keith Kanios48af1772007-08-17 07:37:52 +00002709 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002710 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002711 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002712 x = ix, ix = bx, bx = x;
2713 }
2714 if (it == REG_NUM_ESP ||
2715 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002716 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002717
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002718 output->rex |= rexflags(it, ix, REX_X);
2719 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002720
Keith Kanios48af1772007-08-17 07:37:52 +00002721 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002722 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002723 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002724
Keith Kaniosb7a89542007-04-12 02:40:54 +00002725 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002726 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002727 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002728 } else {
2729 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002730 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002731 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002732 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002733 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002734 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002735 mod = 1;
2736 else
2737 mod = 2;
2738 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002739
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002740 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002741 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2742 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002743 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002744 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002745 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002746
Keith Kaniosb7a89542007-04-12 02:40:54 +00002747 if (it == -1)
2748 index = 4, s = 1;
2749 else
2750 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002751
H. Peter Anvine2c80182005-01-15 22:15:51 +00002752 switch (s) {
2753 case 1:
2754 scale = 0;
2755 break;
2756 case 2:
2757 scale = 1;
2758 break;
2759 case 4:
2760 scale = 2;
2761 break;
2762 case 8:
2763 scale = 3;
2764 break;
2765 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002766 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002767 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002768
Keith Kaniosb7a89542007-04-12 02:40:54 +00002769 if (bt == -1) {
2770 base = 5;
2771 mod = 0;
2772 } else {
2773 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002774 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002775 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002776 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002777 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002778 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002779 mod = 1;
2780 else
2781 mod = 2;
2782 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002783
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002784 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002785 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2786 output->modrm = GEN_MODRM(mod, rfield, 4);
2787 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002788 }
2789 } else { /* it's 16-bit */
2790 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002791 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002792
Keith Kaniosb7a89542007-04-12 02:40:54 +00002793 /* check for 64-bit long mode */
2794 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002795 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002796
H. Peter Anvine2c80182005-01-15 22:15:51 +00002797 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002798 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2799 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002800 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002801
Keith Kaniosb7a89542007-04-12 02:40:54 +00002802 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002803 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002804 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002805
H. Peter Anvine2c80182005-01-15 22:15:51 +00002806 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002807 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 if (b == -1 && i != -1) {
2809 int tmp = b;
2810 b = i;
2811 i = tmp;
2812 } /* swap */
2813 if ((b == R_SI || b == R_DI) && i != -1) {
2814 int tmp = b;
2815 b = i;
2816 i = tmp;
2817 }
2818 /* have BX/BP as base, SI/DI index */
2819 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002820 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002821 if (i != -1 && b != -1 &&
2822 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002823 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002824 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002825 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002826
H. Peter Anvine2c80182005-01-15 22:15:51 +00002827 rm = -1;
2828 if (i != -1)
2829 switch (i * 256 + b) {
2830 case R_SI * 256 + R_BX:
2831 rm = 0;
2832 break;
2833 case R_DI * 256 + R_BX:
2834 rm = 1;
2835 break;
2836 case R_SI * 256 + R_BP:
2837 rm = 2;
2838 break;
2839 case R_DI * 256 + R_BP:
2840 rm = 3;
2841 break;
2842 } else
2843 switch (b) {
2844 case R_SI:
2845 rm = 4;
2846 break;
2847 case R_DI:
2848 rm = 5;
2849 break;
2850 case R_BP:
2851 rm = 6;
2852 break;
2853 case R_BX:
2854 rm = 7;
2855 break;
2856 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002857 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002858 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002859
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002860 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002861 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002862 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002863 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002864 mod = 1;
2865 else
2866 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002867
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002868 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002869 output->bytes = mod; /* bytes of offset needed */
2870 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002871 }
2872 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002873 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002874
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002875 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002876 return output->type;
2877
2878err:
2879 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002880}
2881
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002882static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002883{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002884 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002885 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002886
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002887 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002888
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002889 switch (ins->prefixes[PPS_ASIZE]) {
2890 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002891 valid &= 16;
2892 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002893 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002894 valid &= 32;
2895 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002896 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002897 valid &= 64;
2898 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002899 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002900 valid &= (addrbits == 32) ? 16 : 32;
2901 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002902 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002903 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002904 }
2905
2906 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002907 if (is_class(MEMORY, ins->oprs[j].type)) {
2908 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002909
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002910 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002911 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002912 i = 0;
2913 else
2914 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002915
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002916 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002917 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002918 b = 0;
2919 else
2920 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002921
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002922 if (ins->oprs[j].scale == 0)
2923 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002924
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002925 if (!i && !b) {
2926 int ds = ins->oprs[j].disp_size;
2927 if ((addrbits != 64 && ds > 8) ||
2928 (addrbits == 64 && ds == 16))
2929 valid &= ds;
2930 } else {
2931 if (!(REG16 & ~b))
2932 valid &= 16;
2933 if (!(REG32 & ~b))
2934 valid &= 32;
2935 if (!(REG64 & ~b))
2936 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002937
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002938 if (!(REG16 & ~i))
2939 valid &= 16;
2940 if (!(REG32 & ~i))
2941 valid &= 32;
2942 if (!(REG64 & ~i))
2943 valid &= 64;
2944 }
2945 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002946 }
2947
2948 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002949 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002950 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002951 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002952 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002953 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002954 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002955 /* Impossible... */
H. Peter Anvin215186f2016-02-17 20:27:41 -08002956 nasm_error(ERR_NONFATAL, "impossible combination of address sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002957 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002958 }
2959
2960 defdisp = ins->addr_size == 16 ? 16 : 32;
2961
2962 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002963 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2964 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2965 /*
2966 * mem_offs sizes must match the address size; if not,
2967 * strip the MEM_OFFS bit and match only EA instructions
2968 */
2969 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2970 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002971 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002972}