blob: 11ccd4b35b8129bff28875abf63b6eac7c3e5940 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
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/* This file contains codegen for the X86 ISA */
18
19#include "codegen_x86.h"
20#include "dex/quick/mir_to_lir-inl.h"
21#include "mirror/array.h"
22#include "x86_lir.h"
23
24namespace art {
25
26/*
27 * Perform register memory operation.
28 */
29LIR* X86Mir2Lir::GenRegMemCheck(ConditionCode c_code,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070030 int reg1, int base, int offset, ThrowKind kind) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070031 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind,
32 current_dalvik_offset_, reg1, base, offset);
33 OpRegMem(kOpCmp, reg1, base, offset);
34 LIR* branch = OpCondBranch(c_code, tgt);
35 // Remember branch target - will process later
36 throw_launchpads_.Insert(tgt);
37 return branch;
38}
39
40/*
Mark Mendell343adb52013-12-18 06:02:17 -080041 * Perform a compare of memory to immediate value
42 */
43LIR* X86Mir2Lir::GenMemImmedCheck(ConditionCode c_code,
44 int base, int offset, int check_value, ThrowKind kind) {
45 LIR* tgt = RawLIR(0, kPseudoThrowTarget, kind,
46 current_dalvik_offset_, base, check_value, 0);
47 NewLIR3(IS_SIMM8(check_value) ? kX86Cmp32MI8 : kX86Cmp32MI, base, offset, check_value);
48 LIR* branch = OpCondBranch(c_code, tgt);
49 // Remember branch target - will process later
50 throw_launchpads_.Insert(tgt);
51 return branch;
52}
53
54/*
Brian Carlstrom7940e442013-07-12 13:46:57 -070055 * Compare two 64-bit values
56 * x = y return 0
57 * x < y return -1
58 * x > y return 1
59 */
60void X86Mir2Lir::GenCmpLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070061 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070062 FlushAllRegs();
63 LockCallTemps(); // Prepare for explicit register usage
64 LoadValueDirectWideFixed(rl_src1, r0, r1);
65 LoadValueDirectWideFixed(rl_src2, r2, r3);
66 // Compute (r1:r0) = (r1:r0) - (r3:r2)
67 OpRegReg(kOpSub, r0, r2); // r0 = r0 - r2
68 OpRegReg(kOpSbc, r1, r3); // r1 = r1 - r3 - CF
69 NewLIR2(kX86Set8R, r2, kX86CondL); // r2 = (r1:r0) < (r3:r2) ? 1 : 0
70 NewLIR2(kX86Movzx8RR, r2, r2);
71 OpReg(kOpNeg, r2); // r2 = -r2
72 OpRegReg(kOpOr, r0, r1); // r0 = high | low - sets ZF
73 NewLIR2(kX86Set8R, r0, kX86CondNz); // r0 = (r1:r0) != (r3:r2) ? 1 : 0
74 NewLIR2(kX86Movzx8RR, r0, r0);
75 OpRegReg(kOpOr, r0, r2); // r0 = r0 | r2
76 RegLocation rl_result = LocCReturn();
77 StoreValue(rl_dest, rl_result);
78}
79
80X86ConditionCode X86ConditionEncoding(ConditionCode cond) {
81 switch (cond) {
82 case kCondEq: return kX86CondEq;
83 case kCondNe: return kX86CondNe;
84 case kCondCs: return kX86CondC;
85 case kCondCc: return kX86CondNc;
Vladimir Marko58af1f92013-12-19 13:31:15 +000086 case kCondUlt: return kX86CondC;
87 case kCondUge: return kX86CondNc;
Brian Carlstrom7940e442013-07-12 13:46:57 -070088 case kCondMi: return kX86CondS;
89 case kCondPl: return kX86CondNs;
90 case kCondVs: return kX86CondO;
91 case kCondVc: return kX86CondNo;
92 case kCondHi: return kX86CondA;
93 case kCondLs: return kX86CondBe;
94 case kCondGe: return kX86CondGe;
95 case kCondLt: return kX86CondL;
96 case kCondGt: return kX86CondG;
97 case kCondLe: return kX86CondLe;
98 case kCondAl:
99 case kCondNv: LOG(FATAL) << "Should not reach here";
100 }
101 return kX86CondO;
102}
103
104LIR* X86Mir2Lir::OpCmpBranch(ConditionCode cond, int src1, int src2,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700105 LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700106 NewLIR2(kX86Cmp32RR, src1, src2);
107 X86ConditionCode cc = X86ConditionEncoding(cond);
108 LIR* branch = NewLIR2(kX86Jcc8, 0 /* lir operand for Jcc offset */ ,
109 cc);
110 branch->target = target;
111 return branch;
112}
113
114LIR* X86Mir2Lir::OpCmpImmBranch(ConditionCode cond, int reg,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700115 int check_value, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700116 if ((check_value == 0) && (cond == kCondEq || cond == kCondNe)) {
117 // TODO: when check_value == 0 and reg is rCX, use the jcxz/nz opcode
118 NewLIR2(kX86Test32RR, reg, reg);
119 } else {
120 NewLIR2(IS_SIMM8(check_value) ? kX86Cmp32RI8 : kX86Cmp32RI, reg, check_value);
121 }
122 X86ConditionCode cc = X86ConditionEncoding(cond);
123 LIR* branch = NewLIR2(kX86Jcc8, 0 /* lir operand for Jcc offset */ , cc);
124 branch->target = target;
125 return branch;
126}
127
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700128LIR* X86Mir2Lir::OpRegCopyNoInsert(int r_dest, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700129 if (X86_FPREG(r_dest) || X86_FPREG(r_src))
130 return OpFpRegCopy(r_dest, r_src);
131 LIR* res = RawLIR(current_dalvik_offset_, kX86Mov32RR,
132 r_dest, r_src);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800133 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700134 res->flags.is_nop = true;
135 }
136 return res;
137}
138
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700139LIR* X86Mir2Lir::OpRegCopy(int r_dest, int r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140 LIR *res = OpRegCopyNoInsert(r_dest, r_src);
141 AppendLIR(res);
142 return res;
143}
144
145void X86Mir2Lir::OpRegCopyWide(int dest_lo, int dest_hi,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700146 int src_lo, int src_hi) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700147 bool dest_fp = X86_FPREG(dest_lo) && X86_FPREG(dest_hi);
148 bool src_fp = X86_FPREG(src_lo) && X86_FPREG(src_hi);
149 assert(X86_FPREG(src_lo) == X86_FPREG(src_hi));
150 assert(X86_FPREG(dest_lo) == X86_FPREG(dest_hi));
151 if (dest_fp) {
152 if (src_fp) {
153 OpRegCopy(S2d(dest_lo, dest_hi), S2d(src_lo, src_hi));
154 } else {
155 // TODO: Prevent this from happening in the code. The result is often
156 // unused or could have been loaded more easily from memory.
157 NewLIR2(kX86MovdxrRR, dest_lo, src_lo);
158 NewLIR2(kX86MovdxrRR, dest_hi, src_hi);
159 NewLIR2(kX86PsllqRI, dest_hi, 32);
160 NewLIR2(kX86OrpsRR, dest_lo, dest_hi);
161 }
162 } else {
163 if (src_fp) {
164 NewLIR2(kX86MovdrxRR, dest_lo, src_lo);
165 NewLIR2(kX86PsrlqRI, src_lo, 32);
166 NewLIR2(kX86MovdrxRR, dest_hi, src_lo);
167 } else {
168 // Handle overlap
169 if (src_hi == dest_lo) {
170 OpRegCopy(dest_hi, src_hi);
171 OpRegCopy(dest_lo, src_lo);
172 } else {
173 OpRegCopy(dest_lo, src_lo);
174 OpRegCopy(dest_hi, src_hi);
175 }
176 }
177 }
178}
179
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700180void X86Mir2Lir::GenSelect(BasicBlock* bb, MIR* mir) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700181 UNIMPLEMENTED(FATAL) << "Need codegen for GenSelect";
182}
183
184void X86Mir2Lir::GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir) {
buzbee0d829482013-10-11 15:24:55 -0700185 LIR* taken = &block_label_list_[bb->taken];
Brian Carlstrom7940e442013-07-12 13:46:57 -0700186 RegLocation rl_src1 = mir_graph_->GetSrcWide(mir, 0);
187 RegLocation rl_src2 = mir_graph_->GetSrcWide(mir, 2);
Mark Mendell412d4f82013-12-18 13:32:36 -0800188 ConditionCode ccode = static_cast<ConditionCode>(mir->dalvikInsn.arg[0]);
189
190 if (rl_src1.is_const) {
191 std::swap(rl_src1, rl_src2);
192 ccode = FlipComparisonOrder(ccode);
193 }
194 if (rl_src2.is_const) {
195 // Do special compare/branch against simple const operand
196 int64_t val = mir_graph_->ConstantValueWide(rl_src2);
197 GenFusedLongCmpImmBranch(bb, rl_src1, val, ccode);
198 return;
199 }
200
Brian Carlstrom7940e442013-07-12 13:46:57 -0700201 FlushAllRegs();
202 LockCallTemps(); // Prepare for explicit register usage
203 LoadValueDirectWideFixed(rl_src1, r0, r1);
204 LoadValueDirectWideFixed(rl_src2, r2, r3);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700205 // Swap operands and condition code to prevent use of zero flag.
206 if (ccode == kCondLe || ccode == kCondGt) {
207 // Compute (r3:r2) = (r3:r2) - (r1:r0)
208 OpRegReg(kOpSub, r2, r0); // r2 = r2 - r0
209 OpRegReg(kOpSbc, r3, r1); // r3 = r3 - r1 - CF
210 } else {
211 // Compute (r1:r0) = (r1:r0) - (r3:r2)
212 OpRegReg(kOpSub, r0, r2); // r0 = r0 - r2
213 OpRegReg(kOpSbc, r1, r3); // r1 = r1 - r3 - CF
214 }
215 switch (ccode) {
216 case kCondEq:
217 case kCondNe:
218 OpRegReg(kOpOr, r0, r1); // r0 = r0 | r1
219 break;
220 case kCondLe:
221 ccode = kCondGe;
222 break;
223 case kCondGt:
224 ccode = kCondLt;
225 break;
226 case kCondLt:
227 case kCondGe:
228 break;
229 default:
230 LOG(FATAL) << "Unexpected ccode: " << ccode;
231 }
232 OpCondBranch(ccode, taken);
233}
234
Mark Mendell412d4f82013-12-18 13:32:36 -0800235void X86Mir2Lir::GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
236 int64_t val, ConditionCode ccode) {
237 int32_t val_lo = Low32Bits(val);
238 int32_t val_hi = High32Bits(val);
239 LIR* taken = &block_label_list_[bb->taken];
240 LIR* not_taken = &block_label_list_[bb->fall_through];
241 rl_src1 = LoadValueWide(rl_src1, kCoreReg);
242 int32_t low_reg = rl_src1.low_reg;
243 int32_t high_reg = rl_src1.high_reg;
244
245 if (val == 0 && (ccode == kCondEq || ccode == kCondNe)) {
246 int t_reg = AllocTemp();
247 OpRegRegReg(kOpOr, t_reg, low_reg, high_reg);
248 FreeTemp(t_reg);
249 OpCondBranch(ccode, taken);
250 return;
251 }
252
253 OpRegImm(kOpCmp, high_reg, val_hi);
254 switch (ccode) {
255 case kCondEq:
256 case kCondNe:
257 OpCondBranch(kCondNe, (ccode == kCondEq) ? not_taken : taken);
258 break;
259 case kCondLt:
260 OpCondBranch(kCondLt, taken);
261 OpCondBranch(kCondGt, not_taken);
262 ccode = kCondUlt;
263 break;
264 case kCondLe:
265 OpCondBranch(kCondLt, taken);
266 OpCondBranch(kCondGt, not_taken);
267 ccode = kCondLs;
268 break;
269 case kCondGt:
270 OpCondBranch(kCondGt, taken);
271 OpCondBranch(kCondLt, not_taken);
272 ccode = kCondHi;
273 break;
274 case kCondGe:
275 OpCondBranch(kCondGt, taken);
276 OpCondBranch(kCondLt, not_taken);
277 ccode = kCondUge;
278 break;
279 default:
280 LOG(FATAL) << "Unexpected ccode: " << ccode;
281 }
282 OpCmpImmBranch(ccode, low_reg, val_lo, taken);
283}
284
Brian Carlstrom7940e442013-07-12 13:46:57 -0700285RegLocation X86Mir2Lir::GenDivRemLit(RegLocation rl_dest, int reg_lo,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700286 int lit, bool is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 LOG(FATAL) << "Unexpected use of GenDivRemLit for x86";
288 return rl_dest;
289}
290
291RegLocation X86Mir2Lir::GenDivRem(RegLocation rl_dest, int reg_lo,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700292 int reg_hi, bool is_div) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700293 LOG(FATAL) << "Unexpected use of GenDivRem for x86";
294 return rl_dest;
295}
296
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700297bool X86Mir2Lir::GenInlinedMinMaxInt(CallInfo* info, bool is_min) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700298 DCHECK_EQ(cu_->instruction_set, kX86);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800299
300 // Get the two arguments to the invoke and place them in GP registers.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700301 RegLocation rl_src1 = info->args[0];
302 RegLocation rl_src2 = info->args[1];
303 rl_src1 = LoadValue(rl_src1, kCoreReg);
304 rl_src2 = LoadValue(rl_src2, kCoreReg);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800305
Brian Carlstrom7940e442013-07-12 13:46:57 -0700306 RegLocation rl_dest = InlineTarget(info);
307 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800308
309 /*
310 * If the result register is the same as the second element, then we need to be careful.
311 * The reason is that the first copy will inadvertently clobber the second element with
312 * the first one thus yielding the wrong result. Thus we do a swap in that case.
313 */
314 if (rl_result.low_reg == rl_src2.low_reg) {
315 std::swap(rl_src1, rl_src2);
316 }
317
318 // Pick the first integer as min/max.
319 OpRegCopy(rl_result.low_reg, rl_src1.low_reg);
320
321 // If the integers are both in the same register, then there is nothing else to do
322 // because they are equal and we have already moved one into the result.
323 if (rl_src1.low_reg != rl_src2.low_reg) {
324 // It is possible we didn't pick correctly so do the actual comparison now.
325 OpRegReg(kOpCmp, rl_src1.low_reg, rl_src2.low_reg);
326
327 // Conditionally move the other integer into the destination register.
328 ConditionCode condition_code = is_min ? kCondGt : kCondLt;
329 OpCondRegReg(kOpCmov, condition_code, rl_result.low_reg, rl_src2.low_reg);
330 }
331
Brian Carlstrom7940e442013-07-12 13:46:57 -0700332 StoreValue(rl_dest, rl_result);
333 return true;
334}
335
Vladimir Markoe508a202013-11-04 15:24:22 +0000336bool X86Mir2Lir::GenInlinedPeek(CallInfo* info, OpSize size) {
337 RegLocation rl_src_address = info->args[0]; // long address
338 rl_src_address.wide = 0; // ignore high half in info->args[1]
339 RegLocation rl_dest = InlineTarget(info);
340 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
341 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
342 if (size == kLong) {
343 // Unaligned access is allowed on x86.
344 LoadBaseDispWide(rl_address.low_reg, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
345 StoreValueWide(rl_dest, rl_result);
346 } else {
347 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
348 // Unaligned access is allowed on x86.
349 LoadBaseDisp(rl_address.low_reg, 0, rl_result.low_reg, size, INVALID_SREG);
350 StoreValue(rl_dest, rl_result);
351 }
352 return true;
353}
354
355bool X86Mir2Lir::GenInlinedPoke(CallInfo* info, OpSize size) {
356 RegLocation rl_src_address = info->args[0]; // long address
357 rl_src_address.wide = 0; // ignore high half in info->args[1]
358 RegLocation rl_src_value = info->args[2]; // [size] value
359 RegLocation rl_address = LoadValue(rl_src_address, kCoreReg);
360 if (size == kLong) {
361 // Unaligned access is allowed on x86.
362 RegLocation rl_value = LoadValueWide(rl_src_value, kCoreReg);
363 StoreBaseDispWide(rl_address.low_reg, 0, rl_value.low_reg, rl_value.high_reg);
364 } else {
365 DCHECK(size == kSignedByte || size == kSignedHalf || size == kWord);
366 // Unaligned access is allowed on x86.
367 RegLocation rl_value = LoadValue(rl_src_value, kCoreReg);
368 StoreBaseDisp(rl_address.low_reg, 0, rl_value.low_reg, size);
369 }
370 return true;
371}
372
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700373void X86Mir2Lir::OpLea(int rBase, int reg1, int reg2, int scale, int offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 NewLIR5(kX86Lea32RA, rBase, reg1, reg2, scale, offset);
375}
376
Ian Rogers468532e2013-08-05 10:56:33 -0700377void X86Mir2Lir::OpTlsCmp(ThreadOffset offset, int val) {
378 NewLIR2(kX86Cmp16TI8, offset.Int32Value(), val);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700379}
380
Vladimir Marko1c282e22013-11-21 14:49:47 +0000381bool X86Mir2Lir::GenInlinedCas(CallInfo* info, bool is_long, bool is_object) {
Vladimir Markoc29bb612013-11-27 16:47:25 +0000382 DCHECK_EQ(cu_->instruction_set, kX86);
383 // Unused - RegLocation rl_src_unsafe = info->args[0];
384 RegLocation rl_src_obj = info->args[1]; // Object - known non-null
385 RegLocation rl_src_offset = info->args[2]; // long low
386 rl_src_offset.wide = 0; // ignore high half in info->args[3]
387 RegLocation rl_src_expected = info->args[4]; // int, long or Object
388 // If is_long, high half is in info->args[5]
389 RegLocation rl_src_new_value = info->args[is_long ? 6 : 5]; // int, long or Object
390 // If is_long, high half is in info->args[7]
391
392 if (is_long) {
Vladimir Marko70b797d2013-12-03 15:25:24 +0000393 FlushAllRegs();
394 LockCallTemps();
Vladimir Markoa6fd8ba2013-12-13 10:53:49 +0000395 LoadValueDirectWideFixed(rl_src_expected, rAX, rDX);
396 LoadValueDirectWideFixed(rl_src_new_value, rBX, rCX);
Vladimir Marko70b797d2013-12-03 15:25:24 +0000397 NewLIR1(kX86Push32R, rDI);
398 MarkTemp(rDI);
399 LockTemp(rDI);
400 NewLIR1(kX86Push32R, rSI);
401 MarkTemp(rSI);
402 LockTemp(rSI);
Vladimir Markoa6fd8ba2013-12-13 10:53:49 +0000403 const int push_offset = 4 /* push edi */ + 4 /* push esi */;
404 LoadWordDisp(TargetReg(kSp), SRegOffset(rl_src_obj.s_reg_low) + push_offset, rDI);
405 LoadWordDisp(TargetReg(kSp), SRegOffset(rl_src_offset.s_reg_low) + push_offset, rSI);
Vladimir Marko70b797d2013-12-03 15:25:24 +0000406 NewLIR4(kX86LockCmpxchg8bA, rDI, rSI, 0, 0);
407 FreeTemp(rSI);
408 UnmarkTemp(rSI);
409 NewLIR1(kX86Pop32R, rSI);
410 FreeTemp(rDI);
411 UnmarkTemp(rDI);
412 NewLIR1(kX86Pop32R, rDI);
413 FreeCallTemps();
Vladimir Markoc29bb612013-11-27 16:47:25 +0000414 } else {
415 // EAX must hold expected for CMPXCHG. Neither rl_new_value, nor r_ptr may be in EAX.
416 FlushReg(r0);
417 LockTemp(r0);
418
419 // Release store semantics, get the barrier out of the way. TODO: revisit
420 GenMemBarrier(kStoreLoad);
421
422 RegLocation rl_object = LoadValue(rl_src_obj, kCoreReg);
423 RegLocation rl_new_value = LoadValue(rl_src_new_value, kCoreReg);
424
425 if (is_object && !mir_graph_->IsConstantNullRef(rl_new_value)) {
426 // Mark card for object assuming new value is stored.
427 FreeTemp(r0); // Temporarily release EAX for MarkGCCard().
428 MarkGCCard(rl_new_value.low_reg, rl_object.low_reg);
429 LockTemp(r0);
430 }
431
432 RegLocation rl_offset = LoadValue(rl_src_offset, kCoreReg);
433 LoadValueDirect(rl_src_expected, r0);
434 NewLIR5(kX86LockCmpxchgAR, rl_object.low_reg, rl_offset.low_reg, 0, 0, rl_new_value.low_reg);
435
436 FreeTemp(r0);
437 }
438
439 // Convert ZF to boolean
440 RegLocation rl_dest = InlineTarget(info); // boolean place for result
441 RegLocation rl_result = EvalLoc(rl_dest, kCoreReg, true);
442 NewLIR2(kX86Set8R, rl_result.low_reg, kX86CondZ);
443 NewLIR2(kX86Movzx8RR, rl_result.low_reg, rl_result.low_reg);
444 StoreValue(rl_dest, rl_result);
445 return true;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700446}
447
448LIR* X86Mir2Lir::OpPcRelLoad(int reg, LIR* target) {
449 LOG(FATAL) << "Unexpected use of OpPcRelLoad for x86";
450 return NULL;
451}
452
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700453LIR* X86Mir2Lir::OpVldm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700454 LOG(FATAL) << "Unexpected use of OpVldm for x86";
455 return NULL;
456}
457
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700458LIR* X86Mir2Lir::OpVstm(int rBase, int count) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700459 LOG(FATAL) << "Unexpected use of OpVstm for x86";
460 return NULL;
461}
462
463void X86Mir2Lir::GenMultiplyByTwoBitMultiplier(RegLocation rl_src,
464 RegLocation rl_result, int lit,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700465 int first_bit, int second_bit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700466 int t_reg = AllocTemp();
467 OpRegRegImm(kOpLsl, t_reg, rl_src.low_reg, second_bit - first_bit);
468 OpRegRegReg(kOpAdd, rl_result.low_reg, rl_src.low_reg, t_reg);
469 FreeTemp(t_reg);
470 if (first_bit != 0) {
471 OpRegRegImm(kOpLsl, rl_result.low_reg, rl_result.low_reg, first_bit);
472 }
473}
474
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700475void X86Mir2Lir::GenDivZeroCheck(int reg_lo, int reg_hi) {
Razvan A Lupusoru090dd442013-12-20 14:35:03 -0800476 // We are not supposed to clobber either of the provided registers, so allocate
477 // a temporary to use for the check.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700478 int t_reg = AllocTemp();
Razvan A Lupusoru090dd442013-12-20 14:35:03 -0800479
480 // Doing an OR is a quick way to check if both registers are zero. This will set the flags.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 OpRegRegReg(kOpOr, t_reg, reg_lo, reg_hi);
Razvan A Lupusoru090dd442013-12-20 14:35:03 -0800482
483 // In case of zero, throw ArithmeticException.
484 GenCheck(kCondEq, kThrowDivZero);
485
486 // The temp is no longer needed so free it at this time.
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487 FreeTemp(t_reg);
488}
489
490// Test suspend flag, return target of taken suspend branch
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700491LIR* X86Mir2Lir::OpTestSuspend(LIR* target) {
Ian Rogers468532e2013-08-05 10:56:33 -0700492 OpTlsCmp(Thread::ThreadFlagsOffset(), 0);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700493 return OpCondBranch((target == NULL) ? kCondNe : kCondEq, target);
494}
495
496// Decrement register and branch on condition
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700497LIR* X86Mir2Lir::OpDecAndBranch(ConditionCode c_code, int reg, LIR* target) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 OpRegImm(kOpSub, reg, 1);
499 return OpCmpImmBranch(c_code, reg, 0, target);
500}
501
buzbee11b63d12013-08-27 07:34:17 -0700502bool X86Mir2Lir::SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700503 RegLocation rl_src, RegLocation rl_dest, int lit) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700504 LOG(FATAL) << "Unexpected use of smallLiteralDive in x86";
505 return false;
506}
507
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700508LIR* X86Mir2Lir::OpIT(ConditionCode cond, const char* guide) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700509 LOG(FATAL) << "Unexpected use of OpIT in x86";
510 return NULL;
511}
512
513void X86Mir2Lir::GenMulLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700514 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700515 LOG(FATAL) << "Unexpected use of GenX86Long for x86";
516}
517void X86Mir2Lir::GenAddLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700518 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700519 // TODO: fixed register usage here as we only have 4 temps and temporary allocation isn't smart
520 // enough.
521 FlushAllRegs();
522 LockCallTemps(); // Prepare for explicit register usage
523 LoadValueDirectWideFixed(rl_src1, r0, r1);
524 LoadValueDirectWideFixed(rl_src2, r2, r3);
525 // Compute (r1:r0) = (r1:r0) + (r2:r3)
526 OpRegReg(kOpAdd, r0, r2); // r0 = r0 + r2
527 OpRegReg(kOpAdc, r1, r3); // r1 = r1 + r3 + CF
Bill Buzbee8ff67e32014-01-11 20:46:07 +0000528 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, r0, r1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700529 INVALID_SREG, INVALID_SREG};
530 StoreValueWide(rl_dest, rl_result);
531}
532
533void X86Mir2Lir::GenSubLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700534 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700535 // TODO: fixed register usage here as we only have 4 temps and temporary allocation isn't smart
536 // enough.
537 FlushAllRegs();
538 LockCallTemps(); // Prepare for explicit register usage
539 LoadValueDirectWideFixed(rl_src1, r0, r1);
540 LoadValueDirectWideFixed(rl_src2, r2, r3);
541 // Compute (r1:r0) = (r1:r0) + (r2:r3)
542 OpRegReg(kOpSub, r0, r2); // r0 = r0 - r2
543 OpRegReg(kOpSbc, r1, r3); // r1 = r1 - r3 - CF
Bill Buzbee8ff67e32014-01-11 20:46:07 +0000544 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, r0, r1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700545 INVALID_SREG, INVALID_SREG};
546 StoreValueWide(rl_dest, rl_result);
547}
548
549void X86Mir2Lir::GenAndLong(RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700550 RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 // TODO: fixed register usage here as we only have 4 temps and temporary allocation isn't smart
552 // enough.
553 FlushAllRegs();
554 LockCallTemps(); // Prepare for explicit register usage
555 LoadValueDirectWideFixed(rl_src1, r0, r1);
556 LoadValueDirectWideFixed(rl_src2, r2, r3);
557 // Compute (r1:r0) = (r1:r0) & (r2:r3)
558 OpRegReg(kOpAnd, r0, r2); // r0 = r0 & r2
559 OpRegReg(kOpAnd, r1, r3); // r1 = r1 & r3
Bill Buzbee8ff67e32014-01-11 20:46:07 +0000560 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, r0, r1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700561 INVALID_SREG, INVALID_SREG};
562 StoreValueWide(rl_dest, rl_result);
563}
564
565void X86Mir2Lir::GenOrLong(RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700566 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700567 // TODO: fixed register usage here as we only have 4 temps and temporary allocation isn't smart
568 // enough.
569 FlushAllRegs();
570 LockCallTemps(); // Prepare for explicit register usage
571 LoadValueDirectWideFixed(rl_src1, r0, r1);
572 LoadValueDirectWideFixed(rl_src2, r2, r3);
573 // Compute (r1:r0) = (r1:r0) | (r2:r3)
574 OpRegReg(kOpOr, r0, r2); // r0 = r0 | r2
575 OpRegReg(kOpOr, r1, r3); // r1 = r1 | r3
Bill Buzbee8ff67e32014-01-11 20:46:07 +0000576 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, r0, r1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700577 INVALID_SREG, INVALID_SREG};
578 StoreValueWide(rl_dest, rl_result);
579}
580
581void X86Mir2Lir::GenXorLong(RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700582 RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583 // TODO: fixed register usage here as we only have 4 temps and temporary allocation isn't smart
584 // enough.
585 FlushAllRegs();
586 LockCallTemps(); // Prepare for explicit register usage
587 LoadValueDirectWideFixed(rl_src1, r0, r1);
588 LoadValueDirectWideFixed(rl_src2, r2, r3);
589 // Compute (r1:r0) = (r1:r0) ^ (r2:r3)
590 OpRegReg(kOpXor, r0, r2); // r0 = r0 ^ r2
591 OpRegReg(kOpXor, r1, r3); // r1 = r1 ^ r3
Bill Buzbee8ff67e32014-01-11 20:46:07 +0000592 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, r0, r1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700593 INVALID_SREG, INVALID_SREG};
594 StoreValueWide(rl_dest, rl_result);
595}
596
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700597void X86Mir2Lir::GenNegLong(RegLocation rl_dest, RegLocation rl_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598 FlushAllRegs();
599 LockCallTemps(); // Prepare for explicit register usage
600 LoadValueDirectWideFixed(rl_src, r0, r1);
601 // Compute (r1:r0) = -(r1:r0)
602 OpRegReg(kOpNeg, r0, r0); // r0 = -r0
603 OpRegImm(kOpAdc, r1, 0); // r1 = r1 + CF
604 OpRegReg(kOpNeg, r1, r1); // r1 = -r1
Bill Buzbee8ff67e32014-01-11 20:46:07 +0000605 RegLocation rl_result = {kLocPhysReg, 1, 0, 0, 0, 0, 0, 0, 1, r0, r1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700606 INVALID_SREG, INVALID_SREG};
607 StoreValueWide(rl_dest, rl_result);
608}
609
Ian Rogers468532e2013-08-05 10:56:33 -0700610void X86Mir2Lir::OpRegThreadMem(OpKind op, int r_dest, ThreadOffset thread_offset) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700611 X86OpCode opcode = kX86Bkpt;
612 switch (op) {
613 case kOpCmp: opcode = kX86Cmp32RT; break;
614 case kOpMov: opcode = kX86Mov32RT; break;
615 default:
616 LOG(FATAL) << "Bad opcode: " << op;
617 break;
618 }
Ian Rogers468532e2013-08-05 10:56:33 -0700619 NewLIR2(opcode, r_dest, thread_offset.Int32Value());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700620}
621
622/*
623 * Generate array load
624 */
625void X86Mir2Lir::GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700626 RegLocation rl_index, RegLocation rl_dest, int scale) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700627 RegisterClass reg_class = oat_reg_class_by_size(size);
628 int len_offset = mirror::Array::LengthOffset().Int32Value();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700629 RegLocation rl_result;
630 rl_array = LoadValue(rl_array, kCoreReg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700631
Mark Mendell343adb52013-12-18 06:02:17 -0800632 int data_offset;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700633 if (size == kLong || size == kDouble) {
634 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
635 } else {
636 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
637 }
638
Mark Mendell343adb52013-12-18 06:02:17 -0800639 bool constant_index = rl_index.is_const;
640 int32_t constant_index_value = 0;
641 if (!constant_index) {
642 rl_index = LoadValue(rl_index, kCoreReg);
643 } else {
644 constant_index_value = mir_graph_->ConstantValue(rl_index);
645 // If index is constant, just fold it into the data offset
646 data_offset += constant_index_value << scale;
647 // treat as non array below
648 rl_index.low_reg = INVALID_REG;
649 }
650
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 /* null object? */
652 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
653
654 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -0800655 if (constant_index) {
656 GenMemImmedCheck(kCondLs, rl_array.low_reg, len_offset,
657 constant_index_value, kThrowConstantArrayBounds);
658 } else {
659 GenRegMemCheck(kCondUge, rl_index.low_reg, rl_array.low_reg,
660 len_offset, kThrowArrayBounds);
661 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700662 }
Mark Mendell343adb52013-12-18 06:02:17 -0800663 rl_result = EvalLoc(rl_dest, reg_class, true);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700664 if ((size == kLong) || (size == kDouble)) {
Mark Mendell343adb52013-12-18 06:02:17 -0800665 LoadBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, data_offset, rl_result.low_reg,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700666 rl_result.high_reg, size, INVALID_SREG);
667 StoreValueWide(rl_dest, rl_result);
668 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700669 LoadBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale,
670 data_offset, rl_result.low_reg, INVALID_REG, size,
671 INVALID_SREG);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700672 StoreValue(rl_dest, rl_result);
673 }
674}
675
676/*
677 * Generate array store
678 *
679 */
680void X86Mir2Lir::GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -0700681 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700682 RegisterClass reg_class = oat_reg_class_by_size(size);
683 int len_offset = mirror::Array::LengthOffset().Int32Value();
684 int data_offset;
685
686 if (size == kLong || size == kDouble) {
687 data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Int32Value();
688 } else {
689 data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Int32Value();
690 }
691
692 rl_array = LoadValue(rl_array, kCoreReg);
Mark Mendell343adb52013-12-18 06:02:17 -0800693 bool constant_index = rl_index.is_const;
694 int32_t constant_index_value = 0;
695 if (!constant_index) {
696 rl_index = LoadValue(rl_index, kCoreReg);
697 } else {
698 // If index is constant, just fold it into the data offset
699 constant_index_value = mir_graph_->ConstantValue(rl_index);
700 data_offset += constant_index_value << scale;
701 // treat as non array below
702 rl_index.low_reg = INVALID_REG;
703 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700704
705 /* null object? */
706 GenNullCheck(rl_array.s_reg_low, rl_array.low_reg, opt_flags);
707
708 if (!(opt_flags & MIR_IGNORE_RANGE_CHECK)) {
Mark Mendell343adb52013-12-18 06:02:17 -0800709 if (constant_index) {
710 GenMemImmedCheck(kCondLs, rl_array.low_reg, len_offset,
711 constant_index_value, kThrowConstantArrayBounds);
712 } else {
713 GenRegMemCheck(kCondUge, rl_index.low_reg, rl_array.low_reg,
714 len_offset, kThrowArrayBounds);
715 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700716 }
717 if ((size == kLong) || (size == kDouble)) {
718 rl_src = LoadValueWide(rl_src, reg_class);
719 } else {
720 rl_src = LoadValue(rl_src, reg_class);
721 }
722 // If the src reg can't be byte accessed, move it to a temp first.
723 if ((size == kSignedByte || size == kUnsignedByte) && rl_src.low_reg >= 4) {
724 int temp = AllocTemp();
725 OpRegCopy(temp, rl_src.low_reg);
726 StoreBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, data_offset, temp,
727 INVALID_REG, size, INVALID_SREG);
728 } else {
729 StoreBaseIndexedDisp(rl_array.low_reg, rl_index.low_reg, scale, data_offset, rl_src.low_reg,
730 rl_src.high_reg, size, INVALID_SREG);
731 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700732 if (card_mark) {
Ian Rogers773aab12013-10-14 13:50:10 -0700733 // Free rl_index if its a temp. Ensures there are 2 free regs for card mark.
Mark Mendell343adb52013-12-18 06:02:17 -0800734 if (!constant_index) {
735 FreeTemp(rl_index.low_reg);
736 }
Ian Rogersa9a82542013-10-04 11:17:26 -0700737 MarkGCCard(rl_src.low_reg, rl_array.low_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700738 }
739}
740
741void X86Mir2Lir::GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700742 RegLocation rl_src1, RegLocation rl_shift) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700743 // Default implementation is just to ignore the constant case.
744 GenShiftOpLong(opcode, rl_dest, rl_src1, rl_shift);
745}
746
747void X86Mir2Lir::GenArithImmOpLong(Instruction::Code opcode,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700748 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700749 // Default - bail to non-const handler.
750 GenArithOpLong(opcode, rl_dest, rl_src1, rl_src2);
751}
752
753} // namespace art