blob: 275aee5a683d504ea7c29b8cc08798234122b7ca [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
buzbeefa57c472012-11-21 12:06:18 -0800373 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker, *cu->dex_file,
374 cu->code_item, 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,
466 *cu->dex_file,
467 cu->code_item, 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::GenMoveException(CompilationUnit* cu, RegLocation rl_dest)
Ian Rogers474b6da2012-09-25 00:20:38 -0700919{
buzbeefa57c472012-11-21 12:06:18 -0800920 FlushAllRegs(cu); /* Everything to home location */
921 int func_offset = ENTRYPOINT_OFFSET(pGetAndClearException);
922 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700923 // Runtime helper will load argument for x86.
buzbeefa57c472012-11-21 12:06:18 -0800924 CallRuntimeHelperReg(cu, func_offset, TargetReg(kArg0), false);
buzbeeb046e162012-10-30 15:48:42 -0700925 } else {
buzbeefa57c472012-11-21 12:06:18 -0800926 CallRuntimeHelperReg(cu, func_offset, TargetReg(kSelf), false);
buzbeeb046e162012-10-30 15:48:42 -0700927 }
buzbeefa57c472012-11-21 12:06:18 -0800928 RegLocation rl_result = GetReturn(cu, false);
929 StoreValue(cu, rl_dest, rl_result);
Ian Rogers474b6da2012-09-25 00:20:38 -0700930}
931
buzbee02031b12012-11-23 09:41:35 -0800932void Codegen::GenThrow(CompilationUnit* cu, RegLocation rl_src)
Ian Rogersab2b55d2012-03-18 00:06:11 -0700933{
buzbeefa57c472012-11-21 12:06:18 -0800934 FlushAllRegs(cu);
935 CallRuntimeHelperRegLocation(cu, ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700936}
937
buzbee02031b12012-11-23 09:41:35 -0800938void Codegen::GenInstanceof(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest,
939 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800940{
buzbeefa57c472012-11-21 12:06:18 -0800941 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700942 // May generate a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -0800943 LockCallTemps(cu);
944 LoadCurrMethodDirect(cu, TargetReg(kArg1)); // kArg1 <= current Method*
945 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
946 if (!cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
947 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 type_idx)) {
949 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbeef0504cd2012-11-13 16:31:10 -0800950 // returns Class* in kArg0
buzbeefa57c472012-11-21 12:06:18 -0800951 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbee8320f382012-09-11 16:29:42 -0700952 type_idx, true);
buzbeefa57c472012-11-21 12:06:18 -0800953 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
954 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -0700955 } else {
buzbeefa57c472012-11-21 12:06:18 -0800956 // Load dex cache entry into class_reg (kArg2)
957 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
958 LoadWordDisp(cu, TargetReg(kArg1),
959 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700960 int32_t offset_of_type =
961 Array::DataOffset(sizeof(Class*)).Int32Value() + (sizeof(Class*)
962 * type_idx);
buzbeefa57c472012-11-21 12:06:18 -0800963 LoadWordDisp(cu, class_reg, offset_of_type, class_reg);
964 if (!cu->compiler->CanAssumeTypeIsPresentInDexCache(
965 *cu->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700966 // Need to test presence of type in dex cache at runtime
buzbeefa57c472012-11-21 12:06:18 -0800967 LIR* hop_branch = OpCmpImmBranch(cu, kCondNe, class_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700968 // Not resolved
buzbeef0504cd2012-11-13 16:31:10 -0800969 // Call out to helper, which will return resolved type in kRet0
buzbeefa57c472012-11-21 12:06:18 -0800970 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, true);
971 OpRegCopy(cu, TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
972 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); /* reload Ref */
Bill Buzbeea114add2012-05-03 15:00:40 -0700973 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -0800974 LIR* hop_target = NewLIR0(cu, kPseudoTargetLabel);
975 hop_branch->target = hop_target;
buzbee31a4a6f2012-02-28 15:36:15 -0800976 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700977 }
buzbeef0504cd2012-11-13 16:31:10 -0800978 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeefa57c472012-11-21 12:06:18 -0800979 RegLocation rl_result = GetReturn(cu, false);
980 if (cu->instruction_set == kMips) {
981 LoadConstant(cu, rl_result.low_reg, 0); // store false result for if branch is taken
buzbeeb046e162012-10-30 15:48:42 -0700982 }
buzbeefa57c472012-11-21 12:06:18 -0800983 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, TargetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700984 /* load object->klass_ */
985 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbeefa57c472012-11-21 12:06:18 -0800986 LoadWordDisp(cu, TargetReg(kArg0), Object::ClassOffset().Int32Value(), TargetReg(kArg1));
buzbeef0504cd2012-11-13 16:31:10 -0800987 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
buzbeefa57c472012-11-21 12:06:18 -0800988 LIR* call_inst;
buzbeeb046e162012-10-30 15:48:42 -0700989 LIR* branchover = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800990 if (cu->instruction_set == kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -0700991 /* Uses conditional nullification */
buzbeefa57c472012-11-21 12:06:18 -0800992 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
993 OpRegReg(cu, kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
buzbee02031b12012-11-23 09:41:35 -0800994 OpIT(cu, kCondEq, "EE"); // if-convert the test
buzbeefa57c472012-11-21 12:06:18 -0800995 LoadConstant(cu, TargetReg(kArg0), 1); // .eq case - load true
996 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
997 call_inst = OpReg(cu, kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
998 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700999 } else {
1000 /* Uses branchovers */
buzbeefa57c472012-11-21 12:06:18 -08001001 LoadConstant(cu, rl_result.low_reg, 1); // assume true
1002 branchover = OpCmpBranch(cu, kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
1003 if (cu->instruction_set != kX86) {
1004 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
1005 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
1006 call_inst = OpReg(cu, kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
1007 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -07001008 } else {
buzbeefa57c472012-11-21 12:06:18 -08001009 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2));
1010 call_inst = OpThreadMem(cu, kOpBlx, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
buzbeeb046e162012-10-30 15:48:42 -07001011 }
1012 }
buzbeefa57c472012-11-21 12:06:18 -08001013 MarkSafepointPC(cu, call_inst);
1014 ClobberCalleeSave(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001015 /* branch targets here */
buzbeefa57c472012-11-21 12:06:18 -08001016 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
1017 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001018 branch1->target = target;
buzbeefa57c472012-11-21 12:06:18 -08001019 if (cu->instruction_set != kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -07001020 branchover->target = target;
1021 }
buzbee31a4a6f2012-02-28 15:36:15 -08001022}
1023
buzbee02031b12012-11-23 09:41:35 -08001024void Codegen::GenCheckCast(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -08001025{
buzbeefa57c472012-11-21 12:06:18 -08001026 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001027 // May generate a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -08001028 LockCallTemps(cu);
1029 LoadCurrMethodDirect(cu, TargetReg(kArg1)); // kArg1 <= current Method*
1030 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1031 if (!cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
1032 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -07001033 type_idx)) {
1034 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbeef0504cd2012-11-13 16:31:10 -08001035 // returns Class* in kRet0
Bill Buzbeea114add2012-05-03 15:00:40 -07001036 // InitializeTypeAndVerifyAccess(idx, method)
buzbeefa57c472012-11-21 12:06:18 -08001037 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbee52a77fc2012-11-20 19:50:46 -08001038 type_idx, TargetReg(kArg1), true);
buzbeefa57c472012-11-21 12:06:18 -08001039 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001040 } else {
buzbeefa57c472012-11-21 12:06:18 -08001041 // Load dex cache entry into class_reg (kArg2)
1042 LoadWordDisp(cu, TargetReg(kArg1),
1043 AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001044 int32_t offset_of_type =
1045 Array::DataOffset(sizeof(Class*)).Int32Value() +
1046 (sizeof(Class*) * type_idx);
buzbeefa57c472012-11-21 12:06:18 -08001047 LoadWordDisp(cu, class_reg, offset_of_type, class_reg);
1048 if (!cu->compiler->CanAssumeTypeIsPresentInDexCache(
1049 *cu->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001050 // Need to test presence of type in dex cache at runtime
buzbeefa57c472012-11-21 12:06:18 -08001051 LIR* hop_branch = OpCmpImmBranch(cu, kCondNe, class_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001052 // Not resolved
buzbeef0504cd2012-11-13 16:31:10 -08001053 // Call out to helper, which will return resolved type in kArg0
Bill Buzbeea114add2012-05-03 15:00:40 -07001054 // InitializeTypeFromCode(idx, method)
buzbeefa57c472012-11-21 12:06:18 -08001055 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, TargetReg(kArg1),
buzbee8320f382012-09-11 16:29:42 -07001056 true);
buzbeefa57c472012-11-21 12:06:18 -08001057 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001058 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -08001059 LIR* hop_target = NewLIR0(cu, kPseudoTargetLabel);
1060 hop_branch->target = hop_target;
buzbee31a4a6f2012-02-28 15:36:15 -08001061 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001062 }
buzbeefa57c472012-11-21 12:06:18 -08001063 // At this point, class_reg (kArg2) has class
1064 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -07001065 /* Null is OK - continue */
buzbeefa57c472012-11-21 12:06:18 -08001066 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, TargetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001067 /* load object->klass_ */
1068 DCHECK_EQ(Object::ClassOffset().Int32Value(), 0);
buzbeefa57c472012-11-21 12:06:18 -08001069 LoadWordDisp(cu, TargetReg(kArg0), Object::ClassOffset().Int32Value(), TargetReg(kArg1));
buzbeef0504cd2012-11-13 16:31:10 -08001070 /* kArg1 now contains object->klass_ */
buzbeeb046e162012-10-30 15:48:42 -07001071 LIR* branch2;
buzbeefa57c472012-11-21 12:06:18 -08001072 if (cu->instruction_set == kThumb2) {
1073 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1074 OpRegReg(cu, kOpCmp, TargetReg(kArg1), class_reg);
1075 branch2 = OpCondBranch(cu, kCondEq, NULL); /* If eq, trivial yes */
1076 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg1));
1077 OpRegCopy(cu, TargetReg(kArg1), TargetReg(kArg2));
1078 ClobberCalleeSave(cu);
1079 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt);
1080 MarkSafepointPC(cu, call_inst);
1081 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -07001082 } else {
buzbeefa57c472012-11-21 12:06:18 -08001083 branch2 = OpCmpBranch(cu, kCondEq, TargetReg(kArg1), class_reg, NULL);
1084 CallRuntimeHelperRegReg(cu, ENTRYPOINT_OFFSET(pCheckCastFromCode), TargetReg(kArg1), TargetReg(kArg2), true);
buzbeeb046e162012-10-30 15:48:42 -07001085 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001086 /* branch target here */
buzbeefa57c472012-11-21 12:06:18 -08001087 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -07001088 branch1->target = target;
1089 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001090}
1091
buzbee02031b12012-11-23 09:41:35 -08001092void Codegen::GenLong3Addr(CompilationUnit* cu, OpKind first_op, OpKind second_op,
1093 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001094{
buzbeefa57c472012-11-21 12:06:18 -08001095 RegLocation rl_result;
1096 if (cu->instruction_set == kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -07001097 /*
1098 * NOTE: This is the one place in the code in which we might have
1099 * as many as six live temporary registers. There are 5 in the normal
1100 * set for Arm. Until we have spill capabilities, temporarily add
1101 * lr to the temp set. It is safe to do this locally, but note that
1102 * lr is used explicitly elsewhere in the code generator and cannot
1103 * normally be used as a general temp register.
1104 */
buzbeefa57c472012-11-21 12:06:18 -08001105 MarkTemp(cu, TargetReg(kLr)); // Add lr to the temp pool
1106 FreeTemp(cu, TargetReg(kLr)); // and make it available
buzbeeb046e162012-10-30 15:48:42 -07001107 }
buzbeefa57c472012-11-21 12:06:18 -08001108 rl_src1 = LoadValueWide(cu, rl_src1, kCoreReg);
1109 rl_src2 = LoadValueWide(cu, rl_src2, kCoreReg);
1110 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001111 // The longs may overlap - use intermediate temp if so
buzbeefa57c472012-11-21 12:06:18 -08001112 if ((rl_result.low_reg == rl_src1.high_reg) || (rl_result.low_reg == rl_src2.high_reg)){
1113 int t_reg = AllocTemp(cu);
1114 OpRegRegReg(cu, first_op, t_reg, rl_src1.low_reg, rl_src2.low_reg);
1115 OpRegRegReg(cu, second_op, rl_result.high_reg, rl_src1.high_reg, rl_src2.high_reg);
1116 OpRegCopy(cu, rl_result.low_reg, t_reg);
1117 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001118 } else {
buzbeefa57c472012-11-21 12:06:18 -08001119 OpRegRegReg(cu, first_op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1120 OpRegRegReg(cu, second_op, rl_result.high_reg, rl_src1.high_reg,
1121 rl_src2.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001122 }
1123 /*
buzbeefa57c472012-11-21 12:06:18 -08001124 * NOTE: If rl_dest refers to a frame variable in a large frame, the
buzbee52a77fc2012-11-20 19:50:46 -08001125 * following StoreValueWide might need to allocate a temp register.
Bill Buzbeea114add2012-05-03 15:00:40 -07001126 * To further work around the lack of a spill capability, explicitly
buzbeefa57c472012-11-21 12:06:18 -08001127 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
Bill Buzbeea114add2012-05-03 15:00:40 -07001128 * Remove when spill is functional.
1129 */
buzbeefa57c472012-11-21 12:06:18 -08001130 FreeRegLocTemps(cu, rl_result, rl_src1);
1131 FreeRegLocTemps(cu, rl_result, rl_src2);
1132 StoreValueWide(cu, rl_dest, rl_result);
1133 if (cu->instruction_set == kThumb2) {
1134 Clobber(cu, TargetReg(kLr));
1135 UnmarkTemp(cu, TargetReg(kLr)); // Remove lr from the temp pool
buzbeeb046e162012-10-30 15:48:42 -07001136 }
buzbee31a4a6f2012-02-28 15:36:15 -08001137}
1138
1139
buzbee02031b12012-11-23 09:41:35 -08001140bool Codegen::GenShiftOpLong(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
1141 RegLocation rl_src1, RegLocation rl_shift)
buzbee31a4a6f2012-02-28 15:36:15 -08001142{
buzbeefa57c472012-11-21 12:06:18 -08001143 int func_offset;
buzbee31a4a6f2012-02-28 15:36:15 -08001144
buzbee408ad162012-06-06 16:45:18 -07001145 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001146 case Instruction::SHL_LONG:
1147 case Instruction::SHL_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001148 func_offset = ENTRYPOINT_OFFSET(pShlLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001149 break;
1150 case Instruction::SHR_LONG:
1151 case Instruction::SHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001152 func_offset = ENTRYPOINT_OFFSET(pShrLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001153 break;
1154 case Instruction::USHR_LONG:
1155 case Instruction::USHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001156 func_offset = ENTRYPOINT_OFFSET(pUshrLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001157 break;
1158 default:
1159 LOG(FATAL) << "Unexpected case";
1160 return true;
1161 }
buzbeefa57c472012-11-21 12:06:18 -08001162 FlushAllRegs(cu); /* Send everything to home location */
1163 CallRuntimeHelperRegLocationRegLocation(cu, func_offset, rl_src1, rl_shift, false);
1164 RegLocation rl_result = GetReturnWide(cu, false);
1165 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001166 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001167}
1168
1169
buzbee02031b12012-11-23 09:41:35 -08001170bool Codegen::GenArithOpInt(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
1171 RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001172{
Bill Buzbeea114add2012-05-03 15:00:40 -07001173 OpKind op = kOpBkpt;
buzbeefa57c472012-11-21 12:06:18 -08001174 bool is_div_rem = false;
1175 bool check_zero = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001176 bool unary = false;
buzbeefa57c472012-11-21 12:06:18 -08001177 RegLocation rl_result;
1178 bool shift_op = false;
buzbee408ad162012-06-06 16:45:18 -07001179 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001180 case Instruction::NEG_INT:
1181 op = kOpNeg;
1182 unary = true;
1183 break;
1184 case Instruction::NOT_INT:
1185 op = kOpMvn;
1186 unary = true;
1187 break;
1188 case Instruction::ADD_INT:
1189 case Instruction::ADD_INT_2ADDR:
1190 op = kOpAdd;
1191 break;
1192 case Instruction::SUB_INT:
1193 case Instruction::SUB_INT_2ADDR:
1194 op = kOpSub;
1195 break;
1196 case Instruction::MUL_INT:
1197 case Instruction::MUL_INT_2ADDR:
1198 op = kOpMul;
1199 break;
1200 case Instruction::DIV_INT:
1201 case Instruction::DIV_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001202 check_zero = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001203 op = kOpDiv;
buzbeefa57c472012-11-21 12:06:18 -08001204 is_div_rem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001205 break;
buzbeef0504cd2012-11-13 16:31:10 -08001206 /* NOTE: returns in kArg1 */
Bill Buzbeea114add2012-05-03 15:00:40 -07001207 case Instruction::REM_INT:
1208 case Instruction::REM_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001209 check_zero = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001210 op = kOpRem;
buzbeefa57c472012-11-21 12:06:18 -08001211 is_div_rem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001212 break;
1213 case Instruction::AND_INT:
1214 case Instruction::AND_INT_2ADDR:
1215 op = kOpAnd;
1216 break;
1217 case Instruction::OR_INT:
1218 case Instruction::OR_INT_2ADDR:
1219 op = kOpOr;
1220 break;
1221 case Instruction::XOR_INT:
1222 case Instruction::XOR_INT_2ADDR:
1223 op = kOpXor;
1224 break;
1225 case Instruction::SHL_INT:
1226 case Instruction::SHL_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001227 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001228 op = kOpLsl;
1229 break;
1230 case Instruction::SHR_INT:
1231 case Instruction::SHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001232 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001233 op = kOpAsr;
1234 break;
1235 case Instruction::USHR_INT:
1236 case Instruction::USHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001237 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001238 op = kOpLsr;
1239 break;
1240 default:
buzbeecbd6d442012-11-17 14:11:25 -08001241 LOG(FATAL) << "Invalid word arith op: " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001242 }
buzbeefa57c472012-11-21 12:06:18 -08001243 if (!is_div_rem) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001244 if (unary) {
buzbeefa57c472012-11-21 12:06:18 -08001245 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1246 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1247 OpRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg);
buzbee31a4a6f2012-02-28 15:36:15 -08001248 } else {
buzbeefa57c472012-11-21 12:06:18 -08001249 if (shift_op) {
1250 int t_reg = INVALID_REG;
1251 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -07001252 // X86 doesn't require masking and must use ECX
buzbeefa57c472012-11-21 12:06:18 -08001253 t_reg = TargetReg(kCount); // rCX
1254 LoadValueDirectFixed(cu, rl_src2, t_reg);
buzbeeb046e162012-10-30 15:48:42 -07001255 } else {
buzbeefa57c472012-11-21 12:06:18 -08001256 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1257 t_reg = AllocTemp(cu);
1258 OpRegRegImm(cu, kOpAnd, t_reg, rl_src2.low_reg, 31);
buzbeeb046e162012-10-30 15:48:42 -07001259 }
buzbeefa57c472012-11-21 12:06:18 -08001260 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1261 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1262 OpRegRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg, t_reg);
1263 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001264 } else {
buzbeefa57c472012-11-21 12:06:18 -08001265 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1266 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1267 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1268 OpRegRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001269 }
buzbee31a4a6f2012-02-28 15:36:15 -08001270 }
buzbeefa57c472012-11-21 12:06:18 -08001271 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001272 } else {
buzbeefa57c472012-11-21 12:06:18 -08001273 if (cu->instruction_set == kMips) {
1274 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1275 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1276 if (check_zero) {
1277 GenImmedCheck(cu, kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001278 }
buzbeefa57c472012-11-21 12:06:18 -08001279 rl_result = GenDivRem(cu, rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
jeffhao4f8f04a2012-10-02 18:10:35 -07001280 } else {
buzbeefa57c472012-11-21 12:06:18 -08001281 int func_offset = ENTRYPOINT_OFFSET(pIdivmod);
1282 FlushAllRegs(cu); /* Send everything to home location */
1283 LoadValueDirectFixed(cu, rl_src2, TargetReg(kArg1));
1284 int r_tgt = CallHelperSetup(cu, func_offset);
1285 LoadValueDirectFixed(cu, rl_src1, TargetReg(kArg0));
1286 if (check_zero) {
1287 GenImmedCheck(cu, kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001288 }
1289 // NOTE: callout here is not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001290 CallHelper(cu, r_tgt, func_offset, false /* not a safepoint */ );
buzbeeb046e162012-10-30 15:48:42 -07001291 if (op == kOpDiv)
buzbeefa57c472012-11-21 12:06:18 -08001292 rl_result = GetReturn(cu, false);
buzbeeb046e162012-10-30 15:48:42 -07001293 else
buzbeefa57c472012-11-21 12:06:18 -08001294 rl_result = GetReturnAlt(cu);
jeffhao4f8f04a2012-10-02 18:10:35 -07001295 }
buzbeefa57c472012-11-21 12:06:18 -08001296 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001297 }
1298 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001299}
1300
1301/*
1302 * The following are the first-level codegen routines that analyze the format
1303 * of each bytecode then either dispatch special purpose codegen routines
1304 * or produce corresponding Thumb instructions directly.
1305 */
1306
buzbeeaad94382012-11-21 07:40:50 -08001307static bool IsPowerOfTwo(int x)
buzbee31a4a6f2012-02-28 15:36:15 -08001308{
Bill Buzbeea114add2012-05-03 15:00:40 -07001309 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001310}
1311
1312// Returns true if no more than two bits are set in 'x'.
buzbeeaad94382012-11-21 07:40:50 -08001313static bool IsPopCountLE2(unsigned int x)
buzbee31a4a6f2012-02-28 15:36:15 -08001314{
Bill Buzbeea114add2012-05-03 15:00:40 -07001315 x &= x - 1;
1316 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001317}
1318
1319// Returns the index of the lowest set bit in 'x'.
buzbeeaad94382012-11-21 07:40:50 -08001320static int LowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001321 int bit_posn = 0;
1322 while ((x & 0xf) == 0) {
1323 bit_posn += 4;
1324 x >>= 4;
1325 }
1326 while ((x & 1) == 0) {
1327 bit_posn++;
1328 x >>= 1;
1329 }
1330 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001331}
1332
buzbeefa57c472012-11-21 12:06:18 -08001333// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1334// and store the result in 'rl_dest'.
1335static bool HandleEasyDivide(CompilationUnit* cu, Instruction::Code dalvik_opcode,
1336 RegLocation rl_src, RegLocation rl_dest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001337{
buzbeefa57c472012-11-21 12:06:18 -08001338 if ((lit < 2) || ((cu->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001339 return false;
buzbee0f79d722012-11-01 15:35:27 -07001340 }
buzbee02031b12012-11-23 09:41:35 -08001341 Codegen* cg = cu->cg.get();
buzbeeb046e162012-10-30 15:48:42 -07001342 // No divide instruction for Arm, so check for more special cases
buzbeefa57c472012-11-21 12:06:18 -08001343 if ((cu->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee02031b12012-11-23 09:41:35 -08001344 return cg->SmallLiteralDivide(cu, dalvik_opcode, rl_src, rl_dest, lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001345 }
buzbee52a77fc2012-11-20 19:50:46 -08001346 int k = LowestSetBit(lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001347 if (k >= 30) {
1348 // Avoid special cases.
1349 return false;
1350 }
buzbeefa57c472012-11-21 12:06:18 -08001351 bool div = (dalvik_opcode == Instruction::DIV_INT_LIT8 ||
1352 dalvik_opcode == Instruction::DIV_INT_LIT16);
buzbee02031b12012-11-23 09:41:35 -08001353 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001354 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001355 if (div) {
buzbeefa57c472012-11-21 12:06:18 -08001356 int t_reg = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001357 if (lit == 2) {
1358 // Division by 2 is by far the most common division by constant.
buzbee02031b12012-11-23 09:41:35 -08001359 cg->OpRegRegImm(cu, kOpLsr, t_reg, rl_src.low_reg, 32 - k);
1360 cg->OpRegRegReg(cu, kOpAdd, t_reg, t_reg, rl_src.low_reg);
1361 cg->OpRegRegImm(cu, kOpAsr, rl_result.low_reg, t_reg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001362 } else {
buzbee02031b12012-11-23 09:41:35 -08001363 cg->OpRegRegImm(cu, kOpAsr, t_reg, rl_src.low_reg, 31);
1364 cg->OpRegRegImm(cu, kOpLsr, t_reg, t_reg, 32 - k);
1365 cg->OpRegRegReg(cu, kOpAdd, t_reg, t_reg, rl_src.low_reg);
1366 cg->OpRegRegImm(cu, kOpAsr, rl_result.low_reg, t_reg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001367 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001368 } else {
buzbeefa57c472012-11-21 12:06:18 -08001369 int t_reg1 = AllocTemp(cu);
1370 int t_reg2 = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001371 if (lit == 2) {
buzbee02031b12012-11-23 09:41:35 -08001372 cg->OpRegRegImm(cu, kOpLsr, t_reg1, rl_src.low_reg, 32 - k);
1373 cg->OpRegRegReg(cu, kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1374 cg->OpRegRegImm(cu, kOpAnd, t_reg2, t_reg2, lit -1);
1375 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Bill Buzbeea114add2012-05-03 15:00:40 -07001376 } else {
buzbee02031b12012-11-23 09:41:35 -08001377 cg->OpRegRegImm(cu, kOpAsr, t_reg1, rl_src.low_reg, 31);
1378 cg->OpRegRegImm(cu, kOpLsr, t_reg1, t_reg1, 32 - k);
1379 cg->OpRegRegReg(cu, kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1380 cg->OpRegRegImm(cu, kOpAnd, t_reg2, t_reg2, lit - 1);
1381 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Bill Buzbeea114add2012-05-03 15:00:40 -07001382 }
1383 }
buzbee02031b12012-11-23 09:41:35 -08001384 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001385 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001386}
1387
buzbeefa57c472012-11-21 12:06:18 -08001388// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1389// and store the result in 'rl_dest'.
1390static bool HandleEasyMultiply(CompilationUnit* cu, RegLocation rl_src,
1391 RegLocation rl_dest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001392{
Bill Buzbeea114add2012-05-03 15:00:40 -07001393 // Can we simplify this multiplication?
buzbeefa57c472012-11-21 12:06:18 -08001394 bool power_of_two = false;
1395 bool pop_count_le2 = false;
1396 bool power_of_two_minus_one = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001397 if (lit < 2) {
1398 // Avoid special cases.
1399 return false;
buzbee52a77fc2012-11-20 19:50:46 -08001400 } else if (IsPowerOfTwo(lit)) {
buzbeefa57c472012-11-21 12:06:18 -08001401 power_of_two = true;
buzbee52a77fc2012-11-20 19:50:46 -08001402 } else if (IsPopCountLE2(lit)) {
buzbeefa57c472012-11-21 12:06:18 -08001403 pop_count_le2 = true;
buzbee52a77fc2012-11-20 19:50:46 -08001404 } else if (IsPowerOfTwo(lit + 1)) {
buzbeefa57c472012-11-21 12:06:18 -08001405 power_of_two_minus_one = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001406 } else {
1407 return false;
1408 }
buzbee02031b12012-11-23 09:41:35 -08001409 Codegen* cg = cu->cg.get();
1410 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001411 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1412 if (power_of_two) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001413 // Shift.
buzbee02031b12012-11-23 09:41:35 -08001414 cg->OpRegRegImm(cu, kOpLsl, rl_result.low_reg, rl_src.low_reg, LowestSetBit(lit));
buzbeefa57c472012-11-21 12:06:18 -08001415 } else if (pop_count_le2) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001416 // Shift and add and shift.
buzbeefa57c472012-11-21 12:06:18 -08001417 int first_bit = LowestSetBit(lit);
1418 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
buzbee02031b12012-11-23 09:41:35 -08001419 cg->GenMultiplyByTwoBitMultiplier(cu, rl_src, rl_result, lit, first_bit, second_bit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001420 } else {
1421 // Reverse subtract: (src << (shift + 1)) - src.
buzbeefa57c472012-11-21 12:06:18 -08001422 DCHECK(power_of_two_minus_one);
buzbee52a77fc2012-11-20 19:50:46 -08001423 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbeefa57c472012-11-21 12:06:18 -08001424 int t_reg = AllocTemp(cu);
buzbee02031b12012-11-23 09:41:35 -08001425 cg->OpRegRegImm(cu, kOpLsl, t_reg, rl_src.low_reg, LowestSetBit(lit + 1));
1426 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001427 }
buzbee02031b12012-11-23 09:41:35 -08001428 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001429 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001430}
1431
buzbee02031b12012-11-23 09:41:35 -08001432bool Codegen::GenArithOpIntLit(CompilationUnit* cu, Instruction::Code opcode,
1433 RegLocation rl_dest, RegLocation rl_src, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001434{
buzbeefa57c472012-11-21 12:06:18 -08001435 RegLocation rl_result;
buzbeecbd6d442012-11-17 14:11:25 -08001436 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
buzbeefa57c472012-11-21 12:06:18 -08001437 int shift_op = false;
1438 bool is_div = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001439
buzbee408ad162012-06-06 16:45:18 -07001440 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001441 case Instruction::RSUB_INT_LIT8:
1442 case Instruction::RSUB_INT: {
buzbeefa57c472012-11-21 12:06:18 -08001443 int t_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001444 //TUNING: add support for use of Arm rsub op
buzbeefa57c472012-11-21 12:06:18 -08001445 rl_src = LoadValue(cu, rl_src, kCoreReg);
1446 t_reg = AllocTemp(cu);
1447 LoadConstant(cu, t_reg, lit);
1448 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1449 OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
1450 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001451 return false;
1452 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001453 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001454
buzbeee6285f92012-12-06 15:57:46 -08001455 case Instruction::SUB_INT:
1456 case Instruction::SUB_INT_2ADDR:
1457 lit = -lit;
1458 // Intended fallthrough
1459 case Instruction::ADD_INT:
1460 case Instruction::ADD_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001461 case Instruction::ADD_INT_LIT8:
1462 case Instruction::ADD_INT_LIT16:
1463 op = kOpAdd;
1464 break;
buzbeee6285f92012-12-06 15:57:46 -08001465 case Instruction::MUL_INT:
1466 case Instruction::MUL_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001467 case Instruction::MUL_INT_LIT8:
1468 case Instruction::MUL_INT_LIT16: {
buzbeefa57c472012-11-21 12:06:18 -08001469 if (HandleEasyMultiply(cu, rl_src, rl_dest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001470 return false;
1471 }
1472 op = kOpMul;
1473 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001474 }
buzbeee6285f92012-12-06 15:57:46 -08001475 case Instruction::AND_INT:
1476 case Instruction::AND_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001477 case Instruction::AND_INT_LIT8:
1478 case Instruction::AND_INT_LIT16:
1479 op = kOpAnd;
1480 break;
buzbeee6285f92012-12-06 15:57:46 -08001481 case Instruction::OR_INT:
1482 case Instruction::OR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001483 case Instruction::OR_INT_LIT8:
1484 case Instruction::OR_INT_LIT16:
1485 op = kOpOr;
1486 break;
buzbeee6285f92012-12-06 15:57:46 -08001487 case Instruction::XOR_INT:
1488 case Instruction::XOR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001489 case Instruction::XOR_INT_LIT8:
1490 case Instruction::XOR_INT_LIT16:
1491 op = kOpXor;
1492 break;
1493 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001494 case Instruction::SHL_INT:
buzbeee6285f92012-12-06 15:57:46 -08001495 case Instruction::SHL_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 = kOpLsl;
1499 break;
1500 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001501 case Instruction::SHR_INT:
buzbeee6285f92012-12-06 15:57:46 -08001502 case Instruction::SHR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001503 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001504 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001505 op = kOpAsr;
1506 break;
1507 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001508 case Instruction::USHR_INT:
buzbeee6285f92012-12-06 15:57:46 -08001509 case Instruction::USHR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001510 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001511 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001512 op = kOpLsr;
1513 break;
1514
buzbeee6285f92012-12-06 15:57:46 -08001515 case Instruction::DIV_INT:
1516 case Instruction::DIV_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001517 case Instruction::DIV_INT_LIT8:
1518 case Instruction::DIV_INT_LIT16:
buzbeee6285f92012-12-06 15:57:46 -08001519 case Instruction::REM_INT:
1520 case Instruction::REM_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001521 case Instruction::REM_INT_LIT8:
jeffhao4f8f04a2012-10-02 18:10:35 -07001522 case Instruction::REM_INT_LIT16: {
Bill Buzbeea114add2012-05-03 15:00:40 -07001523 if (lit == 0) {
buzbeefa57c472012-11-21 12:06:18 -08001524 GenImmedCheck(cu, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001525 return false;
1526 }
buzbeefa57c472012-11-21 12:06:18 -08001527 if (HandleEasyDivide(cu, opcode, rl_src, rl_dest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001528 return false;
1529 }
buzbee408ad162012-06-06 16:45:18 -07001530 if ((opcode == Instruction::DIV_INT_LIT8) ||
buzbeee6285f92012-12-06 15:57:46 -08001531 (opcode == Instruction::DIV_INT) ||
1532 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee408ad162012-06-06 16:45:18 -07001533 (opcode == Instruction::DIV_INT_LIT16)) {
buzbeefa57c472012-11-21 12:06:18 -08001534 is_div = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001535 } else {
buzbeefa57c472012-11-21 12:06:18 -08001536 is_div = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001537 }
buzbeefa57c472012-11-21 12:06:18 -08001538 if (cu->instruction_set == kMips) {
1539 rl_src = LoadValue(cu, rl_src, kCoreReg);
1540 rl_result = GenDivRemLit(cu, rl_dest, rl_src.low_reg, lit, is_div);
jeffhao4f8f04a2012-10-02 18:10:35 -07001541 } else {
buzbeefa57c472012-11-21 12:06:18 -08001542 FlushAllRegs(cu); /* Everything to home location */
1543 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0));
1544 Clobber(cu, TargetReg(kArg0));
1545 int func_offset = ENTRYPOINT_OFFSET(pIdivmod);
1546 CallRuntimeHelperRegImm(cu, func_offset, TargetReg(kArg0), lit, false);
1547 if (is_div)
1548 rl_result = GetReturn(cu, false);
buzbeeb046e162012-10-30 15:48:42 -07001549 else
buzbeefa57c472012-11-21 12:06:18 -08001550 rl_result = GetReturnAlt(cu);
jeffhao4f8f04a2012-10-02 18:10:35 -07001551 }
buzbeefa57c472012-11-21 12:06:18 -08001552 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001553 return false;
1554 break;
jeffhao4f8f04a2012-10-02 18:10:35 -07001555 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001556 default:
buzbeee6285f92012-12-06 15:57:46 -08001557 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001558 }
buzbeefa57c472012-11-21 12:06:18 -08001559 rl_src = LoadValue(cu, rl_src, kCoreReg);
1560 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001561 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
buzbeefa57c472012-11-21 12:06:18 -08001562 if (shift_op && (lit == 0)) {
1563 OpRegCopy(cu, rl_result.low_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001564 } else {
buzbeefa57c472012-11-21 12:06:18 -08001565 OpRegRegImm(cu, op, rl_result.low_reg, rl_src.low_reg, lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001566 }
buzbeefa57c472012-11-21 12:06:18 -08001567 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001568 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001569}
1570
buzbee02031b12012-11-23 09:41:35 -08001571bool Codegen::GenArithOpLong(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
1572 RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001573{
buzbeefa57c472012-11-21 12:06:18 -08001574 RegLocation rl_result;
1575 OpKind first_op = kOpBkpt;
1576 OpKind second_op = kOpBkpt;
1577 bool call_out = false;
1578 bool check_zero = false;
1579 int func_offset;
1580 int ret_reg = TargetReg(kRet0);
buzbee31a4a6f2012-02-28 15:36:15 -08001581
buzbee408ad162012-06-06 16:45:18 -07001582 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001583 case Instruction::NOT_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001584 rl_src2 = LoadValueWide(cu, rl_src2, kCoreReg);
1585 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001586 // Check for destructive overlap
buzbeefa57c472012-11-21 12:06:18 -08001587 if (rl_result.low_reg == rl_src2.high_reg) {
1588 int t_reg = AllocTemp(cu);
1589 OpRegCopy(cu, t_reg, rl_src2.high_reg);
1590 OpRegReg(cu, kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1591 OpRegReg(cu, kOpMvn, rl_result.high_reg, t_reg);
1592 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001593 } else {
buzbeefa57c472012-11-21 12:06:18 -08001594 OpRegReg(cu, kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1595 OpRegReg(cu, kOpMvn, rl_result.high_reg, rl_src2.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001596 }
buzbeefa57c472012-11-21 12:06:18 -08001597 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001598 return false;
1599 break;
1600 case Instruction::ADD_LONG:
1601 case Instruction::ADD_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001602 if (cu->instruction_set != kThumb2) {
1603 return GenAddLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001604 }
buzbeefa57c472012-11-21 12:06:18 -08001605 first_op = kOpAdd;
1606 second_op = kOpAdc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001607 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001608 case Instruction::SUB_LONG:
1609 case Instruction::SUB_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001610 if (cu->instruction_set != kThumb2) {
1611 return GenSubLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001612 }
buzbeefa57c472012-11-21 12:06:18 -08001613 first_op = kOpSub;
1614 second_op = kOpSbc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001615 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001616 case Instruction::MUL_LONG:
1617 case Instruction::MUL_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001618 call_out = true;
1619 ret_reg = TargetReg(kRet0);
1620 func_offset = ENTRYPOINT_OFFSET(pLmul);
Bill Buzbeea114add2012-05-03 15:00:40 -07001621 break;
1622 case Instruction::DIV_LONG:
1623 case Instruction::DIV_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001624 call_out = true;
1625 check_zero = true;
1626 ret_reg = TargetReg(kRet0);
1627 func_offset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07001628 break;
1629 case Instruction::REM_LONG:
1630 case Instruction::REM_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001631 call_out = true;
1632 check_zero = true;
1633 func_offset = ENTRYPOINT_OFFSET(pLdivmod);
buzbeef0504cd2012-11-13 16:31:10 -08001634 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbeefa57c472012-11-21 12:06:18 -08001635 ret_reg = (cu->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001636 break;
1637 case Instruction::AND_LONG_2ADDR:
1638 case Instruction::AND_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001639 if (cu->instruction_set == kX86) {
1640 return GenAndLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001641 }
buzbeefa57c472012-11-21 12:06:18 -08001642 first_op = kOpAnd;
1643 second_op = kOpAnd;
Bill Buzbeea114add2012-05-03 15:00:40 -07001644 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001645 case Instruction::OR_LONG:
1646 case Instruction::OR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001647 if (cu->instruction_set == kX86) {
1648 return GenOrLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001649 }
buzbeefa57c472012-11-21 12:06:18 -08001650 first_op = kOpOr;
1651 second_op = kOpOr;
Bill Buzbeea114add2012-05-03 15:00:40 -07001652 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001653 case Instruction::XOR_LONG:
1654 case Instruction::XOR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001655 if (cu->instruction_set == kX86) {
1656 return GenXorLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001657 }
buzbeefa57c472012-11-21 12:06:18 -08001658 first_op = kOpXor;
1659 second_op = kOpXor;
Bill Buzbeea114add2012-05-03 15:00:40 -07001660 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001661 case Instruction::NEG_LONG: {
buzbeefa57c472012-11-21 12:06:18 -08001662 return GenNegLong(cu, rl_dest, rl_src2);
buzbee31a4a6f2012-02-28 15:36:15 -08001663 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001664 default:
1665 LOG(FATAL) << "Invalid long arith op";
1666 }
buzbeefa57c472012-11-21 12:06:18 -08001667 if (!call_out) {
1668 GenLong3Addr(cu, first_op, second_op, rl_dest, rl_src1, rl_src2);
Bill Buzbeea114add2012-05-03 15:00:40 -07001669 } else {
buzbeefa57c472012-11-21 12:06:18 -08001670 FlushAllRegs(cu); /* Send everything to home location */
1671 if (check_zero) {
1672 LoadValueDirectWideFixed(cu, rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1673 int r_tgt = CallHelperSetup(cu, func_offset);
1674 GenDivZeroCheck(cu, TargetReg(kArg2), TargetReg(kArg3));
1675 LoadValueDirectWideFixed(cu, rl_src1, TargetReg(kArg0), TargetReg(kArg1));
buzbee8320f382012-09-11 16:29:42 -07001676 // NOTE: callout here is not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001677 CallHelper(cu, r_tgt, func_offset, false /* not safepoint */);
buzbee31a4a6f2012-02-28 15:36:15 -08001678 } else {
buzbeefa57c472012-11-21 12:06:18 -08001679 CallRuntimeHelperRegLocationRegLocation(cu, func_offset,
1680 rl_src1, rl_src2, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001681 }
buzbeef0504cd2012-11-13 16:31:10 -08001682 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbeefa57c472012-11-21 12:06:18 -08001683 if (ret_reg == TargetReg(kRet0))
1684 rl_result = GetReturnWide(cu, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07001685 else
buzbeefa57c472012-11-21 12:06:18 -08001686 rl_result = GetReturnWideAlt(cu);
1687 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001688 }
1689 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001690}
1691
buzbee02031b12012-11-23 09:41:35 -08001692bool Codegen::GenConversionCall(CompilationUnit* cu, int func_offset,
1693 RegLocation rl_dest, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -08001694{
Bill Buzbeea114add2012-05-03 15:00:40 -07001695 /*
1696 * Don't optimize the register usage since it calls out to support
1697 * functions
1698 */
buzbeefa57c472012-11-21 12:06:18 -08001699 FlushAllRegs(cu); /* Send everything to home location */
1700 if (rl_src.wide) {
1701 LoadValueDirectWideFixed(cu, rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0),
1702 rl_src.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
buzbee408ad162012-06-06 16:45:18 -07001703 } else {
buzbeefa57c472012-11-21 12:06:18 -08001704 LoadValueDirectFixed(cu, rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -07001705 }
buzbeefa57c472012-11-21 12:06:18 -08001706 CallRuntimeHelperRegLocation(cu, func_offset, rl_src, false);
1707 if (rl_dest.wide) {
1708 RegLocation rl_result;
1709 rl_result = GetReturnWide(cu, rl_dest.fp);
1710 StoreValueWide(cu, rl_dest, rl_result);
buzbee408ad162012-06-06 16:45:18 -07001711 } else {
buzbeefa57c472012-11-21 12:06:18 -08001712 RegLocation rl_result;
1713 rl_result = GetReturn(cu, rl_dest.fp);
1714 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001715 }
1716 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001717}
1718
buzbee31a4a6f2012-02-28 15:36:15 -08001719/* Check if we need to check for pending suspend request */
buzbee02031b12012-11-23 09:41:35 -08001720void Codegen::GenSuspendTest(CompilationUnit* cu, int opt_flags)
buzbee31a4a6f2012-02-28 15:36:15 -08001721{
buzbeefa57c472012-11-21 12:06:18 -08001722 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001723 return;
1724 }
buzbeefa57c472012-11-21 12:06:18 -08001725 FlushAllRegs(cu);
1726 LIR* branch = OpTestSuspend(cu, NULL);
1727 LIR* ret_lab = NewLIR0(cu, kPseudoTargetLabel);
1728 LIR* target = RawLIR(cu, cu->current_dalvik_offset, kPseudoSuspendTarget,
1729 reinterpret_cast<uintptr_t>(ret_lab), cu->current_dalvik_offset);
buzbeecbd6d442012-11-17 14:11:25 -08001730 branch->target = target;
buzbeefa57c472012-11-21 12:06:18 -08001731 InsertGrowableList(cu, &cu->suspend_launchpads, reinterpret_cast<uintptr_t>(target));
buzbee31a4a6f2012-02-28 15:36:15 -08001732}
1733
buzbeefead2932012-03-30 14:02:01 -07001734/* Check if we need to check for pending suspend request */
buzbee02031b12012-11-23 09:41:35 -08001735void Codegen::GenSuspendTestAndBranch(CompilationUnit* cu, int opt_flags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07001736{
buzbeefa57c472012-11-21 12:06:18 -08001737 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1738 OpUnconditionalBranch(cu, target);
Bill Buzbeea114add2012-05-03 15:00:40 -07001739 return;
1740 }
buzbeefa57c472012-11-21 12:06:18 -08001741 OpTestSuspend(cu, target);
1742 LIR* launch_pad =
1743 RawLIR(cu, cu->current_dalvik_offset, kPseudoSuspendTarget,
1744 reinterpret_cast<uintptr_t>(target), cu->current_dalvik_offset);
1745 FlushAllRegs(cu);
1746 OpUnconditionalBranch(cu, launch_pad);
1747 InsertGrowableList(cu, &cu->suspend_launchpads, reinterpret_cast<uintptr_t>(launch_pad));
buzbeefead2932012-03-30 14:02:01 -07001748}
1749
buzbee31a4a6f2012-02-28 15:36:15 -08001750} // namespace art