blob: b1e682179369fc7ec59fcb4e6583fa78d90d67cc [file] [log] [blame]
H. Peter Anvin9e6747c2009-06-28 17:13:04 -07001/* ----------------------------------------------------------------------- *
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002 *
H. Peter Anvind24dd5f2016-02-08 10:32:13 -08003 * Copyright 1996-2016 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:
84 * cc 000 0mm
85 * c = 2 for EVEX and m is the legacy escape (0f, 0f38, 0f3a)
86 * 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>
Keith Kaniosb7a89542007-04-12 02:40:54 +0000183#include <inttypes.h>
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000184
185#include "nasm.h"
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000186#include "nasmlib.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 Anvinea6e34d2002-04-30 20:51:32 +0000191
H. Peter Anvin65289e82009-07-25 17:25:11 -0700192enum match_result {
193 /*
194 * Matching errors. These should be sorted so that more specific
195 * errors come later in the sequence.
196 */
197 MERR_INVALOP,
198 MERR_OPSIZEMISSING,
199 MERR_OPSIZEMISMATCH,
Jin Kyu Song25c22122013-10-30 03:12:45 -0700200 MERR_BRNUMMISMATCH,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700201 MERR_BADCPU,
202 MERR_BADMODE,
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -0800203 MERR_BADHLE,
Jin Kyu Song66c61922013-08-26 20:28:43 -0700204 MERR_ENCMISMATCH,
Jin Kyu Song03041092013-10-15 19:38:51 -0700205 MERR_BADBND,
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800206 MERR_BADREPNE,
H. Peter Anvin65289e82009-07-25 17:25:11 -0700207 /*
208 * Matching success; the conditional ones first
209 */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400210 MOK_JUMP, /* Matching OK but needs jmp_match() */
211 MOK_GOOD /* Matching unconditionally OK */
H. Peter Anvin65289e82009-07-25 17:25:11 -0700212};
213
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000214typedef struct {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700215 enum ea_type type; /* what kind of EA is this? */
216 int sib_present; /* is a SIB byte necessary? */
217 int bytes; /* # of bytes of offset needed */
218 int size; /* lazy - this is sib+bytes+1 */
219 uint8_t modrm, sib, rex, rip; /* the bytes themselves */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700220 int8_t disp8; /* compressed displacement for EVEX */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000221} ea;
222
Cyrill Gorcunov10734c72011-08-29 00:07:17 +0400223#define GEN_SIB(scale, index, base) \
224 (((scale) << 6) | ((index) << 3) | ((base)))
225
226#define GEN_MODRM(mod, reg, rm) \
227 (((mod) << 6) | (((reg) & 7) << 3) | ((rm) & 7))
228
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400229static iflag_t cpu; /* cpu level received from nasm.c */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000230static efunc errfunc;
231static struct ofmt *outfmt;
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000232static ListGen *list;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000233
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800234static int64_t calcsize(int32_t, int64_t, int, insn *,
235 const struct itemplate *);
H. Peter Anvin833caea2008-10-04 19:02:30 -0700236static void gencode(int32_t segment, int64_t offset, int bits,
237 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400238 int64_t insn_end);
H. Peter Anvin23595f52009-07-25 17:44:25 -0700239static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400240 insn *instruction,
241 int32_t segment, int64_t offset, int bits);
H. Peter Anvin65289e82009-07-25 17:25:11 -0700242static enum match_result matches(const struct itemplate *, insn *, int bits);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700243static opflags_t regflag(const operand *);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000244static int32_t regval(const operand *);
H. Peter Anvinf8563f72009-10-13 12:28:14 -0700245static int rexflags(int, opflags_t, int);
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000246static int op_rexflags(const operand *, int);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700247static int op_evexflags(const operand *, int, uint8_t);
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700248static void add_asp(insn *, int);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000249
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700250static enum ea_type process_ea(operand *, ea *, int, int, opflags_t, insn *);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700251
Cyrill Gorcunov18914e62011-11-12 11:41:51 +0400252static int has_prefix(insn * ins, enum prefix_pos pos, int prefix)
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000253{
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700254 return ins->prefixes[pos] == prefix;
255}
256
257static void assert_no_prefix(insn * ins, enum prefix_pos pos)
258{
259 if (ins->prefixes[pos])
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400260 errfunc(ERR_NONFATAL, "invalid %s prefix",
261 prefix_name(ins->prefixes[pos]));
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700262}
263
264static const char *size_name(int size)
265{
266 switch (size) {
267 case 1:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400268 return "byte";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700269 case 2:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400270 return "word";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700271 case 4:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400272 return "dword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700273 case 8:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400274 return "qword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700275 case 10:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400276 return "tword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700277 case 16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400278 return "oword";
H. Peter Anvindfb91802008-05-20 11:43:53 -0700279 case 32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400280 return "yword";
Jin Kyu Songd4760c12013-08-21 19:29:11 -0700281 case 64:
282 return "zword";
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700283 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400284 return "???";
H. Peter Anvin0db11e22007-04-17 20:23:11 +0000285 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700286}
287
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400288static void warn_overflow(int pass, int size)
289{
290 errfunc(ERR_WARNING | pass | ERR_WARN_NOV,
291 "%s data exceeds bounds", size_name(size));
292}
293
294static void warn_overflow_const(int64_t data, int size)
295{
296 if (overflow_general(data, size))
297 warn_overflow(ERR_PASS1, size);
298}
299
300static void warn_overflow_opd(const struct operand *o, int size)
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700301{
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100302 if (o->wrt == NO_SEG && o->segment == NO_SEG) {
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400303 if (overflow_general(o->offset, size))
304 warn_overflow(ERR_PASS2, size);
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700305 }
306}
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400307
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000308/*
H. Peter Anvinb6412502016-02-11 21:07:40 -0800309 * Size of an address relocation, or zero if not an address
310 */
311static int addrsize(enum out_type type, uint64_t size)
312{
313 switch (type) {
314 case OUT_ADDRESS:
315 return abs((int)size);
316 case OUT_REL1ADR:
317 return 1;
318 case OUT_REL2ADR:
319 return 2;
320 case OUT_REL4ADR:
321 return 4;
322 case OUT_REL8ADR:
323 return 8;
324 default:
325 return 0;
326 }
327}
328
329/*
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000330 * This routine wrappers the real output format's output routine,
331 * in order to pass a copy of the data off to the listing file
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800332 * generator at the same time, flatten unnecessary relocations,
333 * and verify backend compatibility.
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000334 */
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800335static void out(int64_t offset, int32_t segto, const void *data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800336 enum out_type type, uint64_t size,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400337 int32_t segment, int32_t wrt)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000338{
Keith Kaniosb7a89542007-04-12 02:40:54 +0000339 static int32_t lineno = 0; /* static!!! */
Keith Kaniosa6dfa782007-04-13 16:47:53 +0000340 static char *lnfname = NULL;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800341 uint8_t p[8];
H. Peter Anvinb6412502016-02-11 21:07:40 -0800342 int asize = addrsize(type, size); /* Address size in bytes */
H. Peter Anvin33814132016-02-11 20:40:07 -0800343 const int amax = outfmt->maxbits >> 3; /* Maximum address size in bytes */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000344
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800345 if (type == OUT_ADDRESS && segment == NO_SEG && wrt == NO_SEG) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400346 /*
347 * This is a non-relocated address, and we're going to
348 * convert it into RAWDATA format.
349 */
350 uint8_t *q = p;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800351
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800352 if (asize > 8) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400353 errfunc(ERR_PANIC, "OUT_ADDRESS with size > 8");
354 return;
355 }
H. Peter Anvind85d2502008-05-04 17:53:31 -0700356
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800357 WRITEADDR(q, *(int64_t *)data, asize);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400358 data = p;
359 type = OUT_RAWDATA;
H. Peter Anvinb6412502016-02-11 21:07:40 -0800360
361 asize = 0; /* No longer an address */
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000362 }
363
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800364 list->output(offset, data, type, size);
365
Frank Kotlerabebb082003-09-06 04:45:37 +0000366 /*
367 * this call to src_get determines when we call the
368 * debug-format-specific "linenum" function
369 * it updates lineno and lnfname to the current values
370 * returning 0 if "same as last time", -2 if lnfname
371 * changed, and the amount by which lineno changed,
372 * if it did. thus, these variables must be static
373 */
374
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400375 if (src_get(&lineno, &lnfname))
H. Peter Anvine2c80182005-01-15 22:15:51 +0000376 outfmt->current_dfmt->linenum(lnfname, lineno, segto);
H. Peter Anvineba20a72002-04-30 20:53:55 +0000377
H. Peter Anvinb6412502016-02-11 21:07:40 -0800378 if (asize && asize > amax) {
379 if (type != OUT_ADDRESS || (int)size < 0) {
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800380 errfunc(ERR_NONFATAL,
381 "%d-bit signed relocation unsupported by output format %s\n",
H. Peter Anvin33814132016-02-11 20:40:07 -0800382 asize << 3, outfmt->shortname);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800383 } else {
384 errfunc(ERR_WARNING | ERR_WARN_ZEXTRELOC,
H. Peter Anvinecc9e0e2016-02-11 20:29:34 -0800385 "%d-bit unsigned relocation zero-extended from %d bits\n",
H. Peter Anvin33814132016-02-11 20:40:07 -0800386 asize << 4, outfmt->maxbits);
H. Peter Anvind24dd5f2016-02-08 10:32:13 -0800387 outfmt->output(segto, data, type, amax, segment, wrt);
388 size -= amax;
389 }
390 data = zero_buffer;
391 type = OUT_RAWDATA;
392 }
393
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800394 outfmt->output(segto, data, type, size, segment, wrt);
H. Peter Anvin6768eb72002-04-30 20:52:26 +0000395}
396
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800397static void out_imm8(int64_t offset, int32_t segment,
398 struct operand *opx, int asize)
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400399{
400 if (opx->segment != NO_SEG) {
401 uint64_t data = opx->offset;
H. Peter Anvin89a2ac02013-11-26 18:23:20 -0800402 out(offset, segment, &data, OUT_ADDRESS, asize, opx->segment, opx->wrt);
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +0400403 } else {
404 uint8_t byte = opx->offset;
405 out(offset, segment, &byte, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
406 }
407}
408
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700409static bool jmp_match(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800410 insn * ins, const struct itemplate *temp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000411{
Charles Crayne5fbbc8c2007-11-07 19:03:46 -0800412 int64_t isize;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800413 const uint8_t *code = temp->code;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000414 uint8_t c = code[0];
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800415 bool is_byte;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000416
H. Peter Anvin755f5212012-02-25 11:41:34 -0800417 if (((c & ~1) != 0370) || (ins->oprs[0].type & STRICT))
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700418 return false;
419 if (!optimizing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400420 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700421 if (optimizing < 0 && c == 0371)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400422 return false;
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700423
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800424 isize = calcsize(segment, offset, bits, ins, temp);
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100425
Victor van den Elzen154e5922009-02-25 17:32:00 +0100426 if (ins->oprs[0].opflags & OPFLAG_UNKNOWN)
Victor van den Elzenccafc3c2009-02-23 04:35:00 +0100427 /* Be optimistic in pass 1 */
428 return true;
429
H. Peter Anvine2c80182005-01-15 22:15:51 +0000430 if (ins->oprs[0].segment != segment)
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700431 return false;
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000432
H. Peter Anvin2d5baaa2008-09-30 16:31:06 -0700433 isize = ins->oprs[0].offset - offset - isize; /* isize is delta */
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800434 is_byte = (isize >= -128 && isize <= 127); /* is it byte size? */
435
436 if (is_byte && c == 0371 && ins->prefixes[PPS_REP] == P_BND) {
437 /* jmp short (opcode eb) cannot be used with bnd prefix. */
438 ins->prefixes[PPS_REP] = P_none;
Jin Kyu Songbb8cf3f2013-11-29 00:38:29 -0800439 errfunc(ERR_WARNING | ERR_WARN_BND | ERR_PASS2 ,
440 "jmp short does not init bnd regs - bnd prefix dropped.");
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800441 }
442
443 return is_byte;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000444}
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000445
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400446int64_t assemble(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400447 insn * instruction, struct ofmt *output, efunc error,
448 ListGen * listgen)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000449{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000450 const struct itemplate *temp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000451 int j;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700452 enum match_result m;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800453 int64_t insn_end;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000454 int32_t itimes;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800455 int64_t start = offset;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300456 int64_t wsize; /* size for DB etc. */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000457
H. Peter Anvine2c80182005-01-15 22:15:51 +0000458 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000459 cpu = cp;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000460 outfmt = output; /* likewise */
461 list = listgen; /* and again */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000462
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300463 wsize = idata_bytes(instruction->opcode);
464 if (wsize == -1)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000465 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000466
H. Peter Anvineba20a72002-04-30 20:53:55 +0000467 if (wsize) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000468 extop *e;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000469 int32_t t = instruction->times;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000470 if (t < 0)
471 errfunc(ERR_PANIC,
472 "instruction->times < 0 (%ld) in assemble()", t);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000473
H. Peter Anvine2c80182005-01-15 22:15:51 +0000474 while (t--) { /* repeat TIMES times */
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400475 list_for_each(e, instruction->eops) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000476 if (e->type == EOT_DB_NUMBER) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400477 if (wsize > 8) {
H. Peter Anvin3be5d852008-05-20 14:49:32 -0700478 errfunc(ERR_NONFATAL,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400479 "integer supplied to a DT, DO or DY"
Keith Kanios61ff53c2007-04-14 18:54:52 +0000480 " instruction");
H. Peter Anvin55ae1202010-05-06 15:25:43 -0700481 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000482 out(offset, segment, &e->offset,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800483 OUT_ADDRESS, wsize, e->segment, e->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400484 offset += wsize;
485 }
H. Peter Anvin518df302008-06-14 16:53:48 -0700486 } else if (e->type == EOT_DB_STRING ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400487 e->type == EOT_DB_STRING_FREE) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000488 int align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000489
H. Peter Anvine2c80182005-01-15 22:15:51 +0000490 out(offset, segment, e->stringval,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800491 OUT_RAWDATA, e->stringlen, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000492 align = e->stringlen % wsize;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000493
H. Peter Anvine2c80182005-01-15 22:15:51 +0000494 if (align) {
495 align = wsize - align;
H. Peter Anvin999868f2009-02-09 11:03:33 +0100496 out(offset, segment, zero_buffer,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800497 OUT_RAWDATA, align, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000498 }
499 offset += e->stringlen + align;
500 }
501 }
502 if (t > 0 && t == instruction->times - 1) {
503 /*
504 * Dummy call to list->output to give the offset to the
505 * listing module.
506 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800507 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000508 list->uplevel(LIST_TIMES);
509 }
510 }
511 if (instruction->times > 1)
512 list->downlevel(LIST_TIMES);
513 return offset - start;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000514 }
515
H. Peter Anvine2c80182005-01-15 22:15:51 +0000516 if (instruction->opcode == I_INCBIN) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700517 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000518 FILE *fp;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000519
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400520 fp = fopen(fname, "rb");
521 if (!fp) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000522 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
523 fname);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400524 } else if (fseek(fp, 0L, SEEK_END) < 0) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000525 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
526 fname);
Philipp Klokedae212d2013-03-31 12:02:30 +0200527 fclose(fp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400528 } else {
H. Peter Anvin518df302008-06-14 16:53:48 -0700529 static char buf[4096];
530 size_t t = instruction->times;
531 size_t base = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400532 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000533
H. Peter Anvine2c80182005-01-15 22:15:51 +0000534 len = ftell(fp);
535 if (instruction->eops->next) {
536 base = instruction->eops->next->offset;
537 len -= base;
538 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700539 len > (size_t)instruction->eops->next->next->offset)
540 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000541 }
542 /*
543 * Dummy call to list->output to give the offset to the
544 * listing module.
545 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800546 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000547 list->uplevel(LIST_INCBIN);
548 while (t--) {
H. Peter Anvin518df302008-06-14 16:53:48 -0700549 size_t l;
H. Peter Anvineba20a72002-04-30 20:53:55 +0000550
H. Peter Anvine2c80182005-01-15 22:15:51 +0000551 fseek(fp, base, SEEK_SET);
552 l = len;
553 while (l > 0) {
H. Peter Anvin4a5a6df2009-06-27 16:14:18 -0700554 int32_t m;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400555 m = fread(buf, 1, l > sizeof(buf) ? sizeof(buf) : l, fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000556 if (!m) {
557 /*
558 * This shouldn't happen unless the file
559 * actually changes while we are reading
560 * it.
561 */
562 error(ERR_NONFATAL,
563 "`incbin': unexpected EOF while"
564 " reading file `%s'", fname);
565 t = 0; /* Try to exit cleanly */
566 break;
567 }
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800568 out(offset, segment, buf, OUT_RAWDATA, m,
H. Peter Anvine2c80182005-01-15 22:15:51 +0000569 NO_SEG, NO_SEG);
570 l -= m;
571 }
572 }
573 list->downlevel(LIST_INCBIN);
574 if (instruction->times > 1) {
575 /*
576 * Dummy call to list->output to give the offset to the
577 * listing module.
578 */
H. Peter Anvin34f6fb02007-11-09 14:44:02 -0800579 list->output(offset, NULL, OUT_RAWDATA, 0);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000580 list->uplevel(LIST_TIMES);
581 list->downlevel(LIST_TIMES);
582 }
583 fclose(fp);
584 return instruction->times * len;
585 }
586 return 0; /* if we're here, there's an error */
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000587 }
588
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700589 /* Check to see if we need an address-size prefix */
590 add_asp(instruction, bits);
591
H. Peter Anvin23595f52009-07-25 17:44:25 -0700592 m = find_match(&temp, instruction, segment, offset, bits);
H. Peter Anvin70653092007-10-19 14:42:29 -0700593
H. Peter Anvin23595f52009-07-25 17:44:25 -0700594 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400595 /* Matches! */
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800596 int64_t insn_size = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400597 itimes = instruction->times;
598 if (insn_size < 0) /* shouldn't be, on pass two */
599 error(ERR_PANIC, "errors made it through from pass one");
600 else
601 while (itimes--) {
602 for (j = 0; j < MAXPREFIX; j++) {
603 uint8_t c = 0;
604 switch (instruction->prefixes[j]) {
605 case P_WAIT:
606 c = 0x9B;
607 break;
608 case P_LOCK:
609 c = 0xF0;
610 break;
611 case P_REPNE:
612 case P_REPNZ:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800613 case P_XACQUIRE:
Jin Kyu Song03041092013-10-15 19:38:51 -0700614 case P_BND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400615 c = 0xF2;
616 break;
617 case P_REPE:
618 case P_REPZ:
619 case P_REP:
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800620 case P_XRELEASE:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400621 c = 0xF3;
622 break;
623 case R_CS:
624 if (bits == 64) {
625 error(ERR_WARNING | ERR_PASS2,
626 "cs segment base generated, but will be ignored in 64-bit mode");
627 }
628 c = 0x2E;
629 break;
630 case R_DS:
631 if (bits == 64) {
632 error(ERR_WARNING | ERR_PASS2,
633 "ds segment base generated, but will be ignored in 64-bit mode");
634 }
635 c = 0x3E;
636 break;
637 case R_ES:
638 if (bits == 64) {
639 error(ERR_WARNING | ERR_PASS2,
640 "es segment base generated, but will be ignored in 64-bit mode");
641 }
642 c = 0x26;
643 break;
644 case R_FS:
645 c = 0x64;
646 break;
647 case R_GS:
648 c = 0x65;
649 break;
650 case R_SS:
651 if (bits == 64) {
652 error(ERR_WARNING | ERR_PASS2,
653 "ss segment base generated, but will be ignored in 64-bit mode");
654 }
655 c = 0x36;
656 break;
657 case R_SEGR6:
658 case R_SEGR7:
659 error(ERR_NONFATAL,
660 "segr6 and segr7 cannot be used as prefixes");
661 break;
662 case P_A16:
663 if (bits == 64) {
664 error(ERR_NONFATAL,
665 "16-bit addressing is not supported "
666 "in 64-bit mode");
667 } else if (bits != 16)
668 c = 0x67;
669 break;
670 case P_A32:
671 if (bits != 32)
672 c = 0x67;
673 break;
674 case P_A64:
675 if (bits != 64) {
676 error(ERR_NONFATAL,
677 "64-bit addressing is only supported "
678 "in 64-bit mode");
679 }
680 break;
681 case P_ASP:
682 c = 0x67;
683 break;
684 case P_O16:
685 if (bits != 16)
686 c = 0x66;
687 break;
688 case P_O32:
689 if (bits == 16)
690 c = 0x66;
691 break;
692 case P_O64:
693 /* REX.W */
694 break;
695 case P_OSP:
696 c = 0x66;
697 break;
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700698 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800699 case P_VEX3:
700 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800701 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400702 case P_none:
703 break;
704 default:
705 error(ERR_PANIC, "invalid instruction prefix");
706 }
707 if (c != 0) {
708 out(offset, segment, &c, OUT_RAWDATA, 1,
709 NO_SEG, NO_SEG);
710 offset++;
711 }
712 }
713 insn_end = offset + insn_size;
714 gencode(segment, offset, bits, instruction,
715 temp, insn_end);
716 offset += insn_size;
717 if (itimes > 0 && itimes == instruction->times - 1) {
718 /*
719 * Dummy call to list->output to give the offset to the
720 * listing module.
721 */
722 list->output(offset, NULL, OUT_RAWDATA, 0);
723 list->uplevel(LIST_TIMES);
724 }
725 }
726 if (instruction->times > 1)
727 list->downlevel(LIST_TIMES);
728 return offset - start;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700729 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400730 /* No match */
731 switch (m) {
732 case MERR_OPSIZEMISSING:
733 error(ERR_NONFATAL, "operation size not specified");
734 break;
735 case MERR_OPSIZEMISMATCH:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000736 error(ERR_NONFATAL, "mismatch in operand sizes");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400737 break;
Jin Kyu Song25c22122013-10-30 03:12:45 -0700738 case MERR_BRNUMMISMATCH:
739 error(ERR_NONFATAL,
740 "mismatch in the number of broadcasting elements");
741 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400742 case MERR_BADCPU:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000743 error(ERR_NONFATAL, "no instruction for this cpu level");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400744 break;
745 case MERR_BADMODE:
H. Peter Anvin6cda4142008-12-29 20:52:28 -0800746 error(ERR_NONFATAL, "instruction not supported in %d-bit mode",
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400747 bits);
748 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -0800749 case MERR_ENCMISMATCH:
750 error(ERR_NONFATAL, "specific encoding scheme not available");
751 break;
Jin Kyu Song305f3ce2013-11-21 19:40:42 -0800752 case MERR_BADBND:
753 error(ERR_NONFATAL, "bnd prefix is not allowed");
754 break;
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800755 case MERR_BADREPNE:
756 error(ERR_NONFATAL, "%s prefix is not allowed",
757 (has_prefix(instruction, PPS_REP, P_REPNE) ?
758 "repne" : "repnz"));
759 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400760 default:
H. Peter Anvine2c80182005-01-15 22:15:51 +0000761 error(ERR_NONFATAL,
762 "invalid combination of opcode and operands");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400763 break;
764 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000765 }
766 return 0;
767}
768
Cyrill Gorcunov08359152013-11-09 22:16:11 +0400769int64_t insn_size(int32_t segment, int64_t offset, int bits, iflag_t cp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400770 insn * instruction, efunc error)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000771{
H. Peter Anvin3360d792007-09-11 04:16:57 +0000772 const struct itemplate *temp;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700773 enum match_result m;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000774
H. Peter Anvine2c80182005-01-15 22:15:51 +0000775 errfunc = error; /* to pass to other functions */
H. Peter Anvinaf535c12002-04-30 20:59:21 +0000776 cpu = cp;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000777
Cyrill Gorcunov37575242009-08-16 12:00:01 +0400778 if (instruction->opcode == I_none)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000779 return 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000780
H. Peter Anvincfbe7c32007-09-18 17:49:09 -0700781 if (instruction->opcode == I_DB || instruction->opcode == I_DW ||
782 instruction->opcode == I_DD || instruction->opcode == I_DQ ||
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400783 instruction->opcode == I_DT || instruction->opcode == I_DO ||
784 instruction->opcode == I_DY) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000785 extop *e;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300786 int32_t isize, osize, wsize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000787
H. Peter Anvine2c80182005-01-15 22:15:51 +0000788 isize = 0;
Cyrill Gorcunovbafd8772009-10-31 20:02:14 +0300789 wsize = idata_bytes(instruction->opcode);
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000790
Cyrill Gorcunova92a3a52009-07-27 22:33:59 +0400791 list_for_each(e, instruction->eops) {
Keith Kaniosb7a89542007-04-12 02:40:54 +0000792 int32_t align;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000793
H. Peter Anvine2c80182005-01-15 22:15:51 +0000794 osize = 0;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400795 if (e->type == EOT_DB_NUMBER) {
H. Peter Anvine2c80182005-01-15 22:15:51 +0000796 osize = 1;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +0400797 warn_overflow_const(e->offset, wsize);
798 } else if (e->type == EOT_DB_STRING ||
799 e->type == EOT_DB_STRING_FREE)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000800 osize = e->stringlen;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000801
H. Peter Anvine2c80182005-01-15 22:15:51 +0000802 align = (-osize) % wsize;
803 if (align < 0)
804 align += wsize;
805 isize += osize + align;
806 }
807 return isize * instruction->times;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000808 }
809
H. Peter Anvine2c80182005-01-15 22:15:51 +0000810 if (instruction->opcode == I_INCBIN) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400811 const char *fname = instruction->eops->stringval;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000812 FILE *fp;
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300813 int64_t val = 0;
H. Peter Anvin518df302008-06-14 16:53:48 -0700814 size_t len;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000815
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400816 fp = fopen(fname, "rb");
817 if (!fp)
H. Peter Anvine2c80182005-01-15 22:15:51 +0000818 error(ERR_NONFATAL, "`incbin': unable to open file `%s'",
819 fname);
820 else if (fseek(fp, 0L, SEEK_END) < 0)
821 error(ERR_NONFATAL, "`incbin': unable to seek on file `%s'",
822 fname);
823 else {
824 len = ftell(fp);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000825 if (instruction->eops->next) {
826 len -= instruction->eops->next->offset;
827 if (instruction->eops->next->next &&
H. Peter Anvin518df302008-06-14 16:53:48 -0700828 len > (size_t)instruction->eops->next->next->offset) {
829 len = (size_t)instruction->eops->next->next->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000830 }
831 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300832 val = instruction->times * len;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000833 }
Cyrill Gorcunov6531d6d2009-12-05 14:04:55 +0300834 if (fp)
835 fclose(fp);
836 return val;
H. Peter Anvind7ed89e2002-04-30 20:52:08 +0000837 }
838
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -0700839 /* Check to see if we need an address-size prefix */
840 add_asp(instruction, bits);
841
H. Peter Anvin23595f52009-07-25 17:44:25 -0700842 m = find_match(&temp, instruction, segment, offset, bits);
843 if (m == MOK_GOOD) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400844 /* we've matched an instruction. */
845 int64_t isize;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400846 int j;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +0100847
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800848 isize = calcsize(segment, offset, bits, instruction, temp);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400849 if (isize < 0)
850 return -1;
851 for (j = 0; j < MAXPREFIX; j++) {
852 switch (instruction->prefixes[j]) {
853 case P_A16:
854 if (bits != 16)
855 isize++;
856 break;
857 case P_A32:
858 if (bits != 32)
859 isize++;
860 break;
861 case P_O16:
862 if (bits != 16)
863 isize++;
864 break;
865 case P_O32:
866 if (bits == 16)
867 isize++;
868 break;
869 case P_A64:
870 case P_O64:
Jin Kyu Song945b1b82013-10-25 19:29:53 -0700871 case P_EVEX:
H. Peter Anvin621a69a2013-11-28 12:11:24 -0800872 case P_VEX3:
873 case P_VEX2:
Jin Kyu Songb287ff02013-12-04 20:05:55 -0800874 case P_NOBND:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400875 case P_none:
876 break;
877 default:
878 isize++;
879 break;
880 }
881 }
882 return isize * instruction->times;
H. Peter Anvin23595f52009-07-25 17:44:25 -0700883 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400884 return -1; /* didn't match any instruction */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000885 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000886}
887
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800888static void bad_hle_warn(const insn * ins, uint8_t hleok)
889{
890 enum prefixes rep_pfx = ins->prefixes[PPS_REP];
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800891 enum whatwarn { w_none, w_lock, w_inval } ww;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800892 static const enum whatwarn warn[2][4] =
893 {
894 { w_inval, w_inval, w_none, w_lock }, /* XACQUIRE */
895 { w_inval, w_none, w_none, w_lock }, /* XRELEASE */
896 };
897 unsigned int n;
898
899 n = (unsigned int)rep_pfx - P_XACQUIRE;
900 if (n > 1)
901 return; /* Not XACQUIRE/XRELEASE */
902
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800903 ww = warn[n][hleok];
904 if (!is_class(MEMORY, ins->oprs[0].type))
905 ww = w_inval; /* HLE requires operand 0 to be memory */
906
907 switch (ww) {
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800908 case w_none:
909 break;
910
911 case w_lock:
912 if (ins->prefixes[PPS_LOCK] != P_LOCK) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800913 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800914 "%s with this instruction requires lock",
915 prefix_name(rep_pfx));
916 }
917 break;
918
919 case w_inval:
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -0800920 errfunc(ERR_WARNING | ERR_WARN_HLE | ERR_PASS2,
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800921 "%s invalid with this instruction",
922 prefix_name(rep_pfx));
923 break;
924 }
925}
926
H. Peter Anvin507ae032008-10-09 15:37:10 -0700927/* Common construct */
Cyrill Gorcunov62576a02012-12-02 02:47:16 +0400928#define case3(x) case (x): case (x)+1: case (x)+2
929#define case4(x) case3(x): case (x)+3
H. Peter Anvin507ae032008-10-09 15:37:10 -0700930
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800931static int64_t calcsize(int32_t segment, int64_t offset, int bits,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800932 insn * ins, const struct itemplate *temp)
H. Peter Anvineba20a72002-04-30 20:53:55 +0000933{
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800934 const uint8_t *codes = temp->code;
Charles Crayne1f8bc4c2007-11-06 18:27:23 -0800935 int64_t length = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +0000936 uint8_t c;
H. Peter Anvin3df97a72007-05-30 03:25:21 +0000937 int rex_mask = ~0;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700938 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -0700939 struct operand *opx;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700940 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700941 enum ea_type eat;
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -0800942 uint8_t hleok = 0;
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -0800943 bool lockcheck = true;
Jin Kyu Song164d6072013-10-15 19:10:13 -0700944 enum reg_enum mib_index = R_none; /* For a separate index MIB reg form */
H. Peter Anvineba20a72002-04-30 20:53:55 +0000945
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700946 ins->rex = 0; /* Ensure REX is reset */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -0700947 eat = EA_SCALAR; /* Expect a scalar EA */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -0700948 memset(ins->evex_p, 0, 3); /* Ensure EVEX is reset */
H. Peter Anvine3917fc2007-11-01 14:53:32 -0700949
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700950 if (ins->prefixes[PPS_OSIZE] == P_O64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400951 ins->rex |= REX_W;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -0700952
H. Peter Anvine2c80182005-01-15 22:15:51 +0000953 (void)segment; /* Don't warn that this parameter is unused */
954 (void)offset; /* Don't warn that this parameter is unused */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +0000955
H. Peter Anvin839eca22007-10-29 23:12:47 -0700956 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400957 c = *codes++;
958 op1 = (c & 3) + ((opex & 1) << 2);
959 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
960 opx = &ins->oprs[op1];
961 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700962
H. Peter Anvin839eca22007-10-29 23:12:47 -0700963 switch (c) {
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400964 case4(01):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000965 codes += c, length += c;
966 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700967
Cyrill Gorcunov59df4212012-12-02 02:51:18 +0400968 case3(05):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400969 opex = c;
970 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -0700971
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400972 case4(010):
973 ins->rex |=
974 op_rexflags(opx, REX_B|REX_H|REX_P|REX_W);
H. Peter Anvine2c80182005-01-15 22:15:51 +0000975 codes++, length++;
976 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700977
Jin Kyu Song164d6072013-10-15 19:10:13 -0700978 case4(014):
979 /* this is an index reg of MIB operand */
980 mib_index = opx->basereg;
981 break;
982
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400983 case4(020):
984 case4(024):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000985 length++;
986 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700987
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400988 case4(030):
H. Peter Anvine2c80182005-01-15 22:15:51 +0000989 length += 2;
990 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700991
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400992 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -0700993 if (opx->type & (BITS16 | BITS32 | BITS64))
994 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +0000995 else
996 length += (bits == 16) ? 2 : 4;
997 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -0700998
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +0400999 case4(040):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001000 length += 4;
1001 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001002
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001003 case4(044):
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001004 length += ins->addr_size >> 3;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001005 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001006
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001007 case4(050):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001008 length++;
1009 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001010
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001011 case4(054):
Keith Kaniosb7a89542007-04-12 02:40:54 +00001012 length += 8; /* MOV reg64/imm */
1013 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001014
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001015 case4(060):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001016 length += 2;
1017 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001018
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001019 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001020 if (opx->type & (BITS16 | BITS32 | BITS64))
1021 length += (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001022 else
1023 length += (bits == 16) ? 2 : 4;
1024 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001025
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001026 case4(070):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001027 length += 4;
1028 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001029
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001030 case4(074):
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001031 length += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001032 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001033
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001034 case 0172:
1035 case 0173:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001036 codes++;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001037 length++;
1038 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001039
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001040 case4(0174):
1041 length++;
1042 break;
1043
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001044 case4(0240):
1045 ins->rex |= REX_EV;
1046 ins->vexreg = regval(opx);
1047 ins->evex_p[2] |= op_evexflags(opx, EVEX_P2VP, 2); /* High-16 NDS */
1048 ins->vex_cm = *codes++;
1049 ins->vex_wlp = *codes++;
1050 ins->evex_tuple = (*codes++ - 0300);
1051 break;
1052
1053 case 0250:
1054 ins->rex |= REX_EV;
1055 ins->vexreg = 0;
1056 ins->vex_cm = *codes++;
1057 ins->vex_wlp = *codes++;
1058 ins->evex_tuple = (*codes++ - 0300);
1059 break;
1060
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001061 case4(0254):
1062 length += 4;
1063 break;
1064
1065 case4(0260):
1066 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001067 ins->vexreg = regval(opx);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001068 ins->vex_cm = *codes++;
1069 ins->vex_wlp = *codes++;
1070 break;
1071
1072 case 0270:
1073 ins->rex |= REX_V;
H. Peter Anvinfc561202011-07-07 16:58:22 -07001074 ins->vexreg = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001075 ins->vex_cm = *codes++;
1076 ins->vex_wlp = *codes++;
1077 break;
1078
Cyrill Gorcunov59df4212012-12-02 02:51:18 +04001079 case3(0271):
H. Peter Anvin574784d2012-02-25 22:33:46 -08001080 hleok = c & 3;
1081 break;
1082
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001083 case4(0274):
1084 length++;
1085 break;
1086
1087 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001088 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001089
H. Peter Anvine2c80182005-01-15 22:15:51 +00001090 case 0310:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001091 if (bits == 64)
1092 return -1;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001093 length += (bits != 16) && !has_prefix(ins, PPS_ASIZE, P_A16);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001094 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001095
H. Peter Anvine2c80182005-01-15 22:15:51 +00001096 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001097 length += (bits != 32) && !has_prefix(ins, PPS_ASIZE, P_A32);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001098 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001099
H. Peter Anvine2c80182005-01-15 22:15:51 +00001100 case 0312:
H. Peter Anvin70653092007-10-19 14:42:29 -07001101 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001102
Keith Kaniosb7a89542007-04-12 02:40:54 +00001103 case 0313:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001104 if (bits != 64 || has_prefix(ins, PPS_ASIZE, P_A16) ||
1105 has_prefix(ins, PPS_ASIZE, P_A32))
1106 return -1;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001107 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001108
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001109 case4(0314):
1110 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001111
H. Peter Anvine2c80182005-01-15 22:15:51 +00001112 case 0320:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001113 {
1114 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1115 if (pfx == P_O16)
1116 break;
1117 if (pfx != P_none)
1118 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1119 else
1120 ins->prefixes[PPS_OSIZE] = P_O16;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001121 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001122 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001123
H. Peter Anvine2c80182005-01-15 22:15:51 +00001124 case 0321:
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001125 {
1126 enum prefixes pfx = ins->prefixes[PPS_OSIZE];
1127 if (pfx == P_O32)
1128 break;
1129 if (pfx != P_none)
1130 errfunc(ERR_WARNING | ERR_PASS2, "invalid operand size prefix");
1131 else
1132 ins->prefixes[PPS_OSIZE] = P_O32;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001133 break;
Victor van den Elzen6dfbddb2010-12-29 17:13:38 +00001134 }
H. Peter Anvin507ae032008-10-09 15:37:10 -07001135
H. Peter Anvine2c80182005-01-15 22:15:51 +00001136 case 0322:
1137 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001138
Keith Kaniosb7a89542007-04-12 02:40:54 +00001139 case 0323:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001140 rex_mask &= ~REX_W;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001141 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001142
Keith Kaniosb7a89542007-04-12 02:40:54 +00001143 case 0324:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001144 ins->rex |= REX_W;
H. Peter Anvin8d7316a2007-04-18 02:27:18 +00001145 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001146
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001147 case 0325:
1148 ins->rex |= REX_NH;
1149 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001150
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001151 case 0326:
1152 break;
1153
H. Peter Anvine2c80182005-01-15 22:15:51 +00001154 case 0330:
1155 codes++, length++;
1156 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001157
H. Peter Anvine2c80182005-01-15 22:15:51 +00001158 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001159 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001160
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001161 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001162 case 0333:
1163 length++;
1164 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001165
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001166 case 0334:
1167 ins->rex |= REX_L;
1168 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001169
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001170 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001171 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001172
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001173 case 0336:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001174 if (!ins->prefixes[PPS_REP])
1175 ins->prefixes[PPS_REP] = P_REP;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001176 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001177
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001178 case 0337:
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001179 if (!ins->prefixes[PPS_REP])
1180 ins->prefixes[PPS_REP] = P_REPNE;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001181 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001182
H. Peter Anvine2c80182005-01-15 22:15:51 +00001183 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001184 if (ins->oprs[0].segment != NO_SEG)
1185 errfunc(ERR_NONFATAL, "attempt to reserve non-constant"
1186 " quantity of BSS space");
1187 else
H. Peter Anvin428fd672007-11-15 10:25:52 -08001188 length += ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001189 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001190
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001191 case 0341:
1192 if (!ins->prefixes[PPS_WAIT])
1193 ins->prefixes[PPS_WAIT] = P_WAIT;
1194 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001195
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001196 case 0360:
1197 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001198
Ben Rudiak-Gould94ba02f2013-03-10 21:46:12 +04001199 case 0361:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001200 length++;
1201 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001202
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001203 case 0364:
1204 case 0365:
1205 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001206
Keith Kanios48af1772007-08-17 07:37:52 +00001207 case 0366:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001208 case 0367:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001209 length++;
1210 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001211
Jin Kyu Songb4e1ae12013-11-08 13:31:58 -08001212 case 0370:
1213 case 0371:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001214 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001215
H. Peter Anvine2c80182005-01-15 22:15:51 +00001216 case 0373:
1217 length++;
1218 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001219
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001220 case 0374:
1221 eat = EA_XMMVSIB;
1222 break;
1223
1224 case 0375:
1225 eat = EA_YMMVSIB;
1226 break;
1227
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001228 case 0376:
1229 eat = EA_ZMMVSIB;
1230 break;
1231
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001232 case4(0100):
1233 case4(0110):
1234 case4(0120):
1235 case4(0130):
1236 case4(0200):
1237 case4(0204):
1238 case4(0210):
1239 case4(0214):
1240 case4(0220):
1241 case4(0224):
1242 case4(0230):
1243 case4(0234):
1244 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001245 ea ea_data;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001246 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001247 opflags_t rflags;
1248 struct operand *opy = &ins->oprs[op2];
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001249 struct operand *op_er_sae;
H. Peter Anvinae64c9d2008-10-25 00:41:00 -07001250
Keith Kaniosb7a89542007-04-12 02:40:54 +00001251 ea_data.rex = 0; /* Ensure ea.REX is initially 0 */
H. Peter Anvin70653092007-10-19 14:42:29 -07001252
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001253 if (c <= 0177) {
1254 /* pick rfield from operand b (opx) */
1255 rflags = regflag(opx);
1256 rfield = nasm_regvals[opx->basereg];
1257 } else {
1258 rflags = 0;
1259 rfield = c & 7;
1260 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001261
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07001262 /* EVEX.b1 : evex_brerop contains the operand position */
1263 op_er_sae = (ins->evex_brerop >= 0 ?
1264 &ins->oprs[ins->evex_brerop] : NULL);
1265
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001266 if (op_er_sae && (op_er_sae->decoflags & (ER | SAE))) {
1267 /* set EVEX.b */
1268 ins->evex_p[2] |= EVEX_P2B;
1269 if (op_er_sae->decoflags & ER) {
1270 /* set EVEX.RC (rounding control) */
1271 ins->evex_p[2] |= ((ins->evex_rm - BRC_RN) << 5)
1272 & EVEX_P2RC;
1273 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001274 } else {
1275 /* set EVEX.L'L (vector length) */
1276 ins->evex_p[2] |= ((ins->vex_wlp << (5 - 2)) & EVEX_P2LL);
Jin Kyu Song5f3bfee2013-11-20 15:32:52 -08001277 ins->evex_p[1] |= ((ins->vex_wlp << (7 - 4)) & EVEX_P1W);
Jin Kyu Songc47ef942013-08-30 18:10:35 -07001278 if (opy->decoflags & BRDCAST_MASK) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001279 /* set EVEX.b */
1280 ins->evex_p[2] |= EVEX_P2B;
1281 }
1282 }
1283
Jin Kyu Song4360ba22013-12-10 16:24:45 -08001284 if (itemp_has(temp, IF_MIB)) {
1285 opy->eaflags |= EAF_MIB;
1286 /*
1287 * if a separate form of MIB (ICC style) is used,
1288 * the index reg info is merged into mem operand
1289 */
1290 if (mib_index != R_none) {
1291 opy->indexreg = mib_index;
1292 opy->scale = 1;
1293 opy->hintbase = mib_index;
1294 opy->hinttype = EAH_NOTBASE;
1295 }
Jin Kyu Song3b653232013-11-08 11:41:12 -08001296 }
1297
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001298 if (process_ea(opy, &ea_data, bits,
1299 rfield, rflags, ins) != eat) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001300 errfunc(ERR_NONFATAL, "invalid effective address");
1301 return -1;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001302 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001303 ins->rex |= ea_data.rex;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001304 length += ea_data.size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001305 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001306 }
1307 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001308
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001309 default:
1310 errfunc(ERR_PANIC, "internal instruction table corrupt"
1311 ": instruction code \\%o (0x%02X) given", c, c);
1312 break;
1313 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001314 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001315
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001316 ins->rex &= rex_mask;
H. Peter Anvin70653092007-10-19 14:42:29 -07001317
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001318 if (ins->rex & REX_NH) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001319 if (ins->rex & REX_H) {
1320 errfunc(ERR_NONFATAL, "instruction cannot use high registers");
1321 return -1;
1322 }
1323 ins->rex &= ~REX_P; /* Don't force REX prefix due to high reg */
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001324 }
1325
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001326 switch (ins->prefixes[PPS_VEX]) {
1327 case P_EVEX:
1328 if (!(ins->rex & REX_EV))
1329 return -1;
1330 break;
1331 case P_VEX3:
1332 case P_VEX2:
1333 if (!(ins->rex & REX_V))
1334 return -1;
1335 break;
1336 default:
1337 break;
1338 }
1339
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001340 if (ins->rex & (REX_V | REX_EV)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001341 int bad32 = REX_R|REX_W|REX_X|REX_B;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001342
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001343 if (ins->rex & REX_H) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001344 errfunc(ERR_NONFATAL, "cannot use high register in AVX instruction");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001345 return -1;
1346 }
H. Peter Anvin421059c2010-08-16 14:56:33 -07001347 switch (ins->vex_wlp & 060) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001348 case 000:
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001349 case 040:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001350 ins->rex &= ~REX_W;
1351 break;
H. Peter Anvin229fa6c2010-08-16 15:21:48 -07001352 case 020:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001353 ins->rex |= REX_W;
1354 bad32 &= ~REX_W;
1355 break;
H. Peter Anvin421059c2010-08-16 14:56:33 -07001356 case 060:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001357 /* Follow REX_W */
1358 break;
1359 }
H. Peter Anvind85d2502008-05-04 17:53:31 -07001360
H. Peter Anvinfc561202011-07-07 16:58:22 -07001361 if (bits != 64 && ((ins->rex & bad32) || ins->vexreg > 7)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001362 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1363 return -1;
Jin Kyu Song66c61922013-08-26 20:28:43 -07001364 } else if (!(ins->rex & REX_EV) &&
1365 ((ins->vexreg > 15) || (ins->evex_p[0] & 0xf0))) {
1366 errfunc(ERR_NONFATAL, "invalid high-16 register in non-AVX-512");
1367 return -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001368 }
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001369 if (ins->rex & REX_EV)
1370 length += 4;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001371 else if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1372 ins->prefixes[PPS_VEX] == P_VEX3)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001373 length += 3;
1374 else
1375 length += 2;
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001376 } else if (ins->rex & REX_MASK) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001377 if (ins->rex & REX_H) {
1378 errfunc(ERR_NONFATAL, "cannot use high register in rex instruction");
1379 return -1;
1380 } else if (bits == 64) {
1381 length++;
1382 } else if ((ins->rex & REX_L) &&
1383 !(ins->rex & (REX_P|REX_W|REX_X|REX_B)) &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001384 iflag_ffs(&cpu) >= IF_X86_64) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001385 /* LOCK-as-REX.R */
H. Peter Anvin10da41e2012-02-24 20:57:04 -08001386 assert_no_prefix(ins, PPS_LOCK);
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001387 lockcheck = false; /* Already errored, no need for warning */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001388 length++;
1389 } else {
1390 errfunc(ERR_NONFATAL, "invalid operands in non-64-bit mode");
1391 return -1;
1392 }
Keith Kaniosb7a89542007-04-12 02:40:54 +00001393 }
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001394
1395 if (has_prefix(ins, PPS_LOCK, P_LOCK) && lockcheck &&
Cyrill Gorcunov08359152013-11-09 22:16:11 +04001396 (!itemp_has(temp,IF_LOCK) || !is_class(MEMORY, ins->oprs[0].type))) {
H. Peter Anvin5a24fdd2012-02-25 15:10:04 -08001397 errfunc(ERR_WARNING | ERR_WARN_LOCK | ERR_PASS2 ,
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08001398 "instruction is not lockable");
1399 }
1400
H. Peter Anvin4ecd5d72012-02-24 21:51:46 -08001401 bad_hle_warn(ins, hleok);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001402
Jin Kyu Songb287ff02013-12-04 20:05:55 -08001403 /*
1404 * when BND prefix is set by DEFAULT directive,
1405 * BND prefix is added to every appropriate instruction line
1406 * unless it is overridden by NOBND prefix.
1407 */
1408 if (globalbnd &&
1409 (itemp_has(temp, IF_BND) && !has_prefix(ins, PPS_REP, P_NOBND)))
1410 ins->prefixes[PPS_REP] = P_BND;
1411
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001412 return length;
1413}
Keith Kaniosb7a89542007-04-12 02:40:54 +00001414
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001415static inline unsigned int emit_rex(insn *ins, int32_t segment, int64_t offset, int bits)
1416{
1417 if (bits == 64) {
H. Peter Anvin89f78f52014-05-21 08:30:40 -07001418 if ((ins->rex & REX_MASK) &&
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001419 !(ins->rex & (REX_V | REX_EV)) &&
1420 !ins->rex_done) {
Cyrill Gorcunov5b144752014-05-06 01:50:22 +04001421 int rex = (ins->rex & REX_MASK) | REX_P;
Cyrill Gorcunovaa29b1d2014-05-05 00:30:58 +04001422 out(offset, segment, &rex, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001423 ins->rex_done = true;
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001424 return 1;
1425 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001426 }
1427
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001428 return 0;
1429}
1430
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001431static void gencode(int32_t segment, int64_t offset, int bits,
H. Peter Anvin833caea2008-10-04 19:02:30 -07001432 insn * ins, const struct itemplate *temp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001433 int64_t insn_end)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001434{
Keith Kaniosb7a89542007-04-12 02:40:54 +00001435 uint8_t c;
1436 uint8_t bytes[4];
Charles Crayne1f8bc4c2007-11-06 18:27:23 -08001437 int64_t size;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001438 int64_t data;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001439 int op1, op2;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001440 struct operand *opx;
H. Peter Anvin833caea2008-10-04 19:02:30 -07001441 const uint8_t *codes = temp->code;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001442 uint8_t opex = 0;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001443 enum ea_type eat = EA_SCALAR;
H. Peter Anvin70653092007-10-19 14:42:29 -07001444
H. Peter Anvin0a9250c2014-05-21 08:19:16 -07001445 ins->rex_done = false;
1446
H. Peter Anvin839eca22007-10-29 23:12:47 -07001447 while (*codes) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001448 c = *codes++;
1449 op1 = (c & 3) + ((opex & 1) << 2);
1450 op2 = ((c >> 3) & 3) + ((opex & 2) << 1);
1451 opx = &ins->oprs[op1];
1452 opex = 0; /* For the next iteration */
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001453
H. Peter Anvin839eca22007-10-29 23:12:47 -07001454 switch (c) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001455 case 01:
1456 case 02:
1457 case 03:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001458 case 04:
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001459 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001460 out(offset, segment, codes, OUT_RAWDATA, c, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001461 codes += c;
1462 offset += c;
1463 break;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001464
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001465 case 05:
1466 case 06:
1467 case 07:
1468 opex = c;
1469 break;
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001470
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001471 case4(010):
Cyrill Gorcunov98238762013-03-02 02:48:23 +04001472 offset += emit_rex(ins, segment, offset, bits);
H. Peter Anvindcffe4b2008-10-10 22:10:31 -07001473 bytes[0] = *codes++ + (regval(opx) & 7);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001474 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001475 offset += 1;
1476 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001477
Jin Kyu Song164d6072013-10-15 19:10:13 -07001478 case4(014):
1479 break;
1480
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001481 case4(020):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001482 if (opx->offset < -256 || opx->offset > 255) {
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001483 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001484 "byte value exceeds bounds");
H. Peter Anvine2c80182005-01-15 22:15:51 +00001485 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001486 out_imm8(offset, segment, opx, -1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001487 offset += 1;
1488 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001489
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001490 case4(024):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001491 if (opx->offset < 0 || opx->offset > 255)
H. Peter Anvine9d7f1a2008-10-05 19:42:55 -07001492 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001493 "unsigned byte value exceeds bounds");
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001494 out_imm8(offset, segment, opx, 1);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001495 offset += 1;
1496 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001497
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001498 case4(030):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001499 warn_overflow_opd(opx, 2);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001500 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001501 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001502 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001503 offset += 2;
1504 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001505
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001506 case4(034):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001507 if (opx->type & (BITS16 | BITS32))
1508 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001509 else
1510 size = (bits == 16) ? 2 : 4;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001511 warn_overflow_opd(opx, size);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001512 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001513 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001514 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001515 offset += size;
1516 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001517
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001518 case4(040):
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001519 warn_overflow_opd(opx, 4);
H. Peter Anvin839eca22007-10-29 23:12:47 -07001520 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001521 out(offset, segment, &data, OUT_ADDRESS, 4,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001522 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001523 offset += 4;
1524 break;
H. Peter Anvin3ba46772002-05-27 23:19:35 +00001525
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001526 case4(044):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001527 data = opx->offset;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001528 size = ins->addr_size >> 3;
Cyrill Gorcunov9ccabd22009-09-21 00:56:20 +04001529 warn_overflow_opd(opx, size);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001530 out(offset, segment, &data, OUT_ADDRESS, size,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001531 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001532 offset += size;
1533 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001534
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001535 case4(050):
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001536 if (opx->segment != segment) {
1537 data = opx->offset;
1538 out(offset, segment, &data,
1539 OUT_REL1ADR, insn_end - offset,
1540 opx->segment, opx->wrt);
1541 } else {
1542 data = opx->offset - insn_end;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001543 if (data > 127 || data < -128)
1544 errfunc(ERR_NONFATAL, "short jump is out of range");
H. Peter Anvinfea84d72010-05-06 15:32:20 -07001545 out(offset, segment, &data,
1546 OUT_ADDRESS, 1, NO_SEG, NO_SEG);
1547 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001548 offset += 1;
1549 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001550
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001551 case4(054):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001552 data = (int64_t)opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001553 out(offset, segment, &data, OUT_ADDRESS, 8,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001554 opx->segment, opx->wrt);
Keith Kaniosb7a89542007-04-12 02:40:54 +00001555 offset += 8;
1556 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001557
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001558 case4(060):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001559 if (opx->segment != segment) {
1560 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001561 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001562 OUT_REL2ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001563 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001564 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001565 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001566 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001567 OUT_ADDRESS, 2, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001568 }
1569 offset += 2;
1570 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001571
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001572 case4(064):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001573 if (opx->type & (BITS16 | BITS32 | BITS64))
1574 size = (opx->type & BITS16) ? 2 : 4;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001575 else
1576 size = (bits == 16) ? 2 : 4;
H. Peter Anvin839eca22007-10-29 23:12:47 -07001577 if (opx->segment != segment) {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001578 data = opx->offset;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001579 out(offset, segment, &data,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001580 size == 2 ? OUT_REL2ADR : OUT_REL4ADR,
1581 insn_end - offset, opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001582 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001583 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001584 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001585 OUT_ADDRESS, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001586 }
1587 offset += size;
1588 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001589
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001590 case4(070):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001591 if (opx->segment != segment) {
1592 data = opx->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001593 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001594 OUT_REL4ADR, insn_end - offset,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001595 opx->segment, opx->wrt);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001596 } else {
H. Peter Anvin839eca22007-10-29 23:12:47 -07001597 data = opx->offset - insn_end;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001598 out(offset, segment, &data,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001599 OUT_ADDRESS, 4, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001600 }
1601 offset += 4;
1602 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001603
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001604 case4(074):
H. Peter Anvin839eca22007-10-29 23:12:47 -07001605 if (opx->segment == NO_SEG)
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001606 errfunc(ERR_NONFATAL, "value referenced by FAR is not"
1607 " relocatable");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001608 data = 0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001609 out(offset, segment, &data, OUT_ADDRESS, 2,
H. Peter Anvin839eca22007-10-29 23:12:47 -07001610 outfmt->segbase(1 + opx->segment),
1611 opx->wrt);
H. Peter Anvin7eb4a382007-09-17 15:49:30 -07001612 offset += 2;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001613 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001614
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001615 case 0172:
1616 c = *codes++;
1617 opx = &ins->oprs[c >> 3];
1618 bytes[0] = nasm_regvals[opx->basereg] << 4;
1619 opx = &ins->oprs[c & 7];
1620 if (opx->segment != NO_SEG || opx->wrt != NO_SEG) {
1621 errfunc(ERR_NONFATAL,
1622 "non-absolute expression not permitted as argument %d",
1623 c & 7);
1624 } else {
1625 if (opx->offset & ~15) {
1626 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1627 "four-bit argument exceeds bounds");
1628 }
1629 bytes[0] |= opx->offset & 15;
1630 }
1631 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1632 offset++;
1633 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001634
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001635 case 0173:
1636 c = *codes++;
1637 opx = &ins->oprs[c >> 4];
1638 bytes[0] = nasm_regvals[opx->basereg] << 4;
1639 bytes[0] |= c & 15;
1640 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1641 offset++;
1642 break;
H. Peter Anvind58656f2008-05-06 20:11:14 -07001643
H. Peter Anvincffe61e2011-07-07 17:21:24 -07001644 case4(0174):
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001645 bytes[0] = nasm_regvals[opx->basereg] << 4;
1646 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1647 offset++;
1648 break;
H. Peter Anvin52dc3532008-05-20 19:29:04 -07001649
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001650 case4(0254):
H. Peter Anvin588df782008-10-07 10:05:10 -07001651 data = opx->offset;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001652 if (opx->wrt == NO_SEG && opx->segment == NO_SEG &&
1653 (int32_t)data != (int64_t)data) {
1654 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
1655 "signed dword immediate exceeds bounds");
1656 }
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001657 out(offset, segment, &data, OUT_ADDRESS, -4,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001658 opx->segment, opx->wrt);
1659 offset += 4;
H. Peter Anvin588df782008-10-07 10:05:10 -07001660 break;
1661
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001662 case4(0240):
1663 case 0250:
1664 codes += 3;
1665 ins->evex_p[2] |= op_evexflags(&ins->oprs[0],
1666 EVEX_P2Z | EVEX_P2AAA, 2);
1667 ins->evex_p[2] ^= EVEX_P2VP; /* 1's complement */
1668 bytes[0] = 0x62;
1669 /* EVEX.X can be set by either REX or EVEX for different reasons */
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08001670 bytes[1] = ((((ins->rex & 7) << 5) |
1671 (ins->evex_p[0] & (EVEX_P0X | EVEX_P0RP))) ^ 0xf0) |
1672 (ins->vex_cm & 3);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001673 bytes[2] = ((ins->rex & REX_W) << (7 - 3)) |
1674 ((~ins->vexreg & 15) << 3) |
1675 (1 << 2) | (ins->vex_wlp & 3);
1676 bytes[3] = ins->evex_p[2];
1677 out(offset, segment, &bytes, OUT_RAWDATA, 4, NO_SEG, NO_SEG);
1678 offset += 4;
1679 break;
1680
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001681 case4(0260):
1682 case 0270:
1683 codes += 2;
H. Peter Anvin621a69a2013-11-28 12:11:24 -08001684 if (ins->vex_cm != 1 || (ins->rex & (REX_W|REX_X|REX_B)) ||
1685 ins->prefixes[PPS_VEX] == P_VEX3) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001686 bytes[0] = (ins->vex_cm >> 6) ? 0x8f : 0xc4;
1687 bytes[1] = (ins->vex_cm & 31) | ((~ins->rex & 7) << 5);
1688 bytes[2] = ((ins->rex & REX_W) << (7-3)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001689 ((~ins->vexreg & 15)<< 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001690 out(offset, segment, &bytes, OUT_RAWDATA, 3, NO_SEG, NO_SEG);
1691 offset += 3;
1692 } else {
1693 bytes[0] = 0xc5;
1694 bytes[1] = ((~ins->rex & REX_R) << (7-2)) |
H. Peter Anvinfc561202011-07-07 16:58:22 -07001695 ((~ins->vexreg & 15) << 3) | (ins->vex_wlp & 07);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001696 out(offset, segment, &bytes, OUT_RAWDATA, 2, NO_SEG, NO_SEG);
1697 offset += 2;
1698 }
1699 break;
H. Peter Anvind85d2502008-05-04 17:53:31 -07001700
H. Peter Anvine014f352012-02-25 22:35:19 -08001701 case 0271:
1702 case 0272:
1703 case 0273:
H. Peter Anvin8ea22002012-02-25 10:24:24 -08001704 break;
1705
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001706 case4(0274):
1707 {
1708 uint64_t uv, um;
1709 int s;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001710
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001711 if (ins->rex & REX_W)
1712 s = 64;
1713 else if (ins->prefixes[PPS_OSIZE] == P_O16)
1714 s = 16;
1715 else if (ins->prefixes[PPS_OSIZE] == P_O32)
1716 s = 32;
1717 else
1718 s = bits;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001719
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001720 um = (uint64_t)2 << (s-1);
1721 uv = opx->offset;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001722
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001723 if (uv > 127 && uv < (uint64_t)-128 &&
1724 (uv < um-128 || uv > um-1)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001725 /* If this wasn't explicitly byte-sized, warn as though we
1726 * had fallen through to the imm16/32/64 case.
1727 */
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001728 errfunc(ERR_WARNING | ERR_PASS2 | ERR_WARN_NOV,
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04001729 "%s value exceeds bounds",
1730 (opx->type & BITS8) ? "signed byte" :
1731 s == 16 ? "word" :
1732 s == 32 ? "dword" :
1733 "signed dword");
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001734 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001735 if (opx->segment != NO_SEG) {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001736 data = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001737 out(offset, segment, &data, OUT_ADDRESS, 1,
1738 opx->segment, opx->wrt);
1739 } else {
H. Peter Anvin779ed8b2008-10-16 13:01:43 -07001740 bytes[0] = uv;
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001741 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG,
1742 NO_SEG);
1743 }
1744 offset += 1;
1745 break;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001746 }
H. Peter Anvinc1377e92008-10-06 23:40:31 -07001747
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001748 case4(0300):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001749 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001750
H. Peter Anvine2c80182005-01-15 22:15:51 +00001751 case 0310:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001752 if (bits == 32 && !has_prefix(ins, PPS_ASIZE, P_A16)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001753 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001754 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001755 offset += 1;
1756 } else
1757 offset += 0;
1758 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001759
H. Peter Anvine2c80182005-01-15 22:15:51 +00001760 case 0311:
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07001761 if (bits != 32 && !has_prefix(ins, PPS_ASIZE, P_A32)) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001762 *bytes = 0x67;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001763 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001764 offset += 1;
1765 } else
1766 offset += 0;
1767 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001768
H. Peter Anvine2c80182005-01-15 22:15:51 +00001769 case 0312:
1770 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001771
Keith Kaniosb7a89542007-04-12 02:40:54 +00001772 case 0313:
1773 ins->rex = 0;
1774 break;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07001775
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001776 case4(0314):
1777 break;
H. Peter Anvin23440102007-11-12 21:02:33 -08001778
H. Peter Anvine2c80182005-01-15 22:15:51 +00001779 case 0320:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001780 case 0321:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001781 break;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00001782
H. Peter Anvine2c80182005-01-15 22:15:51 +00001783 case 0322:
H. Peter Anvin70653092007-10-19 14:42:29 -07001784 case 0323:
1785 break;
1786
Keith Kaniosb7a89542007-04-12 02:40:54 +00001787 case 0324:
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001788 ins->rex |= REX_W;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001789 break;
H. Peter Anvin70653092007-10-19 14:42:29 -07001790
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001791 case 0325:
1792 break;
H. Peter Anvin9472dab2009-06-24 21:38:29 -07001793
Ben Rudiak-Gouldd7ab1f92013-02-20 23:25:54 +04001794 case 0326:
1795 break;
1796
H. Peter Anvine2c80182005-01-15 22:15:51 +00001797 case 0330:
Cyrill Gorcunov83e69242013-03-03 14:34:31 +04001798 *bytes = *codes++ ^ get_cond_opcode(ins->condition);
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001799 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001800 offset += 1;
1801 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001802
H. Peter Anvine2c80182005-01-15 22:15:51 +00001803 case 0331:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001804 break;
H. Peter Anvinaf535c12002-04-30 20:59:21 +00001805
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001806 case 0332:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001807 case 0333:
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001808 *bytes = c - 0332 + 0xF2;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001809 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001810 offset += 1;
1811 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001812
Keith Kanios48af1772007-08-17 07:37:52 +00001813 case 0334:
1814 if (ins->rex & REX_R) {
1815 *bytes = 0xF0;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001816 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001817 offset += 1;
1818 }
1819 ins->rex &= ~(REX_L|REX_R);
1820 break;
H. Peter Anvin0db11e22007-04-17 20:23:11 +00001821
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001822 case 0335:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001823 break;
H. Peter Anvincb9b6902007-09-12 21:58:51 -07001824
H. Peter Anvin962e3052008-08-28 17:47:16 -07001825 case 0336:
1826 case 0337:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001827 break;
H. Peter Anvin962e3052008-08-28 17:47:16 -07001828
H. Peter Anvine2c80182005-01-15 22:15:51 +00001829 case 0340:
H. Peter Anvine2c80182005-01-15 22:15:51 +00001830 if (ins->oprs[0].segment != NO_SEG)
1831 errfunc(ERR_PANIC, "non-constant BSS size in pass two");
1832 else {
H. Peter Anvin428fd672007-11-15 10:25:52 -08001833 int64_t size = ins->oprs[0].offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001834 if (size > 0)
1835 out(offset, segment, NULL,
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001836 OUT_RESERVE, size, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001837 offset += size;
1838 }
1839 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001840
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001841 case 0341:
1842 break;
H. Peter Anvinc2acf7b2009-02-21 18:22:56 -08001843
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001844 case 0360:
1845 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001846
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001847 case 0361:
1848 bytes[0] = 0x66;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001849 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
1850 offset += 1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001851 break;
H. Peter Anvinfff5a472008-05-20 09:46:24 -07001852
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001853 case 0364:
1854 case 0365:
1855 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001856
Keith Kanios48af1772007-08-17 07:37:52 +00001857 case 0366:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001858 case 0367:
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001859 *bytes = c - 0366 + 0x66;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001860 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
Keith Kanios48af1772007-08-17 07:37:52 +00001861 offset += 1;
1862 break;
H. Peter Anvin62cb6062007-09-11 22:44:03 +00001863
Jin Kyu Song03041092013-10-15 19:38:51 -07001864 case3(0370):
H. Peter Anvine2c80182005-01-15 22:15:51 +00001865 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001866
H. Peter Anvine2c80182005-01-15 22:15:51 +00001867 case 0373:
1868 *bytes = bits == 16 ? 3 : 5;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001869 out(offset, segment, bytes, OUT_RAWDATA, 1, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001870 offset += 1;
1871 break;
H. Peter Anvineba20a72002-04-30 20:53:55 +00001872
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07001873 case 0374:
1874 eat = EA_XMMVSIB;
1875 break;
1876
1877 case 0375:
1878 eat = EA_YMMVSIB;
1879 break;
1880
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001881 case 0376:
1882 eat = EA_ZMMVSIB;
1883 break;
1884
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001885 case4(0100):
1886 case4(0110):
1887 case4(0120):
1888 case4(0130):
1889 case4(0200):
1890 case4(0204):
1891 case4(0210):
1892 case4(0214):
1893 case4(0220):
1894 case4(0224):
1895 case4(0230):
1896 case4(0234):
1897 {
H. Peter Anvine2c80182005-01-15 22:15:51 +00001898 ea ea_data;
1899 int rfield;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001900 opflags_t rflags;
Keith Kaniosb7a89542007-04-12 02:40:54 +00001901 uint8_t *p;
1902 int32_t s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001903 struct operand *opy = &ins->oprs[op2];
H. Peter Anvin70653092007-10-19 14:42:29 -07001904
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001905 if (c <= 0177) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001906 /* pick rfield from operand b (opx) */
1907 rflags = regflag(opx);
H. Peter Anvin33d5fc02008-10-23 23:07:53 -07001908 rfield = nasm_regvals[opx->basereg];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001909 } else {
1910 /* rfield is constant */
1911 rflags = 0;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001912 rfield = c & 7;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001913 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001914
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001915 if (process_ea(opy, &ea_data, bits,
1916 rfield, rflags, ins) != eat)
H. Peter Anvine2c80182005-01-15 22:15:51 +00001917 errfunc(ERR_NONFATAL, "invalid effective address");
Charles Crayne7e975552007-11-03 22:06:13 -07001918
H. Peter Anvine2c80182005-01-15 22:15:51 +00001919 p = bytes;
1920 *p++ = ea_data.modrm;
1921 if (ea_data.sib_present)
1922 *p++ = ea_data.sib;
1923
1924 s = p - bytes;
H. Peter Anvin34f6fb02007-11-09 14:44:02 -08001925 out(offset, segment, bytes, OUT_RAWDATA, s, NO_SEG, NO_SEG);
H. Peter Anvine2c80182005-01-15 22:15:51 +00001926
Victor van den Elzencf9332c2008-10-01 12:18:28 +02001927 /*
1928 * Make sure the address gets the right offset in case
1929 * the line breaks in the .lst file (BR 1197827)
1930 */
1931 offset += s;
1932 s = 0;
1933
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001934 if (ea_data.bytes) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001935 /* use compressed displacement, if available */
1936 data = ea_data.disp8 ? ea_data.disp8 : opy->offset;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001937 s += ea_data.bytes;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001938 if (ea_data.rip) {
1939 if (opy->segment == segment) {
1940 data -= insn_end;
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001941 if (overflow_signed(data, ea_data.bytes))
1942 warn_overflow(ERR_PASS2, ea_data.bytes);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001943 out(offset, segment, &data, OUT_ADDRESS,
1944 ea_data.bytes, NO_SEG, NO_SEG);
1945 } else {
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001946 /* overflow check in output/linker? */
H. Peter Anvin89a2ac02013-11-26 18:23:20 -08001947 out(offset, segment, &data, OUT_REL4ADR,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001948 insn_end - offset, opy->segment, opy->wrt);
1949 }
1950 } else {
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001951 int asize = ins->addr_size >> 3;
1952 int atype = ea_data.bytes;
1953
1954 if (overflow_general(data, asize) ||
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07001955 signed_bits(data, ins->addr_size) !=
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001956 signed_bits(data, ea_data.bytes << 3))
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01001957 warn_overflow(ERR_PASS2, ea_data.bytes);
1958
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001959 if (asize > ea_data.bytes) {
1960 /*
1961 * If the address isn't the full width of
1962 * the address size, treat is as signed...
1963 */
1964 atype = -atype;
1965 }
1966
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001967 out(offset, segment, &data, OUT_ADDRESS,
H. Peter Anvin72bf3fe2013-11-26 20:19:53 -08001968 atype, opy->segment, opy->wrt);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001969 }
H. Peter Anvine2c80182005-01-15 22:15:51 +00001970 }
1971 offset += s;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001972 }
1973 break;
H. Peter Anvin507ae032008-10-09 15:37:10 -07001974
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04001975 default:
1976 errfunc(ERR_PANIC, "internal instruction table corrupt"
1977 ": instruction code \\%o (0x%02X) given", c, c);
1978 break;
H. Peter Anvine2c80182005-01-15 22:15:51 +00001979 }
H. Peter Anvin839eca22007-10-29 23:12:47 -07001980 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001981}
1982
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001983static opflags_t regflag(const operand * o)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001984{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001985 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001986 errfunc(ERR_PANIC, "invalid operand passed to regflag()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001987 return nasm_reg_flags[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001988}
1989
H. Peter Anvin5b0e3ec2007-07-07 02:01:08 +00001990static int32_t regval(const operand * o)
H. Peter Anvineba20a72002-04-30 20:53:55 +00001991{
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04001992 if (!is_register(o->basereg))
H. Peter Anvine2c80182005-01-15 22:15:51 +00001993 errfunc(ERR_PANIC, "invalid operand passed to regval()");
H. Peter Anvina4835d42008-05-20 14:21:29 -07001994 return nasm_regvals[o->basereg];
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00001995}
1996
H. Peter Anvin3df97a72007-05-30 03:25:21 +00001997static int op_rexflags(const operand * o, int mask)
1998{
H. Peter Anvinf8563f72009-10-13 12:28:14 -07001999 opflags_t flags;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002000 int val;
2001
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002002 if (!is_register(o->basereg))
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002003 errfunc(ERR_PANIC, "invalid operand passed to op_rexflags()");
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002004
H. Peter Anvina4835d42008-05-20 14:21:29 -07002005 flags = nasm_reg_flags[o->basereg];
2006 val = nasm_regvals[o->basereg];
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002007
2008 return rexflags(val, flags, mask);
2009}
2010
H. Peter Anvinf8563f72009-10-13 12:28:14 -07002011static int rexflags(int val, opflags_t flags, int mask)
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002012{
2013 int rex = 0;
2014
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002015 if (val >= 0 && (val & 8))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002016 rex |= REX_B|REX_X|REX_R;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002017 if (flags & BITS64)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002018 rex |= REX_W;
2019 if (!(REG_HIGH & ~flags)) /* AH, CH, DH, BH */
2020 rex |= REX_H;
2021 else if (!(REG8 & ~flags) && val >= 4) /* SPL, BPL, SIL, DIL */
2022 rex |= REX_P;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002023
2024 return rex & mask;
2025}
2026
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002027static int evexflags(int val, decoflags_t deco,
2028 int mask, uint8_t byte)
2029{
2030 int evex = 0;
2031
Jin Kyu Song1be09ee2013-11-08 01:14:39 -08002032 switch (byte) {
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002033 case 0:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002034 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002035 evex |= (EVEX_P0RP | EVEX_P0X);
2036 break;
2037 case 2:
H. Peter Anvinc6c750c2013-11-08 15:28:19 -08002038 if (val >= 0 && (val & 16))
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002039 evex |= EVEX_P2VP;
2040 if (deco & Z)
2041 evex |= EVEX_P2Z;
2042 if (deco & OPMASK_MASK)
2043 evex |= deco & EVEX_P2AAA;
2044 break;
2045 }
2046 return evex & mask;
2047}
2048
2049static int op_evexflags(const operand * o, int mask, uint8_t byte)
2050{
2051 int val;
2052
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002053 val = nasm_regvals[o->basereg];
2054
2055 return evexflags(val, o->decoflags, mask, byte);
2056}
2057
H. Peter Anvin23595f52009-07-25 17:44:25 -07002058static enum match_result find_match(const struct itemplate **tempp,
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002059 insn *instruction,
2060 int32_t segment, int64_t offset, int bits)
H. Peter Anvin23595f52009-07-25 17:44:25 -07002061{
2062 const struct itemplate *temp;
2063 enum match_result m, merr;
H. Peter Anvina7643f42009-10-13 12:32:20 -07002064 opflags_t xsizeflags[MAX_OPERANDS];
H. Peter Anvina81655b2009-07-25 18:15:28 -07002065 bool opsizemissing = false;
Jin Kyu Songe3a06b92013-08-28 19:15:23 -07002066 int8_t broadcast = instruction->evex_brerop;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002067 int i;
2068
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002069 /* broadcasting uses a different data element size */
2070 for (i = 0; i < instruction->operands; i++)
2071 if (i == broadcast)
2072 xsizeflags[i] = instruction->oprs[i].decoflags & BRSIZE_MASK;
2073 else
2074 xsizeflags[i] = instruction->oprs[i].type & SIZE_MASK;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002075
2076 merr = MERR_INVALOP;
2077
2078 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002079 temp->opcode != I_none; temp++) {
2080 m = matches(temp, instruction, bits);
2081 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002082 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002083 m = MOK_GOOD;
2084 else
2085 m = MERR_INVALOP;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002086 } else if (m == MERR_OPSIZEMISSING && !itemp_has(temp, IF_SX)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002087 /*
2088 * Missing operand size and a candidate for fuzzy matching...
2089 */
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002090 for (i = 0; i < temp->operands; i++)
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002091 if (i == broadcast)
2092 xsizeflags[i] |= temp->deco[i] & BRSIZE_MASK;
2093 else
2094 xsizeflags[i] |= temp->opd[i] & SIZE_MASK;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002095 opsizemissing = true;
2096 }
2097 if (m > merr)
2098 merr = m;
2099 if (merr == MOK_GOOD)
2100 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002101 }
2102
2103 /* No match, but see if we can get a fuzzy operand size match... */
2104 if (!opsizemissing)
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002105 goto done;
H. Peter Anvina81655b2009-07-25 18:15:28 -07002106
2107 for (i = 0; i < instruction->operands; i++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002108 /*
2109 * We ignore extrinsic operand sizes on registers, so we should
2110 * never try to fuzzy-match on them. This also resolves the case
2111 * when we have e.g. "xmmrm128" in two different positions.
2112 */
2113 if (is_class(REGISTER, instruction->oprs[i].type))
2114 continue;
H. Peter Anvinff5d6562009-10-05 14:08:05 -07002115
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002116 /* This tests if xsizeflags[i] has more than one bit set */
2117 if ((xsizeflags[i] & (xsizeflags[i]-1)))
2118 goto done; /* No luck */
H. Peter Anvina81655b2009-07-25 18:15:28 -07002119
Jin Kyu Song7903c072013-10-30 03:00:12 -07002120 if (i == broadcast) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002121 instruction->oprs[i].decoflags |= xsizeflags[i];
Jin Kyu Song7903c072013-10-30 03:00:12 -07002122 instruction->oprs[i].type |= (xsizeflags[i] == BR_BITS32 ?
2123 BITS32 : BITS64);
2124 } else {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002125 instruction->oprs[i].type |= xsizeflags[i]; /* Set the size */
Jin Kyu Song7903c072013-10-30 03:00:12 -07002126 }
H. Peter Anvina81655b2009-07-25 18:15:28 -07002127 }
2128
2129 /* Try matching again... */
2130 for (temp = nasm_instructions[instruction->opcode];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002131 temp->opcode != I_none; temp++) {
2132 m = matches(temp, instruction, bits);
2133 if (m == MOK_JUMP) {
H. Peter Anvin8cc8a1d2012-02-25 11:11:42 -08002134 if (jmp_match(segment, offset, bits, instruction, temp))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002135 m = MOK_GOOD;
2136 else
2137 m = MERR_INVALOP;
2138 }
2139 if (m > merr)
2140 merr = m;
2141 if (merr == MOK_GOOD)
2142 goto done;
H. Peter Anvin23595f52009-07-25 17:44:25 -07002143 }
2144
H. Peter Anvina81655b2009-07-25 18:15:28 -07002145done:
H. Peter Anvin23595f52009-07-25 17:44:25 -07002146 *tempp = temp;
2147 return merr;
2148}
2149
Mark Charneydcaef4b2014-10-09 13:45:17 -04002150static uint8_t get_broadcast_num(opflags_t opflags, opflags_t brsize)
2151{
2152 opflags_t opsize = opflags & SIZE_MASK;
2153 uint8_t brcast_num;
2154
2155 /*
2156 * Due to discontinuity between BITS64 and BITS128 (BITS80),
2157 * this cannot be a simple arithmetic calculation.
2158 */
2159 if (brsize > BITS64)
2160 errfunc(ERR_FATAL,
2161 "size of broadcasting element is greater than 64 bits");
2162
2163 switch (opsize) {
2164 case BITS64:
2165 brcast_num = BITS64 / brsize;
2166 break;
2167 default:
2168 brcast_num = (opsize / BITS128) * (BITS64 / brsize) * 2;
2169 break;
2170 }
2171
2172 return brcast_num;
2173}
2174
H. Peter Anvin65289e82009-07-25 17:25:11 -07002175static enum match_result matches(const struct itemplate *itemp,
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002176 insn *instruction, int bits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002177{
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002178 opflags_t size[MAX_OPERANDS], asize;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002179 bool opsizemissing = false;
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002180 int i, oprs;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002181
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002182 /*
2183 * Check the opcode
2184 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002185 if (itemp->opcode != instruction->opcode)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002186 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002187
2188 /*
2189 * Count the operands
2190 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002191 if (itemp->operands != instruction->operands)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002192 return MERR_INVALOP;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002193
2194 /*
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002195 * Is it legal?
2196 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002197 if (!(optimizing > 0) && itemp_has(itemp, IF_OPT))
H. Peter Anvin47fb7bc2010-08-24 13:53:22 -07002198 return MERR_INVALOP;
2199
2200 /*
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002201 * {evex} available?
2202 */
H. Peter Anvin621a69a2013-11-28 12:11:24 -08002203 switch (instruction->prefixes[PPS_VEX]) {
2204 case P_EVEX:
2205 if (!itemp_has(itemp, IF_EVEX))
2206 return MERR_ENCMISMATCH;
2207 break;
2208 case P_VEX3:
2209 case P_VEX2:
2210 if (!itemp_has(itemp, IF_VEX))
2211 return MERR_ENCMISMATCH;
2212 break;
2213 default:
2214 break;
Jin Kyu Song6cfa9682013-11-26 17:27:48 -08002215 }
2216
2217 /*
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002218 * Check that no spurious colons or TOs are present
2219 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002220 for (i = 0; i < itemp->operands; i++)
2221 if (instruction->oprs[i].type & ~itemp->opd[i] & (COLON | TO))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002222 return MERR_INVALOP;
H. Peter Anvin70653092007-10-19 14:42:29 -07002223
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002224 /*
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002225 * Process size flags
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002226 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002227 switch (itemp_smask(itemp)) {
2228 case IF_GENBIT(IF_SB):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002229 asize = BITS8;
2230 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002231 case IF_GENBIT(IF_SW):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002232 asize = BITS16;
2233 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002234 case IF_GENBIT(IF_SD):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002235 asize = BITS32;
2236 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002237 case IF_GENBIT(IF_SQ):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002238 asize = BITS64;
2239 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002240 case IF_GENBIT(IF_SO):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002241 asize = BITS128;
2242 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002243 case IF_GENBIT(IF_SY):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002244 asize = BITS256;
2245 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002246 case IF_GENBIT(IF_SZ):
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002247 asize = BITS512;
2248 break;
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002249 case IF_GENBIT(IF_SIZE):
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002250 switch (bits) {
2251 case 16:
2252 asize = BITS16;
2253 break;
2254 case 32:
2255 asize = BITS32;
2256 break;
2257 case 64:
2258 asize = BITS64;
2259 break;
2260 default:
2261 asize = 0;
2262 break;
2263 }
2264 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002265 default:
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002266 asize = 0;
2267 break;
H. Peter Anvin60926242009-07-26 16:25:38 -07002268 }
2269
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002270 if (itemp_armask(itemp)) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002271 /* S- flags only apply to a specific operand */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002272 i = itemp_arg(itemp);
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002273 memset(size, 0, sizeof size);
2274 size[i] = asize;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002275 } else {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002276 /* S- flags apply to all operands */
2277 for (i = 0; i < MAX_OPERANDS; i++)
2278 size[i] = asize;
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002279 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002280
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002281 /*
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002282 * Check that the operand flags all match up,
2283 * it's a bit tricky so lets be verbose:
2284 *
2285 * 1) Find out the size of operand. If instruction
2286 * doesn't have one specified -- we're trying to
2287 * guess it either from template (IF_S* flag) or
2288 * from code bits.
2289 *
Ben Rudiak-Gould6e878932013-02-27 10:13:14 -08002290 * 2) If template operand do not match the instruction OR
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002291 * template has an operand size specified AND this size differ
2292 * from which instruction has (perhaps we got it from code bits)
2293 * we are:
2294 * a) Check that only size of instruction and operand is differ
2295 * other characteristics do match
2296 * b) Perhaps it's a register specified in instruction so
2297 * for such a case we just mark that operand as "size
2298 * missing" and this will turn on fuzzy operand size
2299 * logic facility (handled by a caller)
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002300 */
2301 for (i = 0; i < itemp->operands; i++) {
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002302 opflags_t type = instruction->oprs[i].type;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002303 decoflags_t deco = instruction->oprs[i].decoflags;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002304 bool is_broadcast = deco & BRDCAST_MASK;
Jin Kyu Song25c22122013-10-30 03:12:45 -07002305 uint8_t brcast_num = 0;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002306 opflags_t template_opsize, insn_opsize;
2307
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002308 if (!(type & SIZE_MASK))
2309 type |= size[i];
H. Peter Anvind85d2502008-05-04 17:53:31 -07002310
Jin Kyu Song7903c072013-10-30 03:00:12 -07002311 insn_opsize = type & SIZE_MASK;
2312 if (!is_broadcast) {
2313 template_opsize = itemp->opd[i] & SIZE_MASK;
2314 } else {
2315 decoflags_t deco_brsize = itemp->deco[i] & BRSIZE_MASK;
2316 /*
2317 * when broadcasting, the element size depends on
2318 * the instruction type. decorator flag should match.
2319 */
2320
2321 if (deco_brsize) {
2322 template_opsize = (deco_brsize == BR_BITS32 ? BITS32 : BITS64);
Jin Kyu Song25c22122013-10-30 03:12:45 -07002323 /* calculate the proper number : {1to<brcast_num>} */
Mark Charneydcaef4b2014-10-09 13:45:17 -04002324 brcast_num = get_broadcast_num(itemp->opd[i], template_opsize);
Jin Kyu Song7903c072013-10-30 03:00:12 -07002325 } else {
2326 template_opsize = 0;
2327 }
2328 }
2329
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002330 if ((itemp->opd[i] & ~type & ~SIZE_MASK) ||
Jin Kyu Song25c22122013-10-30 03:12:45 -07002331 (deco & ~itemp->deco[i] & ~BRNUM_MASK)) {
Ben Rudiak-Gould4e8396b2013-03-01 10:28:32 +04002332 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002333 } else if (template_opsize) {
2334 if (template_opsize != insn_opsize) {
2335 if (insn_opsize) {
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002336 return MERR_INVALOP;
Jin Kyu Song7903c072013-10-30 03:00:12 -07002337 } else if (!is_class(REGISTER, type)) {
2338 /*
2339 * Note: we don't honor extrinsic operand sizes for registers,
2340 * so "missing operand size" for a register should be
2341 * considered a wildcard match rather than an error.
2342 */
2343 opsizemissing = true;
Jin Kyu Song4d1fc3f2013-08-21 19:29:10 -07002344 }
Jin Kyu Song25c22122013-10-30 03:12:45 -07002345 } else if (is_broadcast &&
2346 (brcast_num !=
Mark Charneydcaef4b2014-10-09 13:45:17 -04002347 (2U << ((deco & BRNUM_MASK) >> BRNUM_SHIFT)))) {
Jin Kyu Song25c22122013-10-30 03:12:45 -07002348 /*
2349 * broadcasting opsize matches but the number of repeated memory
2350 * element does not match.
Mark Charneydcaef4b2014-10-09 13:45:17 -04002351 * if 64b double precision float is broadcasted to ymm (256b),
2352 * broadcasting decorator must be {1to4}.
Jin Kyu Song25c22122013-10-30 03:12:45 -07002353 */
2354 return MERR_BRNUMMISMATCH;
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002355 }
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002356 }
2357 }
2358
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002359 if (opsizemissing)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002360 return MERR_OPSIZEMISSING;
H. Peter Anvin3fb86f22009-07-25 19:12:10 -07002361
H. Peter Anvin32cd4c22008-04-04 13:34:53 -07002362 /*
2363 * Check operand sizes
2364 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002365 if (itemp_has(itemp, IF_SM) || itemp_has(itemp, IF_SM2)) {
2366 oprs = (itemp_has(itemp, IF_SM2) ? 2 : itemp->operands);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002367 for (i = 0; i < oprs; i++) {
Cyrill Gorcunovbc31bee2009-11-01 23:16:01 +03002368 asize = itemp->opd[i] & SIZE_MASK;
2369 if (asize) {
2370 for (i = 0; i < oprs; i++)
2371 size[i] = asize;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002372 break;
2373 }
2374 }
H. Peter Anvinef7468f2002-04-30 20:57:59 +00002375 } else {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002376 oprs = itemp->operands;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002377 }
2378
Keith Kaniosb7a89542007-04-12 02:40:54 +00002379 for (i = 0; i < itemp->operands; i++) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002380 if (!(itemp->opd[i] & SIZE_MASK) &&
2381 (instruction->oprs[i].type & SIZE_MASK & ~size[i]))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002382 return MERR_OPSIZEMISMATCH;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002383 }
2384
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002385 /*
2386 * Check template is okay at the set cpu level
2387 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002388 if (iflag_cmp_cpu_level(&insns_flags[itemp->iflag_idx], &cpu) > 0)
H. Peter Anvin65289e82009-07-25 17:25:11 -07002389 return MERR_BADCPU;
H. Peter Anvin70653092007-10-19 14:42:29 -07002390
Keith Kaniosb7a89542007-04-12 02:40:54 +00002391 /*
H. Peter Anvin6cda4142008-12-29 20:52:28 -08002392 * Verify the appropriate long mode flag.
Keith Kaniosb7a89542007-04-12 02:40:54 +00002393 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002394 if (itemp_has(itemp, (bits == 64 ? IF_NOLONG : IF_LONG)))
H. Peter Anvin65289e82009-07-25 17:25:11 -07002395 return MERR_BADMODE;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002396
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002397 /*
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002398 * If we have a HLE prefix, look for the NOHLE flag
2399 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002400 if (itemp_has(itemp, IF_NOHLE) &&
H. Peter Anvinfb3f4e62012-02-25 22:22:07 -08002401 (has_prefix(instruction, PPS_REP, P_XACQUIRE) ||
2402 has_prefix(instruction, PPS_REP, P_XRELEASE)))
2403 return MERR_BADHLE;
2404
2405 /*
H. Peter Anvinaf535c12002-04-30 20:59:21 +00002406 * Check if special handling needed for Jumps
2407 */
H. Peter Anvin755f5212012-02-25 11:41:34 -08002408 if ((itemp->code[0] & ~1) == 0370)
Cyrill Gorcunov1de95002009-11-06 00:08:38 +03002409 return MOK_JUMP;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002410
Jin Kyu Song03041092013-10-15 19:38:51 -07002411 /*
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002412 * Check if BND prefix is allowed.
2413 * Other 0xF2 (REPNE/REPNZ) prefix is prohibited.
Jin Kyu Song03041092013-10-15 19:38:51 -07002414 */
Cyrill Gorcunov08359152013-11-09 22:16:11 +04002415 if (!itemp_has(itemp, IF_BND) &&
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002416 (has_prefix(instruction, PPS_REP, P_BND) ||
2417 has_prefix(instruction, PPS_REP, P_NOBND)))
Jin Kyu Song03041092013-10-15 19:38:51 -07002418 return MERR_BADBND;
Jin Kyu Songb287ff02013-12-04 20:05:55 -08002419 else if (itemp_has(itemp, IF_BND) &&
2420 (has_prefix(instruction, PPS_REP, P_REPNE) ||
2421 has_prefix(instruction, PPS_REP, P_REPNZ)))
2422 return MERR_BADREPNE;
Jin Kyu Song03041092013-10-15 19:38:51 -07002423
H. Peter Anvin60926242009-07-26 16:25:38 -07002424 return MOK_GOOD;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002425}
2426
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002427/*
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002428 * Check if ModR/M.mod should/can be 01.
2429 * - EAF_BYTEOFFS is set
2430 * - offset can fit in a byte when EVEX is not used
2431 * - offset can be compressed when EVEX is used
2432 */
2433#define IS_MOD_01() (input->eaflags & EAF_BYTEOFFS || \
2434 (o >= -128 && o <= 127 && \
2435 seg == NO_SEG && !forw_ref && \
2436 !(input->eaflags & EAF_WORDOFFS) && \
2437 !(ins->rex & REX_EV)) || \
2438 (ins->rex & REX_EV && \
2439 is_disp8n(input, ins, &output->disp8)))
2440
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002441static enum ea_type process_ea(operand *input, ea *output, int bits,
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002442 int rfield, opflags_t rflags, insn *ins)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002443{
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002444 bool forw_ref = !!(input->opflags & OPFLAG_UNKNOWN);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002445 int addrbits = ins->addr_size;
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002446 int eaflags = input->eaflags;
H. Peter Anvin1c3277b2008-07-19 21:38:56 -07002447
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002448 output->type = EA_SCALAR;
2449 output->rip = false;
Jin Kyu Songdb358a22013-09-20 20:36:19 -07002450 output->disp8 = 0;
H. Peter Anvin99c4ecd2007-08-28 23:06:00 +00002451
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002452 /* REX flags for the rfield operand */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002453 output->rex |= rexflags(rfield, rflags, REX_R | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002454 /* EVEX.R' flag for the REG operand */
2455 ins->evex_p[0] |= evexflags(rfield, 0, EVEX_P0RP, 0);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002456
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002457 if (is_class(REGISTER, input->type)) {
2458 /*
2459 * It's a direct register.
2460 */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002461 if (!is_register(input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002462 goto err;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002463
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002464 if (!is_reg_class(REG_EA, input->basereg))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002465 goto err;
H. Peter Anvin70653092007-10-19 14:42:29 -07002466
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002467 /* broadcasting is not available with a direct register operand. */
2468 if (input->decoflags & BRDCAST_MASK) {
2469 nasm_error(ERR_NONFATAL, "Broadcasting not allowed from a register");
2470 goto err;
2471 }
2472
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002473 output->rex |= op_rexflags(input, REX_B | REX_P | REX_W | REX_H);
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002474 ins->evex_p[0] |= op_evexflags(input, EVEX_P0X, 0);
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002475 output->sib_present = false; /* no SIB necessary */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002476 output->bytes = 0; /* no offset necessary either */
2477 output->modrm = GEN_MODRM(3, rfield, nasm_regvals[input->basereg]);
2478 } else {
2479 /*
2480 * It's a memory reference.
2481 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002482
2483 /* Embedded rounding or SAE is not available with a mem ref operand. */
2484 if (input->decoflags & (ER | SAE)) {
2485 nasm_error(ERR_NONFATAL,
2486 "Embedded rounding is available only with reg-reg op.");
2487 return -1;
2488 }
2489
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002490 if (input->basereg == -1 &&
2491 (input->indexreg == -1 || input->scale == 0)) {
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002492 /*
2493 * It's a pure offset.
2494 */
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002495 if (bits == 64 && ((input->type & IP_REL) == IP_REL) &&
2496 input->segment == NO_SEG) {
2497 nasm_error(ERR_WARNING | ERR_PASS1, "absolute address can not be RIP-relative");
2498 input->type &= ~IP_REL;
2499 input->type |= MEMORY;
2500 }
2501
Jin Kyu Song97f6fae2013-12-18 21:28:17 -08002502 if (bits == 64 &&
2503 !(IP_REL & ~input->type) && (eaflags & EAF_MIB)) {
2504 nasm_error(ERR_NONFATAL, "RIP-relative addressing is prohibited for mib.");
2505 return -1;
2506 }
2507
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002508 if (eaflags & EAF_BYTEOFFS ||
2509 (eaflags & EAF_WORDOFFS &&
Victor van den Elzen0d268fb2010-01-24 21:24:57 +01002510 input->disp_size != (addrbits != 16 ? 32 : 16))) {
2511 nasm_error(ERR_WARNING | ERR_PASS1, "displacement size ignored on absolute address");
2512 }
2513
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002514 if (bits == 64 && (~input->type & IP_REL)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002515 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002516 output->sib = GEN_SIB(0, 4, 5);
2517 output->bytes = 4;
2518 output->modrm = GEN_MODRM(0, rfield, 4);
2519 output->rip = false;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002520 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002521 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002522 output->bytes = (addrbits != 16 ? 4 : 2);
2523 output->modrm = GEN_MODRM(0, rfield, (addrbits != 16 ? 5 : 6));
2524 output->rip = bits == 64;
Chuck Crayne42fe6ce2007-06-03 02:42:41 +00002525 }
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002526 } else {
2527 /*
2528 * It's an indirection.
2529 */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002530 int i = input->indexreg, b = input->basereg, s = input->scale;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002531 int32_t seg = input->segment;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002532 int hb = input->hintbase, ht = input->hinttype;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002533 int t, it, bt; /* register numbers */
2534 opflags_t x, ix, bx; /* register flags */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002535
H. Peter Anvine2c80182005-01-15 22:15:51 +00002536 if (s == 0)
2537 i = -1; /* make this easy, at least */
H. Peter Anvin70653092007-10-19 14:42:29 -07002538
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002539 if (is_register(i)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002540 it = nasm_regvals[i];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002541 ix = nasm_reg_flags[i];
2542 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002543 it = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002544 ix = 0;
2545 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002546
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002547 if (is_register(b)) {
H. Peter Anvina4835d42008-05-20 14:21:29 -07002548 bt = nasm_regvals[b];
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002549 bx = nasm_reg_flags[b];
2550 } else {
Keith Kaniosb7a89542007-04-12 02:40:54 +00002551 bt = -1;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002552 bx = 0;
2553 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002554
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002555 /* if either one are a vector register... */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002556 if ((ix|bx) & (XMMREG|YMMREG|ZMMREG) & ~REG_EA) {
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002557 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002558 int32_t o = input->offset;
2559 int mod, scale, index, base;
2560
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002561 /*
2562 * For a vector SIB, one has to be a vector and the other,
2563 * if present, a GPR. The vector must be the index operand.
2564 */
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002565 if (it == -1 || (bx & (XMMREG|YMMREG|ZMMREG) & ~REG_EA)) {
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002566 if (s == 0)
2567 s = 1;
2568 else if (s != 1)
2569 goto err;
2570
2571 t = bt, bt = it, it = t;
2572 x = bx, bx = ix, ix = x;
2573 }
2574
2575 if (bt != -1) {
2576 if (REG_GPR & ~bx)
2577 goto err;
2578 if (!(REG64 & ~bx) || !(REG32 & ~bx))
2579 sok &= bx;
2580 else
2581 goto err;
2582 }
2583
2584 /*
2585 * While we're here, ensure the user didn't specify
2586 * WORD or QWORD
2587 */
2588 if (input->disp_size == 16 || input->disp_size == 64)
2589 goto err;
2590
2591 if (addrbits == 16 ||
2592 (addrbits == 32 && !(sok & BITS32)) ||
2593 (addrbits == 64 && !(sok & BITS64)))
2594 goto err;
2595
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002596 output->type = ((ix & ZMMREG & ~REG_EA) ? EA_ZMMVSIB
2597 : ((ix & YMMREG & ~REG_EA)
2598 ? EA_YMMVSIB : EA_XMMVSIB));
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002599
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002600 output->rex |= rexflags(it, ix, REX_X);
2601 output->rex |= rexflags(bt, bx, REX_B);
2602 ins->evex_p[2] |= evexflags(it, 0, EVEX_P2VP, 2);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002603
2604 index = it & 7; /* it is known to be != -1 */
2605
2606 switch (s) {
2607 case 1:
2608 scale = 0;
2609 break;
2610 case 2:
2611 scale = 1;
2612 break;
2613 case 4:
2614 scale = 2;
2615 break;
2616 case 8:
2617 scale = 3;
2618 break;
2619 default: /* then what the smeg is it? */
2620 goto err; /* panic */
2621 }
2622
2623 if (bt == -1) {
2624 base = 5;
2625 mod = 0;
2626 } else {
2627 base = (bt & 7);
2628 if (base != REG_NUM_EBP && o == 0 &&
2629 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002630 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002631 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002632 else if (IS_MOD_01())
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002633 mod = 1;
2634 else
2635 mod = 2;
2636 }
2637
2638 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002639 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2640 output->modrm = GEN_MODRM(mod, rfield, 4);
2641 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002642 } else if ((ix|bx) & (BITS32|BITS64)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002643 /*
2644 * it must be a 32/64-bit memory reference. Firstly we have
2645 * to check that all registers involved are type E/Rxx.
2646 */
Cyrill Gorcunov167917a2012-09-10 00:19:12 +04002647 opflags_t sok = BITS32 | BITS64;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002648 int32_t o = input->offset;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002649
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002650 if (it != -1) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002651 if (!(REG64 & ~ix) || !(REG32 & ~ix))
2652 sok &= ix;
2653 else
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002654 goto err;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002655 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002656
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002657 if (bt != -1) {
2658 if (REG_GPR & ~bx)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002659 goto err; /* Invalid register */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002660 if (~sok & bx & SIZE_MASK)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002661 goto err; /* Invalid size */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002662 sok &= bx;
2663 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002664
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002665 /*
2666 * While we're here, ensure the user didn't specify
2667 * WORD or QWORD
2668 */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002669 if (input->disp_size == 16 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002670 goto err;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002671
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002672 if (addrbits == 16 ||
2673 (addrbits == 32 && !(sok & BITS32)) ||
2674 (addrbits == 64 && !(sok & BITS64)))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002675 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002676
Keith Kaniosb7a89542007-04-12 02:40:54 +00002677 /* now reorganize base/index */
2678 if (s == 1 && bt != it && bt != -1 && it != -1 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002679 ((hb == b && ht == EAH_NOTBASE) ||
2680 (hb == i && ht == EAH_MAKEBASE))) {
2681 /* swap if hints say so */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002682 t = bt, bt = it, it = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002683 x = bx, bx = ix, ix = x;
2684 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002685
Jin Kyu Song164d6072013-10-15 19:10:13 -07002686 if (bt == -1 && s == 1 && !(hb == i && ht == EAH_NOTBASE)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002687 /* make single reg base, unless hint */
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002688 bt = it, bx = ix, it = -1, ix = 0;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002689 }
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002690 if (eaflags & EAF_MIB) {
2691 /* only for mib operands */
2692 if (it == -1 && (hb == b && ht == EAH_NOTBASE)) {
2693 /*
2694 * make a single reg index [reg*1].
2695 * gas uses this form for an explicit index register.
2696 */
2697 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2698 }
2699 if ((ht == EAH_SUMMED) && bt == -1) {
2700 /* separate once summed index into [base, index] */
2701 bt = it, bx = ix, s--;
2702 }
2703 } else {
2704 if (((s == 2 && it != REG_NUM_ESP &&
Jin Kyu Song3d06af22013-12-18 21:28:41 -08002705 (!(eaflags & EAF_TIMESTWO) || (ht == EAH_SUMMED))) ||
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002706 s == 3 || s == 5 || s == 9) && bt == -1) {
2707 /* convert 3*EAX to EAX+2*EAX */
2708 bt = it, bx = ix, s--;
2709 }
2710 if (it == -1 && (bt & 7) != REG_NUM_ESP &&
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002711 (eaflags & EAF_TIMESTWO) &&
2712 (hb == b && ht == EAH_NOTBASE)) {
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002713 /*
Jin Kyu Song26ddad62013-12-18 22:01:14 -08002714 * convert [NOSPLIT EAX*1]
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002715 * to sib format with 0x0 displacement - [EAX*1+0].
2716 */
2717 it = bt, ix = bx, bt = -1, bx = 0, s = 1;
2718 }
2719 }
Keith Kanios48af1772007-08-17 07:37:52 +00002720 if (s == 1 && it == REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002721 /* swap ESP into base if scale is 1 */
Keith Kaniosb7a89542007-04-12 02:40:54 +00002722 t = it, it = bt, bt = t;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002723 x = ix, ix = bx, bx = x;
2724 }
2725 if (it == REG_NUM_ESP ||
2726 (s != 1 && s != 2 && s != 4 && s != 8 && it != -1))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002727 goto err; /* wrong, for various reasons */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002728
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002729 output->rex |= rexflags(it, ix, REX_X);
2730 output->rex |= rexflags(bt, bx, REX_B);
Keith Kaniosb7a89542007-04-12 02:40:54 +00002731
Keith Kanios48af1772007-08-17 07:37:52 +00002732 if (it == -1 && (bt & 7) != REG_NUM_ESP) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002733 /* no SIB needed */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002734 int mod, rm;
H. Peter Anvin70653092007-10-19 14:42:29 -07002735
Keith Kaniosb7a89542007-04-12 02:40:54 +00002736 if (bt == -1) {
H. Peter Anvine2c80182005-01-15 22:15:51 +00002737 rm = 5;
H. Peter Anvine2c80182005-01-15 22:15:51 +00002738 mod = 0;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002739 } else {
2740 rm = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002741 if (rm != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002742 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002743 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002744 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002745 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002746 mod = 1;
2747 else
2748 mod = 2;
2749 }
H. Peter Anvinea838272002-04-30 20:51:53 +00002750
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002751 output->sib_present = false;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002752 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2753 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002754 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002755 /* we need a SIB */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002756 int mod, scale, index, base;
H. Peter Anvin70653092007-10-19 14:42:29 -07002757
Keith Kaniosb7a89542007-04-12 02:40:54 +00002758 if (it == -1)
2759 index = 4, s = 1;
2760 else
2761 index = (it & 7);
H. Peter Anvin70653092007-10-19 14:42:29 -07002762
H. Peter Anvine2c80182005-01-15 22:15:51 +00002763 switch (s) {
2764 case 1:
2765 scale = 0;
2766 break;
2767 case 2:
2768 scale = 1;
2769 break;
2770 case 4:
2771 scale = 2;
2772 break;
2773 case 8:
2774 scale = 3;
2775 break;
2776 default: /* then what the smeg is it? */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002777 goto err; /* panic */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002778 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002779
Keith Kaniosb7a89542007-04-12 02:40:54 +00002780 if (bt == -1) {
2781 base = 5;
2782 mod = 0;
2783 } else {
2784 base = (bt & 7);
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002785 if (base != REG_NUM_EBP && o == 0 &&
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002786 seg == NO_SEG && !forw_ref &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002787 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
Keith Kaniosb7a89542007-04-12 02:40:54 +00002788 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002789 else if (IS_MOD_01())
Keith Kaniosb7a89542007-04-12 02:40:54 +00002790 mod = 1;
2791 else
2792 mod = 2;
2793 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002794
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002795 output->sib_present = true;
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002796 output->bytes = (bt == -1 || mod == 2 ? 4 : mod);
2797 output->modrm = GEN_MODRM(mod, rfield, 4);
2798 output->sib = GEN_SIB(scale, index, base);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002799 }
2800 } else { /* it's 16-bit */
2801 int mod, rm;
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002802 int16_t o = input->offset;
H. Peter Anvin70653092007-10-19 14:42:29 -07002803
Keith Kaniosb7a89542007-04-12 02:40:54 +00002804 /* check for 64-bit long mode */
2805 if (addrbits == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002806 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002807
H. Peter Anvine2c80182005-01-15 22:15:51 +00002808 /* check all registers are BX, BP, SI or DI */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002809 if ((b != -1 && b != R_BP && b != R_BX && b != R_SI && b != R_DI) ||
2810 (i != -1 && i != R_BP && i != R_BX && i != R_SI && i != R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002811 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002812
Keith Kaniosb7a89542007-04-12 02:40:54 +00002813 /* ensure the user didn't specify DWORD/QWORD */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002814 if (input->disp_size == 32 || input->disp_size == 64)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002815 goto err;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002816
H. Peter Anvine2c80182005-01-15 22:15:51 +00002817 if (s != 1 && i != -1)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002818 goto err; /* no can do, in 16-bit EA */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002819 if (b == -1 && i != -1) {
2820 int tmp = b;
2821 b = i;
2822 i = tmp;
2823 } /* swap */
2824 if ((b == R_SI || b == R_DI) && i != -1) {
2825 int tmp = b;
2826 b = i;
2827 i = tmp;
2828 }
2829 /* have BX/BP as base, SI/DI index */
2830 if (b == i)
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002831 goto err; /* shouldn't ever happen, in theory */
H. Peter Anvine2c80182005-01-15 22:15:51 +00002832 if (i != -1 && b != -1 &&
2833 (i == R_BP || i == R_BX || b == R_SI || b == R_DI))
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002834 goto err; /* invalid combinations */
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002835 if (b == -1) /* pure offset: handled above */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002836 goto err; /* so if it gets to here, panic! */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002837
H. Peter Anvine2c80182005-01-15 22:15:51 +00002838 rm = -1;
2839 if (i != -1)
2840 switch (i * 256 + b) {
2841 case R_SI * 256 + R_BX:
2842 rm = 0;
2843 break;
2844 case R_DI * 256 + R_BX:
2845 rm = 1;
2846 break;
2847 case R_SI * 256 + R_BP:
2848 rm = 2;
2849 break;
2850 case R_DI * 256 + R_BP:
2851 rm = 3;
2852 break;
2853 } else
2854 switch (b) {
2855 case R_SI:
2856 rm = 4;
2857 break;
2858 case R_DI:
2859 rm = 5;
2860 break;
2861 case R_BP:
2862 rm = 6;
2863 break;
2864 case R_BX:
2865 rm = 7;
2866 break;
2867 }
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002868 if (rm == -1) /* can't happen, in theory */
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002869 goto err; /* so panic if it does */
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002870
H. Peter Anvinab5bd052010-07-25 12:43:30 -07002871 if (o == 0 && seg == NO_SEG && !forw_ref && rm != 6 &&
Jin Kyu Song4360ba22013-12-10 16:24:45 -08002872 !(eaflags & (EAF_BYTEOFFS | EAF_WORDOFFS)))
H. Peter Anvine2c80182005-01-15 22:15:51 +00002873 mod = 0;
Jin Kyu Songcc1dc9d2013-08-15 19:01:25 -07002874 else if (IS_MOD_01())
H. Peter Anvine2c80182005-01-15 22:15:51 +00002875 mod = 1;
2876 else
2877 mod = 2;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002878
H. Peter Anvin6867acc2007-10-10 14:58:45 -07002879 output->sib_present = false; /* no SIB - it's 16-bit */
Cyrill Gorcunov10734c72011-08-29 00:07:17 +04002880 output->bytes = mod; /* bytes of offset needed */
2881 output->modrm = GEN_MODRM(mod, rfield, rm);
H. Peter Anvine2c80182005-01-15 22:15:51 +00002882 }
2883 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002884 }
H. Peter Anvin70653092007-10-19 14:42:29 -07002885
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002886 output->size = 1 + output->sib_present + output->bytes;
H. Peter Anvin3089f7e2011-06-22 18:19:28 -07002887 return output->type;
2888
2889err:
2890 return output->type = EA_INVALID;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002891}
2892
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002893static void add_asp(insn *ins, int addrbits)
H. Peter Anvineba20a72002-04-30 20:53:55 +00002894{
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002895 int j, valid;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002896 int defdisp;
Keith Kaniosb7a89542007-04-12 02:40:54 +00002897
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002898 valid = (addrbits == 64) ? 64|32 : 32|16;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002899
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002900 switch (ins->prefixes[PPS_ASIZE]) {
2901 case P_A16:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002902 valid &= 16;
2903 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002904 case P_A32:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002905 valid &= 32;
2906 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002907 case P_A64:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002908 valid &= 64;
2909 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002910 case P_ASP:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002911 valid &= (addrbits == 32) ? 16 : 32;
2912 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002913 default:
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002914 break;
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002915 }
2916
2917 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002918 if (is_class(MEMORY, ins->oprs[j].type)) {
2919 opflags_t i, b;
H. Peter Anvin70653092007-10-19 14:42:29 -07002920
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002921 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002922 if (!is_register(ins->oprs[j].indexreg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002923 i = 0;
2924 else
2925 i = nasm_reg_flags[ins->oprs[j].indexreg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002926
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002927 /* Verify as Register */
Cyrill Gorcunov2124b7b2010-07-25 01:16:33 +04002928 if (!is_register(ins->oprs[j].basereg))
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002929 b = 0;
2930 else
2931 b = nasm_reg_flags[ins->oprs[j].basereg];
H. Peter Anvin70653092007-10-19 14:42:29 -07002932
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002933 if (ins->oprs[j].scale == 0)
2934 i = 0;
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002935
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002936 if (!i && !b) {
2937 int ds = ins->oprs[j].disp_size;
2938 if ((addrbits != 64 && ds > 8) ||
2939 (addrbits == 64 && ds == 16))
2940 valid &= ds;
2941 } else {
2942 if (!(REG16 & ~b))
2943 valid &= 16;
2944 if (!(REG32 & ~b))
2945 valid &= 32;
2946 if (!(REG64 & ~b))
2947 valid &= 64;
H. Peter Anvin70653092007-10-19 14:42:29 -07002948
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002949 if (!(REG16 & ~i))
2950 valid &= 16;
2951 if (!(REG32 & ~i))
2952 valid &= 32;
2953 if (!(REG64 & ~i))
2954 valid &= 64;
2955 }
2956 }
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002957 }
2958
2959 if (valid & addrbits) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002960 ins->addr_size = addrbits;
H. Peter Anvinc5b9ce02007-09-22 21:49:51 -07002961 } else if (valid & ((addrbits == 32) ? 16 : 32)) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002962 /* Add an address size prefix */
Cyrill Gorcunovd6851d42011-09-25 18:01:45 +04002963 ins->prefixes[PPS_ASIZE] = (addrbits == 32) ? P_A16 : P_A32;;
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002964 ins->addr_size = (addrbits == 32) ? 16 : 32;
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002965 } else {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002966 /* Impossible... */
2967 errfunc(ERR_NONFATAL, "impossible combination of address sizes");
2968 ins->addr_size = addrbits; /* Error recovery */
H. Peter Anvinde4b89b2007-10-01 15:41:25 -07002969 }
2970
2971 defdisp = ins->addr_size == 16 ? 16 : 32;
2972
2973 for (j = 0; j < ins->operands; j++) {
Cyrill Gorcunovd6f31242010-07-26 23:14:40 +04002974 if (!(MEM_OFFS & ~ins->oprs[j].type) &&
2975 (ins->oprs[j].disp_size ? ins->oprs[j].disp_size : defdisp) != ins->addr_size) {
2976 /*
2977 * mem_offs sizes must match the address size; if not,
2978 * strip the MEM_OFFS bit and match only EA instructions
2979 */
2980 ins->oprs[j].type &= ~(MEM_OFFS & ~MEMORY);
2981 }
H. Peter Anvin3df97a72007-05-30 03:25:21 +00002982 }
H. Peter Anvinea6e34d2002-04-30 20:51:32 +00002983}