blob: e1054db201ccfe3b8e63a719a247122030677670 [file] [log] [blame]
buzbee31a4a6f2012-02-28 15:36:15 -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
Ian Rogers57b86d42012-03-27 16:05:41 -070017#include "oat/runtime/oat_support_entrypoints.h"
buzbee1bc37c62012-11-20 13:35:41 -080018#include "../compiler_ir.h"
19#include "ralloc_util.h"
20#include "codegen_util.h"
Ian Rogers57b86d42012-03-27 16:05:41 -070021
buzbee31a4a6f2012-02-28 15:36:15 -080022namespace art {
23
24/*
25 * This source files contains "gen" codegen routines that should
26 * be applicable to most targets. Only mid-level support utilities
27 * and "op" calls may be used here.
28 */
buzbee31a4a6f2012-02-28 15:36:15 -080029
buzbee31a4a6f2012-02-28 15:36:15 -080030/*
31 * Generate an kPseudoBarrier marker to indicate the boundary of special
32 * blocks.
33 */
buzbee02031b12012-11-23 09:41:35 -080034void Codegen::GenBarrier(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -080035{
buzbeefa57c472012-11-21 12:06:18 -080036 LIR* barrier = NewLIR0(cu, kPseudoBarrier);
Bill Buzbeea114add2012-05-03 15:00:40 -070037 /* Mark all resources as being clobbered */
buzbeefa57c472012-11-21 12:06:18 -080038 barrier->def_mask = -1;
buzbee31a4a6f2012-02-28 15:36:15 -080039}
40
buzbee5de34942012-03-01 14:51:57 -080041// FIXME: need to do some work to split out targets with
42// condition codes and those without
buzbee02031b12012-11-23 09:41:35 -080043LIR* Codegen::GenCheck(CompilationUnit* cu, ConditionCode c_code, ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -080044{
buzbeefa57c472012-11-21 12:06:18 -080045 DCHECK_NE(cu->instruction_set, kMips);
46 LIR* tgt = RawLIR(cu, 0, kPseudoThrowTarget, kind,
47 cu->current_dalvik_offset);
48 LIR* branch = OpCondBranch(cu, c_code, tgt);
Bill Buzbeea114add2012-05-03 15:00:40 -070049 // Remember branch target - will process later
buzbeefa57c472012-11-21 12:06:18 -080050 InsertGrowableList(cu, &cu->throw_launchpads, reinterpret_cast<uintptr_t>(tgt));
Bill Buzbeea114add2012-05-03 15:00:40 -070051 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -080052}
53
buzbee02031b12012-11-23 09:41:35 -080054LIR* Codegen::GenImmedCheck(CompilationUnit* cu, ConditionCode c_code, int reg, int imm_val,
55 ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -080056{
buzbeefa57c472012-11-21 12:06:18 -080057 LIR* tgt = RawLIR(cu, 0, kPseudoThrowTarget, kind,
58 cu->current_dalvik_offset);
Bill Buzbeea114add2012-05-03 15:00:40 -070059 LIR* branch;
buzbeefa57c472012-11-21 12:06:18 -080060 if (c_code == kCondAl) {
61 branch = OpUnconditionalBranch(cu, tgt);
Bill Buzbeea114add2012-05-03 15:00:40 -070062 } else {
buzbeefa57c472012-11-21 12:06:18 -080063 branch = OpCmpImmBranch(cu, c_code, reg, imm_val, tgt);
Bill Buzbeea114add2012-05-03 15:00:40 -070064 }
65 // Remember branch target - will process later
buzbeefa57c472012-11-21 12:06:18 -080066 InsertGrowableList(cu, &cu->throw_launchpads, reinterpret_cast<uintptr_t>(tgt));
Bill Buzbeea114add2012-05-03 15:00:40 -070067 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -080068}
69
70/* Perform null-check on a register. */
buzbee02031b12012-11-23 09:41:35 -080071LIR* Codegen::GenNullCheck(CompilationUnit* cu, int s_reg, int m_reg, int opt_flags)
buzbee31a4a6f2012-02-28 15:36:15 -080072{
buzbeefa57c472012-11-21 12:06:18 -080073 if (!(cu->disable_opt & (1 << kNullCheckElimination)) &&
74 opt_flags & MIR_IGNORE_NULL_CHECK) {
Bill Buzbeea114add2012-05-03 15:00:40 -070075 return NULL;
76 }
buzbeefa57c472012-11-21 12:06:18 -080077 return GenImmedCheck(cu, kCondEq, m_reg, 0, kThrowNullPointer);
buzbee31a4a6f2012-02-28 15:36:15 -080078}
79
80/* Perform check on two registers */
buzbee02031b12012-11-23 09:41:35 -080081LIR* Codegen::GenRegRegCheck(CompilationUnit* cu, ConditionCode c_code, int reg1, int reg2,
82 ThrowKind kind)
buzbee31a4a6f2012-02-28 15:36:15 -080083{
buzbeefa57c472012-11-21 12:06:18 -080084 LIR* tgt = RawLIR(cu, 0, kPseudoThrowTarget, kind,
85 cu->current_dalvik_offset, reg1, reg2);
86 LIR* branch = OpCmpBranch(cu, c_code, reg1, reg2, tgt);
Bill Buzbeea114add2012-05-03 15:00:40 -070087 // Remember branch target - will process later
buzbeefa57c472012-11-21 12:06:18 -080088 InsertGrowableList(cu, &cu->throw_launchpads, reinterpret_cast<uintptr_t>(tgt));
Bill Buzbeea114add2012-05-03 15:00:40 -070089 return branch;
buzbee31a4a6f2012-02-28 15:36:15 -080090}
91
buzbeee6285f92012-12-06 15:57:46 -080092// Convert relation of src1/src2 to src2/src1
93ConditionCode FlipComparisonOrder(ConditionCode before) {
94 ConditionCode res;
95 switch (before) {
96 case kCondEq: res = kCondEq; break;
97 case kCondNe: res = kCondNe; break;
98 case kCondLt: res = kCondGt; break;
99 case kCondGt: res = kCondLt; break;
100 case kCondLe: res = kCondGe; break;
101 case kCondGe: res = kCondLe; break;
102 default:
103 res = static_cast<ConditionCode>(0);
104 LOG(FATAL) << "Unexpected ccode " << before;
105 }
106 return res;
107}
108
buzbee02031b12012-11-23 09:41:35 -0800109void Codegen::GenCompareAndBranch(CompilationUnit* cu, Instruction::Code opcode,
110 RegLocation rl_src1, RegLocation rl_src2, LIR* taken,
111 LIR* fall_through)
buzbee31a4a6f2012-02-28 15:36:15 -0800112{
Bill Buzbeea114add2012-05-03 15:00:40 -0700113 ConditionCode cond;
Bill Buzbeea114add2012-05-03 15:00:40 -0700114 switch (opcode) {
115 case Instruction::IF_EQ:
116 cond = kCondEq;
117 break;
118 case Instruction::IF_NE:
119 cond = kCondNe;
120 break;
121 case Instruction::IF_LT:
122 cond = kCondLt;
123 break;
124 case Instruction::IF_GE:
125 cond = kCondGe;
126 break;
127 case Instruction::IF_GT:
128 cond = kCondGt;
129 break;
130 case Instruction::IF_LE:
131 cond = kCondLe;
132 break;
133 default:
buzbeecbd6d442012-11-17 14:11:25 -0800134 cond = static_cast<ConditionCode>(0);
135 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -0700136 }
buzbeee6285f92012-12-06 15:57:46 -0800137
138 // Normalize such that if either operand is constant, src2 will be constant
139 if (rl_src1.is_const) {
140 RegLocation rl_temp = rl_src1;
141 rl_src1 = rl_src2;
142 rl_src2 = rl_temp;
143 cond = FlipComparisonOrder(cond);
144 }
145
146 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
147 // Is this really an immediate comparison?
148 if (rl_src2.is_const) {
149 int immval = cu->constant_values[rl_src2.orig_sreg];
150 // If it's already live in a register or not easily materialized, just keep going
151 RegLocation rl_temp = UpdateLoc(cu, rl_src2);
152 if ((rl_temp.location == kLocDalvikFrame) && InexpensiveConstant(rl_src1.low_reg, immval)) {
153 // OK - convert this to a compare immediate and branch
154 OpCmpImmBranch(cu, cond, rl_src1.low_reg, immval, taken);
155 OpUnconditionalBranch(cu, fall_through);
156 return;
157 }
158 }
159 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -0800160 OpCmpBranch(cu, cond, rl_src1.low_reg, rl_src2.low_reg, taken);
161 OpUnconditionalBranch(cu, fall_through);
buzbee31a4a6f2012-02-28 15:36:15 -0800162}
163
buzbee02031b12012-11-23 09:41:35 -0800164void Codegen::GenCompareZeroAndBranch(CompilationUnit* cu, Instruction::Code opcode,
165 RegLocation rl_src, LIR* taken, LIR* fall_through)
buzbee31a4a6f2012-02-28 15:36:15 -0800166{
Bill Buzbeea114add2012-05-03 15:00:40 -0700167 ConditionCode cond;
buzbeefa57c472012-11-21 12:06:18 -0800168 rl_src = LoadValue(cu, rl_src, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700169 switch (opcode) {
170 case Instruction::IF_EQZ:
171 cond = kCondEq;
172 break;
173 case Instruction::IF_NEZ:
174 cond = kCondNe;
175 break;
176 case Instruction::IF_LTZ:
177 cond = kCondLt;
178 break;
179 case Instruction::IF_GEZ:
180 cond = kCondGe;
181 break;
182 case Instruction::IF_GTZ:
183 cond = kCondGt;
184 break;
185 case Instruction::IF_LEZ:
186 cond = kCondLe;
187 break;
188 default:
buzbeecbd6d442012-11-17 14:11:25 -0800189 cond = static_cast<ConditionCode>(0);
190 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -0700191 }
buzbeee6285f92012-12-06 15:57:46 -0800192 OpCmpImmBranch(cu, cond, rl_src.low_reg, 0, taken);
buzbeefa57c472012-11-21 12:06:18 -0800193 OpUnconditionalBranch(cu, fall_through);
buzbee31a4a6f2012-02-28 15:36:15 -0800194}
195
buzbee02031b12012-11-23 09:41:35 -0800196void Codegen::GenIntToLong(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800197{
buzbeefa57c472012-11-21 12:06:18 -0800198 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
199 if (rl_src.location == kLocPhysReg) {
200 OpRegCopy(cu, rl_result.low_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700201 } else {
buzbeefa57c472012-11-21 12:06:18 -0800202 LoadValueDirect(cu, rl_src, rl_result.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700203 }
buzbeefa57c472012-11-21 12:06:18 -0800204 OpRegRegImm(cu, kOpAsr, rl_result.high_reg, rl_result.low_reg, 31);
205 StoreValueWide(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800206}
207
buzbee02031b12012-11-23 09:41:35 -0800208void Codegen::GenIntNarrowing(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
209 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800210{
buzbeefa57c472012-11-21 12:06:18 -0800211 rl_src = LoadValue(cu, rl_src, kCoreReg);
212 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700213 OpKind op = kOpInvalid;
buzbee408ad162012-06-06 16:45:18 -0700214 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700215 case Instruction::INT_TO_BYTE:
216 op = kOp2Byte;
217 break;
218 case Instruction::INT_TO_SHORT:
219 op = kOp2Short;
220 break;
221 case Instruction::INT_TO_CHAR:
222 op = kOp2Char;
223 break;
224 default:
225 LOG(ERROR) << "Bad int conversion type";
226 }
buzbeefa57c472012-11-21 12:06:18 -0800227 OpRegReg(cu, op, rl_result.low_reg, rl_src.low_reg);
228 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800229}
230
231/*
232 * Let helper function take care of everything. Will call
233 * Array::AllocFromCode(type_idx, method, count);
234 * Note: AllocFromCode will handle checks for errNegativeArraySize.
235 */
buzbee02031b12012-11-23 09:41:35 -0800236void Codegen::GenNewArray(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest,
237 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800238{
buzbeefa57c472012-11-21 12:06:18 -0800239 FlushAllRegs(cu); /* Everything to home location */
240 int func_offset;
241 if (cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
242 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700243 type_idx)) {
buzbeefa57c472012-11-21 12:06:18 -0800244 func_offset = ENTRYPOINT_OFFSET(pAllocArrayFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700245 } else {
buzbeefa57c472012-11-21 12:06:18 -0800246 func_offset= ENTRYPOINT_OFFSET(pAllocArrayFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700247 }
buzbeefa57c472012-11-21 12:06:18 -0800248 CallRuntimeHelperImmMethodRegLocation(cu, func_offset, type_idx, rl_src, true);
249 RegLocation rl_result = GetReturn(cu, false);
250 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800251}
252
253/*
buzbee52a77fc2012-11-20 19:50:46 -0800254 * Similar to GenNewArray, but with post-allocation initialization.
buzbee31a4a6f2012-02-28 15:36:15 -0800255 * Verifier guarantees we're dealing with an array class. Current
256 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
257 * Current code also throws internal unimp if not 'L', '[' or 'I'.
258 */
buzbee02031b12012-11-23 09:41:35 -0800259void Codegen::GenFilledNewArray(CompilationUnit* cu, CallInfo* info)
buzbee31a4a6f2012-02-28 15:36:15 -0800260{
buzbeefa57c472012-11-21 12:06:18 -0800261 int elems = info->num_arg_words;
262 int type_idx = info->index;
263 FlushAllRegs(cu); /* Everything to home location */
264 int func_offset;
265 if (cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
266 *cu->dex_file,
267 type_idx)) {
268 func_offset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700269 } else {
buzbeefa57c472012-11-21 12:06:18 -0800270 func_offset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700271 }
buzbeefa57c472012-11-21 12:06:18 -0800272 CallRuntimeHelperImmMethodImm(cu, func_offset, type_idx, elems, true);
273 FreeTemp(cu, TargetReg(kArg2));
274 FreeTemp(cu, TargetReg(kArg1));
Bill Buzbeea114add2012-05-03 15:00:40 -0700275 /*
276 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
277 * return region. Because AllocFromCode placed the new array
buzbeef0504cd2012-11-13 16:31:10 -0800278 * in kRet0, we'll just lock it into place. When debugger support is
Bill Buzbeea114add2012-05-03 15:00:40 -0700279 * added, it may be necessary to additionally copy all return
280 * values to a home location in thread-local storage
281 */
buzbeefa57c472012-11-21 12:06:18 -0800282 LockTemp(cu, TargetReg(kRet0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700283
284 // TODO: use the correct component size, currently all supported types
285 // share array alignment with ints (see comment at head of function)
286 size_t component_size = sizeof(int32_t);
287
288 // Having a range of 0 is legal
buzbeefa57c472012-11-21 12:06:18 -0800289 if (info->is_range && (elems > 0)) {
buzbee31a4a6f2012-02-28 15:36:15 -0800290 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700291 * Bit of ugliness here. We're going generate a mem copy loop
292 * on the register range, but it is possible that some regs
293 * in the range have been promoted. This is unlikely, but
294 * before generating the copy, we'll just force a flush
295 * of any regs in the source range that have been promoted to
296 * home location.
buzbee31a4a6f2012-02-28 15:36:15 -0800297 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700298 for (int i = 0; i < elems; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800299 RegLocation loc = UpdateLoc(cu, info->args[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700300 if (loc.location == kLocPhysReg) {
buzbeefa57c472012-11-21 12:06:18 -0800301 StoreBaseDisp(cu, TargetReg(kSp), SRegOffset(cu, loc.s_reg_low),
302 loc.low_reg, kWord);
Bill Buzbeea114add2012-05-03 15:00:40 -0700303 }
buzbee31a4a6f2012-02-28 15:36:15 -0800304 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700305 /*
306 * TUNING note: generated code here could be much improved, but
307 * this is an uncommon operation and isn't especially performance
308 * critical.
309 */
buzbeefa57c472012-11-21 12:06:18 -0800310 int r_src = AllocTemp(cu);
311 int r_dst = AllocTemp(cu);
312 int r_idx = AllocTemp(cu);
313 int r_val = INVALID_REG;
314 switch(cu->instruction_set) {
buzbeeb046e162012-10-30 15:48:42 -0700315 case kThumb2:
buzbeefa57c472012-11-21 12:06:18 -0800316 r_val = TargetReg(kLr);
buzbeeb046e162012-10-30 15:48:42 -0700317 break;
318 case kX86:
buzbeefa57c472012-11-21 12:06:18 -0800319 FreeTemp(cu, TargetReg(kRet0));
320 r_val = AllocTemp(cu);
buzbeeb046e162012-10-30 15:48:42 -0700321 break;
322 case kMips:
buzbeefa57c472012-11-21 12:06:18 -0800323 r_val = AllocTemp(cu);
buzbeeb046e162012-10-30 15:48:42 -0700324 break;
buzbeefa57c472012-11-21 12:06:18 -0800325 default: LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
buzbeeb046e162012-10-30 15:48:42 -0700326 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700327 // Set up source pointer
buzbeefa57c472012-11-21 12:06:18 -0800328 RegLocation rl_first = info->args[0];
329 OpRegRegImm(cu, kOpAdd, r_src, TargetReg(kSp),
330 SRegOffset(cu, rl_first.s_reg_low));
Bill Buzbeea114add2012-05-03 15:00:40 -0700331 // Set up the target pointer
buzbeefa57c472012-11-21 12:06:18 -0800332 OpRegRegImm(cu, kOpAdd, r_dst, TargetReg(kRet0),
Bill Buzbeea114add2012-05-03 15:00:40 -0700333 Array::DataOffset(component_size).Int32Value());
334 // Set up the loop counter (known to be > 0)
buzbeefa57c472012-11-21 12:06:18 -0800335 LoadConstant(cu, r_idx, elems - 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700336 // Generate the copy loop. Going backwards for convenience
buzbeefa57c472012-11-21 12:06:18 -0800337 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -0700338 // Copy next element
buzbeefa57c472012-11-21 12:06:18 -0800339 LoadBaseIndexed(cu, r_src, r_idx, r_val, 2, kWord);
340 StoreBaseIndexed(cu, r_dst, r_idx, r_val, 2, kWord);
341 FreeTemp(cu, r_val);
342 OpDecAndBranch(cu, kCondGe, r_idx, target);
343 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700344 // Restore the target pointer
buzbeefa57c472012-11-21 12:06:18 -0800345 OpRegRegImm(cu, kOpAdd, TargetReg(kRet0), r_dst, -Array::DataOffset(component_size).Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700346 }
buzbeefa57c472012-11-21 12:06:18 -0800347 } else if (!info->is_range) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700348 // TUNING: interleave
buzbee3b3dbdd2012-06-13 13:39:34 -0700349 for (int i = 0; i < elems; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800350 RegLocation rl_arg = LoadValue(cu, info->args[i], kCoreReg);
351 StoreBaseDisp(cu, TargetReg(kRet0),
Bill Buzbeea114add2012-05-03 15:00:40 -0700352 Array::DataOffset(component_size).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800353 i * 4, rl_arg.low_reg, kWord);
buzbee52a77fc2012-11-20 19:50:46 -0800354 // If the LoadValue caused a temp to be allocated, free it
buzbeefa57c472012-11-21 12:06:18 -0800355 if (IsTemp(cu, rl_arg.low_reg)) {
356 FreeTemp(cu, rl_arg.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700357 }
358 }
359 }
buzbeee5f01222012-06-14 15:19:35 -0700360 if (info->result.location != kLocInvalid) {
buzbeefa57c472012-11-21 12:06:18 -0800361 StoreValue(cu, info->result, GetReturn(cu, false /* not fp */));
buzbeee5f01222012-06-14 15:19:35 -0700362 }
buzbee31a4a6f2012-02-28 15:36:15 -0800363}
364
buzbee02031b12012-11-23 09:41:35 -0800365void Codegen::GenSput(CompilationUnit* cu, uint32_t field_idx, RegLocation rl_src,
366 bool is_long_or_double, bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800367{
buzbeefa57c472012-11-21 12:06:18 -0800368 int field_offset;
369 int ssb_index;
370 bool is_volatile;
371 bool is_referrers_class;
buzbee31a4a6f2012-02-28 15:36:15 -0800372
TDYa127dc5daa02013-01-09 21:31:37 +0800373 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker, *cu->dex_file, cu->code_item,
374 cu->class_def_idx, cu->method_idx, cu->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800375
buzbeefa57c472012-11-21 12:06:18 -0800376 bool fast_path =
377 cu->compiler->ComputeStaticFieldInfo(field_idx, &m_unit,
378 field_offset, ssb_index,
379 is_referrers_class, is_volatile,
Bill Buzbeea114add2012-05-03 15:00:40 -0700380 true);
buzbeefa57c472012-11-21 12:06:18 -0800381 if (fast_path && !SLOW_FIELD_PATH) {
382 DCHECK_GE(field_offset, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700383 int rBase;
buzbeefa57c472012-11-21 12:06:18 -0800384 if (is_referrers_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700385 // Fast path, static storage base is this method's class
buzbeefa57c472012-11-21 12:06:18 -0800386 RegLocation rl_method = LoadCurrMethod(cu);
387 rBase = AllocTemp(cu);
388 LoadWordDisp(cu, rl_method.low_reg,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700389 AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
buzbeefa57c472012-11-21 12:06:18 -0800390 if (IsTemp(cu, rl_method.low_reg)) {
391 FreeTemp(cu, rl_method.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 }
buzbee31a4a6f2012-02-28 15:36:15 -0800393 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700394 // Medium path, static storage base in a different class which
395 // requires checks that the other class is initialized.
buzbeefa57c472012-11-21 12:06:18 -0800396 DCHECK_GE(ssb_index, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700397 // May do runtime call so everything to home locations.
buzbeefa57c472012-11-21 12:06:18 -0800398 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700399 // Using fixed register to sync with possible call to runtime
400 // support.
buzbeefa57c472012-11-21 12:06:18 -0800401 int r_method = TargetReg(kArg1);
402 LockTemp(cu, r_method);
403 LoadCurrMethodDirect(cu, r_method);
buzbee52a77fc2012-11-20 19:50:46 -0800404 rBase = TargetReg(kArg0);
buzbeefa57c472012-11-21 12:06:18 -0800405 LockTemp(cu, rBase);
406 LoadWordDisp(cu, r_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700407 AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700408 rBase);
buzbeefa57c472012-11-21 12:06:18 -0800409 LoadWordDisp(cu, rBase,
Bill Buzbeea114add2012-05-03 15:00:40 -0700410 Array::DataOffset(sizeof(Object*)).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800411 sizeof(int32_t*) * ssb_index, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700412 // rBase now points at appropriate static storage base (Class*)
413 // or NULL if not initialized. Check for NULL and call helper if NULL.
414 // TUNING: fast path should fall through
buzbeefa57c472012-11-21 12:06:18 -0800415 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, rBase, 0, NULL);
416 LoadConstant(cu, TargetReg(kArg0), ssb_index);
417 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssb_index, true);
418 if (cu->instruction_set == kMips) {
buzbeef0504cd2012-11-13 16:31:10 -0800419 // For Arm, kRet0 = kArg0 = rBase, for Mips, we need to copy
buzbeefa57c472012-11-21 12:06:18 -0800420 OpRegCopy(cu, rBase, TargetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700421 }
buzbeefa57c472012-11-21 12:06:18 -0800422 LIR* skip_target = NewLIR0(cu, kPseudoTargetLabel);
423 branch_over->target = skip_target;
424 FreeTemp(cu, r_method);
buzbee31a4a6f2012-02-28 15:36:15 -0800425 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 // rBase now holds static storage base
buzbeefa57c472012-11-21 12:06:18 -0800427 if (is_long_or_double) {
428 rl_src = LoadValueWide(cu, rl_src, kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700429 } else {
buzbeefa57c472012-11-21 12:06:18 -0800430 rl_src = LoadValue(cu, rl_src, kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700431 }
buzbeefa57c472012-11-21 12:06:18 -0800432 if (is_volatile) {
433 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700434 }
buzbeefa57c472012-11-21 12:06:18 -0800435 if (is_long_or_double) {
436 StoreBaseDispWide(cu, rBase, field_offset, rl_src.low_reg,
437 rl_src.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700438 } else {
buzbeefa57c472012-11-21 12:06:18 -0800439 StoreWordDisp(cu, rBase, field_offset, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700440 }
buzbeefa57c472012-11-21 12:06:18 -0800441 if (is_volatile) {
442 GenMemBarrier(cu, kStoreLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700443 }
buzbeefa57c472012-11-21 12:06:18 -0800444 if (is_object) {
445 MarkGCCard(cu, rl_src.low_reg, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700446 }
buzbeefa57c472012-11-21 12:06:18 -0800447 FreeTemp(cu, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700448 } else {
buzbeefa57c472012-11-21 12:06:18 -0800449 FlushAllRegs(cu); // Everything to home locations
450 int setter_offset = is_long_or_double ? ENTRYPOINT_OFFSET(pSet64Static) :
451 (is_object ? ENTRYPOINT_OFFSET(pSetObjStatic)
Bill Buzbeea114add2012-05-03 15:00:40 -0700452 : ENTRYPOINT_OFFSET(pSet32Static));
buzbeefa57c472012-11-21 12:06:18 -0800453 CallRuntimeHelperImmRegLocation(cu, setter_offset, field_idx, rl_src, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700454 }
buzbee31a4a6f2012-02-28 15:36:15 -0800455}
456
buzbee02031b12012-11-23 09:41:35 -0800457void Codegen::GenSget(CompilationUnit* cu, uint32_t field_idx, RegLocation rl_dest,
458 bool is_long_or_double, bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800459{
buzbeefa57c472012-11-21 12:06:18 -0800460 int field_offset;
461 int ssb_index;
462 bool is_volatile;
463 bool is_referrers_class;
buzbee31a4a6f2012-02-28 15:36:15 -0800464
buzbeefa57c472012-11-21 12:06:18 -0800465 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker,
TDYa127dc5daa02013-01-09 21:31:37 +0800466 *cu->dex_file, cu->code_item,
467 cu->class_def_idx, cu->method_idx,
468 cu->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800469
buzbeefa57c472012-11-21 12:06:18 -0800470 bool fast_path =
471 cu->compiler->ComputeStaticFieldInfo(field_idx, &m_unit,
472 field_offset, ssb_index,
473 is_referrers_class, is_volatile,
Bill Buzbeea114add2012-05-03 15:00:40 -0700474 false);
buzbeefa57c472012-11-21 12:06:18 -0800475 if (fast_path && !SLOW_FIELD_PATH) {
476 DCHECK_GE(field_offset, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700477 int rBase;
buzbeefa57c472012-11-21 12:06:18 -0800478 if (is_referrers_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700479 // Fast path, static storage base is this method's class
buzbeefa57c472012-11-21 12:06:18 -0800480 RegLocation rl_method = LoadCurrMethod(cu);
481 rBase = AllocTemp(cu);
482 LoadWordDisp(cu, rl_method.low_reg,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700483 AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
buzbee31a4a6f2012-02-28 15:36:15 -0800484 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700485 // Medium path, static storage base in a different class which
486 // requires checks that the other class is initialized
buzbeefa57c472012-11-21 12:06:18 -0800487 DCHECK_GE(ssb_index, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700488 // May do runtime call so everything to home locations.
buzbeefa57c472012-11-21 12:06:18 -0800489 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700490 // Using fixed register to sync with possible call to runtime
491 // support
buzbeefa57c472012-11-21 12:06:18 -0800492 int r_method = TargetReg(kArg1);
493 LockTemp(cu, r_method);
494 LoadCurrMethodDirect(cu, r_method);
buzbee52a77fc2012-11-20 19:50:46 -0800495 rBase = TargetReg(kArg0);
buzbeefa57c472012-11-21 12:06:18 -0800496 LockTemp(cu, rBase);
497 LoadWordDisp(cu, r_method,
Mathieu Chartier66f19252012-09-18 08:57:04 -0700498 AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700499 rBase);
buzbeefa57c472012-11-21 12:06:18 -0800500 LoadWordDisp(cu, rBase,
Bill Buzbeea114add2012-05-03 15:00:40 -0700501 Array::DataOffset(sizeof(Object*)).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800502 sizeof(int32_t*) * ssb_index, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700503 // rBase now points at appropriate static storage base (Class*)
504 // or NULL if not initialized. Check for NULL and call helper if NULL.
505 // TUNING: fast path should fall through
buzbeefa57c472012-11-21 12:06:18 -0800506 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, rBase, 0, NULL);
507 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssb_index, true);
508 if (cu->instruction_set == kMips) {
buzbeef0504cd2012-11-13 16:31:10 -0800509 // For Arm, kRet0 = kArg0 = rBase, for Mips, we need to copy
buzbeefa57c472012-11-21 12:06:18 -0800510 OpRegCopy(cu, rBase, TargetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700511 }
buzbeefa57c472012-11-21 12:06:18 -0800512 LIR* skip_target = NewLIR0(cu, kPseudoTargetLabel);
513 branch_over->target = skip_target;
514 FreeTemp(cu, r_method);
buzbee31a4a6f2012-02-28 15:36:15 -0800515 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700516 // rBase now holds static storage base
buzbeefa57c472012-11-21 12:06:18 -0800517 RegLocation rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
518 if (is_volatile) {
519 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700520 }
buzbeefa57c472012-11-21 12:06:18 -0800521 if (is_long_or_double) {
522 LoadBaseDispWide(cu, rBase, field_offset, rl_result.low_reg,
523 rl_result.high_reg, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700524 } else {
buzbeefa57c472012-11-21 12:06:18 -0800525 LoadWordDisp(cu, rBase, field_offset, rl_result.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 }
buzbeefa57c472012-11-21 12:06:18 -0800527 FreeTemp(cu, rBase);
528 if (is_long_or_double) {
529 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700530 } else {
buzbeefa57c472012-11-21 12:06:18 -0800531 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700532 }
533 } else {
buzbeefa57c472012-11-21 12:06:18 -0800534 FlushAllRegs(cu); // Everything to home locations
535 int getterOffset = is_long_or_double ? ENTRYPOINT_OFFSET(pGet64Static) :
536 (is_object ? ENTRYPOINT_OFFSET(pGetObjStatic)
Bill Buzbeea114add2012-05-03 15:00:40 -0700537 : ENTRYPOINT_OFFSET(pGet32Static));
buzbeefa57c472012-11-21 12:06:18 -0800538 CallRuntimeHelperImm(cu, getterOffset, field_idx, true);
539 if (is_long_or_double) {
540 RegLocation rl_result = GetReturnWide(cu, rl_dest.fp);
541 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700542 } else {
buzbeefa57c472012-11-21 12:06:18 -0800543 RegLocation rl_result = GetReturn(cu, rl_dest.fp);
544 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700545 }
546 }
buzbee31a4a6f2012-02-28 15:36:15 -0800547}
548
549
550// Debugging routine - if null target, branch to DebugMe
buzbee02031b12012-11-23 09:41:35 -0800551void Codegen::GenShowTarget(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800552{
buzbeefa57c472012-11-21 12:06:18 -0800553 DCHECK_NE(cu->instruction_set, kX86) << "unimplemented GenShowTarget";
554 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, TargetReg(kInvokeTgt), 0, NULL);
555 LoadWordDisp(cu, TargetReg(kSelf), ENTRYPOINT_OFFSET(pDebugMe), TargetReg(kInvokeTgt));
556 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
557 branch_over->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -0800558}
559
buzbee02031b12012-11-23 09:41:35 -0800560void Codegen::HandleSuspendLaunchPads(CompilationUnit *cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800561{
buzbeefa57c472012-11-21 12:06:18 -0800562 LIR** suspend_label = reinterpret_cast<LIR**>(cu->suspend_launchpads.elem_list);
563 int num_elems = cu->suspend_launchpads.num_used;
564 int helper_offset = ENTRYPOINT_OFFSET(pTestSuspendFromCode);
565 for (int i = 0; i < num_elems; i++) {
566 ResetRegPool(cu);
567 ResetDefTracking(cu);
568 LIR* lab = suspend_label[i];
569 LIR* resume_lab = reinterpret_cast<LIR*>(lab->operands[0]);
570 cu->current_dalvik_offset = lab->operands[1];
571 AppendLIR(cu, lab);
572 int r_tgt = CallHelperSetup(cu, helper_offset);
573 CallHelper(cu, r_tgt, helper_offset, true /* MarkSafepointPC */);
574 OpUnconditionalBranch(cu, resume_lab);
Bill Buzbeea114add2012-05-03 15:00:40 -0700575 }
buzbee31a4a6f2012-02-28 15:36:15 -0800576}
577
buzbee02031b12012-11-23 09:41:35 -0800578void Codegen::HandleIntrinsicLaunchPads(CompilationUnit *cu)
buzbeefc9e6fa2012-03-23 15:14:29 -0700579{
buzbeefa57c472012-11-21 12:06:18 -0800580 LIR** intrinsic_label = reinterpret_cast<LIR**>(cu->intrinsic_launchpads.elem_list);
581 int num_elems = cu->intrinsic_launchpads.num_used;
582 for (int i = 0; i < num_elems; i++) {
583 ResetRegPool(cu);
584 ResetDefTracking(cu);
585 LIR* lab = intrinsic_label[i];
buzbeecbd6d442012-11-17 14:11:25 -0800586 CallInfo* info = reinterpret_cast<CallInfo*>(lab->operands[0]);
buzbeefa57c472012-11-21 12:06:18 -0800587 cu->current_dalvik_offset = info->offset;
588 AppendLIR(cu, lab);
buzbee52a77fc2012-11-20 19:50:46 -0800589 // NOTE: GenInvoke handles MarkSafepointPC
buzbeefa57c472012-11-21 12:06:18 -0800590 GenInvoke(cu, info);
591 LIR* resume_lab = reinterpret_cast<LIR*>(lab->operands[2]);
592 if (resume_lab != NULL) {
593 OpUnconditionalBranch(cu, resume_lab);
buzbeefc9e6fa2012-03-23 15:14:29 -0700594 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700596}
597
buzbee02031b12012-11-23 09:41:35 -0800598void Codegen::HandleThrowLaunchPads(CompilationUnit *cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800599{
buzbeefa57c472012-11-21 12:06:18 -0800600 LIR** throw_label = reinterpret_cast<LIR**>(cu->throw_launchpads.elem_list);
601 int num_elems = cu->throw_launchpads.num_used;
602 for (int i = 0; i < num_elems; i++) {
603 ResetRegPool(cu);
604 ResetDefTracking(cu);
605 LIR* lab = throw_label[i];
606 cu->current_dalvik_offset = lab->operands[1];
607 AppendLIR(cu, lab);
608 int func_offset = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700609 int v1 = lab->operands[2];
610 int v2 = lab->operands[3];
buzbeefa57c472012-11-21 12:06:18 -0800611 bool target_x86 = (cu->instruction_set == kX86);
Bill Buzbeea114add2012-05-03 15:00:40 -0700612 switch (lab->operands[0]) {
613 case kThrowNullPointer:
buzbeefa57c472012-11-21 12:06:18 -0800614 func_offset = ENTRYPOINT_OFFSET(pThrowNullPointerFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700615 break;
616 case kThrowArrayBounds:
buzbeef0504cd2012-11-13 16:31:10 -0800617 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
buzbee52a77fc2012-11-20 19:50:46 -0800618 if (v2 != TargetReg(kArg0)) {
buzbeefa57c472012-11-21 12:06:18 -0800619 OpRegCopy(cu, TargetReg(kArg0), v1);
620 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700621 // x86 leaves the array pointer in v2, so load the array length that the handler expects
buzbeefa57c472012-11-21 12:06:18 -0800622 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700623 } else {
buzbeefa57c472012-11-21 12:06:18 -0800624 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700625 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700626 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800627 if (v1 == TargetReg(kArg1)) {
buzbeef0504cd2012-11-13 16:31:10 -0800628 // Swap v1 and v2, using kArg2 as a temp
buzbeefa57c472012-11-21 12:06:18 -0800629 OpRegCopy(cu, TargetReg(kArg2), v1);
630 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700631 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbeefa57c472012-11-21 12:06:18 -0800632 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700633 } else {
buzbeefa57c472012-11-21 12:06:18 -0800634 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700635 }
buzbeefa57c472012-11-21 12:06:18 -0800636 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2));
Bill Buzbeea114add2012-05-03 15:00:40 -0700637 } else {
buzbeefa57c472012-11-21 12:06:18 -0800638 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700639 // x86 leaves the array pointer in v2; load the array length that the handler expects
buzbeefa57c472012-11-21 12:06:18 -0800640 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700641 } else {
buzbeefa57c472012-11-21 12:06:18 -0800642 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700643 }
buzbeefa57c472012-11-21 12:06:18 -0800644 OpRegCopy(cu, TargetReg(kArg0), v1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700645 }
buzbee31a4a6f2012-02-28 15:36:15 -0800646 }
buzbeefa57c472012-11-21 12:06:18 -0800647 func_offset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 break;
649 case kThrowDivZero:
buzbeefa57c472012-11-21 12:06:18 -0800650 func_offset = ENTRYPOINT_OFFSET(pThrowDivZeroFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700651 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700652 case kThrowNoSuchMethod:
buzbeefa57c472012-11-21 12:06:18 -0800653 OpRegCopy(cu, TargetReg(kArg0), v1);
654 func_offset =
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 ENTRYPOINT_OFFSET(pThrowNoSuchMethodFromCode);
656 break;
657 case kThrowStackOverflow:
buzbeefa57c472012-11-21 12:06:18 -0800658 func_offset = ENTRYPOINT_OFFSET(pThrowStackOverflowFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700659 // Restore stack alignment
buzbeefa57c472012-11-21 12:06:18 -0800660 if (target_x86) {
661 OpRegImm(cu, kOpAdd, TargetReg(kSp), cu->frame_size);
buzbeeb046e162012-10-30 15:48:42 -0700662 } else {
buzbeefa57c472012-11-21 12:06:18 -0800663 OpRegImm(cu, kOpAdd, TargetReg(kSp), (cu->num_core_spills + cu->num_fp_spills) * 4);
buzbeeb046e162012-10-30 15:48:42 -0700664 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700665 break;
666 default:
667 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
buzbee31a4a6f2012-02-28 15:36:15 -0800668 }
buzbeefa57c472012-11-21 12:06:18 -0800669 ClobberCalleeSave(cu);
670 int r_tgt = CallHelperSetup(cu, func_offset);
671 CallHelper(cu, r_tgt, func_offset, true /* MarkSafepointPC */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700672 }
buzbee31a4a6f2012-02-28 15:36:15 -0800673}
674
buzbee02031b12012-11-23 09:41:35 -0800675void Codegen::GenIGet(CompilationUnit* cu, uint32_t field_idx, int opt_flags, OpSize size,
676 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
677 bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800678{
buzbeefa57c472012-11-21 12:06:18 -0800679 int field_offset;
680 bool is_volatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800681
buzbeefa57c472012-11-21 12:06:18 -0800682 bool fast_path = FastInstance(cu, field_idx, field_offset, is_volatile, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800683
buzbeefa57c472012-11-21 12:06:18 -0800684 if (fast_path && !SLOW_FIELD_PATH) {
685 RegLocation rl_result;
686 RegisterClass reg_class = oat_reg_class_by_size(size);
687 DCHECK_GE(field_offset, 0);
688 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
689 if (is_long_or_double) {
690 DCHECK(rl_dest.wide);
691 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
692 if (cu->instruction_set == kX86) {
693 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
694 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
695 LoadBaseDispWide(cu, rl_obj.low_reg, field_offset, rl_result.low_reg,
696 rl_result.high_reg, rl_obj.s_reg_low);
697 if (is_volatile) {
698 GenMemBarrier(cu, kLoadLoad);
buzbeeb046e162012-10-30 15:48:42 -0700699 }
700 } else {
buzbeefa57c472012-11-21 12:06:18 -0800701 int reg_ptr = AllocTemp(cu);
702 OpRegRegImm(cu, kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
703 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
buzbeee6285f92012-12-06 15:57:46 -0800704 LoadBaseDispWide(cu, reg_ptr, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
buzbeefa57c472012-11-21 12:06:18 -0800705 if (is_volatile) {
706 GenMemBarrier(cu, kLoadLoad);
buzbeeb046e162012-10-30 15:48:42 -0700707 }
buzbeefa57c472012-11-21 12:06:18 -0800708 FreeTemp(cu, reg_ptr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700709 }
buzbeefa57c472012-11-21 12:06:18 -0800710 StoreValueWide(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800711 } else {
buzbeefa57c472012-11-21 12:06:18 -0800712 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
713 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
714 LoadBaseDisp(cu, rl_obj.low_reg, field_offset, rl_result.low_reg,
715 kWord, rl_obj.s_reg_low);
716 if (is_volatile) {
717 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700718 }
buzbeefa57c472012-11-21 12:06:18 -0800719 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800720 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700721 } else {
buzbeefa57c472012-11-21 12:06:18 -0800722 int getterOffset = is_long_or_double ? ENTRYPOINT_OFFSET(pGet64Instance) :
723 (is_object ? ENTRYPOINT_OFFSET(pGetObjInstance)
Bill Buzbeea114add2012-05-03 15:00:40 -0700724 : ENTRYPOINT_OFFSET(pGet32Instance));
buzbeefa57c472012-11-21 12:06:18 -0800725 CallRuntimeHelperImmRegLocation(cu, getterOffset, field_idx, rl_obj, true);
726 if (is_long_or_double) {
727 RegLocation rl_result = GetReturnWide(cu, rl_dest.fp);
728 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700729 } else {
buzbeefa57c472012-11-21 12:06:18 -0800730 RegLocation rl_result = GetReturn(cu, rl_dest.fp);
731 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700732 }
733 }
buzbee31a4a6f2012-02-28 15:36:15 -0800734}
735
buzbee02031b12012-11-23 09:41:35 -0800736void Codegen::GenIPut(CompilationUnit* cu, uint32_t field_idx, int opt_flags, OpSize size,
737 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
738 bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800739{
buzbeefa57c472012-11-21 12:06:18 -0800740 int field_offset;
741 bool is_volatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800742
buzbeefa57c472012-11-21 12:06:18 -0800743 bool fast_path = FastInstance(cu, field_idx, field_offset, is_volatile,
Bill Buzbeea114add2012-05-03 15:00:40 -0700744 true);
buzbeefa57c472012-11-21 12:06:18 -0800745 if (fast_path && !SLOW_FIELD_PATH) {
746 RegisterClass reg_class = oat_reg_class_by_size(size);
747 DCHECK_GE(field_offset, 0);
748 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
749 if (is_long_or_double) {
750 int reg_ptr;
751 rl_src = LoadValueWide(cu, rl_src, kAnyReg);
752 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
753 reg_ptr = AllocTemp(cu);
754 OpRegRegImm(cu, kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
755 if (is_volatile) {
756 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700757 }
buzbeefa57c472012-11-21 12:06:18 -0800758 StoreBaseDispWide(cu, reg_ptr, 0, rl_src.low_reg, rl_src.high_reg);
759 if (is_volatile) {
760 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700761 }
buzbeefa57c472012-11-21 12:06:18 -0800762 FreeTemp(cu, reg_ptr);
buzbee31a4a6f2012-02-28 15:36:15 -0800763 } else {
buzbeefa57c472012-11-21 12:06:18 -0800764 rl_src = LoadValue(cu, rl_src, reg_class);
765 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
766 if (is_volatile) {
767 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700768 }
buzbeefa57c472012-11-21 12:06:18 -0800769 StoreBaseDisp(cu, rl_obj.low_reg, field_offset, rl_src.low_reg, kWord);
770 if (is_volatile) {
771 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700772 }
buzbeefa57c472012-11-21 12:06:18 -0800773 if (is_object) {
774 MarkGCCard(cu, rl_src.low_reg, rl_obj.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700775 }
buzbee31a4a6f2012-02-28 15:36:15 -0800776 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700777 } else {
buzbeefa57c472012-11-21 12:06:18 -0800778 int setter_offset = is_long_or_double ? ENTRYPOINT_OFFSET(pSet64Instance) :
779 (is_object ? ENTRYPOINT_OFFSET(pSetObjInstance)
Bill Buzbeea114add2012-05-03 15:00:40 -0700780 : ENTRYPOINT_OFFSET(pSet32Instance));
buzbeefa57c472012-11-21 12:06:18 -0800781 CallRuntimeHelperImmRegLocationRegLocation(cu, setter_offset, field_idx, rl_obj, rl_src, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700782 }
buzbee31a4a6f2012-02-28 15:36:15 -0800783}
784
buzbee02031b12012-11-23 09:41:35 -0800785void Codegen::GenConstClass(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800786{
buzbeefa57c472012-11-21 12:06:18 -0800787 RegLocation rl_method = LoadCurrMethod(cu);
788 int res_reg = AllocTemp(cu);
789 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
790 if (!cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
791 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700792 type_idx)) {
793 // Call out to helper which resolves type and verifies access.
buzbeef0504cd2012-11-13 16:31:10 -0800794 // Resolved type returned in kRet0.
buzbeefa57c472012-11-21 12:06:18 -0800795 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
796 type_idx, rl_method.low_reg, true);
797 RegLocation rl_result = GetReturn(cu, false);
798 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700799 } else {
800 // We're don't need access checks, load type from dex cache
801 int32_t dex_cache_offset =
Mathieu Chartier66f19252012-09-18 08:57:04 -0700802 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbeefa57c472012-11-21 12:06:18 -0800803 LoadWordDisp(cu, rl_method.low_reg, dex_cache_offset, res_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700804 int32_t offset_of_type =
805 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
806 * type_idx);
buzbeefa57c472012-11-21 12:06:18 -0800807 LoadWordDisp(cu, res_reg, offset_of_type, rl_result.low_reg);
808 if (!cu->compiler->CanAssumeTypeIsPresentInDexCache(*cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700809 type_idx) || SLOW_TYPE_PATH) {
810 // Slow path, at runtime test if type is null and if so initialize
buzbeefa57c472012-11-21 12:06:18 -0800811 FlushAllRegs(cu);
812 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, rl_result.low_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700813 // Resolved, store and hop over following code
buzbeefa57c472012-11-21 12:06:18 -0800814 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700815 /*
816 * Because we have stores of the target value on two paths,
817 * clobber temp tracking for the destination using the ssa name
818 */
buzbeefa57c472012-11-21 12:06:18 -0800819 ClobberSReg(cu, rl_dest.s_reg_low);
820 LIR* branch2 = OpUnconditionalBranch(cu,0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700821 // TUNING: move slow path to end & remove unconditional branch
buzbeefa57c472012-11-21 12:06:18 -0800822 LIR* target1 = NewLIR0(cu, kPseudoTargetLabel);
buzbeef0504cd2012-11-13 16:31:10 -0800823 // Call out to helper, which will return resolved type in kArg0
buzbeefa57c472012-11-21 12:06:18 -0800824 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx,
825 rl_method.low_reg, true);
826 RegLocation rl_result = GetReturn(cu, false);
827 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700828 /*
829 * Because we have stores of the target value on two paths,
830 * clobber temp tracking for the destination using the ssa name
831 */
buzbeefa57c472012-11-21 12:06:18 -0800832 ClobberSReg(cu, rl_dest.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700833 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -0800834 LIR* target2 = NewLIR0(cu, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800835 branch1->target = target1;
836 branch2->target = target2;
buzbee31a4a6f2012-02-28 15:36:15 -0800837 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700838 // Fast path, we're done - just store result
buzbeefa57c472012-11-21 12:06:18 -0800839 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800840 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700841 }
buzbee31a4a6f2012-02-28 15:36:15 -0800842}
Ian Rogersab2b55d2012-03-18 00:06:11 -0700843
buzbee02031b12012-11-23 09:41:35 -0800844void Codegen::GenConstString(CompilationUnit* cu, uint32_t string_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800845{
Bill Buzbeea114add2012-05-03 15:00:40 -0700846 /* NOTE: Most strings should be available at compile time */
Bill Buzbeea114add2012-05-03 15:00:40 -0700847 int32_t offset_of_string = Array::DataOffset(sizeof(String*)).Int32Value() +
848 (sizeof(String*) * string_idx);
buzbeefa57c472012-11-21 12:06:18 -0800849 if (!cu->compiler->CanAssumeStringIsPresentInDexCache(
850 *cu->dex_file, string_idx) || SLOW_STRING_PATH) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700851 // slow path, resolve string if not in dex cache
buzbeefa57c472012-11-21 12:06:18 -0800852 FlushAllRegs(cu);
853 LockCallTemps(cu); // Using explicit registers
854 LoadCurrMethodDirect(cu, TargetReg(kArg2));
855 LoadWordDisp(cu, TargetReg(kArg2),
buzbee52a77fc2012-11-20 19:50:46 -0800856 AbstractMethod::DexCacheStringsOffset().Int32Value(), TargetReg(kArg0));
buzbeef0504cd2012-11-13 16:31:10 -0800857 // Might call out to helper, which will return resolved string in kRet0
buzbeefa57c472012-11-21 12:06:18 -0800858 int r_tgt = CallHelperSetup(cu, ENTRYPOINT_OFFSET(pResolveStringFromCode));
859 LoadWordDisp(cu, TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
860 LoadConstant(cu, TargetReg(kArg1), string_idx);
861 if (cu->instruction_set == kThumb2) {
862 OpRegImm(cu, kOpCmp, TargetReg(kRet0), 0); // Is resolved?
863 GenBarrier(cu);
buzbeeb046e162012-10-30 15:48:42 -0700864 // For testing, always force through helper
865 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee02031b12012-11-23 09:41:35 -0800866 OpIT(cu, kCondEq, "T");
buzbeeb046e162012-10-30 15:48:42 -0700867 }
buzbeefa57c472012-11-21 12:06:18 -0800868 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .eq
869 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt); // .eq, helper(Method*, string_idx)
870 MarkSafepointPC(cu, call_inst);
871 FreeTemp(cu, r_tgt);
872 } else if (cu->instruction_set == kMips) {
873 LIR* branch = OpCmpImmBranch(cu, kCondNe, TargetReg(kRet0), 0, NULL);
874 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .eq
875 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt);
876 MarkSafepointPC(cu, call_inst);
877 FreeTemp(cu, r_tgt);
878 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
buzbeeb046e162012-10-30 15:48:42 -0700879 branch->target = target;
880 } else {
buzbeefa57c472012-11-21 12:06:18 -0800881 DCHECK_EQ(cu->instruction_set, kX86);
882 CallRuntimeHelperRegReg(cu, ENTRYPOINT_OFFSET(pResolveStringFromCode), TargetReg(kArg2), TargetReg(kArg1), true);
buzbee31a4a6f2012-02-28 15:36:15 -0800883 }
buzbeefa57c472012-11-21 12:06:18 -0800884 GenBarrier(cu);
885 StoreValue(cu, rl_dest, GetReturn(cu, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700886 } else {
buzbeefa57c472012-11-21 12:06:18 -0800887 RegLocation rl_method = LoadCurrMethod(cu);
888 int res_reg = AllocTemp(cu);
889 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
890 LoadWordDisp(cu, rl_method.low_reg,
891 AbstractMethod::DexCacheStringsOffset().Int32Value(), res_reg);
892 LoadWordDisp(cu, res_reg, offset_of_string, rl_result.low_reg);
893 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700894 }
buzbee31a4a6f2012-02-28 15:36:15 -0800895}
896
897/*
898 * Let helper function take care of everything. Will
899 * call Class::NewInstanceFromCode(type_idx, method);
900 */
buzbee02031b12012-11-23 09:41:35 -0800901void Codegen::GenNewInstance(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800902{
buzbeefa57c472012-11-21 12:06:18 -0800903 FlushAllRegs(cu); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -0700904 // alloc will always check for resolution, do we also need to verify
905 // access because the verifier was unable to?
buzbeefa57c472012-11-21 12:06:18 -0800906 int func_offset;
907 if (cu->compiler->CanAccessInstantiableTypeWithoutChecks(
908 cu->method_idx, *cu->dex_file, type_idx)) {
909 func_offset = ENTRYPOINT_OFFSET(pAllocObjectFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700910 } else {
buzbeefa57c472012-11-21 12:06:18 -0800911 func_offset = ENTRYPOINT_OFFSET(pAllocObjectFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700912 }
buzbeefa57c472012-11-21 12:06:18 -0800913 CallRuntimeHelperImmMethod(cu, func_offset, type_idx, true);
914 RegLocation rl_result = GetReturn(cu, false);
915 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800916}
917
buzbee02031b12012-11-23 09:41:35 -0800918void Codegen::GenThrow(CompilationUnit* cu, RegLocation rl_src)
Ian Rogersab2b55d2012-03-18 00:06:11 -0700919{
buzbeefa57c472012-11-21 12:06:18 -0800920 FlushAllRegs(cu);
921 CallRuntimeHelperRegLocation(cu, ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700922}
923
buzbee02031b12012-11-23 09:41:35 -0800924void Codegen::GenInstanceof(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest,
925 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800926{
buzbeefa57c472012-11-21 12:06:18 -0800927 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700928 // May generate a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -0800929 LockCallTemps(cu);
930 LoadCurrMethodDirect(cu, TargetReg(kArg1)); // kArg1 <= current Method*
931 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
932 if (!cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
933 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700934 type_idx)) {
935 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbeef0504cd2012-11-13 16:31:10 -0800936 // returns Class* in kArg0
buzbeefa57c472012-11-21 12:06:18 -0800937 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbee8320f382012-09-11 16:29:42 -0700938 type_idx, true);
buzbeefa57c472012-11-21 12:06:18 -0800939 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
940 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -0700941 } else {
buzbeefa57c472012-11-21 12:06:18 -0800942 // Load dex cache entry into class_reg (kArg2)
943 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
944 LoadWordDisp(cu, TargetReg(kArg1),
945 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700946 int32_t offset_of_type =
947 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
948 * type_idx);
buzbeefa57c472012-11-21 12:06:18 -0800949 LoadWordDisp(cu, class_reg, offset_of_type, class_reg);
950 if (!cu->compiler->CanAssumeTypeIsPresentInDexCache(
951 *cu->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700952 // Need to test presence of type in dex cache at runtime
buzbeefa57c472012-11-21 12:06:18 -0800953 LIR* hop_branch = OpCmpImmBranch(cu, kCondNe, class_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700954 // Not resolved
buzbeef0504cd2012-11-13 16:31:10 -0800955 // Call out to helper, which will return resolved type in kRet0
buzbeefa57c472012-11-21 12:06:18 -0800956 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, true);
957 OpRegCopy(cu, TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
958 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); /* reload Ref */
Bill Buzbeea114add2012-05-03 15:00:40 -0700959 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -0800960 LIR* hop_target = NewLIR0(cu, kPseudoTargetLabel);
961 hop_branch->target = hop_target;
buzbee31a4a6f2012-02-28 15:36:15 -0800962 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700963 }
buzbeef0504cd2012-11-13 16:31:10 -0800964 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeefa57c472012-11-21 12:06:18 -0800965 RegLocation rl_result = GetReturn(cu, false);
966 if (cu->instruction_set == kMips) {
967 LoadConstant(cu, rl_result.low_reg, 0); // store false result for if branch is taken
buzbeeb046e162012-10-30 15:48:42 -0700968 }
buzbeefa57c472012-11-21 12:06:18 -0800969 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, TargetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700970 /* load object->klass_ */
971 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbeefa57c472012-11-21 12:06:18 -0800972 LoadWordDisp(cu, TargetReg(kArg0), Object::ClassOffset().Int32Value(), TargetReg(kArg1));
buzbeef0504cd2012-11-13 16:31:10 -0800973 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
buzbeefa57c472012-11-21 12:06:18 -0800974 LIR* call_inst;
buzbeeb046e162012-10-30 15:48:42 -0700975 LIR* branchover = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800976 if (cu->instruction_set == kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -0700977 /* Uses conditional nullification */
buzbeefa57c472012-11-21 12:06:18 -0800978 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
979 OpRegReg(cu, kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
buzbee02031b12012-11-23 09:41:35 -0800980 OpIT(cu, kCondEq, "EE"); // if-convert the test
buzbeefa57c472012-11-21 12:06:18 -0800981 LoadConstant(cu, TargetReg(kArg0), 1); // .eq case - load true
982 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
983 call_inst = OpReg(cu, kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
984 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700985 } else {
986 /* Uses branchovers */
buzbeefa57c472012-11-21 12:06:18 -0800987 LoadConstant(cu, rl_result.low_reg, 1); // assume true
988 branchover = OpCmpBranch(cu, kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
989 if (cu->instruction_set != kX86) {
990 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
991 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
992 call_inst = OpReg(cu, kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
993 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700994 } else {
buzbeefa57c472012-11-21 12:06:18 -0800995 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2));
996 call_inst = OpThreadMem(cu, kOpBlx, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
buzbeeb046e162012-10-30 15:48:42 -0700997 }
998 }
buzbeefa57c472012-11-21 12:06:18 -0800999 MarkSafepointPC(cu, call_inst);
1000 ClobberCalleeSave(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001001 /* branch targets here */
buzbeefa57c472012-11-21 12:06:18 -08001002 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
1003 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001004 branch1->target = target;
buzbeefa57c472012-11-21 12:06:18 -08001005 if (cu->instruction_set != kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -07001006 branchover->target = target;
1007 }
buzbee31a4a6f2012-02-28 15:36:15 -08001008}
1009
buzbee02031b12012-11-23 09:41:35 -08001010void Codegen::GenCheckCast(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -08001011{
buzbeefa57c472012-11-21 12:06:18 -08001012 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001013 // May generate a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -08001014 LockCallTemps(cu);
1015 LoadCurrMethodDirect(cu, TargetReg(kArg1)); // kArg1 <= current Method*
1016 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1017 if (!cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
1018 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -07001019 type_idx)) {
1020 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbeef0504cd2012-11-13 16:31:10 -08001021 // returns Class* in kRet0
Bill Buzbeea114add2012-05-03 15:00:40 -07001022 // InitializeTypeAndVerifyAccess(idx, method)
buzbeefa57c472012-11-21 12:06:18 -08001023 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbee52a77fc2012-11-20 19:50:46 -08001024 type_idx, TargetReg(kArg1), true);
buzbeefa57c472012-11-21 12:06:18 -08001025 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001026 } else {
buzbeefa57c472012-11-21 12:06:18 -08001027 // Load dex cache entry into class_reg (kArg2)
1028 LoadWordDisp(cu, TargetReg(kArg1),
1029 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001030 int32_t offset_of_type =
1031 Array::DataOffset(sizeof(Class*)).Int32Value() +
1032 (sizeof(Class*) * type_idx);
buzbeefa57c472012-11-21 12:06:18 -08001033 LoadWordDisp(cu, class_reg, offset_of_type, class_reg);
1034 if (!cu->compiler->CanAssumeTypeIsPresentInDexCache(
1035 *cu->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001036 // Need to test presence of type in dex cache at runtime
buzbeefa57c472012-11-21 12:06:18 -08001037 LIR* hop_branch = OpCmpImmBranch(cu, kCondNe, class_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001038 // Not resolved
buzbeef0504cd2012-11-13 16:31:10 -08001039 // Call out to helper, which will return resolved type in kArg0
Bill Buzbeea114add2012-05-03 15:00:40 -07001040 // InitializeTypeFromCode(idx, method)
buzbeefa57c472012-11-21 12:06:18 -08001041 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, TargetReg(kArg1),
buzbee8320f382012-09-11 16:29:42 -07001042 true);
buzbeefa57c472012-11-21 12:06:18 -08001043 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001044 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -08001045 LIR* hop_target = NewLIR0(cu, kPseudoTargetLabel);
1046 hop_branch->target = hop_target;
buzbee31a4a6f2012-02-28 15:36:15 -08001047 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001048 }
buzbeefa57c472012-11-21 12:06:18 -08001049 // At this point, class_reg (kArg2) has class
1050 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -07001051 /* Null is OK - continue */
buzbeefa57c472012-11-21 12:06:18 -08001052 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, TargetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001053 /* load object->klass_ */
1054 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbeefa57c472012-11-21 12:06:18 -08001055 LoadWordDisp(cu, TargetReg(kArg0), Object::ClassOffset().Int32Value(), TargetReg(kArg1));
buzbeef0504cd2012-11-13 16:31:10 -08001056 /* kArg1 now contains object->klass_ */
buzbeeb046e162012-10-30 15:48:42 -07001057 LIR* branch2;
buzbeefa57c472012-11-21 12:06:18 -08001058 if (cu->instruction_set == kThumb2) {
1059 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1060 OpRegReg(cu, kOpCmp, TargetReg(kArg1), class_reg);
1061 branch2 = OpCondBranch(cu, kCondEq, NULL); /* If eq, trivial yes */
1062 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg1));
1063 OpRegCopy(cu, TargetReg(kArg1), TargetReg(kArg2));
1064 ClobberCalleeSave(cu);
1065 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt);
1066 MarkSafepointPC(cu, call_inst);
1067 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -07001068 } else {
buzbeefa57c472012-11-21 12:06:18 -08001069 branch2 = OpCmpBranch(cu, kCondEq, TargetReg(kArg1), class_reg, NULL);
1070 CallRuntimeHelperRegReg(cu, ENTRYPOINT_OFFSET(pCheckCastFromCode), TargetReg(kArg1), TargetReg(kArg2), true);
buzbeeb046e162012-10-30 15:48:42 -07001071 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001072 /* branch target here */
buzbeefa57c472012-11-21 12:06:18 -08001073 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -07001074 branch1->target = target;
1075 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001076}
1077
buzbee02031b12012-11-23 09:41:35 -08001078void Codegen::GenLong3Addr(CompilationUnit* cu, OpKind first_op, OpKind second_op,
1079 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001080{
buzbeefa57c472012-11-21 12:06:18 -08001081 RegLocation rl_result;
1082 if (cu->instruction_set == kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -07001083 /*
1084 * NOTE: This is the one place in the code in which we might have
1085 * as many as six live temporary registers. There are 5 in the normal
1086 * set for Arm. Until we have spill capabilities, temporarily add
1087 * lr to the temp set. It is safe to do this locally, but note that
1088 * lr is used explicitly elsewhere in the code generator and cannot
1089 * normally be used as a general temp register.
1090 */
buzbeefa57c472012-11-21 12:06:18 -08001091 MarkTemp(cu, TargetReg(kLr)); // Add lr to the temp pool
1092 FreeTemp(cu, TargetReg(kLr)); // and make it available
buzbeeb046e162012-10-30 15:48:42 -07001093 }
buzbeefa57c472012-11-21 12:06:18 -08001094 rl_src1 = LoadValueWide(cu, rl_src1, kCoreReg);
1095 rl_src2 = LoadValueWide(cu, rl_src2, kCoreReg);
1096 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001097 // The longs may overlap - use intermediate temp if so
buzbeefa57c472012-11-21 12:06:18 -08001098 if ((rl_result.low_reg == rl_src1.high_reg) || (rl_result.low_reg == rl_src2.high_reg)){
1099 int t_reg = AllocTemp(cu);
1100 OpRegRegReg(cu, first_op, t_reg, rl_src1.low_reg, rl_src2.low_reg);
1101 OpRegRegReg(cu, second_op, rl_result.high_reg, rl_src1.high_reg, rl_src2.high_reg);
1102 OpRegCopy(cu, rl_result.low_reg, t_reg);
1103 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001104 } else {
buzbeefa57c472012-11-21 12:06:18 -08001105 OpRegRegReg(cu, first_op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1106 OpRegRegReg(cu, second_op, rl_result.high_reg, rl_src1.high_reg,
1107 rl_src2.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001108 }
1109 /*
buzbeefa57c472012-11-21 12:06:18 -08001110 * NOTE: If rl_dest refers to a frame variable in a large frame, the
buzbee52a77fc2012-11-20 19:50:46 -08001111 * following StoreValueWide might need to allocate a temp register.
Bill Buzbeea114add2012-05-03 15:00:40 -07001112 * To further work around the lack of a spill capability, explicitly
buzbeefa57c472012-11-21 12:06:18 -08001113 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
Bill Buzbeea114add2012-05-03 15:00:40 -07001114 * Remove when spill is functional.
1115 */
buzbeefa57c472012-11-21 12:06:18 -08001116 FreeRegLocTemps(cu, rl_result, rl_src1);
1117 FreeRegLocTemps(cu, rl_result, rl_src2);
1118 StoreValueWide(cu, rl_dest, rl_result);
1119 if (cu->instruction_set == kThumb2) {
1120 Clobber(cu, TargetReg(kLr));
1121 UnmarkTemp(cu, TargetReg(kLr)); // Remove lr from the temp pool
buzbeeb046e162012-10-30 15:48:42 -07001122 }
buzbee31a4a6f2012-02-28 15:36:15 -08001123}
1124
1125
buzbee02031b12012-11-23 09:41:35 -08001126bool Codegen::GenShiftOpLong(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
1127 RegLocation rl_src1, RegLocation rl_shift)
buzbee31a4a6f2012-02-28 15:36:15 -08001128{
buzbeefa57c472012-11-21 12:06:18 -08001129 int func_offset;
buzbee31a4a6f2012-02-28 15:36:15 -08001130
buzbee408ad162012-06-06 16:45:18 -07001131 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001132 case Instruction::SHL_LONG:
1133 case Instruction::SHL_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001134 func_offset = ENTRYPOINT_OFFSET(pShlLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001135 break;
1136 case Instruction::SHR_LONG:
1137 case Instruction::SHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001138 func_offset = ENTRYPOINT_OFFSET(pShrLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001139 break;
1140 case Instruction::USHR_LONG:
1141 case Instruction::USHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001142 func_offset = ENTRYPOINT_OFFSET(pUshrLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001143 break;
1144 default:
1145 LOG(FATAL) << "Unexpected case";
1146 return true;
1147 }
buzbeefa57c472012-11-21 12:06:18 -08001148 FlushAllRegs(cu); /* Send everything to home location */
1149 CallRuntimeHelperRegLocationRegLocation(cu, func_offset, rl_src1, rl_shift, false);
1150 RegLocation rl_result = GetReturnWide(cu, false);
1151 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001152 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001153}
1154
1155
buzbee02031b12012-11-23 09:41:35 -08001156bool Codegen::GenArithOpInt(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
1157 RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001158{
Bill Buzbeea114add2012-05-03 15:00:40 -07001159 OpKind op = kOpBkpt;
buzbeefa57c472012-11-21 12:06:18 -08001160 bool is_div_rem = false;
1161 bool check_zero = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001162 bool unary = false;
buzbeefa57c472012-11-21 12:06:18 -08001163 RegLocation rl_result;
1164 bool shift_op = false;
buzbee408ad162012-06-06 16:45:18 -07001165 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001166 case Instruction::NEG_INT:
1167 op = kOpNeg;
1168 unary = true;
1169 break;
1170 case Instruction::NOT_INT:
1171 op = kOpMvn;
1172 unary = true;
1173 break;
1174 case Instruction::ADD_INT:
1175 case Instruction::ADD_INT_2ADDR:
1176 op = kOpAdd;
1177 break;
1178 case Instruction::SUB_INT:
1179 case Instruction::SUB_INT_2ADDR:
1180 op = kOpSub;
1181 break;
1182 case Instruction::MUL_INT:
1183 case Instruction::MUL_INT_2ADDR:
1184 op = kOpMul;
1185 break;
1186 case Instruction::DIV_INT:
1187 case Instruction::DIV_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001188 check_zero = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001189 op = kOpDiv;
buzbeefa57c472012-11-21 12:06:18 -08001190 is_div_rem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001191 break;
buzbeef0504cd2012-11-13 16:31:10 -08001192 /* NOTE: returns in kArg1 */
Bill Buzbeea114add2012-05-03 15:00:40 -07001193 case Instruction::REM_INT:
1194 case Instruction::REM_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001195 check_zero = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001196 op = kOpRem;
buzbeefa57c472012-11-21 12:06:18 -08001197 is_div_rem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001198 break;
1199 case Instruction::AND_INT:
1200 case Instruction::AND_INT_2ADDR:
1201 op = kOpAnd;
1202 break;
1203 case Instruction::OR_INT:
1204 case Instruction::OR_INT_2ADDR:
1205 op = kOpOr;
1206 break;
1207 case Instruction::XOR_INT:
1208 case Instruction::XOR_INT_2ADDR:
1209 op = kOpXor;
1210 break;
1211 case Instruction::SHL_INT:
1212 case Instruction::SHL_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001213 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001214 op = kOpLsl;
1215 break;
1216 case Instruction::SHR_INT:
1217 case Instruction::SHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001218 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001219 op = kOpAsr;
1220 break;
1221 case Instruction::USHR_INT:
1222 case Instruction::USHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001223 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001224 op = kOpLsr;
1225 break;
1226 default:
buzbeecbd6d442012-11-17 14:11:25 -08001227 LOG(FATAL) << "Invalid word arith op: " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001228 }
buzbeefa57c472012-11-21 12:06:18 -08001229 if (!is_div_rem) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001230 if (unary) {
buzbeefa57c472012-11-21 12:06:18 -08001231 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1232 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1233 OpRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg);
buzbee31a4a6f2012-02-28 15:36:15 -08001234 } else {
buzbeefa57c472012-11-21 12:06:18 -08001235 if (shift_op) {
1236 int t_reg = INVALID_REG;
1237 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -07001238 // X86 doesn't require masking and must use ECX
buzbeefa57c472012-11-21 12:06:18 -08001239 t_reg = TargetReg(kCount); // rCX
1240 LoadValueDirectFixed(cu, rl_src2, t_reg);
buzbeeb046e162012-10-30 15:48:42 -07001241 } else {
buzbeefa57c472012-11-21 12:06:18 -08001242 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1243 t_reg = AllocTemp(cu);
1244 OpRegRegImm(cu, kOpAnd, t_reg, rl_src2.low_reg, 31);
buzbeeb046e162012-10-30 15:48:42 -07001245 }
buzbeefa57c472012-11-21 12:06:18 -08001246 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1247 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1248 OpRegRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg, t_reg);
1249 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001250 } else {
buzbeefa57c472012-11-21 12:06:18 -08001251 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1252 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1253 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1254 OpRegRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001255 }
buzbee31a4a6f2012-02-28 15:36:15 -08001256 }
buzbeefa57c472012-11-21 12:06:18 -08001257 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001258 } else {
buzbeefa57c472012-11-21 12:06:18 -08001259 if (cu->instruction_set == kMips) {
1260 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1261 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1262 if (check_zero) {
1263 GenImmedCheck(cu, kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001264 }
buzbeefa57c472012-11-21 12:06:18 -08001265 rl_result = GenDivRem(cu, rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
jeffhao4f8f04a2012-10-02 18:10:35 -07001266 } else {
buzbeefa57c472012-11-21 12:06:18 -08001267 int func_offset = ENTRYPOINT_OFFSET(pIdivmod);
1268 FlushAllRegs(cu); /* Send everything to home location */
1269 LoadValueDirectFixed(cu, rl_src2, TargetReg(kArg1));
1270 int r_tgt = CallHelperSetup(cu, func_offset);
1271 LoadValueDirectFixed(cu, rl_src1, TargetReg(kArg0));
1272 if (check_zero) {
1273 GenImmedCheck(cu, kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001274 }
1275 // NOTE: callout here is not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001276 CallHelper(cu, r_tgt, func_offset, false /* not a safepoint */ );
buzbeeb046e162012-10-30 15:48:42 -07001277 if (op == kOpDiv)
buzbeefa57c472012-11-21 12:06:18 -08001278 rl_result = GetReturn(cu, false);
buzbeeb046e162012-10-30 15:48:42 -07001279 else
buzbeefa57c472012-11-21 12:06:18 -08001280 rl_result = GetReturnAlt(cu);
jeffhao4f8f04a2012-10-02 18:10:35 -07001281 }
buzbeefa57c472012-11-21 12:06:18 -08001282 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001283 }
1284 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001285}
1286
1287/*
1288 * The following are the first-level codegen routines that analyze the format
1289 * of each bytecode then either dispatch special purpose codegen routines
1290 * or produce corresponding Thumb instructions directly.
1291 */
1292
buzbeeaad94382012-11-21 07:40:50 -08001293static bool IsPowerOfTwo(int x)
buzbee31a4a6f2012-02-28 15:36:15 -08001294{
Bill Buzbeea114add2012-05-03 15:00:40 -07001295 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001296}
1297
1298// Returns true if no more than two bits are set in 'x'.
buzbeeaad94382012-11-21 07:40:50 -08001299static bool IsPopCountLE2(unsigned int x)
buzbee31a4a6f2012-02-28 15:36:15 -08001300{
Bill Buzbeea114add2012-05-03 15:00:40 -07001301 x &= x - 1;
1302 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001303}
1304
1305// Returns the index of the lowest set bit in 'x'.
buzbeeaad94382012-11-21 07:40:50 -08001306static int LowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001307 int bit_posn = 0;
1308 while ((x & 0xf) == 0) {
1309 bit_posn += 4;
1310 x >>= 4;
1311 }
1312 while ((x & 1) == 0) {
1313 bit_posn++;
1314 x >>= 1;
1315 }
1316 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001317}
1318
buzbeefa57c472012-11-21 12:06:18 -08001319// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1320// and store the result in 'rl_dest'.
1321static bool HandleEasyDivide(CompilationUnit* cu, Instruction::Code dalvik_opcode,
1322 RegLocation rl_src, RegLocation rl_dest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001323{
buzbeefa57c472012-11-21 12:06:18 -08001324 if ((lit < 2) || ((cu->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001325 return false;
buzbee0f79d722012-11-01 15:35:27 -07001326 }
buzbee02031b12012-11-23 09:41:35 -08001327 Codegen* cg = cu->cg.get();
buzbeeb046e162012-10-30 15:48:42 -07001328 // No divide instruction for Arm, so check for more special cases
buzbeefa57c472012-11-21 12:06:18 -08001329 if ((cu->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee02031b12012-11-23 09:41:35 -08001330 return cg->SmallLiteralDivide(cu, dalvik_opcode, rl_src, rl_dest, lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001331 }
buzbee52a77fc2012-11-20 19:50:46 -08001332 int k = LowestSetBit(lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001333 if (k >= 30) {
1334 // Avoid special cases.
1335 return false;
1336 }
buzbeefa57c472012-11-21 12:06:18 -08001337 bool div = (dalvik_opcode == Instruction::DIV_INT_LIT8 ||
1338 dalvik_opcode == Instruction::DIV_INT_LIT16);
buzbee02031b12012-11-23 09:41:35 -08001339 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001340 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001341 if (div) {
buzbeefa57c472012-11-21 12:06:18 -08001342 int t_reg = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001343 if (lit == 2) {
1344 // Division by 2 is by far the most common division by constant.
buzbee02031b12012-11-23 09:41:35 -08001345 cg->OpRegRegImm(cu, kOpLsr, t_reg, rl_src.low_reg, 32 - k);
1346 cg->OpRegRegReg(cu, kOpAdd, t_reg, t_reg, rl_src.low_reg);
1347 cg->OpRegRegImm(cu, kOpAsr, rl_result.low_reg, t_reg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001348 } else {
buzbee02031b12012-11-23 09:41:35 -08001349 cg->OpRegRegImm(cu, kOpAsr, t_reg, rl_src.low_reg, 31);
1350 cg->OpRegRegImm(cu, kOpLsr, t_reg, t_reg, 32 - k);
1351 cg->OpRegRegReg(cu, kOpAdd, t_reg, t_reg, rl_src.low_reg);
1352 cg->OpRegRegImm(cu, kOpAsr, rl_result.low_reg, t_reg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001353 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001354 } else {
buzbeefa57c472012-11-21 12:06:18 -08001355 int t_reg1 = AllocTemp(cu);
1356 int t_reg2 = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001357 if (lit == 2) {
buzbee02031b12012-11-23 09:41:35 -08001358 cg->OpRegRegImm(cu, kOpLsr, t_reg1, rl_src.low_reg, 32 - k);
1359 cg->OpRegRegReg(cu, kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1360 cg->OpRegRegImm(cu, kOpAnd, t_reg2, t_reg2, lit -1);
1361 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Bill Buzbeea114add2012-05-03 15:00:40 -07001362 } else {
buzbee02031b12012-11-23 09:41:35 -08001363 cg->OpRegRegImm(cu, kOpAsr, t_reg1, rl_src.low_reg, 31);
1364 cg->OpRegRegImm(cu, kOpLsr, t_reg1, t_reg1, 32 - k);
1365 cg->OpRegRegReg(cu, kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1366 cg->OpRegRegImm(cu, kOpAnd, t_reg2, t_reg2, lit - 1);
1367 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Bill Buzbeea114add2012-05-03 15:00:40 -07001368 }
1369 }
buzbee02031b12012-11-23 09:41:35 -08001370 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001371 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001372}
1373
buzbeefa57c472012-11-21 12:06:18 -08001374// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1375// and store the result in 'rl_dest'.
1376static bool HandleEasyMultiply(CompilationUnit* cu, RegLocation rl_src,
1377 RegLocation rl_dest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001378{
Bill Buzbeea114add2012-05-03 15:00:40 -07001379 // Can we simplify this multiplication?
buzbeefa57c472012-11-21 12:06:18 -08001380 bool power_of_two = false;
1381 bool pop_count_le2 = false;
1382 bool power_of_two_minus_one = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001383 if (lit < 2) {
1384 // Avoid special cases.
1385 return false;
buzbee52a77fc2012-11-20 19:50:46 -08001386 } else if (IsPowerOfTwo(lit)) {
buzbeefa57c472012-11-21 12:06:18 -08001387 power_of_two = true;
buzbee52a77fc2012-11-20 19:50:46 -08001388 } else if (IsPopCountLE2(lit)) {
buzbeefa57c472012-11-21 12:06:18 -08001389 pop_count_le2 = true;
buzbee52a77fc2012-11-20 19:50:46 -08001390 } else if (IsPowerOfTwo(lit + 1)) {
buzbeefa57c472012-11-21 12:06:18 -08001391 power_of_two_minus_one = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001392 } else {
1393 return false;
1394 }
buzbee02031b12012-11-23 09:41:35 -08001395 Codegen* cg = cu->cg.get();
1396 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001397 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1398 if (power_of_two) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001399 // Shift.
buzbee02031b12012-11-23 09:41:35 -08001400 cg->OpRegRegImm(cu, kOpLsl, rl_result.low_reg, rl_src.low_reg, LowestSetBit(lit));
buzbeefa57c472012-11-21 12:06:18 -08001401 } else if (pop_count_le2) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001402 // Shift and add and shift.
buzbeefa57c472012-11-21 12:06:18 -08001403 int first_bit = LowestSetBit(lit);
1404 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
buzbee02031b12012-11-23 09:41:35 -08001405 cg->GenMultiplyByTwoBitMultiplier(cu, rl_src, rl_result, lit, first_bit, second_bit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001406 } else {
1407 // Reverse subtract: (src << (shift + 1)) - src.
buzbeefa57c472012-11-21 12:06:18 -08001408 DCHECK(power_of_two_minus_one);
buzbee52a77fc2012-11-20 19:50:46 -08001409 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbeefa57c472012-11-21 12:06:18 -08001410 int t_reg = AllocTemp(cu);
buzbee02031b12012-11-23 09:41:35 -08001411 cg->OpRegRegImm(cu, kOpLsl, t_reg, rl_src.low_reg, LowestSetBit(lit + 1));
1412 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001413 }
buzbee02031b12012-11-23 09:41:35 -08001414 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001415 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001416}
1417
buzbee02031b12012-11-23 09:41:35 -08001418bool Codegen::GenArithOpIntLit(CompilationUnit* cu, Instruction::Code opcode,
1419 RegLocation rl_dest, RegLocation rl_src, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001420{
buzbeefa57c472012-11-21 12:06:18 -08001421 RegLocation rl_result;
buzbeecbd6d442012-11-17 14:11:25 -08001422 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
buzbeefa57c472012-11-21 12:06:18 -08001423 int shift_op = false;
1424 bool is_div = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001425
buzbee408ad162012-06-06 16:45:18 -07001426 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001427 case Instruction::RSUB_INT_LIT8:
1428 case Instruction::RSUB_INT: {
buzbeefa57c472012-11-21 12:06:18 -08001429 int t_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001430 //TUNING: add support for use of Arm rsub op
buzbeefa57c472012-11-21 12:06:18 -08001431 rl_src = LoadValue(cu, rl_src, kCoreReg);
1432 t_reg = AllocTemp(cu);
1433 LoadConstant(cu, t_reg, lit);
1434 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1435 OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
1436 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001437 return false;
1438 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001439 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001440
buzbeee6285f92012-12-06 15:57:46 -08001441 case Instruction::SUB_INT:
1442 case Instruction::SUB_INT_2ADDR:
1443 lit = -lit;
1444 // Intended fallthrough
1445 case Instruction::ADD_INT:
1446 case Instruction::ADD_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001447 case Instruction::ADD_INT_LIT8:
1448 case Instruction::ADD_INT_LIT16:
1449 op = kOpAdd;
1450 break;
buzbeee6285f92012-12-06 15:57:46 -08001451 case Instruction::MUL_INT:
1452 case Instruction::MUL_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001453 case Instruction::MUL_INT_LIT8:
1454 case Instruction::MUL_INT_LIT16: {
buzbeefa57c472012-11-21 12:06:18 -08001455 if (HandleEasyMultiply(cu, rl_src, rl_dest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001456 return false;
1457 }
1458 op = kOpMul;
1459 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001460 }
buzbeee6285f92012-12-06 15:57:46 -08001461 case Instruction::AND_INT:
1462 case Instruction::AND_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001463 case Instruction::AND_INT_LIT8:
1464 case Instruction::AND_INT_LIT16:
1465 op = kOpAnd;
1466 break;
buzbeee6285f92012-12-06 15:57:46 -08001467 case Instruction::OR_INT:
1468 case Instruction::OR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001469 case Instruction::OR_INT_LIT8:
1470 case Instruction::OR_INT_LIT16:
1471 op = kOpOr;
1472 break;
buzbeee6285f92012-12-06 15:57:46 -08001473 case Instruction::XOR_INT:
1474 case Instruction::XOR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001475 case Instruction::XOR_INT_LIT8:
1476 case Instruction::XOR_INT_LIT16:
1477 op = kOpXor;
1478 break;
1479 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001480 case Instruction::SHL_INT:
buzbeee6285f92012-12-06 15:57:46 -08001481 case Instruction::SHL_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001482 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001483 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001484 op = kOpLsl;
1485 break;
1486 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001487 case Instruction::SHR_INT:
buzbeee6285f92012-12-06 15:57:46 -08001488 case Instruction::SHR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001489 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001490 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001491 op = kOpAsr;
1492 break;
1493 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001494 case Instruction::USHR_INT:
buzbeee6285f92012-12-06 15:57:46 -08001495 case Instruction::USHR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001496 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001497 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001498 op = kOpLsr;
1499 break;
1500
buzbeee6285f92012-12-06 15:57:46 -08001501 case Instruction::DIV_INT:
1502 case Instruction::DIV_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001503 case Instruction::DIV_INT_LIT8:
1504 case Instruction::DIV_INT_LIT16:
buzbeee6285f92012-12-06 15:57:46 -08001505 case Instruction::REM_INT:
1506 case Instruction::REM_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001507 case Instruction::REM_INT_LIT8:
jeffhao4f8f04a2012-10-02 18:10:35 -07001508 case Instruction::REM_INT_LIT16: {
Bill Buzbeea114add2012-05-03 15:00:40 -07001509 if (lit == 0) {
buzbeefa57c472012-11-21 12:06:18 -08001510 GenImmedCheck(cu, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001511 return false;
1512 }
buzbeefa57c472012-11-21 12:06:18 -08001513 if (HandleEasyDivide(cu, opcode, rl_src, rl_dest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001514 return false;
1515 }
buzbee408ad162012-06-06 16:45:18 -07001516 if ((opcode == Instruction::DIV_INT_LIT8) ||
buzbeee6285f92012-12-06 15:57:46 -08001517 (opcode == Instruction::DIV_INT) ||
1518 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee408ad162012-06-06 16:45:18 -07001519 (opcode == Instruction::DIV_INT_LIT16)) {
buzbeefa57c472012-11-21 12:06:18 -08001520 is_div = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001521 } else {
buzbeefa57c472012-11-21 12:06:18 -08001522 is_div = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001523 }
buzbeefa57c472012-11-21 12:06:18 -08001524 if (cu->instruction_set == kMips) {
1525 rl_src = LoadValue(cu, rl_src, kCoreReg);
1526 rl_result = GenDivRemLit(cu, rl_dest, rl_src.low_reg, lit, is_div);
jeffhao4f8f04a2012-10-02 18:10:35 -07001527 } else {
buzbeefa57c472012-11-21 12:06:18 -08001528 FlushAllRegs(cu); /* Everything to home location */
1529 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0));
1530 Clobber(cu, TargetReg(kArg0));
1531 int func_offset = ENTRYPOINT_OFFSET(pIdivmod);
1532 CallRuntimeHelperRegImm(cu, func_offset, TargetReg(kArg0), lit, false);
1533 if (is_div)
1534 rl_result = GetReturn(cu, false);
buzbeeb046e162012-10-30 15:48:42 -07001535 else
buzbeefa57c472012-11-21 12:06:18 -08001536 rl_result = GetReturnAlt(cu);
jeffhao4f8f04a2012-10-02 18:10:35 -07001537 }
buzbeefa57c472012-11-21 12:06:18 -08001538 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001539 return false;
1540 break;
jeffhao4f8f04a2012-10-02 18:10:35 -07001541 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001542 default:
buzbeee6285f92012-12-06 15:57:46 -08001543 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001544 }
buzbeefa57c472012-11-21 12:06:18 -08001545 rl_src = LoadValue(cu, rl_src, kCoreReg);
1546 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001547 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
buzbeefa57c472012-11-21 12:06:18 -08001548 if (shift_op && (lit == 0)) {
1549 OpRegCopy(cu, rl_result.low_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001550 } else {
buzbeefa57c472012-11-21 12:06:18 -08001551 OpRegRegImm(cu, op, rl_result.low_reg, rl_src.low_reg, lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001552 }
buzbeefa57c472012-11-21 12:06:18 -08001553 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001554 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001555}
1556
buzbee02031b12012-11-23 09:41:35 -08001557bool Codegen::GenArithOpLong(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
1558 RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001559{
buzbeefa57c472012-11-21 12:06:18 -08001560 RegLocation rl_result;
1561 OpKind first_op = kOpBkpt;
1562 OpKind second_op = kOpBkpt;
1563 bool call_out = false;
1564 bool check_zero = false;
1565 int func_offset;
1566 int ret_reg = TargetReg(kRet0);
buzbee31a4a6f2012-02-28 15:36:15 -08001567
buzbee408ad162012-06-06 16:45:18 -07001568 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001569 case Instruction::NOT_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001570 rl_src2 = LoadValueWide(cu, rl_src2, kCoreReg);
1571 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001572 // Check for destructive overlap
buzbeefa57c472012-11-21 12:06:18 -08001573 if (rl_result.low_reg == rl_src2.high_reg) {
1574 int t_reg = AllocTemp(cu);
1575 OpRegCopy(cu, t_reg, rl_src2.high_reg);
1576 OpRegReg(cu, kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1577 OpRegReg(cu, kOpMvn, rl_result.high_reg, t_reg);
1578 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001579 } else {
buzbeefa57c472012-11-21 12:06:18 -08001580 OpRegReg(cu, kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1581 OpRegReg(cu, kOpMvn, rl_result.high_reg, rl_src2.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001582 }
buzbeefa57c472012-11-21 12:06:18 -08001583 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001584 return false;
1585 break;
1586 case Instruction::ADD_LONG:
1587 case Instruction::ADD_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001588 if (cu->instruction_set != kThumb2) {
1589 return GenAddLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001590 }
buzbeefa57c472012-11-21 12:06:18 -08001591 first_op = kOpAdd;
1592 second_op = kOpAdc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001593 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001594 case Instruction::SUB_LONG:
1595 case Instruction::SUB_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001596 if (cu->instruction_set != kThumb2) {
1597 return GenSubLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001598 }
buzbeefa57c472012-11-21 12:06:18 -08001599 first_op = kOpSub;
1600 second_op = kOpSbc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001601 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001602 case Instruction::MUL_LONG:
1603 case Instruction::MUL_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001604 call_out = true;
1605 ret_reg = TargetReg(kRet0);
1606 func_offset = ENTRYPOINT_OFFSET(pLmul);
Bill Buzbeea114add2012-05-03 15:00:40 -07001607 break;
1608 case Instruction::DIV_LONG:
1609 case Instruction::DIV_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001610 call_out = true;
1611 check_zero = true;
1612 ret_reg = TargetReg(kRet0);
1613 func_offset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07001614 break;
1615 case Instruction::REM_LONG:
1616 case Instruction::REM_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001617 call_out = true;
1618 check_zero = true;
1619 func_offset = ENTRYPOINT_OFFSET(pLdivmod);
buzbeef0504cd2012-11-13 16:31:10 -08001620 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbeefa57c472012-11-21 12:06:18 -08001621 ret_reg = (cu->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001622 break;
1623 case Instruction::AND_LONG_2ADDR:
1624 case Instruction::AND_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001625 if (cu->instruction_set == kX86) {
1626 return GenAndLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001627 }
buzbeefa57c472012-11-21 12:06:18 -08001628 first_op = kOpAnd;
1629 second_op = kOpAnd;
Bill Buzbeea114add2012-05-03 15:00:40 -07001630 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001631 case Instruction::OR_LONG:
1632 case Instruction::OR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001633 if (cu->instruction_set == kX86) {
1634 return GenOrLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001635 }
buzbeefa57c472012-11-21 12:06:18 -08001636 first_op = kOpOr;
1637 second_op = kOpOr;
Bill Buzbeea114add2012-05-03 15:00:40 -07001638 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001639 case Instruction::XOR_LONG:
1640 case Instruction::XOR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001641 if (cu->instruction_set == kX86) {
1642 return GenXorLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001643 }
buzbeefa57c472012-11-21 12:06:18 -08001644 first_op = kOpXor;
1645 second_op = kOpXor;
Bill Buzbeea114add2012-05-03 15:00:40 -07001646 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001647 case Instruction::NEG_LONG: {
buzbeefa57c472012-11-21 12:06:18 -08001648 return GenNegLong(cu, rl_dest, rl_src2);
buzbee31a4a6f2012-02-28 15:36:15 -08001649 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001650 default:
1651 LOG(FATAL) << "Invalid long arith op";
1652 }
buzbeefa57c472012-11-21 12:06:18 -08001653 if (!call_out) {
1654 GenLong3Addr(cu, first_op, second_op, rl_dest, rl_src1, rl_src2);
Bill Buzbeea114add2012-05-03 15:00:40 -07001655 } else {
buzbeefa57c472012-11-21 12:06:18 -08001656 FlushAllRegs(cu); /* Send everything to home location */
1657 if (check_zero) {
1658 LoadValueDirectWideFixed(cu, rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1659 int r_tgt = CallHelperSetup(cu, func_offset);
1660 GenDivZeroCheck(cu, TargetReg(kArg2), TargetReg(kArg3));
1661 LoadValueDirectWideFixed(cu, rl_src1, TargetReg(kArg0), TargetReg(kArg1));
buzbee8320f382012-09-11 16:29:42 -07001662 // NOTE: callout here is not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001663 CallHelper(cu, r_tgt, func_offset, false /* not safepoint */);
buzbee31a4a6f2012-02-28 15:36:15 -08001664 } else {
buzbeefa57c472012-11-21 12:06:18 -08001665 CallRuntimeHelperRegLocationRegLocation(cu, func_offset,
1666 rl_src1, rl_src2, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001667 }
buzbeef0504cd2012-11-13 16:31:10 -08001668 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbeefa57c472012-11-21 12:06:18 -08001669 if (ret_reg == TargetReg(kRet0))
1670 rl_result = GetReturnWide(cu, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07001671 else
buzbeefa57c472012-11-21 12:06:18 -08001672 rl_result = GetReturnWideAlt(cu);
1673 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001674 }
1675 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001676}
1677
buzbee02031b12012-11-23 09:41:35 -08001678bool Codegen::GenConversionCall(CompilationUnit* cu, int func_offset,
1679 RegLocation rl_dest, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -08001680{
Bill Buzbeea114add2012-05-03 15:00:40 -07001681 /*
1682 * Don't optimize the register usage since it calls out to support
1683 * functions
1684 */
buzbeefa57c472012-11-21 12:06:18 -08001685 FlushAllRegs(cu); /* Send everything to home location */
1686 if (rl_src.wide) {
1687 LoadValueDirectWideFixed(cu, rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0),
1688 rl_src.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
buzbee408ad162012-06-06 16:45:18 -07001689 } else {
buzbeefa57c472012-11-21 12:06:18 -08001690 LoadValueDirectFixed(cu, rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -07001691 }
buzbeefa57c472012-11-21 12:06:18 -08001692 CallRuntimeHelperRegLocation(cu, func_offset, rl_src, false);
1693 if (rl_dest.wide) {
1694 RegLocation rl_result;
1695 rl_result = GetReturnWide(cu, rl_dest.fp);
1696 StoreValueWide(cu, rl_dest, rl_result);
buzbee408ad162012-06-06 16:45:18 -07001697 } else {
buzbeefa57c472012-11-21 12:06:18 -08001698 RegLocation rl_result;
1699 rl_result = GetReturn(cu, rl_dest.fp);
1700 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001701 }
1702 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001703}
1704
buzbee31a4a6f2012-02-28 15:36:15 -08001705/* Check if we need to check for pending suspend request */
buzbee02031b12012-11-23 09:41:35 -08001706void Codegen::GenSuspendTest(CompilationUnit* cu, int opt_flags)
buzbee31a4a6f2012-02-28 15:36:15 -08001707{
buzbeefa57c472012-11-21 12:06:18 -08001708 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001709 return;
1710 }
buzbeefa57c472012-11-21 12:06:18 -08001711 FlushAllRegs(cu);
1712 LIR* branch = OpTestSuspend(cu, NULL);
1713 LIR* ret_lab = NewLIR0(cu, kPseudoTargetLabel);
1714 LIR* target = RawLIR(cu, cu->current_dalvik_offset, kPseudoSuspendTarget,
1715 reinterpret_cast<uintptr_t>(ret_lab), cu->current_dalvik_offset);
buzbeecbd6d442012-11-17 14:11:25 -08001716 branch->target = target;
buzbeefa57c472012-11-21 12:06:18 -08001717 InsertGrowableList(cu, &cu->suspend_launchpads, reinterpret_cast<uintptr_t>(target));
buzbee31a4a6f2012-02-28 15:36:15 -08001718}
1719
buzbeefead2932012-03-30 14:02:01 -07001720/* Check if we need to check for pending suspend request */
buzbee02031b12012-11-23 09:41:35 -08001721void Codegen::GenSuspendTestAndBranch(CompilationUnit* cu, int opt_flags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07001722{
buzbeefa57c472012-11-21 12:06:18 -08001723 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1724 OpUnconditionalBranch(cu, target);
Bill Buzbeea114add2012-05-03 15:00:40 -07001725 return;
1726 }
buzbeefa57c472012-11-21 12:06:18 -08001727 OpTestSuspend(cu, target);
1728 LIR* launch_pad =
1729 RawLIR(cu, cu->current_dalvik_offset, kPseudoSuspendTarget,
1730 reinterpret_cast<uintptr_t>(target), cu->current_dalvik_offset);
1731 FlushAllRegs(cu);
1732 OpUnconditionalBranch(cu, launch_pad);
1733 InsertGrowableList(cu, &cu->suspend_launchpads, reinterpret_cast<uintptr_t>(launch_pad));
buzbeefead2932012-03-30 14:02:01 -07001734}
1735
buzbee31a4a6f2012-02-28 15:36:15 -08001736} // namespace art