blob: 4c44118e13ce24d8d7d696daca5c2723998a76ad [file] [log] [blame]
Ian Rogerse32ca232012-03-05 10:20:23 -08001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_COMPILER_CODEGEN_X86_X86LIR_H_
18#define ART_COMPILER_COMPILER_CODEGEN_X86_X86LIR_H_
19
20#include "../../Dalvik.h"
21#include "../../CompilerInternals.h"
22
23namespace art {
24
25// Set to 1 to measure cost of suspend check
26#define NO_SUSPEND 0
27
28/*
29 * Runtime register conventions. We consider both x86, x86-64 and x32 (32bit mode x86-64), although
30 * we currently only target x86. The ABI has different conventions and we hope to have a single
31 * convention to simplify code generation. Changing something that is callee save and making it
32 * caller save places a burden on up-calls to save/restore the callee save register, however, there
33 * are few registers that are callee save in the ABI. Changing something that is caller save and
34 * making it callee save places a burden on down-calls to save/restore the callee save register.
Ian Rogersb41b33b2012-03-20 14:22:54 -070035 * For these reasons we aim to match native conventions for caller and callee save. The first 4
36 * registers can be used for byte operations, for this reason they are preferred for temporary
37 * scratch registers.
Ian Rogerse32ca232012-03-05 10:20:23 -080038 *
39 * General Purpose Register:
40 * Native: x86 | x86-64 / x32 | ART
41 * r0/eax: caller save | caller save | caller, Method*, scratch, return value
Ian Rogersb41b33b2012-03-20 14:22:54 -070042 * r1/ecx: caller save | caller save, arg4 | caller, arg1, scratch
43 * r2/edx: caller save | caller save, arg3 | caller, arg2, scratch, high half of long return
44 * r3/ebx: callEE save | callEE save | callER, arg3, scratch
Ian Rogerse32ca232012-03-05 10:20:23 -080045 * r4/esp: stack pointer
46 * r5/ebp: callee save | callee save | callee, available for dalvik register promotion
47 * r6/esi: callEE save | callER save, arg2 | callee, available for dalvik register promotion
48 * r7/edi: callEE save | callER save, arg1 | callee, available for dalvik register promotion
49 * --- x86-64/x32 registers
50 * Native: x86-64 / x32 | ART
51 * r8: caller save, arg5 | caller, scratch
52 * r9: caller save, arg6 | caller, scratch
53 * r10: caller save | caller, scratch
54 * r11: caller save | caller, scratch
55 * r12: callee save | callee, available for dalvik register promotion
56 * r13: callee save | callee, available for dalvik register promotion
57 * r14: callee save | callee, available for dalvik register promotion
58 * r15: callee save | callee, available for dalvik register promotion
59 *
60 * There is no rSELF, instead on x86 fs: has a base address of Thread::Current, whereas on
61 * x86-64/x32 gs: holds it.
62 *
63 * For floating point we don't support CPUs without SSE2 support (ie newer than PIII):
64 * Native: x86 | x86-64 / x32 | ART
65 * XMM0: caller save |caller save, arg1 | caller, float/double return value (except for native x86 code)
66 * XMM1: caller save |caller save, arg2 | caller, scratch
67 * XMM2: caller save |caller save, arg3 | caller, scratch
68 * XMM3: caller save |caller save, arg4 | caller, scratch
69 * XMM4: caller save |caller save, arg5 | caller, scratch
70 * XMM5: caller save |caller save, arg6 | caller, scratch
71 * XMM6: caller save |caller save, arg7 | caller, scratch
72 * XMM7: caller save |caller save, arg8 | caller, scratch
73 * --- x86-64/x32 registers
74 * XMM8 .. 15: caller save
75 *
76 * X87 is a necessary evil outside of ART code:
77 * ST0: x86 float/double native return value, caller save
78 * ST1 .. ST7: caller save
79 *
80 * Stack frame diagram (stack grows down, higher addresses at top):
81 *
82 * +------------------------+
83 * | IN[ins-1] | {Note: resides in caller's frame}
84 * | . |
85 * | IN[0] |
86 * | caller's Method* |
87 * +========================+ {Note: start of callee's frame}
88 * | return address | {pushed by call}
89 * | spill region | {variable sized}
90 * +------------------------+
91 * | ...filler word... | {Note: used as 2nd word of V[locals-1] if long]
92 * +------------------------+
93 * | V[locals-1] |
94 * | V[locals-2] |
95 * | . |
96 * | . |
97 * | V[1] |
98 * | V[0] |
99 * +------------------------+
100 * | 0 to 3 words padding |
101 * +------------------------+
102 * | OUT[outs-1] |
103 * | OUT[outs-2] |
104 * | . |
105 * | OUT[0] |
106 * | curMethod* | <<== sp w/ 16-byte alignment
107 * +========================+
108 */
109
110/* Offset to distingish FP regs */
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700111#define FP_REG_OFFSET 32
Ian Rogerse32ca232012-03-05 10:20:23 -0800112/* Offset to distinguish DP FP regs */
jeffhaoe2962482012-06-28 11:29:57 -0700113#define FP_DOUBLE (FP_REG_OFFSET + 32)
Ian Rogerse32ca232012-03-05 10:20:23 -0800114/* Offset to distingish the extra regs */
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700115#define EXTRA_REG_OFFSET (FP_DOUBLE + 16)
Ian Rogerse32ca232012-03-05 10:20:23 -0800116/* Reg types */
117#define REGTYPE(x) (x & (FP_REG_OFFSET | FP_DOUBLE))
118#define FPREG(x) ((x & FP_REG_OFFSET) == FP_REG_OFFSET)
119#define EXTRAREG(x) ((x & EXTRA_REG_OFFSET) == EXTRA_REG_OFFSET)
120#define LOWREG(x) ((x & 0x1f) == x)
121#define DOUBLEREG(x) ((x & FP_DOUBLE) == FP_DOUBLE)
122#define SINGLEREG(x) (FPREG(x) && !DOUBLEREG(x))
Ian Rogersb5d09b22012-03-06 22:14:17 -0800123
Ian Rogerse32ca232012-03-05 10:20:23 -0800124/*
125 * Note: the low register of a floating point pair is sufficient to
126 * create the name of a double, but require both names to be passed to
127 * allow for asserts to verify that the pair is consecutive if significant
128 * rework is done in this area. Also, it is a good reminder in the calling
129 * code that reg locations always describe doubles as a pair of singles.
130 */
131#define S2D(x,y) ((x) | FP_DOUBLE)
132/* Mask to strip off fp flags */
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700133#define FP_REG_MASK 0xF
Ian Rogerse32ca232012-03-05 10:20:23 -0800134/* non-existent Dalvik register */
135#define vNone (-1)
136/* non-existant physical register */
137#define rNone (-1)
138
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700139/* RegisterLocation templates return values (rAX, rAX/rDX or XMM0) */
buzbee2cfc6392012-05-07 14:51:40 -0700140// location, wide, defined, const, fp, core, ref, highWord, home, lowReg, highReg, sRegLow
141#define LOC_C_RETURN {kLocPhysReg, 0, 0, 0, 0, 0, 0, 0, 1, rAX, INVALID_REG, INVALID_SREG, INVALID_SREG}
142#define LOC_C_RETURN_WIDE {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, rAX, rDX, INVALID_SREG, INVALID_SREG}
143#define LOC_C_RETURN_FLOAT {kLocPhysReg, 0, 0, 0, 1, 0, 0, 0, 1, fr0, INVALID_REG, INVALID_SREG, INVALID_SREG}
144#define LOC_C_RETURN_WIDE_DOUBLE {kLocPhysReg, 1, 0, 0, 1, 0, 0, 0, 1, fr0, fr1, INVALID_SREG, INVALID_SREG}
Ian Rogerse32ca232012-03-05 10:20:23 -0800145
Elliott Hughes719ace42012-03-09 18:06:03 -0800146enum ResourceEncodingPos {
Bill Buzbeea114add2012-05-03 15:00:40 -0700147 kGPReg0 = 0,
148 kRegSP = 4,
149 kRegLR = -1,
150 kFPReg0 = 16, // xmm0 .. xmm7/xmm15
151 kFPRegEnd = 32,
152 kRegEnd = kFPRegEnd,
153 kCCode = kRegEnd,
154 // The following four bits are for memory disambiguation
155 kDalvikReg, // 1 Dalvik Frame (can be fully disambiguated)
156 kLiteral, // 2 Literal pool (can be fully disambiguated)
157 kHeapRef, // 3 Somewhere on the heap (alias with any other heap)
158 kMustNotAlias, // 4 Guaranteed to be non-alias (eg *(r6+x))
Elliott Hughes719ace42012-03-09 18:06:03 -0800159};
Ian Rogerse32ca232012-03-05 10:20:23 -0800160
161#define ENCODE_REG_LIST(N) ((u8) N)
162#define ENCODE_REG_SP (1ULL << kRegSP)
163#define ENCODE_CCODE (1ULL << kCCode)
164#define ENCODE_FP_STATUS (1ULL << kFPStatus)
165
166/* Abstract memory locations */
167#define ENCODE_DALVIK_REG (1ULL << kDalvikReg)
168#define ENCODE_LITERAL (1ULL << kLiteral)
169#define ENCODE_HEAP_REF (1ULL << kHeapRef)
170#define ENCODE_MUST_NOT_ALIAS (1ULL << kMustNotAlias)
171
172#define ENCODE_ALL (~0ULL)
173#define ENCODE_MEM (ENCODE_DALVIK_REG | ENCODE_LITERAL | \
174 ENCODE_HEAP_REF | ENCODE_MUST_NOT_ALIAS)
175
176#define DECODE_ALIAS_INFO_REG(X) (X & 0xffff)
177#define DECODE_ALIAS_INFO_WIDE(X) ((X & 0x80000000) ? 1 : 0)
178
179/*
180 * Annotate special-purpose core registers:
181 */
182
Elliott Hughes719ace42012-03-09 18:06:03 -0800183enum NativeRegisterPool {
Ian Rogerse32ca232012-03-05 10:20:23 -0800184 r0 = 0,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800185 rAX = r0,
Ian Rogerse32ca232012-03-05 10:20:23 -0800186 r1 = 1,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800187 rCX = r1,
Ian Rogerse32ca232012-03-05 10:20:23 -0800188 r2 = 2,
189 rDX = r2,
190 r3 = 3,
191 rBX = r3,
192 r4sp = 4,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800193 rSP = r4sp,
194 r4sib_no_index = r4sp,
Ian Rogerse32ca232012-03-05 10:20:23 -0800195 r5 = 5,
196 rBP = r5,
Ian Rogers7caad772012-03-30 01:07:54 -0700197 r5sib_no_base = r5,
Ian Rogerse32ca232012-03-05 10:20:23 -0800198 r6 = 6,
199 rSI = r6,
200 r7 = 7,
201 rDI = r7,
202 r8 = 8,
203 r9 = 9,
204 r10 = 10,
205 r11 = 11,
206 r12 = 12,
207 r13 = 13,
208 r14 = 14,
209 r15 = 15,
Ian Rogersf7d9ad32012-03-13 18:45:39 -0700210 rRET = 16, // fake return address register for core spill mask
Ian Rogerse32ca232012-03-05 10:20:23 -0800211 fr0 = 0 + FP_REG_OFFSET,
212 fr1 = 1 + FP_REG_OFFSET,
213 fr2 = 2 + FP_REG_OFFSET,
214 fr3 = 3 + FP_REG_OFFSET,
215 fr4 = 4 + FP_REG_OFFSET,
216 fr5 = 5 + FP_REG_OFFSET,
217 fr6 = 6 + FP_REG_OFFSET,
218 fr7 = 7 + FP_REG_OFFSET,
219 fr8 = 8 + FP_REG_OFFSET,
220 fr9 = 9 + FP_REG_OFFSET,
221 fr10 = 10 + FP_REG_OFFSET,
222 fr11 = 11 + FP_REG_OFFSET,
223 fr12 = 12 + FP_REG_OFFSET,
224 fr13 = 13 + FP_REG_OFFSET,
225 fr14 = 14 + FP_REG_OFFSET,
226 fr15 = 15 + FP_REG_OFFSET,
Elliott Hughes719ace42012-03-09 18:06:03 -0800227};
Ian Rogerse32ca232012-03-05 10:20:23 -0800228
229/*
230 * Target-independent aliases
231 */
232
233#define rARG0 rAX
Ian Rogersb41b33b2012-03-20 14:22:54 -0700234#define rARG1 rCX
235#define rARG2 rDX
236#define rARG3 rBX
Ian Rogerse32ca232012-03-05 10:20:23 -0800237#define rRET0 rAX
238#define rRET1 rDX
Ian Rogers6cbb2bd2012-03-16 13:45:30 -0700239#define rINVOKE_TGT rAX
Ian Rogerse32ca232012-03-05 10:20:23 -0800240
241#define isPseudoOpcode(opCode) ((int)(opCode) < 0)
242
Ian Rogersb5d09b22012-03-06 22:14:17 -0800243/* X86 condition encodings */
244enum X86ConditionCode {
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 kX86CondO = 0x0, // overflow
246 kX86CondNo = 0x1, // not overflow
Ian Rogersb5d09b22012-03-06 22:14:17 -0800247
Bill Buzbeea114add2012-05-03 15:00:40 -0700248 kX86CondB = 0x2, // below
249 kX86CondNae = kX86CondB, // not-above-equal
250 kX86CondC = kX86CondB, // carry
Ian Rogersb5d09b22012-03-06 22:14:17 -0800251
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 kX86CondNb = 0x3, // not-below
253 kX86CondAe = kX86CondNb, // above-equal
254 kX86CondNc = kX86CondNb, // not-carry
Ian Rogersb5d09b22012-03-06 22:14:17 -0800255
Bill Buzbeea114add2012-05-03 15:00:40 -0700256 kX86CondZ = 0x4, // zero
257 kX86CondEq = kX86CondZ, // equal
Ian Rogersb5d09b22012-03-06 22:14:17 -0800258
Bill Buzbeea114add2012-05-03 15:00:40 -0700259 kX86CondNz = 0x5, // not-zero
260 kX86CondNe = kX86CondNz, // not-equal
Ian Rogersb5d09b22012-03-06 22:14:17 -0800261
Bill Buzbeea114add2012-05-03 15:00:40 -0700262 kX86CondBe = 0x6, // below-equal
263 kX86CondNa = kX86CondBe, // not-above
Ian Rogersb5d09b22012-03-06 22:14:17 -0800264
Bill Buzbeea114add2012-05-03 15:00:40 -0700265 kX86CondNbe = 0x7, // not-below-equal
266 kX86CondA = kX86CondNbe,// above
Ian Rogersb5d09b22012-03-06 22:14:17 -0800267
Bill Buzbeea114add2012-05-03 15:00:40 -0700268 kX86CondS = 0x8, // sign
269 kX86CondNs = 0x9, // not-sign
Ian Rogersb5d09b22012-03-06 22:14:17 -0800270
Bill Buzbeea114add2012-05-03 15:00:40 -0700271 kX86CondP = 0xA, // 8-bit parity even
272 kX86CondPE = kX86CondP,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800273
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 kX86CondNp = 0xB, // 8-bit parity odd
275 kX86CondPo = kX86CondNp,
Ian Rogersb5d09b22012-03-06 22:14:17 -0800276
Bill Buzbeea114add2012-05-03 15:00:40 -0700277 kX86CondL = 0xC, // less-than
278 kX86CondNge = kX86CondL, // not-greater-equal
Ian Rogersb5d09b22012-03-06 22:14:17 -0800279
Bill Buzbeea114add2012-05-03 15:00:40 -0700280 kX86CondNl = 0xD, // not-less-than
281 kX86CondGe = kX86CondNl, // not-greater-equal
Ian Rogersb5d09b22012-03-06 22:14:17 -0800282
Bill Buzbeea114add2012-05-03 15:00:40 -0700283 kX86CondLe = 0xE, // less-than-equal
284 kX86CondNg = kX86CondLe, // not-greater
Ian Rogersb5d09b22012-03-06 22:14:17 -0800285
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 kX86CondNle = 0xF, // not-less-than
287 kX86CondG = kX86CondNle,// greater
Ian Rogersb5d09b22012-03-06 22:14:17 -0800288};
289
Ian Rogerse32ca232012-03-05 10:20:23 -0800290/*
Ian Rogersde797832012-03-06 10:18:10 -0800291 * The following enum defines the list of supported X86 instructions by the
292 * assembler. Their corresponding EncodingMap positions will be defined in
293 * Assemble.cc.
Ian Rogerse32ca232012-03-05 10:20:23 -0800294 */
Elliott Hughes719ace42012-03-09 18:06:03 -0800295enum X86OpCode {
Bill Buzbeea114add2012-05-03 15:00:40 -0700296 kPseudoIntrinsicRetry = -16,
297 kPseudoSuspendTarget = -15,
298 kPseudoThrowTarget = -14,
299 kPseudoCaseLabel = -13,
300 kPseudoMethodEntry = -12,
301 kPseudoMethodExit = -11,
302 kPseudoBarrier = -10,
303 kPseudoExtended = -9,
304 kPseudoSSARep = -8,
305 kPseudoEntryBlock = -7,
306 kPseudoExitBlock = -6,
307 kPseudoTargetLabel = -5,
308 kPseudoDalvikByteCodeBoundary = -4,
309 kPseudoPseudoAlign4 = -3,
310 kPseudoEHBlockLabel = -2,
311 kPseudoNormalBlockLabel = -1,
312 kX86First,
313 kX8632BitData = kX86First, /* data [31..0] */
314 kX86Bkpt,
315 kX86Nop,
316 // Define groups of binary operations
317 // MR - Memory Register - opcode [base + disp], reg
318 // - lir operands - 0: base, 1: disp, 2: reg
319 // AR - Array Register - opcode [base + index * scale + disp], reg
320 // - lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: reg
321 // TR - Thread Register - opcode fs:[disp], reg - where fs: is equal to Thread::Current()
322 // - lir operands - 0: disp, 1: reg
323 // RR - Register Register - opcode reg1, reg2
324 // - lir operands - 0: reg1, 1: reg2
325 // RM - Register Memory - opcode reg, [base + disp]
326 // - lir operands - 0: reg, 1: base, 2: disp
327 // RA - Register Array - opcode reg, [base + index * scale + disp]
328 // - lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: disp
329 // RT - Register Thread - opcode reg, fs:[disp] - where fs: is equal to Thread::Current()
330 // - lir operands - 0: reg, 1: disp
331 // RI - Register Immediate - opcode reg, #immediate
332 // - lir operands - 0: reg, 1: immediate
333 // MI - Memory Immediate - opcode [base + disp], #immediate
334 // - lir operands - 0: base, 1: disp, 2: immediate
335 // AI - Array Immediate - opcode [base + index * scale + disp], #immediate
336 // - lir operands - 0: base, 1: index, 2: scale, 3: disp 4: immediate
337 // TI - Thread Register - opcode fs:[disp], imm - where fs: is equal to Thread::Current()
338 // - lir operands - 0: disp, 1: imm
Ian Rogers96ab4202012-03-05 19:51:02 -0800339#define BinaryOpCode(opcode) \
Ian Rogersb5d09b22012-03-06 22:14:17 -0800340 opcode ## 8MR, opcode ## 8AR, opcode ## 8TR, \
341 opcode ## 8RR, opcode ## 8RM, opcode ## 8RA, opcode ## 8RT, \
342 opcode ## 8RI, opcode ## 8MI, opcode ## 8AI, opcode ## 8TI, \
343 opcode ## 16MR, opcode ## 16AR, opcode ## 16TR, \
344 opcode ## 16RR, opcode ## 16RM, opcode ## 16RA, opcode ## 16RT, \
345 opcode ## 16RI, opcode ## 16MI, opcode ## 16AI, opcode ## 16TI, \
346 opcode ## 16RI8, opcode ## 16MI8, opcode ## 16AI8, opcode ## 16TI8, \
347 opcode ## 32MR, opcode ## 32AR, opcode ## 32TR, \
348 opcode ## 32RR, opcode ## 32RM, opcode ## 32RA, opcode ## 32RT, \
349 opcode ## 32RI, opcode ## 32MI, opcode ## 32AI, opcode ## 32TI, \
350 opcode ## 32RI8, opcode ## 32MI8, opcode ## 32AI8, opcode ## 32TI8
Bill Buzbeea114add2012-05-03 15:00:40 -0700351 BinaryOpCode(kX86Add),
352 BinaryOpCode(kX86Or),
353 BinaryOpCode(kX86Adc),
354 BinaryOpCode(kX86Sbb),
355 BinaryOpCode(kX86And),
356 BinaryOpCode(kX86Sub),
357 BinaryOpCode(kX86Xor),
358 BinaryOpCode(kX86Cmp),
Ian Rogers96ab4202012-03-05 19:51:02 -0800359#undef BinaryOpCode
Bill Buzbeea114add2012-05-03 15:00:40 -0700360 kX86Imul16RRI, kX86Imul16RMI, kX86Imul16RAI,
361 kX86Imul32RRI, kX86Imul32RMI, kX86Imul32RAI,
362 kX86Imul32RRI8, kX86Imul32RMI8, kX86Imul32RAI8,
363 kX86Mov8MR, kX86Mov8AR, kX86Mov8TR,
364 kX86Mov8RR, kX86Mov8RM, kX86Mov8RA, kX86Mov8RT,
365 kX86Mov8RI, kX86Mov8MI, kX86Mov8AI, kX86Mov8TI,
366 kX86Mov16MR, kX86Mov16AR, kX86Mov16TR,
367 kX86Mov16RR, kX86Mov16RM, kX86Mov16RA, kX86Mov16RT,
368 kX86Mov16RI, kX86Mov16MI, kX86Mov16AI, kX86Mov16TI,
369 kX86Mov32MR, kX86Mov32AR, kX86Mov32TR,
370 kX86Mov32RR, kX86Mov32RM, kX86Mov32RA, kX86Mov32RT,
371 kX86Mov32RI, kX86Mov32MI, kX86Mov32AI, kX86Mov32TI,
372 kX86Lea32RA,
373 // RC - Register CL - opcode reg, CL
374 // - lir operands - 0: reg, 1: CL
375 // MC - Memory CL - opcode [base + disp], CL
376 // - lir operands - 0: base, 1: disp, 2: CL
377 // AC - Array CL - opcode [base + index * scale + disp], CL
378 // - lir operands - 0: base, 1: index, 2: scale, 3: disp, 4: CL
Ian Rogersb5d09b22012-03-06 22:14:17 -0800379#define BinaryShiftOpCode(opcode) \
Bill Buzbeea114add2012-05-03 15:00:40 -0700380 opcode ## 8RI, opcode ## 8MI, opcode ## 8AI, \
381 opcode ## 8RC, opcode ## 8MC, opcode ## 8AC, \
382 opcode ## 16RI, opcode ## 16MI, opcode ## 16AI, \
383 opcode ## 16RC, opcode ## 16MC, opcode ## 16AC, \
384 opcode ## 32RI, opcode ## 32MI, opcode ## 32AI, \
385 opcode ## 32RC, opcode ## 32MC, opcode ## 32AC
386 BinaryShiftOpCode(kX86Rol),
387 BinaryShiftOpCode(kX86Ror),
388 BinaryShiftOpCode(kX86Rcl),
389 BinaryShiftOpCode(kX86Rcr),
390 BinaryShiftOpCode(kX86Sal),
391 BinaryShiftOpCode(kX86Shr),
392 BinaryShiftOpCode(kX86Sar),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800393#undef BinaryShiftOpcode
394#define UnaryOpcode(opcode, reg, mem, array) \
Bill Buzbeea114add2012-05-03 15:00:40 -0700395 opcode ## 8 ## reg, opcode ## 8 ## mem, opcode ## 8 ## array, \
396 opcode ## 16 ## reg, opcode ## 16 ## mem, opcode ## 16 ## array, \
397 opcode ## 32 ## reg, opcode ## 32 ## mem, opcode ## 32 ## array
398 UnaryOpcode(kX86Test, RI, MI, AI),
399 UnaryOpcode(kX86Not, R, M, A),
400 UnaryOpcode(kX86Neg, R, M, A),
401 UnaryOpcode(kX86Mul, DaR, DaM, DaA),
402 UnaryOpcode(kX86Imul, DaR, DaM, DaA),
403 UnaryOpcode(kX86Divmod, DaR, DaM, DaA),
404 UnaryOpcode(kX86Idivmod, DaR, DaM, DaA),
Ian Rogersb5d09b22012-03-06 22:14:17 -0800405#undef UnaryOpcode
406#define Binary0fOpCode(opcode) \
407 opcode ## RR, opcode ## RM, opcode ## RA
Bill Buzbeea114add2012-05-03 15:00:40 -0700408 Binary0fOpCode(kX86Movsd),
409 kX86MovsdMR,
410 kX86MovsdAR,
411 Binary0fOpCode(kX86Movss),
412 kX86MovssMR,
413 kX86MovssAR,
414 Binary0fOpCode(kX86Cvtsi2sd), // int to double
415 Binary0fOpCode(kX86Cvtsi2ss), // int to float
416 Binary0fOpCode(kX86Cvttsd2si),// truncating double to int
417 Binary0fOpCode(kX86Cvttss2si),// truncating float to int
418 Binary0fOpCode(kX86Cvtsd2si), // rounding double to int
419 Binary0fOpCode(kX86Cvtss2si), // rounding float to int
420 Binary0fOpCode(kX86Ucomisd), // unordered double compare
421 Binary0fOpCode(kX86Ucomiss), // unordered float compare
422 Binary0fOpCode(kX86Comisd), // double compare
423 Binary0fOpCode(kX86Comiss), // float compare
424 Binary0fOpCode(kX86Orps), // or of floating point registers
425 Binary0fOpCode(kX86Xorps), // xor of floating point registers
426 Binary0fOpCode(kX86Addsd), // double add
427 Binary0fOpCode(kX86Addss), // float add
428 Binary0fOpCode(kX86Mulsd), // double multiply
429 Binary0fOpCode(kX86Mulss), // float multiply
Bill Buzbeea114add2012-05-03 15:00:40 -0700430 Binary0fOpCode(kX86Cvtsd2ss), // double to float
jeffhao292188d2012-05-17 15:45:04 -0700431 Binary0fOpCode(kX86Cvtss2sd), // float to double
Bill Buzbeea114add2012-05-03 15:00:40 -0700432 Binary0fOpCode(kX86Subsd), // double subtract
433 Binary0fOpCode(kX86Subss), // float subtract
434 Binary0fOpCode(kX86Divsd), // double divide
435 Binary0fOpCode(kX86Divss), // float divide
436 kX86PsllqRI, // shift of floating point registers
437 Binary0fOpCode(kX86Movdxr), // move into xmm from gpr
438 Binary0fOpCode(kX86Movdrx), // move into reg from xmm
439 kX86Set8R, kX86Set8M, kX86Set8A,// set byte depending on condition operand
440 kX86Mfence, // memory barrier
441 Binary0fOpCode(kX86Imul16), // 16bit multiply
442 Binary0fOpCode(kX86Imul32), // 32bit multiply
443 Binary0fOpCode(kX86Movzx8), // zero-extend 8-bit value
444 Binary0fOpCode(kX86Movzx16), // zero-extend 16-bit value
445 Binary0fOpCode(kX86Movsx8), // sign-extend 8-bit value
446 Binary0fOpCode(kX86Movsx16), // sign-extend 16-bit value
Ian Rogersb5d09b22012-03-06 22:14:17 -0800447#undef Binary0fOpCode
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 kX86Jcc8, kX86Jcc32, // jCC rel8/32; lir operands - 0: rel, 1: CC, target assigned
449 kX86Jmp8, kX86Jmp32, // jmp rel8/32; lir operands - 0: rel, target assigned
450 kX86JmpR, // jmp reg; lir operands - 0: reg
451 kX86CallR, // call reg; lir operands - 0: reg
452 kX86CallM, // call [base + disp]; lir operands - 0: base, 1: disp
453 kX86CallA, // call [base + index * scale + disp]
454 // lir operands - 0: base, 1: index, 2: scale, 3: disp
455 kX86CallT, // call fs:[disp]; fs: is equal to Thread::Current(); lir operands - 0: disp
456 kX86Ret, // ret; no lir operands
457 kX86StartOfMethod, // call 0; pop reg; sub reg, # - generate start of method into reg
458 // lir operands - 0: reg
459 kX86PcRelLoadRA, // mov reg, [base + index * scale + PC relative displacement]
460 // lir operands - 0: reg, 1: base, 2: index, 3: scale, 4: table
461 kX86PcRelAdr, // mov reg, PC relative displacement; lir operands - 0: reg, 1: table
462 kX86Last
Elliott Hughes719ace42012-03-09 18:06:03 -0800463};
Ian Rogerse32ca232012-03-05 10:20:23 -0800464
Ian Rogersde797832012-03-06 10:18:10 -0800465/* Instruction assembly fieldLoc kind */
Elliott Hughes719ace42012-03-09 18:06:03 -0800466enum X86EncodingKind {
Ian Rogersb5d09b22012-03-06 22:14:17 -0800467 kData, // Special case for raw data.
468 kNop, // Special case for variable length nop.
469 kNullary, // Opcode that takes no arguments.
470 kReg, kMem, kArray, // R, M and A instruction kinds.
471 kMemReg, kArrayReg, kThreadReg, // MR, AR and TR instruction kinds.
472 kRegReg, kRegMem, kRegArray, kRegThread, // RR, RM, RA and RT instruction kinds.
473 kRegImm, kMemImm, kArrayImm, kThreadImm, // RI, MI, AI and TI instruction kinds.
474 kRegRegImm, kRegMemImm, kRegArrayImm, // RRI, RMI and RAI instruction kinds.
475 kMovRegImm, // Shorter form move RI.
476 kShiftRegImm, kShiftMemImm, kShiftArrayImm, // Shift opcode with immediate.
477 kShiftRegCl, kShiftMemCl, kShiftArrayCl, // Shift opcode with register CL.
478 kRegRegReg, kRegRegMem, kRegRegArray, // RRR, RRM, RRA instruction kinds.
479 kRegCond, kMemCond, kArrayCond, // R, M, A instruction kinds following by a condition.
Bill Buzbeea114add2012-05-03 15:00:40 -0700480 kJmp, kJcc, kCall, // Branch instruction kinds.
481 kPcRel, // Operation with displacement that is PC relative
482 kMacro, // An instruction composing multiple others
483 kUnimplemented // Encoding used when an instruction isn't yet implemented.
Elliott Hughes719ace42012-03-09 18:06:03 -0800484};
Ian Rogersde797832012-03-06 10:18:10 -0800485
Ian Rogersde797832012-03-06 10:18:10 -0800486/* Struct used to define the EncodingMap positions for each X86 opcode */
Elliott Hughes719ace42012-03-09 18:06:03 -0800487struct X86EncodingMap {
Ian Rogersde797832012-03-06 10:18:10 -0800488 X86OpCode opcode; // e.g. kOpAddRI
489 X86EncodingKind kind; // Used to discriminate in the union below
490 int flags;
Ian Rogersb5d09b22012-03-06 22:14:17 -0800491 struct {
Bill Buzbeea114add2012-05-03 15:00:40 -0700492 uint8_t prefix1; // non-zero => a prefix byte
493 uint8_t prefix2; // non-zero => a second prefix byte
494 uint8_t opcode; // 1 byte opcode
495 uint8_t extra_opcode1; // possible extra opcode byte
496 uint8_t extra_opcode2; // possible second extra opcode byte
497 // 3bit opcode that gets encoded in the register bits of the modrm byte, use determined by the
498 // encoding kind
499 uint8_t modrm_opcode;
500 uint8_t ax_opcode; // non-zero => shorter encoding for AX as a destination
501 uint8_t immediate_bytes; // number of bytes of immediate
Ian Rogersde797832012-03-06 10:18:10 -0800502 } skeleton;
503 const char *name;
504 const char* fmt;
Elliott Hughes719ace42012-03-09 18:06:03 -0800505};
Ian Rogersde797832012-03-06 10:18:10 -0800506
507extern X86EncodingMap EncodingMap[kX86Last];
508
buzbeea7678db2012-03-05 15:35:46 -0800509// FIXME: mem barrier type - what do we do for x86?
510#define kSY 0
511#define kST 0
512
Ian Rogerse32ca232012-03-05 10:20:23 -0800513/* Bit flags describing the behavior of each native opcode */
Elliott Hughes719ace42012-03-09 18:06:03 -0800514enum X86OpFeatureFlags {
Bill Buzbeea114add2012-05-03 15:00:40 -0700515 kIsBranch = 0,
516 kRegDef0,
517 kRegDef1,
jeffhaoe2962482012-06-28 11:29:57 -0700518 kRegDefA,
519 kRegDefD,
Bill Buzbeea114add2012-05-03 15:00:40 -0700520 kRegDefSP,
Bill Buzbeea114add2012-05-03 15:00:40 -0700521 kRegUse0,
522 kRegUse1,
523 kRegUse2,
524 kRegUse3,
jeffhaoe2962482012-06-28 11:29:57 -0700525 kRegUse4,
526 kRegUseA,
527 kRegUseC,
528 kRegUseD,
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 kRegUseSP,
Bill Buzbeea114add2012-05-03 15:00:40 -0700530 kNoOperand,
531 kIsUnaryOp,
532 kIsBinaryOp,
533 kIsTertiaryOp,
534 kIsQuadOp,
535 kIsQuinOp,
536 kIsSextupleOp,
537 kIsIT,
538 kSetsCCodes,
539 kUsesCCodes,
540 kMemLoad,
541 kMemStore,
542 kPCRelFixup,
Ian Rogerse32ca232012-03-05 10:20:23 -0800543// FIXME: add NEEDS_FIXUP to instruction attributes
Elliott Hughes719ace42012-03-09 18:06:03 -0800544};
Ian Rogerse32ca232012-03-05 10:20:23 -0800545
546#define IS_LOAD (1 << kMemLoad)
547#define IS_STORE (1 << kMemStore)
548#define IS_BRANCH (1 << kIsBranch)
549#define REG_DEF0 (1 << kRegDef0)
550#define REG_DEF1 (1 << kRegDef1)
jeffhaoe2962482012-06-28 11:29:57 -0700551#define REG_DEFA (1 << kRegDefA)
552#define REG_DEFD (1 << kRegDefD)
Ian Rogerse32ca232012-03-05 10:20:23 -0800553#define REG_DEF_SP (1 << kRegDefSP)
Ian Rogerse32ca232012-03-05 10:20:23 -0800554#define REG_USE0 (1 << kRegUse0)
555#define REG_USE1 (1 << kRegUse1)
556#define REG_USE2 (1 << kRegUse2)
557#define REG_USE3 (1 << kRegUse3)
jeffhaoe2962482012-06-28 11:29:57 -0700558#define REG_USE4 (1 << kRegUse4)
559#define REG_USEA (1 << kRegUseA)
560#define REG_USEC (1 << kRegUseC)
561#define REG_USED (1 << kRegUseD)
Ian Rogerse32ca232012-03-05 10:20:23 -0800562#define REG_USE_SP (1 << kRegUseSP)
Ian Rogerse32ca232012-03-05 10:20:23 -0800563#define NO_OPERAND (1 << kNoOperand)
564#define IS_UNARY_OP (1 << kIsUnaryOp)
565#define IS_BINARY_OP (1 << kIsBinaryOp)
566#define IS_TERTIARY_OP (1 << kIsTertiaryOp)
567#define IS_QUAD_OP (1 << kIsQuadOp)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800568#define IS_QUIN_OP (1 << kIsQuinOp)
569#define IS_SEXTUPLE_OP (1 << kIsSextupleOp)
Ian Rogerse32ca232012-03-05 10:20:23 -0800570#define IS_IT (1 << kIsIT)
571#define SETS_CCODES (1 << kSetsCCodes)
572#define USES_CCODES (1 << kUsesCCodes)
Ian Rogersb5d09b22012-03-06 22:14:17 -0800573#define NEEDS_FIXUP (1 << kPCRelFixup)
Ian Rogerse32ca232012-03-05 10:20:23 -0800574
575/* attributes, included for compatibility */
576#define REG_DEF_FPCS_LIST0 (0)
577#define REG_DEF_FPCS_LIST2 (0)
578
579
580/* Common combo register usage patterns */
581#define REG_USE01 (REG_USE0 | REG_USE1)
582#define REG_USE02 (REG_USE0 | REG_USE2)
583#define REG_USE012 (REG_USE01 | REG_USE2)
jeffhaoe2962482012-06-28 11:29:57 -0700584#define REG_USE014 (REG_USE01 | REG_USE4)
Ian Rogerse32ca232012-03-05 10:20:23 -0800585#define REG_DEF0_USE0 (REG_DEF0 | REG_USE0)
586#define REG_DEF0_USE1 (REG_DEF0 | REG_USE1)
jeffhaoe2962482012-06-28 11:29:57 -0700587#define REG_DEF0_USE12 (REG_DEF0_USE1 | REG_USE2)
588#define REG_DEFA_USEA (REG_DEFA | REG_USEA)
589#define REG_DEFAD_USEA (REG_DEFA_USEA | REG_DEFD)
590#define REG_DEFAD_USEAD (REG_DEFAD_USEA | REG_USED)
Ian Rogerse32ca232012-03-05 10:20:23 -0800591
Ian Rogerse32ca232012-03-05 10:20:23 -0800592/* Keys for target-specific scheduling and other optimization hints */
Elliott Hughes719ace42012-03-09 18:06:03 -0800593enum X86TargetOptHints {
Bill Buzbeea114add2012-05-03 15:00:40 -0700594 kMaxHoistDistance,
Elliott Hughes719ace42012-03-09 18:06:03 -0800595};
Ian Rogerse32ca232012-03-05 10:20:23 -0800596
Ian Rogersb5d09b22012-03-06 22:14:17 -0800597/* Offsets of high and low halves of a 64bit value */
598#define LOWORD_OFFSET 0
599#define HIWORD_OFFSET 4
600
601/* Segment override instruction prefix used for quick TLS access to Thread::Current() */
602#define THREAD_PREFIX 0x64
Ian Rogerse32ca232012-03-05 10:20:23 -0800603
Ian Rogersde797832012-03-06 10:18:10 -0800604#define IS_SIMM8(v) ((-128 <= (v)) && ((v) <= 127))
Ian Rogersb5d09b22012-03-06 22:14:17 -0800605#define IS_SIMM16(v) ((-32768 <= (v)) && ((v) <= 32767))
Ian Rogerse32ca232012-03-05 10:20:23 -0800606
607} // namespace art
608
609#endif // ART_COMPILER_COMPILER_CODEGEN_X86_X86LIR_H_