blob: a4c8d0cf741d026b8e896e57887b47522c3b2084 [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
buzbee1bc37c62012-11-20 13:35:41 -080017#include "codegen_util.h"
Brian Carlstrom641ce032013-01-31 15:21:37 -080018#include "compiler/compiler_ir.h"
19#include "oat/runtime/oat_support_entrypoints.h"
20#include "ralloc_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,
buzbee4ef3e452012-12-14 13:35:28 -080058 cu->current_dalvik_offset, reg, imm_val);
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
buzbee02031b12012-11-23 09:41:35 -080092void Codegen::GenCompareAndBranch(CompilationUnit* cu, Instruction::Code opcode,
93 RegLocation rl_src1, RegLocation rl_src2, LIR* taken,
94 LIR* fall_through)
buzbee31a4a6f2012-02-28 15:36:15 -080095{
Bill Buzbeea114add2012-05-03 15:00:40 -070096 ConditionCode cond;
Bill Buzbeea114add2012-05-03 15:00:40 -070097 switch (opcode) {
98 case Instruction::IF_EQ:
99 cond = kCondEq;
100 break;
101 case Instruction::IF_NE:
102 cond = kCondNe;
103 break;
104 case Instruction::IF_LT:
105 cond = kCondLt;
106 break;
107 case Instruction::IF_GE:
108 cond = kCondGe;
109 break;
110 case Instruction::IF_GT:
111 cond = kCondGt;
112 break;
113 case Instruction::IF_LE:
114 cond = kCondLe;
115 break;
116 default:
buzbeecbd6d442012-11-17 14:11:25 -0800117 cond = static_cast<ConditionCode>(0);
118 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -0700119 }
buzbeee6285f92012-12-06 15:57:46 -0800120
121 // Normalize such that if either operand is constant, src2 will be constant
122 if (rl_src1.is_const) {
123 RegLocation rl_temp = rl_src1;
124 rl_src1 = rl_src2;
125 rl_src2 = rl_temp;
126 cond = FlipComparisonOrder(cond);
127 }
128
129 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
130 // Is this really an immediate comparison?
131 if (rl_src2.is_const) {
buzbeee6285f92012-12-06 15:57:46 -0800132 // If it's already live in a register or not easily materialized, just keep going
133 RegLocation rl_temp = UpdateLoc(cu, rl_src2);
buzbee4ef3e452012-12-14 13:35:28 -0800134 if ((rl_temp.location == kLocDalvikFrame) &&
135 InexpensiveConstantInt(ConstantValue(cu, rl_src2))) {
buzbeee6285f92012-12-06 15:57:46 -0800136 // OK - convert this to a compare immediate and branch
buzbee4ef3e452012-12-14 13:35:28 -0800137 OpCmpImmBranch(cu, cond, rl_src1.low_reg, ConstantValue(cu, rl_src2), taken);
buzbeee6285f92012-12-06 15:57:46 -0800138 OpUnconditionalBranch(cu, fall_through);
139 return;
140 }
141 }
142 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -0800143 OpCmpBranch(cu, cond, rl_src1.low_reg, rl_src2.low_reg, taken);
144 OpUnconditionalBranch(cu, fall_through);
buzbee31a4a6f2012-02-28 15:36:15 -0800145}
146
buzbee02031b12012-11-23 09:41:35 -0800147void Codegen::GenCompareZeroAndBranch(CompilationUnit* cu, Instruction::Code opcode,
148 RegLocation rl_src, LIR* taken, LIR* fall_through)
buzbee31a4a6f2012-02-28 15:36:15 -0800149{
Bill Buzbeea114add2012-05-03 15:00:40 -0700150 ConditionCode cond;
buzbeefa57c472012-11-21 12:06:18 -0800151 rl_src = LoadValue(cu, rl_src, kCoreReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700152 switch (opcode) {
153 case Instruction::IF_EQZ:
154 cond = kCondEq;
155 break;
156 case Instruction::IF_NEZ:
157 cond = kCondNe;
158 break;
159 case Instruction::IF_LTZ:
160 cond = kCondLt;
161 break;
162 case Instruction::IF_GEZ:
163 cond = kCondGe;
164 break;
165 case Instruction::IF_GTZ:
166 cond = kCondGt;
167 break;
168 case Instruction::IF_LEZ:
169 cond = kCondLe;
170 break;
171 default:
buzbeecbd6d442012-11-17 14:11:25 -0800172 cond = static_cast<ConditionCode>(0);
173 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -0700174 }
buzbeee6285f92012-12-06 15:57:46 -0800175 OpCmpImmBranch(cu, cond, rl_src.low_reg, 0, taken);
buzbeefa57c472012-11-21 12:06:18 -0800176 OpUnconditionalBranch(cu, fall_through);
buzbee31a4a6f2012-02-28 15:36:15 -0800177}
178
buzbee02031b12012-11-23 09:41:35 -0800179void Codegen::GenIntToLong(CompilationUnit* cu, RegLocation rl_dest, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800180{
buzbeefa57c472012-11-21 12:06:18 -0800181 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
182 if (rl_src.location == kLocPhysReg) {
183 OpRegCopy(cu, rl_result.low_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700184 } else {
buzbeefa57c472012-11-21 12:06:18 -0800185 LoadValueDirect(cu, rl_src, rl_result.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700186 }
buzbeefa57c472012-11-21 12:06:18 -0800187 OpRegRegImm(cu, kOpAsr, rl_result.high_reg, rl_result.low_reg, 31);
188 StoreValueWide(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800189}
190
buzbee02031b12012-11-23 09:41:35 -0800191void Codegen::GenIntNarrowing(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
192 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800193{
buzbeefa57c472012-11-21 12:06:18 -0800194 rl_src = LoadValue(cu, rl_src, kCoreReg);
195 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700196 OpKind op = kOpInvalid;
buzbee408ad162012-06-06 16:45:18 -0700197 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700198 case Instruction::INT_TO_BYTE:
199 op = kOp2Byte;
200 break;
201 case Instruction::INT_TO_SHORT:
202 op = kOp2Short;
203 break;
204 case Instruction::INT_TO_CHAR:
205 op = kOp2Char;
206 break;
207 default:
208 LOG(ERROR) << "Bad int conversion type";
209 }
buzbeefa57c472012-11-21 12:06:18 -0800210 OpRegReg(cu, op, rl_result.low_reg, rl_src.low_reg);
211 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800212}
213
214/*
215 * Let helper function take care of everything. Will call
216 * Array::AllocFromCode(type_idx, method, count);
217 * Note: AllocFromCode will handle checks for errNegativeArraySize.
218 */
buzbee02031b12012-11-23 09:41:35 -0800219void Codegen::GenNewArray(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest,
220 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800221{
buzbeefa57c472012-11-21 12:06:18 -0800222 FlushAllRegs(cu); /* Everything to home location */
223 int func_offset;
224 if (cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
225 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700226 type_idx)) {
buzbeefa57c472012-11-21 12:06:18 -0800227 func_offset = ENTRYPOINT_OFFSET(pAllocArrayFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700228 } else {
buzbeefa57c472012-11-21 12:06:18 -0800229 func_offset= ENTRYPOINT_OFFSET(pAllocArrayFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700230 }
buzbeefa57c472012-11-21 12:06:18 -0800231 CallRuntimeHelperImmMethodRegLocation(cu, func_offset, type_idx, rl_src, true);
232 RegLocation rl_result = GetReturn(cu, false);
233 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800234}
235
236/*
buzbee52a77fc2012-11-20 19:50:46 -0800237 * Similar to GenNewArray, but with post-allocation initialization.
buzbee31a4a6f2012-02-28 15:36:15 -0800238 * Verifier guarantees we're dealing with an array class. Current
239 * code throws runtime exception "bad Filled array req" for 'D' and 'J'.
240 * Current code also throws internal unimp if not 'L', '[' or 'I'.
241 */
buzbee02031b12012-11-23 09:41:35 -0800242void Codegen::GenFilledNewArray(CompilationUnit* cu, CallInfo* info)
buzbee31a4a6f2012-02-28 15:36:15 -0800243{
buzbeefa57c472012-11-21 12:06:18 -0800244 int elems = info->num_arg_words;
245 int type_idx = info->index;
246 FlushAllRegs(cu); /* Everything to home location */
247 int func_offset;
248 if (cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
249 *cu->dex_file,
250 type_idx)) {
251 func_offset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700252 } else {
buzbeefa57c472012-11-21 12:06:18 -0800253 func_offset = ENTRYPOINT_OFFSET(pCheckAndAllocArrayFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700254 }
buzbeefa57c472012-11-21 12:06:18 -0800255 CallRuntimeHelperImmMethodImm(cu, func_offset, type_idx, elems, true);
256 FreeTemp(cu, TargetReg(kArg2));
257 FreeTemp(cu, TargetReg(kArg1));
Bill Buzbeea114add2012-05-03 15:00:40 -0700258 /*
259 * NOTE: the implicit target for Instruction::FILLED_NEW_ARRAY is the
260 * return region. Because AllocFromCode placed the new array
buzbeef0504cd2012-11-13 16:31:10 -0800261 * in kRet0, we'll just lock it into place. When debugger support is
Bill Buzbeea114add2012-05-03 15:00:40 -0700262 * added, it may be necessary to additionally copy all return
263 * values to a home location in thread-local storage
264 */
buzbeefa57c472012-11-21 12:06:18 -0800265 LockTemp(cu, TargetReg(kRet0));
Bill Buzbeea114add2012-05-03 15:00:40 -0700266
267 // TODO: use the correct component size, currently all supported types
268 // share array alignment with ints (see comment at head of function)
269 size_t component_size = sizeof(int32_t);
270
271 // Having a range of 0 is legal
buzbeefa57c472012-11-21 12:06:18 -0800272 if (info->is_range && (elems > 0)) {
buzbee31a4a6f2012-02-28 15:36:15 -0800273 /*
Bill Buzbeea114add2012-05-03 15:00:40 -0700274 * Bit of ugliness here. We're going generate a mem copy loop
275 * on the register range, but it is possible that some regs
276 * in the range have been promoted. This is unlikely, but
277 * before generating the copy, we'll just force a flush
278 * of any regs in the source range that have been promoted to
279 * home location.
buzbee31a4a6f2012-02-28 15:36:15 -0800280 */
buzbee3b3dbdd2012-06-13 13:39:34 -0700281 for (int i = 0; i < elems; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800282 RegLocation loc = UpdateLoc(cu, info->args[i]);
Bill Buzbeea114add2012-05-03 15:00:40 -0700283 if (loc.location == kLocPhysReg) {
buzbeefa57c472012-11-21 12:06:18 -0800284 StoreBaseDisp(cu, TargetReg(kSp), SRegOffset(cu, loc.s_reg_low),
285 loc.low_reg, kWord);
Bill Buzbeea114add2012-05-03 15:00:40 -0700286 }
buzbee31a4a6f2012-02-28 15:36:15 -0800287 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700288 /*
289 * TUNING note: generated code here could be much improved, but
290 * this is an uncommon operation and isn't especially performance
291 * critical.
292 */
buzbeefa57c472012-11-21 12:06:18 -0800293 int r_src = AllocTemp(cu);
294 int r_dst = AllocTemp(cu);
295 int r_idx = AllocTemp(cu);
296 int r_val = INVALID_REG;
297 switch(cu->instruction_set) {
buzbeeb046e162012-10-30 15:48:42 -0700298 case kThumb2:
buzbeefa57c472012-11-21 12:06:18 -0800299 r_val = TargetReg(kLr);
buzbeeb046e162012-10-30 15:48:42 -0700300 break;
301 case kX86:
buzbeefa57c472012-11-21 12:06:18 -0800302 FreeTemp(cu, TargetReg(kRet0));
303 r_val = AllocTemp(cu);
buzbeeb046e162012-10-30 15:48:42 -0700304 break;
305 case kMips:
buzbeefa57c472012-11-21 12:06:18 -0800306 r_val = AllocTemp(cu);
buzbeeb046e162012-10-30 15:48:42 -0700307 break;
buzbeefa57c472012-11-21 12:06:18 -0800308 default: LOG(FATAL) << "Unexpected instruction set: " << cu->instruction_set;
buzbeeb046e162012-10-30 15:48:42 -0700309 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700310 // Set up source pointer
buzbeefa57c472012-11-21 12:06:18 -0800311 RegLocation rl_first = info->args[0];
312 OpRegRegImm(cu, kOpAdd, r_src, TargetReg(kSp),
313 SRegOffset(cu, rl_first.s_reg_low));
Bill Buzbeea114add2012-05-03 15:00:40 -0700314 // Set up the target pointer
buzbeefa57c472012-11-21 12:06:18 -0800315 OpRegRegImm(cu, kOpAdd, r_dst, TargetReg(kRet0),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800316 mirror::Array::DataOffset(component_size).Int32Value());
Bill Buzbeea114add2012-05-03 15:00:40 -0700317 // Set up the loop counter (known to be > 0)
buzbeefa57c472012-11-21 12:06:18 -0800318 LoadConstant(cu, r_idx, elems - 1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700319 // Generate the copy loop. Going backwards for convenience
buzbeefa57c472012-11-21 12:06:18 -0800320 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -0700321 // Copy next element
buzbeefa57c472012-11-21 12:06:18 -0800322 LoadBaseIndexed(cu, r_src, r_idx, r_val, 2, kWord);
323 StoreBaseIndexed(cu, r_dst, r_idx, r_val, 2, kWord);
324 FreeTemp(cu, r_val);
325 OpDecAndBranch(cu, kCondGe, r_idx, target);
326 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -0700327 // Restore the target pointer
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800328 OpRegRegImm(cu, kOpAdd, TargetReg(kRet0), r_dst,
329 -mirror::Array::DataOffset(component_size).Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700330 }
buzbeefa57c472012-11-21 12:06:18 -0800331 } else if (!info->is_range) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700332 // TUNING: interleave
buzbee3b3dbdd2012-06-13 13:39:34 -0700333 for (int i = 0; i < elems; i++) {
buzbeefa57c472012-11-21 12:06:18 -0800334 RegLocation rl_arg = LoadValue(cu, info->args[i], kCoreReg);
335 StoreBaseDisp(cu, TargetReg(kRet0),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800336 mirror::Array::DataOffset(component_size).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800337 i * 4, rl_arg.low_reg, kWord);
buzbee52a77fc2012-11-20 19:50:46 -0800338 // If the LoadValue caused a temp to be allocated, free it
buzbeefa57c472012-11-21 12:06:18 -0800339 if (IsTemp(cu, rl_arg.low_reg)) {
340 FreeTemp(cu, rl_arg.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700341 }
342 }
343 }
buzbeee5f01222012-06-14 15:19:35 -0700344 if (info->result.location != kLocInvalid) {
buzbeefa57c472012-11-21 12:06:18 -0800345 StoreValue(cu, info->result, GetReturn(cu, false /* not fp */));
buzbeee5f01222012-06-14 15:19:35 -0700346 }
buzbee31a4a6f2012-02-28 15:36:15 -0800347}
348
buzbee02031b12012-11-23 09:41:35 -0800349void Codegen::GenSput(CompilationUnit* cu, uint32_t field_idx, RegLocation rl_src,
350 bool is_long_or_double, bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800351{
buzbeefa57c472012-11-21 12:06:18 -0800352 int field_offset;
353 int ssb_index;
354 bool is_volatile;
355 bool is_referrers_class;
buzbee31a4a6f2012-02-28 15:36:15 -0800356
TDYa127dc5daa02013-01-09 21:31:37 +0800357 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker, *cu->dex_file, cu->code_item,
358 cu->class_def_idx, cu->method_idx, cu->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800359
buzbeefa57c472012-11-21 12:06:18 -0800360 bool fast_path =
361 cu->compiler->ComputeStaticFieldInfo(field_idx, &m_unit,
362 field_offset, ssb_index,
363 is_referrers_class, is_volatile,
Bill Buzbeea114add2012-05-03 15:00:40 -0700364 true);
buzbeefa57c472012-11-21 12:06:18 -0800365 if (fast_path && !SLOW_FIELD_PATH) {
366 DCHECK_GE(field_offset, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700367 int rBase;
buzbeefa57c472012-11-21 12:06:18 -0800368 if (is_referrers_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700369 // Fast path, static storage base is this method's class
buzbeefa57c472012-11-21 12:06:18 -0800370 RegLocation rl_method = LoadCurrMethod(cu);
371 rBase = AllocTemp(cu);
372 LoadWordDisp(cu, rl_method.low_reg,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800373 mirror::AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
buzbeefa57c472012-11-21 12:06:18 -0800374 if (IsTemp(cu, rl_method.low_reg)) {
375 FreeTemp(cu, rl_method.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700376 }
buzbee31a4a6f2012-02-28 15:36:15 -0800377 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700378 // Medium path, static storage base in a different class which
379 // requires checks that the other class is initialized.
buzbeefa57c472012-11-21 12:06:18 -0800380 DCHECK_GE(ssb_index, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700381 // May do runtime call so everything to home locations.
buzbeefa57c472012-11-21 12:06:18 -0800382 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700383 // Using fixed register to sync with possible call to runtime
384 // support.
buzbeefa57c472012-11-21 12:06:18 -0800385 int r_method = TargetReg(kArg1);
386 LockTemp(cu, r_method);
387 LoadCurrMethodDirect(cu, r_method);
buzbee52a77fc2012-11-20 19:50:46 -0800388 rBase = TargetReg(kArg0);
buzbeefa57c472012-11-21 12:06:18 -0800389 LockTemp(cu, rBase);
390 LoadWordDisp(cu, r_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800391 mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700392 rBase);
buzbeefa57c472012-11-21 12:06:18 -0800393 LoadWordDisp(cu, rBase,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800394 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800395 sizeof(int32_t*) * ssb_index, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700396 // rBase now points at appropriate static storage base (Class*)
397 // or NULL if not initialized. Check for NULL and call helper if NULL.
398 // TUNING: fast path should fall through
buzbeefa57c472012-11-21 12:06:18 -0800399 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, rBase, 0, NULL);
400 LoadConstant(cu, TargetReg(kArg0), ssb_index);
401 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssb_index, true);
402 if (cu->instruction_set == kMips) {
buzbeef0504cd2012-11-13 16:31:10 -0800403 // For Arm, kRet0 = kArg0 = rBase, for Mips, we need to copy
buzbeefa57c472012-11-21 12:06:18 -0800404 OpRegCopy(cu, rBase, TargetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700405 }
buzbeefa57c472012-11-21 12:06:18 -0800406 LIR* skip_target = NewLIR0(cu, kPseudoTargetLabel);
407 branch_over->target = skip_target;
408 FreeTemp(cu, r_method);
buzbee31a4a6f2012-02-28 15:36:15 -0800409 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700410 // rBase now holds static storage base
buzbeefa57c472012-11-21 12:06:18 -0800411 if (is_long_or_double) {
412 rl_src = LoadValueWide(cu, rl_src, kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700413 } else {
buzbeefa57c472012-11-21 12:06:18 -0800414 rl_src = LoadValue(cu, rl_src, kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700415 }
buzbeefa57c472012-11-21 12:06:18 -0800416 if (is_volatile) {
417 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700418 }
buzbeefa57c472012-11-21 12:06:18 -0800419 if (is_long_or_double) {
420 StoreBaseDispWide(cu, rBase, field_offset, rl_src.low_reg,
421 rl_src.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700422 } else {
buzbeefa57c472012-11-21 12:06:18 -0800423 StoreWordDisp(cu, rBase, field_offset, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700424 }
buzbeefa57c472012-11-21 12:06:18 -0800425 if (is_volatile) {
426 GenMemBarrier(cu, kStoreLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700427 }
buzbeefa57c472012-11-21 12:06:18 -0800428 if (is_object) {
429 MarkGCCard(cu, rl_src.low_reg, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700430 }
buzbeefa57c472012-11-21 12:06:18 -0800431 FreeTemp(cu, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700432 } else {
buzbeefa57c472012-11-21 12:06:18 -0800433 FlushAllRegs(cu); // Everything to home locations
434 int setter_offset = is_long_or_double ? ENTRYPOINT_OFFSET(pSet64Static) :
435 (is_object ? ENTRYPOINT_OFFSET(pSetObjStatic)
Bill Buzbeea114add2012-05-03 15:00:40 -0700436 : ENTRYPOINT_OFFSET(pSet32Static));
buzbeefa57c472012-11-21 12:06:18 -0800437 CallRuntimeHelperImmRegLocation(cu, setter_offset, field_idx, rl_src, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700438 }
buzbee31a4a6f2012-02-28 15:36:15 -0800439}
440
buzbee02031b12012-11-23 09:41:35 -0800441void Codegen::GenSget(CompilationUnit* cu, uint32_t field_idx, RegLocation rl_dest,
442 bool is_long_or_double, bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800443{
buzbeefa57c472012-11-21 12:06:18 -0800444 int field_offset;
445 int ssb_index;
446 bool is_volatile;
447 bool is_referrers_class;
buzbee31a4a6f2012-02-28 15:36:15 -0800448
buzbeefa57c472012-11-21 12:06:18 -0800449 OatCompilationUnit m_unit(cu->class_loader, cu->class_linker,
TDYa127dc5daa02013-01-09 21:31:37 +0800450 *cu->dex_file, cu->code_item,
451 cu->class_def_idx, cu->method_idx,
452 cu->access_flags);
buzbee31a4a6f2012-02-28 15:36:15 -0800453
buzbeefa57c472012-11-21 12:06:18 -0800454 bool fast_path =
455 cu->compiler->ComputeStaticFieldInfo(field_idx, &m_unit,
456 field_offset, ssb_index,
457 is_referrers_class, is_volatile,
Bill Buzbeea114add2012-05-03 15:00:40 -0700458 false);
buzbeefa57c472012-11-21 12:06:18 -0800459 if (fast_path && !SLOW_FIELD_PATH) {
460 DCHECK_GE(field_offset, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700461 int rBase;
buzbeefa57c472012-11-21 12:06:18 -0800462 if (is_referrers_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700463 // Fast path, static storage base is this method's class
buzbeefa57c472012-11-21 12:06:18 -0800464 RegLocation rl_method = LoadCurrMethod(cu);
465 rBase = AllocTemp(cu);
466 LoadWordDisp(cu, rl_method.low_reg,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800467 mirror::AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
buzbee31a4a6f2012-02-28 15:36:15 -0800468 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700469 // Medium path, static storage base in a different class which
470 // requires checks that the other class is initialized
buzbeefa57c472012-11-21 12:06:18 -0800471 DCHECK_GE(ssb_index, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700472 // May do runtime call so everything to home locations.
buzbeefa57c472012-11-21 12:06:18 -0800473 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700474 // Using fixed register to sync with possible call to runtime
475 // support
buzbeefa57c472012-11-21 12:06:18 -0800476 int r_method = TargetReg(kArg1);
477 LockTemp(cu, r_method);
478 LoadCurrMethodDirect(cu, r_method);
buzbee52a77fc2012-11-20 19:50:46 -0800479 rBase = TargetReg(kArg0);
buzbeefa57c472012-11-21 12:06:18 -0800480 LockTemp(cu, rBase);
481 LoadWordDisp(cu, r_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800482 mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 rBase);
buzbeefa57c472012-11-21 12:06:18 -0800484 LoadWordDisp(cu, rBase,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800485 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800486 sizeof(int32_t*) * ssb_index, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700487 // rBase now points at appropriate static storage base (Class*)
488 // or NULL if not initialized. Check for NULL and call helper if NULL.
489 // TUNING: fast path should fall through
buzbeefa57c472012-11-21 12:06:18 -0800490 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, rBase, 0, NULL);
491 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssb_index, true);
492 if (cu->instruction_set == kMips) {
buzbeef0504cd2012-11-13 16:31:10 -0800493 // For Arm, kRet0 = kArg0 = rBase, for Mips, we need to copy
buzbeefa57c472012-11-21 12:06:18 -0800494 OpRegCopy(cu, rBase, TargetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700495 }
buzbeefa57c472012-11-21 12:06:18 -0800496 LIR* skip_target = NewLIR0(cu, kPseudoTargetLabel);
497 branch_over->target = skip_target;
498 FreeTemp(cu, r_method);
buzbee31a4a6f2012-02-28 15:36:15 -0800499 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700500 // rBase now holds static storage base
buzbeefa57c472012-11-21 12:06:18 -0800501 RegLocation rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
502 if (is_volatile) {
503 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700504 }
buzbeefa57c472012-11-21 12:06:18 -0800505 if (is_long_or_double) {
506 LoadBaseDispWide(cu, rBase, field_offset, rl_result.low_reg,
507 rl_result.high_reg, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700508 } else {
buzbeefa57c472012-11-21 12:06:18 -0800509 LoadWordDisp(cu, rBase, field_offset, rl_result.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700510 }
buzbeefa57c472012-11-21 12:06:18 -0800511 FreeTemp(cu, rBase);
512 if (is_long_or_double) {
513 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700514 } else {
buzbeefa57c472012-11-21 12:06:18 -0800515 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700516 }
517 } else {
buzbeefa57c472012-11-21 12:06:18 -0800518 FlushAllRegs(cu); // Everything to home locations
519 int getterOffset = is_long_or_double ? ENTRYPOINT_OFFSET(pGet64Static) :
520 (is_object ? ENTRYPOINT_OFFSET(pGetObjStatic)
Bill Buzbeea114add2012-05-03 15:00:40 -0700521 : ENTRYPOINT_OFFSET(pGet32Static));
buzbeefa57c472012-11-21 12:06:18 -0800522 CallRuntimeHelperImm(cu, getterOffset, field_idx, true);
523 if (is_long_or_double) {
524 RegLocation rl_result = GetReturnWide(cu, rl_dest.fp);
525 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700526 } else {
buzbeefa57c472012-11-21 12:06:18 -0800527 RegLocation rl_result = GetReturn(cu, rl_dest.fp);
528 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700529 }
530 }
buzbee31a4a6f2012-02-28 15:36:15 -0800531}
532
533
534// Debugging routine - if null target, branch to DebugMe
buzbee02031b12012-11-23 09:41:35 -0800535void Codegen::GenShowTarget(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800536{
buzbeefa57c472012-11-21 12:06:18 -0800537 DCHECK_NE(cu->instruction_set, kX86) << "unimplemented GenShowTarget";
538 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, TargetReg(kInvokeTgt), 0, NULL);
539 LoadWordDisp(cu, TargetReg(kSelf), ENTRYPOINT_OFFSET(pDebugMe), TargetReg(kInvokeTgt));
540 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
541 branch_over->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -0800542}
543
buzbee02031b12012-11-23 09:41:35 -0800544void Codegen::HandleSuspendLaunchPads(CompilationUnit *cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800545{
buzbeefa57c472012-11-21 12:06:18 -0800546 LIR** suspend_label = reinterpret_cast<LIR**>(cu->suspend_launchpads.elem_list);
547 int num_elems = cu->suspend_launchpads.num_used;
548 int helper_offset = ENTRYPOINT_OFFSET(pTestSuspendFromCode);
549 for (int i = 0; i < num_elems; i++) {
550 ResetRegPool(cu);
551 ResetDefTracking(cu);
552 LIR* lab = suspend_label[i];
553 LIR* resume_lab = reinterpret_cast<LIR*>(lab->operands[0]);
554 cu->current_dalvik_offset = lab->operands[1];
555 AppendLIR(cu, lab);
556 int r_tgt = CallHelperSetup(cu, helper_offset);
557 CallHelper(cu, r_tgt, helper_offset, true /* MarkSafepointPC */);
558 OpUnconditionalBranch(cu, resume_lab);
Bill Buzbeea114add2012-05-03 15:00:40 -0700559 }
buzbee31a4a6f2012-02-28 15:36:15 -0800560}
561
buzbee02031b12012-11-23 09:41:35 -0800562void Codegen::HandleIntrinsicLaunchPads(CompilationUnit *cu)
buzbeefc9e6fa2012-03-23 15:14:29 -0700563{
buzbeefa57c472012-11-21 12:06:18 -0800564 LIR** intrinsic_label = reinterpret_cast<LIR**>(cu->intrinsic_launchpads.elem_list);
565 int num_elems = cu->intrinsic_launchpads.num_used;
566 for (int i = 0; i < num_elems; i++) {
567 ResetRegPool(cu);
568 ResetDefTracking(cu);
569 LIR* lab = intrinsic_label[i];
buzbeecbd6d442012-11-17 14:11:25 -0800570 CallInfo* info = reinterpret_cast<CallInfo*>(lab->operands[0]);
buzbeefa57c472012-11-21 12:06:18 -0800571 cu->current_dalvik_offset = info->offset;
572 AppendLIR(cu, lab);
buzbee52a77fc2012-11-20 19:50:46 -0800573 // NOTE: GenInvoke handles MarkSafepointPC
buzbeefa57c472012-11-21 12:06:18 -0800574 GenInvoke(cu, info);
575 LIR* resume_lab = reinterpret_cast<LIR*>(lab->operands[2]);
576 if (resume_lab != NULL) {
577 OpUnconditionalBranch(cu, resume_lab);
buzbeefc9e6fa2012-03-23 15:14:29 -0700578 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700579 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700580}
581
buzbee02031b12012-11-23 09:41:35 -0800582void Codegen::HandleThrowLaunchPads(CompilationUnit *cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800583{
buzbeefa57c472012-11-21 12:06:18 -0800584 LIR** throw_label = reinterpret_cast<LIR**>(cu->throw_launchpads.elem_list);
585 int num_elems = cu->throw_launchpads.num_used;
586 for (int i = 0; i < num_elems; i++) {
587 ResetRegPool(cu);
588 ResetDefTracking(cu);
589 LIR* lab = throw_label[i];
590 cu->current_dalvik_offset = lab->operands[1];
591 AppendLIR(cu, lab);
592 int func_offset = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700593 int v1 = lab->operands[2];
594 int v2 = lab->operands[3];
buzbeefa57c472012-11-21 12:06:18 -0800595 bool target_x86 = (cu->instruction_set == kX86);
Bill Buzbeea114add2012-05-03 15:00:40 -0700596 switch (lab->operands[0]) {
597 case kThrowNullPointer:
buzbeefa57c472012-11-21 12:06:18 -0800598 func_offset = ENTRYPOINT_OFFSET(pThrowNullPointerFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700599 break;
buzbee4ef3e452012-12-14 13:35:28 -0800600 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
601 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
602 if (target_x86) {
603 OpRegMem(cu, kOpMov, TargetReg(kArg1), v1, mirror::Array::LengthOffset().Int32Value());
604 } else {
605 OpRegCopy(cu, TargetReg(kArg1), v1);
606 }
607 // Make sure the following LoadConstant doesn't mess with kArg1.
608 LockTemp(cu, TargetReg(kArg1));
609 LoadConstant(cu, TargetReg(kArg0), v2);
610 func_offset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
611 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700612 case kThrowArrayBounds:
buzbeef0504cd2012-11-13 16:31:10 -0800613 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
buzbee52a77fc2012-11-20 19:50:46 -0800614 if (v2 != TargetReg(kArg0)) {
buzbeefa57c472012-11-21 12:06:18 -0800615 OpRegCopy(cu, TargetReg(kArg0), v1);
616 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700617 // x86 leaves the array pointer in v2, so load the array length that the handler expects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800618 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700619 } else {
buzbeefa57c472012-11-21 12:06:18 -0800620 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700621 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700622 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800623 if (v1 == TargetReg(kArg1)) {
buzbeef0504cd2012-11-13 16:31:10 -0800624 // Swap v1 and v2, using kArg2 as a temp
buzbeefa57c472012-11-21 12:06:18 -0800625 OpRegCopy(cu, TargetReg(kArg2), v1);
626 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700627 // x86 leaves the array pointer in v2; load the array length that the handler expects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800628 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700629 } else {
buzbeefa57c472012-11-21 12:06:18 -0800630 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700631 }
buzbeefa57c472012-11-21 12:06:18 -0800632 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2));
Bill Buzbeea114add2012-05-03 15:00:40 -0700633 } else {
buzbeefa57c472012-11-21 12:06:18 -0800634 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700635 // x86 leaves the array pointer in v2; load the array length that the handler expects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800636 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700637 } else {
buzbeefa57c472012-11-21 12:06:18 -0800638 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700639 }
buzbeefa57c472012-11-21 12:06:18 -0800640 OpRegCopy(cu, TargetReg(kArg0), v1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700641 }
buzbee31a4a6f2012-02-28 15:36:15 -0800642 }
buzbeefa57c472012-11-21 12:06:18 -0800643 func_offset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700644 break;
645 case kThrowDivZero:
buzbeefa57c472012-11-21 12:06:18 -0800646 func_offset = ENTRYPOINT_OFFSET(pThrowDivZeroFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700647 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700648 case kThrowNoSuchMethod:
buzbeefa57c472012-11-21 12:06:18 -0800649 OpRegCopy(cu, TargetReg(kArg0), v1);
650 func_offset =
Bill Buzbeea114add2012-05-03 15:00:40 -0700651 ENTRYPOINT_OFFSET(pThrowNoSuchMethodFromCode);
652 break;
653 case kThrowStackOverflow:
buzbeefa57c472012-11-21 12:06:18 -0800654 func_offset = ENTRYPOINT_OFFSET(pThrowStackOverflowFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700655 // Restore stack alignment
buzbeefa57c472012-11-21 12:06:18 -0800656 if (target_x86) {
657 OpRegImm(cu, kOpAdd, TargetReg(kSp), cu->frame_size);
buzbeeb046e162012-10-30 15:48:42 -0700658 } else {
buzbeefa57c472012-11-21 12:06:18 -0800659 OpRegImm(cu, kOpAdd, TargetReg(kSp), (cu->num_core_spills + cu->num_fp_spills) * 4);
buzbeeb046e162012-10-30 15:48:42 -0700660 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700661 break;
662 default:
663 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
buzbee31a4a6f2012-02-28 15:36:15 -0800664 }
buzbeefa57c472012-11-21 12:06:18 -0800665 ClobberCalleeSave(cu);
666 int r_tgt = CallHelperSetup(cu, func_offset);
667 CallHelper(cu, r_tgt, func_offset, true /* MarkSafepointPC */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700668 }
buzbee31a4a6f2012-02-28 15:36:15 -0800669}
670
buzbee02031b12012-11-23 09:41:35 -0800671void Codegen::GenIGet(CompilationUnit* cu, uint32_t field_idx, int opt_flags, OpSize size,
672 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
673 bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800674{
buzbeefa57c472012-11-21 12:06:18 -0800675 int field_offset;
676 bool is_volatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800677
buzbeefa57c472012-11-21 12:06:18 -0800678 bool fast_path = FastInstance(cu, field_idx, field_offset, is_volatile, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800679
buzbeefa57c472012-11-21 12:06:18 -0800680 if (fast_path && !SLOW_FIELD_PATH) {
681 RegLocation rl_result;
682 RegisterClass reg_class = oat_reg_class_by_size(size);
683 DCHECK_GE(field_offset, 0);
684 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
685 if (is_long_or_double) {
686 DCHECK(rl_dest.wide);
687 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
688 if (cu->instruction_set == kX86) {
689 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
690 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
691 LoadBaseDispWide(cu, rl_obj.low_reg, field_offset, rl_result.low_reg,
692 rl_result.high_reg, rl_obj.s_reg_low);
693 if (is_volatile) {
694 GenMemBarrier(cu, kLoadLoad);
buzbeeb046e162012-10-30 15:48:42 -0700695 }
696 } else {
buzbeefa57c472012-11-21 12:06:18 -0800697 int reg_ptr = AllocTemp(cu);
698 OpRegRegImm(cu, kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
699 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
buzbeee6285f92012-12-06 15:57:46 -0800700 LoadBaseDispWide(cu, reg_ptr, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
buzbeefa57c472012-11-21 12:06:18 -0800701 if (is_volatile) {
702 GenMemBarrier(cu, kLoadLoad);
buzbeeb046e162012-10-30 15:48:42 -0700703 }
buzbeefa57c472012-11-21 12:06:18 -0800704 FreeTemp(cu, reg_ptr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700705 }
buzbeefa57c472012-11-21 12:06:18 -0800706 StoreValueWide(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800707 } else {
buzbeefa57c472012-11-21 12:06:18 -0800708 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
709 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
710 LoadBaseDisp(cu, rl_obj.low_reg, field_offset, rl_result.low_reg,
711 kWord, rl_obj.s_reg_low);
712 if (is_volatile) {
713 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700714 }
buzbeefa57c472012-11-21 12:06:18 -0800715 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800716 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700717 } else {
buzbeefa57c472012-11-21 12:06:18 -0800718 int getterOffset = is_long_or_double ? ENTRYPOINT_OFFSET(pGet64Instance) :
719 (is_object ? ENTRYPOINT_OFFSET(pGetObjInstance)
Bill Buzbeea114add2012-05-03 15:00:40 -0700720 : ENTRYPOINT_OFFSET(pGet32Instance));
buzbeefa57c472012-11-21 12:06:18 -0800721 CallRuntimeHelperImmRegLocation(cu, getterOffset, field_idx, rl_obj, true);
722 if (is_long_or_double) {
723 RegLocation rl_result = GetReturnWide(cu, rl_dest.fp);
724 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700725 } else {
buzbeefa57c472012-11-21 12:06:18 -0800726 RegLocation rl_result = GetReturn(cu, rl_dest.fp);
727 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700728 }
729 }
buzbee31a4a6f2012-02-28 15:36:15 -0800730}
731
buzbee02031b12012-11-23 09:41:35 -0800732void Codegen::GenIPut(CompilationUnit* cu, uint32_t field_idx, int opt_flags, OpSize size,
733 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
734 bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800735{
buzbeefa57c472012-11-21 12:06:18 -0800736 int field_offset;
737 bool is_volatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800738
buzbeefa57c472012-11-21 12:06:18 -0800739 bool fast_path = FastInstance(cu, field_idx, field_offset, is_volatile,
Bill Buzbeea114add2012-05-03 15:00:40 -0700740 true);
buzbeefa57c472012-11-21 12:06:18 -0800741 if (fast_path && !SLOW_FIELD_PATH) {
742 RegisterClass reg_class = oat_reg_class_by_size(size);
743 DCHECK_GE(field_offset, 0);
744 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
745 if (is_long_or_double) {
746 int reg_ptr;
747 rl_src = LoadValueWide(cu, rl_src, kAnyReg);
748 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
749 reg_ptr = AllocTemp(cu);
750 OpRegRegImm(cu, kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
751 if (is_volatile) {
752 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700753 }
buzbeefa57c472012-11-21 12:06:18 -0800754 StoreBaseDispWide(cu, reg_ptr, 0, rl_src.low_reg, rl_src.high_reg);
755 if (is_volatile) {
756 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700757 }
buzbeefa57c472012-11-21 12:06:18 -0800758 FreeTemp(cu, reg_ptr);
buzbee31a4a6f2012-02-28 15:36:15 -0800759 } else {
buzbeefa57c472012-11-21 12:06:18 -0800760 rl_src = LoadValue(cu, rl_src, reg_class);
761 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
762 if (is_volatile) {
763 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700764 }
buzbeefa57c472012-11-21 12:06:18 -0800765 StoreBaseDisp(cu, rl_obj.low_reg, field_offset, rl_src.low_reg, kWord);
766 if (is_volatile) {
767 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700768 }
buzbeefa57c472012-11-21 12:06:18 -0800769 if (is_object) {
770 MarkGCCard(cu, rl_src.low_reg, rl_obj.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700771 }
buzbee31a4a6f2012-02-28 15:36:15 -0800772 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700773 } else {
buzbeefa57c472012-11-21 12:06:18 -0800774 int setter_offset = is_long_or_double ? ENTRYPOINT_OFFSET(pSet64Instance) :
775 (is_object ? ENTRYPOINT_OFFSET(pSetObjInstance)
Bill Buzbeea114add2012-05-03 15:00:40 -0700776 : ENTRYPOINT_OFFSET(pSet32Instance));
buzbeefa57c472012-11-21 12:06:18 -0800777 CallRuntimeHelperImmRegLocationRegLocation(cu, setter_offset, field_idx, rl_obj, rl_src, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700778 }
buzbee31a4a6f2012-02-28 15:36:15 -0800779}
780
buzbee02031b12012-11-23 09:41:35 -0800781void Codegen::GenConstClass(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800782{
buzbeefa57c472012-11-21 12:06:18 -0800783 RegLocation rl_method = LoadCurrMethod(cu);
784 int res_reg = AllocTemp(cu);
785 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
786 if (!cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
787 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700788 type_idx)) {
789 // Call out to helper which resolves type and verifies access.
buzbeef0504cd2012-11-13 16:31:10 -0800790 // Resolved type returned in kRet0.
buzbeefa57c472012-11-21 12:06:18 -0800791 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
792 type_idx, rl_method.low_reg, true);
793 RegLocation rl_result = GetReturn(cu, false);
794 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700795 } else {
796 // We're don't need access checks, load type from dex cache
797 int32_t dex_cache_offset =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800798 mirror::AbstractMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbeefa57c472012-11-21 12:06:18 -0800799 LoadWordDisp(cu, rl_method.low_reg, dex_cache_offset, res_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700800 int32_t offset_of_type =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800801 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
Bill Buzbeea114add2012-05-03 15:00:40 -0700802 * type_idx);
buzbeefa57c472012-11-21 12:06:18 -0800803 LoadWordDisp(cu, res_reg, offset_of_type, rl_result.low_reg);
804 if (!cu->compiler->CanAssumeTypeIsPresentInDexCache(*cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700805 type_idx) || SLOW_TYPE_PATH) {
806 // Slow path, at runtime test if type is null and if so initialize
buzbeefa57c472012-11-21 12:06:18 -0800807 FlushAllRegs(cu);
808 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, rl_result.low_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700809 // Resolved, store and hop over following code
buzbeefa57c472012-11-21 12:06:18 -0800810 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700811 /*
812 * Because we have stores of the target value on two paths,
813 * clobber temp tracking for the destination using the ssa name
814 */
buzbeefa57c472012-11-21 12:06:18 -0800815 ClobberSReg(cu, rl_dest.s_reg_low);
816 LIR* branch2 = OpUnconditionalBranch(cu,0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700817 // TUNING: move slow path to end & remove unconditional branch
buzbeefa57c472012-11-21 12:06:18 -0800818 LIR* target1 = NewLIR0(cu, kPseudoTargetLabel);
buzbeef0504cd2012-11-13 16:31:10 -0800819 // Call out to helper, which will return resolved type in kArg0
buzbeefa57c472012-11-21 12:06:18 -0800820 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx,
821 rl_method.low_reg, true);
822 RegLocation rl_result = GetReturn(cu, false);
823 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700824 /*
825 * Because we have stores of the target value on two paths,
826 * clobber temp tracking for the destination using the ssa name
827 */
buzbeefa57c472012-11-21 12:06:18 -0800828 ClobberSReg(cu, rl_dest.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700829 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -0800830 LIR* target2 = NewLIR0(cu, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800831 branch1->target = target1;
832 branch2->target = target2;
buzbee31a4a6f2012-02-28 15:36:15 -0800833 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700834 // Fast path, we're done - just store result
buzbeefa57c472012-11-21 12:06:18 -0800835 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800836 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700837 }
buzbee31a4a6f2012-02-28 15:36:15 -0800838}
Ian Rogersab2b55d2012-03-18 00:06:11 -0700839
buzbee02031b12012-11-23 09:41:35 -0800840void Codegen::GenConstString(CompilationUnit* cu, uint32_t string_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800841{
Bill Buzbeea114add2012-05-03 15:00:40 -0700842 /* NOTE: Most strings should be available at compile time */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800843 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
844 (sizeof(mirror::String*) * string_idx);
buzbeefa57c472012-11-21 12:06:18 -0800845 if (!cu->compiler->CanAssumeStringIsPresentInDexCache(
846 *cu->dex_file, string_idx) || SLOW_STRING_PATH) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700847 // slow path, resolve string if not in dex cache
buzbeefa57c472012-11-21 12:06:18 -0800848 FlushAllRegs(cu);
849 LockCallTemps(cu); // Using explicit registers
850 LoadCurrMethodDirect(cu, TargetReg(kArg2));
851 LoadWordDisp(cu, TargetReg(kArg2),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800852 mirror::AbstractMethod::DexCacheStringsOffset().Int32Value(), TargetReg(kArg0));
buzbeef0504cd2012-11-13 16:31:10 -0800853 // Might call out to helper, which will return resolved string in kRet0
buzbeefa57c472012-11-21 12:06:18 -0800854 int r_tgt = CallHelperSetup(cu, ENTRYPOINT_OFFSET(pResolveStringFromCode));
855 LoadWordDisp(cu, TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
856 LoadConstant(cu, TargetReg(kArg1), string_idx);
857 if (cu->instruction_set == kThumb2) {
858 OpRegImm(cu, kOpCmp, TargetReg(kRet0), 0); // Is resolved?
859 GenBarrier(cu);
buzbeeb046e162012-10-30 15:48:42 -0700860 // For testing, always force through helper
861 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee02031b12012-11-23 09:41:35 -0800862 OpIT(cu, kCondEq, "T");
buzbeeb046e162012-10-30 15:48:42 -0700863 }
buzbeefa57c472012-11-21 12:06:18 -0800864 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .eq
865 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt); // .eq, helper(Method*, string_idx)
866 MarkSafepointPC(cu, call_inst);
867 FreeTemp(cu, r_tgt);
868 } else if (cu->instruction_set == kMips) {
869 LIR* branch = OpCmpImmBranch(cu, kCondNe, TargetReg(kRet0), 0, NULL);
870 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .eq
871 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt);
872 MarkSafepointPC(cu, call_inst);
873 FreeTemp(cu, r_tgt);
874 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
buzbeeb046e162012-10-30 15:48:42 -0700875 branch->target = target;
876 } else {
buzbeefa57c472012-11-21 12:06:18 -0800877 DCHECK_EQ(cu->instruction_set, kX86);
878 CallRuntimeHelperRegReg(cu, ENTRYPOINT_OFFSET(pResolveStringFromCode), TargetReg(kArg2), TargetReg(kArg1), true);
buzbee31a4a6f2012-02-28 15:36:15 -0800879 }
buzbeefa57c472012-11-21 12:06:18 -0800880 GenBarrier(cu);
881 StoreValue(cu, rl_dest, GetReturn(cu, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700882 } else {
buzbeefa57c472012-11-21 12:06:18 -0800883 RegLocation rl_method = LoadCurrMethod(cu);
884 int res_reg = AllocTemp(cu);
885 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
886 LoadWordDisp(cu, rl_method.low_reg,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800887 mirror::AbstractMethod::DexCacheStringsOffset().Int32Value(), res_reg);
buzbeefa57c472012-11-21 12:06:18 -0800888 LoadWordDisp(cu, res_reg, offset_of_string, rl_result.low_reg);
889 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700890 }
buzbee31a4a6f2012-02-28 15:36:15 -0800891}
892
893/*
894 * Let helper function take care of everything. Will
895 * call Class::NewInstanceFromCode(type_idx, method);
896 */
buzbee02031b12012-11-23 09:41:35 -0800897void Codegen::GenNewInstance(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800898{
buzbeefa57c472012-11-21 12:06:18 -0800899 FlushAllRegs(cu); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -0700900 // alloc will always check for resolution, do we also need to verify
901 // access because the verifier was unable to?
buzbeefa57c472012-11-21 12:06:18 -0800902 int func_offset;
903 if (cu->compiler->CanAccessInstantiableTypeWithoutChecks(
904 cu->method_idx, *cu->dex_file, type_idx)) {
905 func_offset = ENTRYPOINT_OFFSET(pAllocObjectFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700906 } else {
buzbeefa57c472012-11-21 12:06:18 -0800907 func_offset = ENTRYPOINT_OFFSET(pAllocObjectFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700908 }
buzbeefa57c472012-11-21 12:06:18 -0800909 CallRuntimeHelperImmMethod(cu, func_offset, type_idx, true);
910 RegLocation rl_result = GetReturn(cu, false);
911 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800912}
913
buzbee02031b12012-11-23 09:41:35 -0800914void Codegen::GenThrow(CompilationUnit* cu, RegLocation rl_src)
Ian Rogersab2b55d2012-03-18 00:06:11 -0700915{
buzbeefa57c472012-11-21 12:06:18 -0800916 FlushAllRegs(cu);
917 CallRuntimeHelperRegLocation(cu, ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700918}
919
buzbee02031b12012-11-23 09:41:35 -0800920void Codegen::GenInstanceof(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest,
921 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800922{
buzbeefa57c472012-11-21 12:06:18 -0800923 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700924 // May generate a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -0800925 LockCallTemps(cu);
926 LoadCurrMethodDirect(cu, TargetReg(kArg1)); // kArg1 <= current Method*
927 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
928 if (!cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
929 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700930 type_idx)) {
931 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbeef0504cd2012-11-13 16:31:10 -0800932 // returns Class* in kArg0
buzbeefa57c472012-11-21 12:06:18 -0800933 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbee8320f382012-09-11 16:29:42 -0700934 type_idx, true);
buzbeefa57c472012-11-21 12:06:18 -0800935 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
936 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -0700937 } else {
buzbeefa57c472012-11-21 12:06:18 -0800938 // Load dex cache entry into class_reg (kArg2)
939 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
940 LoadWordDisp(cu, TargetReg(kArg1),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800941 mirror::AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700942 int32_t offset_of_type =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800943 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
Bill Buzbeea114add2012-05-03 15:00:40 -0700944 * type_idx);
buzbeefa57c472012-11-21 12:06:18 -0800945 LoadWordDisp(cu, class_reg, offset_of_type, class_reg);
946 if (!cu->compiler->CanAssumeTypeIsPresentInDexCache(
947 *cu->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700948 // Need to test presence of type in dex cache at runtime
buzbeefa57c472012-11-21 12:06:18 -0800949 LIR* hop_branch = OpCmpImmBranch(cu, kCondNe, class_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700950 // Not resolved
buzbeef0504cd2012-11-13 16:31:10 -0800951 // Call out to helper, which will return resolved type in kRet0
buzbeefa57c472012-11-21 12:06:18 -0800952 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, true);
953 OpRegCopy(cu, TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
954 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); /* reload Ref */
Bill Buzbeea114add2012-05-03 15:00:40 -0700955 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -0800956 LIR* hop_target = NewLIR0(cu, kPseudoTargetLabel);
957 hop_branch->target = hop_target;
buzbee31a4a6f2012-02-28 15:36:15 -0800958 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700959 }
buzbeef0504cd2012-11-13 16:31:10 -0800960 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeefa57c472012-11-21 12:06:18 -0800961 RegLocation rl_result = GetReturn(cu, false);
962 if (cu->instruction_set == kMips) {
963 LoadConstant(cu, rl_result.low_reg, 0); // store false result for if branch is taken
buzbeeb046e162012-10-30 15:48:42 -0700964 }
buzbeefa57c472012-11-21 12:06:18 -0800965 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, TargetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700966 /* load object->klass_ */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800967 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
968 LoadWordDisp(cu, TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
buzbeef0504cd2012-11-13 16:31:10 -0800969 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
buzbeefa57c472012-11-21 12:06:18 -0800970 LIR* call_inst;
buzbeeb046e162012-10-30 15:48:42 -0700971 LIR* branchover = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800972 if (cu->instruction_set == kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -0700973 /* Uses conditional nullification */
buzbeefa57c472012-11-21 12:06:18 -0800974 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
975 OpRegReg(cu, kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
buzbee02031b12012-11-23 09:41:35 -0800976 OpIT(cu, kCondEq, "EE"); // if-convert the test
buzbeefa57c472012-11-21 12:06:18 -0800977 LoadConstant(cu, TargetReg(kArg0), 1); // .eq case - load true
978 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
979 call_inst = OpReg(cu, kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
980 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700981 } else {
982 /* Uses branchovers */
buzbeefa57c472012-11-21 12:06:18 -0800983 LoadConstant(cu, rl_result.low_reg, 1); // assume true
984 branchover = OpCmpBranch(cu, kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
985 if (cu->instruction_set != kX86) {
986 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
987 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
988 call_inst = OpReg(cu, kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
989 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700990 } else {
buzbeefa57c472012-11-21 12:06:18 -0800991 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2));
992 call_inst = OpThreadMem(cu, kOpBlx, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
buzbeeb046e162012-10-30 15:48:42 -0700993 }
994 }
buzbeefa57c472012-11-21 12:06:18 -0800995 MarkSafepointPC(cu, call_inst);
996 ClobberCalleeSave(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700997 /* branch targets here */
buzbeefa57c472012-11-21 12:06:18 -0800998 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
999 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001000 branch1->target = target;
buzbeefa57c472012-11-21 12:06:18 -08001001 if (cu->instruction_set != kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -07001002 branchover->target = target;
1003 }
buzbee31a4a6f2012-02-28 15:36:15 -08001004}
1005
buzbee02031b12012-11-23 09:41:35 -08001006void Codegen::GenCheckCast(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -08001007{
buzbeefa57c472012-11-21 12:06:18 -08001008 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001009 // May generate a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -08001010 LockCallTemps(cu);
1011 LoadCurrMethodDirect(cu, TargetReg(kArg1)); // kArg1 <= current Method*
1012 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
1013 if (!cu->compiler->CanAccessTypeWithoutChecks(cu->method_idx,
1014 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -07001015 type_idx)) {
1016 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbeef0504cd2012-11-13 16:31:10 -08001017 // returns Class* in kRet0
Bill Buzbeea114add2012-05-03 15:00:40 -07001018 // InitializeTypeAndVerifyAccess(idx, method)
buzbeefa57c472012-11-21 12:06:18 -08001019 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbee52a77fc2012-11-20 19:50:46 -08001020 type_idx, TargetReg(kArg1), true);
buzbeefa57c472012-11-21 12:06:18 -08001021 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001022 } else {
buzbeefa57c472012-11-21 12:06:18 -08001023 // Load dex cache entry into class_reg (kArg2)
1024 LoadWordDisp(cu, TargetReg(kArg1),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001025 mirror::AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001026 int32_t offset_of_type =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001027 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1028 (sizeof(mirror::Class*) * type_idx);
buzbeefa57c472012-11-21 12:06:18 -08001029 LoadWordDisp(cu, class_reg, offset_of_type, class_reg);
1030 if (!cu->compiler->CanAssumeTypeIsPresentInDexCache(
1031 *cu->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001032 // Need to test presence of type in dex cache at runtime
buzbeefa57c472012-11-21 12:06:18 -08001033 LIR* hop_branch = OpCmpImmBranch(cu, kCondNe, class_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001034 // Not resolved
buzbeef0504cd2012-11-13 16:31:10 -08001035 // Call out to helper, which will return resolved type in kArg0
Bill Buzbeea114add2012-05-03 15:00:40 -07001036 // InitializeTypeFromCode(idx, method)
buzbeefa57c472012-11-21 12:06:18 -08001037 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, TargetReg(kArg1),
buzbee8320f382012-09-11 16:29:42 -07001038 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 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -08001041 LIR* hop_target = NewLIR0(cu, kPseudoTargetLabel);
1042 hop_branch->target = hop_target;
buzbee31a4a6f2012-02-28 15:36:15 -08001043 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001044 }
buzbeefa57c472012-11-21 12:06:18 -08001045 // At this point, class_reg (kArg2) has class
1046 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -07001047 /* Null is OK - continue */
buzbeefa57c472012-11-21 12:06:18 -08001048 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, TargetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001049 /* load object->klass_ */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001050 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1051 LoadWordDisp(cu, TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
buzbeef0504cd2012-11-13 16:31:10 -08001052 /* kArg1 now contains object->klass_ */
buzbeeb046e162012-10-30 15:48:42 -07001053 LIR* branch2;
buzbeefa57c472012-11-21 12:06:18 -08001054 if (cu->instruction_set == kThumb2) {
1055 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1056 OpRegReg(cu, kOpCmp, TargetReg(kArg1), class_reg);
1057 branch2 = OpCondBranch(cu, kCondEq, NULL); /* If eq, trivial yes */
1058 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg1));
1059 OpRegCopy(cu, TargetReg(kArg1), TargetReg(kArg2));
1060 ClobberCalleeSave(cu);
1061 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt);
1062 MarkSafepointPC(cu, call_inst);
1063 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -07001064 } else {
buzbeefa57c472012-11-21 12:06:18 -08001065 branch2 = OpCmpBranch(cu, kCondEq, TargetReg(kArg1), class_reg, NULL);
1066 CallRuntimeHelperRegReg(cu, ENTRYPOINT_OFFSET(pCheckCastFromCode), TargetReg(kArg1), TargetReg(kArg2), true);
buzbeeb046e162012-10-30 15:48:42 -07001067 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001068 /* branch target here */
buzbeefa57c472012-11-21 12:06:18 -08001069 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -07001070 branch1->target = target;
1071 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001072}
1073
buzbee02031b12012-11-23 09:41:35 -08001074void Codegen::GenLong3Addr(CompilationUnit* cu, OpKind first_op, OpKind second_op,
1075 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001076{
buzbeefa57c472012-11-21 12:06:18 -08001077 RegLocation rl_result;
1078 if (cu->instruction_set == kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -07001079 /*
1080 * NOTE: This is the one place in the code in which we might have
1081 * as many as six live temporary registers. There are 5 in the normal
1082 * set for Arm. Until we have spill capabilities, temporarily add
1083 * lr to the temp set. It is safe to do this locally, but note that
1084 * lr is used explicitly elsewhere in the code generator and cannot
1085 * normally be used as a general temp register.
1086 */
buzbeefa57c472012-11-21 12:06:18 -08001087 MarkTemp(cu, TargetReg(kLr)); // Add lr to the temp pool
1088 FreeTemp(cu, TargetReg(kLr)); // and make it available
buzbeeb046e162012-10-30 15:48:42 -07001089 }
buzbeefa57c472012-11-21 12:06:18 -08001090 rl_src1 = LoadValueWide(cu, rl_src1, kCoreReg);
1091 rl_src2 = LoadValueWide(cu, rl_src2, kCoreReg);
1092 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001093 // The longs may overlap - use intermediate temp if so
buzbeefa57c472012-11-21 12:06:18 -08001094 if ((rl_result.low_reg == rl_src1.high_reg) || (rl_result.low_reg == rl_src2.high_reg)){
1095 int t_reg = AllocTemp(cu);
1096 OpRegRegReg(cu, first_op, t_reg, rl_src1.low_reg, rl_src2.low_reg);
1097 OpRegRegReg(cu, second_op, rl_result.high_reg, rl_src1.high_reg, rl_src2.high_reg);
1098 OpRegCopy(cu, rl_result.low_reg, t_reg);
1099 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001100 } else {
buzbeefa57c472012-11-21 12:06:18 -08001101 OpRegRegReg(cu, first_op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1102 OpRegRegReg(cu, second_op, rl_result.high_reg, rl_src1.high_reg,
1103 rl_src2.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001104 }
1105 /*
buzbeefa57c472012-11-21 12:06:18 -08001106 * NOTE: If rl_dest refers to a frame variable in a large frame, the
buzbee52a77fc2012-11-20 19:50:46 -08001107 * following StoreValueWide might need to allocate a temp register.
Bill Buzbeea114add2012-05-03 15:00:40 -07001108 * To further work around the lack of a spill capability, explicitly
buzbeefa57c472012-11-21 12:06:18 -08001109 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
Bill Buzbeea114add2012-05-03 15:00:40 -07001110 * Remove when spill is functional.
1111 */
buzbeefa57c472012-11-21 12:06:18 -08001112 FreeRegLocTemps(cu, rl_result, rl_src1);
1113 FreeRegLocTemps(cu, rl_result, rl_src2);
1114 StoreValueWide(cu, rl_dest, rl_result);
1115 if (cu->instruction_set == kThumb2) {
1116 Clobber(cu, TargetReg(kLr));
1117 UnmarkTemp(cu, TargetReg(kLr)); // Remove lr from the temp pool
buzbeeb046e162012-10-30 15:48:42 -07001118 }
buzbee31a4a6f2012-02-28 15:36:15 -08001119}
1120
1121
buzbee02031b12012-11-23 09:41:35 -08001122bool Codegen::GenShiftOpLong(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
1123 RegLocation rl_src1, RegLocation rl_shift)
buzbee31a4a6f2012-02-28 15:36:15 -08001124{
buzbeefa57c472012-11-21 12:06:18 -08001125 int func_offset;
buzbee31a4a6f2012-02-28 15:36:15 -08001126
buzbee408ad162012-06-06 16:45:18 -07001127 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001128 case Instruction::SHL_LONG:
1129 case Instruction::SHL_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001130 func_offset = ENTRYPOINT_OFFSET(pShlLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001131 break;
1132 case Instruction::SHR_LONG:
1133 case Instruction::SHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001134 func_offset = ENTRYPOINT_OFFSET(pShrLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001135 break;
1136 case Instruction::USHR_LONG:
1137 case Instruction::USHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001138 func_offset = ENTRYPOINT_OFFSET(pUshrLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001139 break;
1140 default:
1141 LOG(FATAL) << "Unexpected case";
1142 return true;
1143 }
buzbeefa57c472012-11-21 12:06:18 -08001144 FlushAllRegs(cu); /* Send everything to home location */
1145 CallRuntimeHelperRegLocationRegLocation(cu, func_offset, rl_src1, rl_shift, false);
1146 RegLocation rl_result = GetReturnWide(cu, false);
1147 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001148 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001149}
1150
1151
buzbee02031b12012-11-23 09:41:35 -08001152bool Codegen::GenArithOpInt(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
1153 RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001154{
Bill Buzbeea114add2012-05-03 15:00:40 -07001155 OpKind op = kOpBkpt;
buzbeefa57c472012-11-21 12:06:18 -08001156 bool is_div_rem = false;
1157 bool check_zero = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001158 bool unary = false;
buzbeefa57c472012-11-21 12:06:18 -08001159 RegLocation rl_result;
1160 bool shift_op = false;
buzbee408ad162012-06-06 16:45:18 -07001161 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001162 case Instruction::NEG_INT:
1163 op = kOpNeg;
1164 unary = true;
1165 break;
1166 case Instruction::NOT_INT:
1167 op = kOpMvn;
1168 unary = true;
1169 break;
1170 case Instruction::ADD_INT:
1171 case Instruction::ADD_INT_2ADDR:
1172 op = kOpAdd;
1173 break;
1174 case Instruction::SUB_INT:
1175 case Instruction::SUB_INT_2ADDR:
1176 op = kOpSub;
1177 break;
1178 case Instruction::MUL_INT:
1179 case Instruction::MUL_INT_2ADDR:
1180 op = kOpMul;
1181 break;
1182 case Instruction::DIV_INT:
1183 case Instruction::DIV_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001184 check_zero = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001185 op = kOpDiv;
buzbeefa57c472012-11-21 12:06:18 -08001186 is_div_rem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001187 break;
buzbeef0504cd2012-11-13 16:31:10 -08001188 /* NOTE: returns in kArg1 */
Bill Buzbeea114add2012-05-03 15:00:40 -07001189 case Instruction::REM_INT:
1190 case Instruction::REM_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001191 check_zero = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001192 op = kOpRem;
buzbeefa57c472012-11-21 12:06:18 -08001193 is_div_rem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001194 break;
1195 case Instruction::AND_INT:
1196 case Instruction::AND_INT_2ADDR:
1197 op = kOpAnd;
1198 break;
1199 case Instruction::OR_INT:
1200 case Instruction::OR_INT_2ADDR:
1201 op = kOpOr;
1202 break;
1203 case Instruction::XOR_INT:
1204 case Instruction::XOR_INT_2ADDR:
1205 op = kOpXor;
1206 break;
1207 case Instruction::SHL_INT:
1208 case Instruction::SHL_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001209 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001210 op = kOpLsl;
1211 break;
1212 case Instruction::SHR_INT:
1213 case Instruction::SHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001214 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001215 op = kOpAsr;
1216 break;
1217 case Instruction::USHR_INT:
1218 case Instruction::USHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001219 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001220 op = kOpLsr;
1221 break;
1222 default:
buzbeecbd6d442012-11-17 14:11:25 -08001223 LOG(FATAL) << "Invalid word arith op: " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001224 }
buzbeefa57c472012-11-21 12:06:18 -08001225 if (!is_div_rem) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001226 if (unary) {
buzbeefa57c472012-11-21 12:06:18 -08001227 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1228 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1229 OpRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg);
buzbee31a4a6f2012-02-28 15:36:15 -08001230 } else {
buzbeefa57c472012-11-21 12:06:18 -08001231 if (shift_op) {
1232 int t_reg = INVALID_REG;
1233 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -07001234 // X86 doesn't require masking and must use ECX
buzbeefa57c472012-11-21 12:06:18 -08001235 t_reg = TargetReg(kCount); // rCX
1236 LoadValueDirectFixed(cu, rl_src2, t_reg);
buzbeeb046e162012-10-30 15:48:42 -07001237 } else {
buzbeefa57c472012-11-21 12:06:18 -08001238 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1239 t_reg = AllocTemp(cu);
1240 OpRegRegImm(cu, kOpAnd, t_reg, rl_src2.low_reg, 31);
buzbeeb046e162012-10-30 15:48:42 -07001241 }
buzbeefa57c472012-11-21 12:06:18 -08001242 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1243 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1244 OpRegRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg, t_reg);
1245 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001246 } else {
buzbeefa57c472012-11-21 12:06:18 -08001247 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1248 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1249 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1250 OpRegRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001251 }
buzbee31a4a6f2012-02-28 15:36:15 -08001252 }
buzbeefa57c472012-11-21 12:06:18 -08001253 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001254 } else {
buzbeefa57c472012-11-21 12:06:18 -08001255 if (cu->instruction_set == kMips) {
1256 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1257 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1258 if (check_zero) {
1259 GenImmedCheck(cu, kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001260 }
buzbeefa57c472012-11-21 12:06:18 -08001261 rl_result = GenDivRem(cu, rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
jeffhao4f8f04a2012-10-02 18:10:35 -07001262 } else {
buzbeefa57c472012-11-21 12:06:18 -08001263 int func_offset = ENTRYPOINT_OFFSET(pIdivmod);
1264 FlushAllRegs(cu); /* Send everything to home location */
1265 LoadValueDirectFixed(cu, rl_src2, TargetReg(kArg1));
1266 int r_tgt = CallHelperSetup(cu, func_offset);
1267 LoadValueDirectFixed(cu, rl_src1, TargetReg(kArg0));
1268 if (check_zero) {
1269 GenImmedCheck(cu, kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001270 }
1271 // NOTE: callout here is not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001272 CallHelper(cu, r_tgt, func_offset, false /* not a safepoint */ );
buzbeeb046e162012-10-30 15:48:42 -07001273 if (op == kOpDiv)
buzbeefa57c472012-11-21 12:06:18 -08001274 rl_result = GetReturn(cu, false);
buzbeeb046e162012-10-30 15:48:42 -07001275 else
buzbeefa57c472012-11-21 12:06:18 -08001276 rl_result = GetReturnAlt(cu);
jeffhao4f8f04a2012-10-02 18:10:35 -07001277 }
buzbeefa57c472012-11-21 12:06:18 -08001278 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001279 }
1280 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001281}
1282
1283/*
1284 * The following are the first-level codegen routines that analyze the format
1285 * of each bytecode then either dispatch special purpose codegen routines
1286 * or produce corresponding Thumb instructions directly.
1287 */
1288
buzbeeaad94382012-11-21 07:40:50 -08001289static bool IsPowerOfTwo(int x)
buzbee31a4a6f2012-02-28 15:36:15 -08001290{
Bill Buzbeea114add2012-05-03 15:00:40 -07001291 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001292}
1293
1294// Returns true if no more than two bits are set in 'x'.
buzbeeaad94382012-11-21 07:40:50 -08001295static bool IsPopCountLE2(unsigned int x)
buzbee31a4a6f2012-02-28 15:36:15 -08001296{
Bill Buzbeea114add2012-05-03 15:00:40 -07001297 x &= x - 1;
1298 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001299}
1300
1301// Returns the index of the lowest set bit in 'x'.
buzbeeaad94382012-11-21 07:40:50 -08001302static int LowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001303 int bit_posn = 0;
1304 while ((x & 0xf) == 0) {
1305 bit_posn += 4;
1306 x >>= 4;
1307 }
1308 while ((x & 1) == 0) {
1309 bit_posn++;
1310 x >>= 1;
1311 }
1312 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001313}
1314
buzbeefa57c472012-11-21 12:06:18 -08001315// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1316// and store the result in 'rl_dest'.
1317static bool HandleEasyDivide(CompilationUnit* cu, Instruction::Code dalvik_opcode,
1318 RegLocation rl_src, RegLocation rl_dest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001319{
buzbeefa57c472012-11-21 12:06:18 -08001320 if ((lit < 2) || ((cu->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001321 return false;
buzbee0f79d722012-11-01 15:35:27 -07001322 }
buzbee02031b12012-11-23 09:41:35 -08001323 Codegen* cg = cu->cg.get();
buzbeeb046e162012-10-30 15:48:42 -07001324 // No divide instruction for Arm, so check for more special cases
buzbeefa57c472012-11-21 12:06:18 -08001325 if ((cu->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee02031b12012-11-23 09:41:35 -08001326 return cg->SmallLiteralDivide(cu, dalvik_opcode, rl_src, rl_dest, lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001327 }
buzbee52a77fc2012-11-20 19:50:46 -08001328 int k = LowestSetBit(lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001329 if (k >= 30) {
1330 // Avoid special cases.
1331 return false;
1332 }
buzbeefa57c472012-11-21 12:06:18 -08001333 bool div = (dalvik_opcode == Instruction::DIV_INT_LIT8 ||
1334 dalvik_opcode == Instruction::DIV_INT_LIT16);
buzbee02031b12012-11-23 09:41:35 -08001335 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001336 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001337 if (div) {
buzbeefa57c472012-11-21 12:06:18 -08001338 int t_reg = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001339 if (lit == 2) {
1340 // Division by 2 is by far the most common division by constant.
buzbee02031b12012-11-23 09:41:35 -08001341 cg->OpRegRegImm(cu, kOpLsr, t_reg, rl_src.low_reg, 32 - k);
1342 cg->OpRegRegReg(cu, kOpAdd, t_reg, t_reg, rl_src.low_reg);
1343 cg->OpRegRegImm(cu, kOpAsr, rl_result.low_reg, t_reg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001344 } else {
buzbee02031b12012-11-23 09:41:35 -08001345 cg->OpRegRegImm(cu, kOpAsr, t_reg, rl_src.low_reg, 31);
1346 cg->OpRegRegImm(cu, kOpLsr, t_reg, t_reg, 32 - k);
1347 cg->OpRegRegReg(cu, kOpAdd, t_reg, t_reg, rl_src.low_reg);
1348 cg->OpRegRegImm(cu, kOpAsr, rl_result.low_reg, t_reg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001349 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001350 } else {
buzbeefa57c472012-11-21 12:06:18 -08001351 int t_reg1 = AllocTemp(cu);
1352 int t_reg2 = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001353 if (lit == 2) {
buzbee02031b12012-11-23 09:41:35 -08001354 cg->OpRegRegImm(cu, kOpLsr, t_reg1, rl_src.low_reg, 32 - k);
1355 cg->OpRegRegReg(cu, kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1356 cg->OpRegRegImm(cu, kOpAnd, t_reg2, t_reg2, lit -1);
1357 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Bill Buzbeea114add2012-05-03 15:00:40 -07001358 } else {
buzbee02031b12012-11-23 09:41:35 -08001359 cg->OpRegRegImm(cu, kOpAsr, t_reg1, rl_src.low_reg, 31);
1360 cg->OpRegRegImm(cu, kOpLsr, t_reg1, t_reg1, 32 - k);
1361 cg->OpRegRegReg(cu, kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1362 cg->OpRegRegImm(cu, kOpAnd, t_reg2, t_reg2, lit - 1);
1363 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Bill Buzbeea114add2012-05-03 15:00:40 -07001364 }
1365 }
buzbee02031b12012-11-23 09:41:35 -08001366 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001367 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001368}
1369
buzbeefa57c472012-11-21 12:06:18 -08001370// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1371// and store the result in 'rl_dest'.
1372static bool HandleEasyMultiply(CompilationUnit* cu, RegLocation rl_src,
1373 RegLocation rl_dest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001374{
Bill Buzbeea114add2012-05-03 15:00:40 -07001375 // Can we simplify this multiplication?
buzbeefa57c472012-11-21 12:06:18 -08001376 bool power_of_two = false;
1377 bool pop_count_le2 = false;
1378 bool power_of_two_minus_one = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001379 if (lit < 2) {
1380 // Avoid special cases.
1381 return false;
buzbee52a77fc2012-11-20 19:50:46 -08001382 } else if (IsPowerOfTwo(lit)) {
buzbeefa57c472012-11-21 12:06:18 -08001383 power_of_two = true;
buzbee52a77fc2012-11-20 19:50:46 -08001384 } else if (IsPopCountLE2(lit)) {
buzbeefa57c472012-11-21 12:06:18 -08001385 pop_count_le2 = true;
buzbee52a77fc2012-11-20 19:50:46 -08001386 } else if (IsPowerOfTwo(lit + 1)) {
buzbeefa57c472012-11-21 12:06:18 -08001387 power_of_two_minus_one = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001388 } else {
1389 return false;
1390 }
buzbee02031b12012-11-23 09:41:35 -08001391 Codegen* cg = cu->cg.get();
1392 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001393 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1394 if (power_of_two) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001395 // Shift.
buzbee02031b12012-11-23 09:41:35 -08001396 cg->OpRegRegImm(cu, kOpLsl, rl_result.low_reg, rl_src.low_reg, LowestSetBit(lit));
buzbeefa57c472012-11-21 12:06:18 -08001397 } else if (pop_count_le2) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001398 // Shift and add and shift.
buzbeefa57c472012-11-21 12:06:18 -08001399 int first_bit = LowestSetBit(lit);
1400 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
buzbee02031b12012-11-23 09:41:35 -08001401 cg->GenMultiplyByTwoBitMultiplier(cu, rl_src, rl_result, lit, first_bit, second_bit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001402 } else {
1403 // Reverse subtract: (src << (shift + 1)) - src.
buzbeefa57c472012-11-21 12:06:18 -08001404 DCHECK(power_of_two_minus_one);
buzbee52a77fc2012-11-20 19:50:46 -08001405 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbeefa57c472012-11-21 12:06:18 -08001406 int t_reg = AllocTemp(cu);
buzbee02031b12012-11-23 09:41:35 -08001407 cg->OpRegRegImm(cu, kOpLsl, t_reg, rl_src.low_reg, LowestSetBit(lit + 1));
1408 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001409 }
buzbee02031b12012-11-23 09:41:35 -08001410 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001411 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001412}
1413
buzbee02031b12012-11-23 09:41:35 -08001414bool Codegen::GenArithOpIntLit(CompilationUnit* cu, Instruction::Code opcode,
1415 RegLocation rl_dest, RegLocation rl_src, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001416{
buzbeefa57c472012-11-21 12:06:18 -08001417 RegLocation rl_result;
buzbeecbd6d442012-11-17 14:11:25 -08001418 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
buzbeefa57c472012-11-21 12:06:18 -08001419 int shift_op = false;
1420 bool is_div = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001421
buzbee408ad162012-06-06 16:45:18 -07001422 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001423 case Instruction::RSUB_INT_LIT8:
1424 case Instruction::RSUB_INT: {
buzbeefa57c472012-11-21 12:06:18 -08001425 int t_reg;
Bill Buzbeea114add2012-05-03 15:00:40 -07001426 //TUNING: add support for use of Arm rsub op
buzbeefa57c472012-11-21 12:06:18 -08001427 rl_src = LoadValue(cu, rl_src, kCoreReg);
1428 t_reg = AllocTemp(cu);
1429 LoadConstant(cu, t_reg, lit);
1430 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1431 OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
1432 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001433 return false;
1434 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001435 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001436
buzbeee6285f92012-12-06 15:57:46 -08001437 case Instruction::SUB_INT:
1438 case Instruction::SUB_INT_2ADDR:
1439 lit = -lit;
1440 // Intended fallthrough
1441 case Instruction::ADD_INT:
1442 case Instruction::ADD_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001443 case Instruction::ADD_INT_LIT8:
1444 case Instruction::ADD_INT_LIT16:
1445 op = kOpAdd;
1446 break;
buzbeee6285f92012-12-06 15:57:46 -08001447 case Instruction::MUL_INT:
1448 case Instruction::MUL_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001449 case Instruction::MUL_INT_LIT8:
1450 case Instruction::MUL_INT_LIT16: {
buzbeefa57c472012-11-21 12:06:18 -08001451 if (HandleEasyMultiply(cu, rl_src, rl_dest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001452 return false;
1453 }
1454 op = kOpMul;
1455 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001456 }
buzbeee6285f92012-12-06 15:57:46 -08001457 case Instruction::AND_INT:
1458 case Instruction::AND_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001459 case Instruction::AND_INT_LIT8:
1460 case Instruction::AND_INT_LIT16:
1461 op = kOpAnd;
1462 break;
buzbeee6285f92012-12-06 15:57:46 -08001463 case Instruction::OR_INT:
1464 case Instruction::OR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001465 case Instruction::OR_INT_LIT8:
1466 case Instruction::OR_INT_LIT16:
1467 op = kOpOr;
1468 break;
buzbeee6285f92012-12-06 15:57:46 -08001469 case Instruction::XOR_INT:
1470 case Instruction::XOR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001471 case Instruction::XOR_INT_LIT8:
1472 case Instruction::XOR_INT_LIT16:
1473 op = kOpXor;
1474 break;
1475 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001476 case Instruction::SHL_INT:
buzbeee6285f92012-12-06 15:57:46 -08001477 case Instruction::SHL_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001478 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001479 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001480 op = kOpLsl;
1481 break;
1482 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001483 case Instruction::SHR_INT:
buzbeee6285f92012-12-06 15:57:46 -08001484 case Instruction::SHR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001485 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001486 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001487 op = kOpAsr;
1488 break;
1489 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001490 case Instruction::USHR_INT:
buzbeee6285f92012-12-06 15:57:46 -08001491 case Instruction::USHR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001492 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001493 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001494 op = kOpLsr;
1495 break;
1496
buzbeee6285f92012-12-06 15:57:46 -08001497 case Instruction::DIV_INT:
1498 case Instruction::DIV_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001499 case Instruction::DIV_INT_LIT8:
1500 case Instruction::DIV_INT_LIT16:
buzbeee6285f92012-12-06 15:57:46 -08001501 case Instruction::REM_INT:
1502 case Instruction::REM_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001503 case Instruction::REM_INT_LIT8:
jeffhao4f8f04a2012-10-02 18:10:35 -07001504 case Instruction::REM_INT_LIT16: {
Bill Buzbeea114add2012-05-03 15:00:40 -07001505 if (lit == 0) {
buzbeefa57c472012-11-21 12:06:18 -08001506 GenImmedCheck(cu, kCondAl, 0, 0, kThrowDivZero);
Bill Buzbeea114add2012-05-03 15:00:40 -07001507 return false;
1508 }
buzbeefa57c472012-11-21 12:06:18 -08001509 if (HandleEasyDivide(cu, opcode, rl_src, rl_dest, lit)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001510 return false;
1511 }
buzbee408ad162012-06-06 16:45:18 -07001512 if ((opcode == Instruction::DIV_INT_LIT8) ||
buzbeee6285f92012-12-06 15:57:46 -08001513 (opcode == Instruction::DIV_INT) ||
1514 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee408ad162012-06-06 16:45:18 -07001515 (opcode == Instruction::DIV_INT_LIT16)) {
buzbeefa57c472012-11-21 12:06:18 -08001516 is_div = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001517 } else {
buzbeefa57c472012-11-21 12:06:18 -08001518 is_div = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001519 }
buzbeefa57c472012-11-21 12:06:18 -08001520 if (cu->instruction_set == kMips) {
1521 rl_src = LoadValue(cu, rl_src, kCoreReg);
1522 rl_result = GenDivRemLit(cu, rl_dest, rl_src.low_reg, lit, is_div);
jeffhao4f8f04a2012-10-02 18:10:35 -07001523 } else {
buzbeefa57c472012-11-21 12:06:18 -08001524 FlushAllRegs(cu); /* Everything to home location */
1525 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0));
1526 Clobber(cu, TargetReg(kArg0));
1527 int func_offset = ENTRYPOINT_OFFSET(pIdivmod);
1528 CallRuntimeHelperRegImm(cu, func_offset, TargetReg(kArg0), lit, false);
1529 if (is_div)
1530 rl_result = GetReturn(cu, false);
buzbeeb046e162012-10-30 15:48:42 -07001531 else
buzbeefa57c472012-11-21 12:06:18 -08001532 rl_result = GetReturnAlt(cu);
jeffhao4f8f04a2012-10-02 18:10:35 -07001533 }
buzbeefa57c472012-11-21 12:06:18 -08001534 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001535 return false;
1536 break;
jeffhao4f8f04a2012-10-02 18:10:35 -07001537 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001538 default:
buzbeee6285f92012-12-06 15:57:46 -08001539 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001540 }
buzbeefa57c472012-11-21 12:06:18 -08001541 rl_src = LoadValue(cu, rl_src, kCoreReg);
1542 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001543 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
buzbeefa57c472012-11-21 12:06:18 -08001544 if (shift_op && (lit == 0)) {
1545 OpRegCopy(cu, rl_result.low_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001546 } else {
buzbeefa57c472012-11-21 12:06:18 -08001547 OpRegRegImm(cu, op, rl_result.low_reg, rl_src.low_reg, lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001548 }
buzbeefa57c472012-11-21 12:06:18 -08001549 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001550 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001551}
1552
buzbee02031b12012-11-23 09:41:35 -08001553bool Codegen::GenArithOpLong(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
1554 RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001555{
buzbeefa57c472012-11-21 12:06:18 -08001556 RegLocation rl_result;
1557 OpKind first_op = kOpBkpt;
1558 OpKind second_op = kOpBkpt;
1559 bool call_out = false;
1560 bool check_zero = false;
1561 int func_offset;
1562 int ret_reg = TargetReg(kRet0);
buzbee31a4a6f2012-02-28 15:36:15 -08001563
buzbee408ad162012-06-06 16:45:18 -07001564 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001565 case Instruction::NOT_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001566 rl_src2 = LoadValueWide(cu, rl_src2, kCoreReg);
1567 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001568 // Check for destructive overlap
buzbeefa57c472012-11-21 12:06:18 -08001569 if (rl_result.low_reg == rl_src2.high_reg) {
1570 int t_reg = AllocTemp(cu);
1571 OpRegCopy(cu, t_reg, rl_src2.high_reg);
1572 OpRegReg(cu, kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1573 OpRegReg(cu, kOpMvn, rl_result.high_reg, t_reg);
1574 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001575 } else {
buzbeefa57c472012-11-21 12:06:18 -08001576 OpRegReg(cu, kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1577 OpRegReg(cu, kOpMvn, rl_result.high_reg, rl_src2.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001578 }
buzbeefa57c472012-11-21 12:06:18 -08001579 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001580 return false;
1581 break;
1582 case Instruction::ADD_LONG:
1583 case Instruction::ADD_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001584 if (cu->instruction_set != kThumb2) {
1585 return GenAddLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001586 }
buzbeefa57c472012-11-21 12:06:18 -08001587 first_op = kOpAdd;
1588 second_op = kOpAdc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001589 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001590 case Instruction::SUB_LONG:
1591 case Instruction::SUB_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001592 if (cu->instruction_set != kThumb2) {
1593 return GenSubLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001594 }
buzbeefa57c472012-11-21 12:06:18 -08001595 first_op = kOpSub;
1596 second_op = kOpSbc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001597 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001598 case Instruction::MUL_LONG:
1599 case Instruction::MUL_LONG_2ADDR:
buzbee4ef3e452012-12-14 13:35:28 -08001600 if (cu->instruction_set == kThumb2) {
1601 GenMulLong(cu, rl_dest, rl_src1, rl_src2);
1602 return false;
1603 } else {
1604 call_out = true;
1605 ret_reg = TargetReg(kRet0);
1606 func_offset = ENTRYPOINT_OFFSET(pLmul);
1607 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001608 break;
1609 case Instruction::DIV_LONG:
1610 case Instruction::DIV_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001611 call_out = true;
1612 check_zero = true;
1613 ret_reg = TargetReg(kRet0);
1614 func_offset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07001615 break;
1616 case Instruction::REM_LONG:
1617 case Instruction::REM_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001618 call_out = true;
1619 check_zero = true;
1620 func_offset = ENTRYPOINT_OFFSET(pLdivmod);
buzbeef0504cd2012-11-13 16:31:10 -08001621 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbeefa57c472012-11-21 12:06:18 -08001622 ret_reg = (cu->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001623 break;
1624 case Instruction::AND_LONG_2ADDR:
1625 case Instruction::AND_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001626 if (cu->instruction_set == kX86) {
1627 return GenAndLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001628 }
buzbeefa57c472012-11-21 12:06:18 -08001629 first_op = kOpAnd;
1630 second_op = kOpAnd;
Bill Buzbeea114add2012-05-03 15:00:40 -07001631 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001632 case Instruction::OR_LONG:
1633 case Instruction::OR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001634 if (cu->instruction_set == kX86) {
1635 return GenOrLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001636 }
buzbeefa57c472012-11-21 12:06:18 -08001637 first_op = kOpOr;
1638 second_op = kOpOr;
Bill Buzbeea114add2012-05-03 15:00:40 -07001639 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001640 case Instruction::XOR_LONG:
1641 case Instruction::XOR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001642 if (cu->instruction_set == kX86) {
1643 return GenXorLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001644 }
buzbeefa57c472012-11-21 12:06:18 -08001645 first_op = kOpXor;
1646 second_op = kOpXor;
Bill Buzbeea114add2012-05-03 15:00:40 -07001647 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001648 case Instruction::NEG_LONG: {
buzbeefa57c472012-11-21 12:06:18 -08001649 return GenNegLong(cu, rl_dest, rl_src2);
buzbee31a4a6f2012-02-28 15:36:15 -08001650 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001651 default:
1652 LOG(FATAL) << "Invalid long arith op";
1653 }
buzbeefa57c472012-11-21 12:06:18 -08001654 if (!call_out) {
1655 GenLong3Addr(cu, first_op, second_op, rl_dest, rl_src1, rl_src2);
Bill Buzbeea114add2012-05-03 15:00:40 -07001656 } else {
buzbeefa57c472012-11-21 12:06:18 -08001657 FlushAllRegs(cu); /* Send everything to home location */
1658 if (check_zero) {
1659 LoadValueDirectWideFixed(cu, rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1660 int r_tgt = CallHelperSetup(cu, func_offset);
1661 GenDivZeroCheck(cu, TargetReg(kArg2), TargetReg(kArg3));
1662 LoadValueDirectWideFixed(cu, rl_src1, TargetReg(kArg0), TargetReg(kArg1));
buzbee8320f382012-09-11 16:29:42 -07001663 // NOTE: callout here is not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001664 CallHelper(cu, r_tgt, func_offset, false /* not safepoint */);
buzbee31a4a6f2012-02-28 15:36:15 -08001665 } else {
buzbeefa57c472012-11-21 12:06:18 -08001666 CallRuntimeHelperRegLocationRegLocation(cu, func_offset,
1667 rl_src1, rl_src2, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001668 }
buzbeef0504cd2012-11-13 16:31:10 -08001669 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbeefa57c472012-11-21 12:06:18 -08001670 if (ret_reg == TargetReg(kRet0))
1671 rl_result = GetReturnWide(cu, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07001672 else
buzbeefa57c472012-11-21 12:06:18 -08001673 rl_result = GetReturnWideAlt(cu);
1674 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001675 }
1676 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001677}
1678
buzbee02031b12012-11-23 09:41:35 -08001679bool Codegen::GenConversionCall(CompilationUnit* cu, int func_offset,
1680 RegLocation rl_dest, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -08001681{
Bill Buzbeea114add2012-05-03 15:00:40 -07001682 /*
1683 * Don't optimize the register usage since it calls out to support
1684 * functions
1685 */
buzbeefa57c472012-11-21 12:06:18 -08001686 FlushAllRegs(cu); /* Send everything to home location */
1687 if (rl_src.wide) {
1688 LoadValueDirectWideFixed(cu, rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0),
1689 rl_src.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
buzbee408ad162012-06-06 16:45:18 -07001690 } else {
buzbeefa57c472012-11-21 12:06:18 -08001691 LoadValueDirectFixed(cu, rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -07001692 }
buzbeefa57c472012-11-21 12:06:18 -08001693 CallRuntimeHelperRegLocation(cu, func_offset, rl_src, false);
1694 if (rl_dest.wide) {
1695 RegLocation rl_result;
1696 rl_result = GetReturnWide(cu, rl_dest.fp);
1697 StoreValueWide(cu, rl_dest, rl_result);
buzbee408ad162012-06-06 16:45:18 -07001698 } else {
buzbeefa57c472012-11-21 12:06:18 -08001699 RegLocation rl_result;
1700 rl_result = GetReturn(cu, rl_dest.fp);
1701 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001702 }
1703 return false;
buzbee31a4a6f2012-02-28 15:36:15 -08001704}
1705
buzbee31a4a6f2012-02-28 15:36:15 -08001706/* Check if we need to check for pending suspend request */
buzbee02031b12012-11-23 09:41:35 -08001707void Codegen::GenSuspendTest(CompilationUnit* cu, int opt_flags)
buzbee31a4a6f2012-02-28 15:36:15 -08001708{
buzbeefa57c472012-11-21 12:06:18 -08001709 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001710 return;
1711 }
buzbeefa57c472012-11-21 12:06:18 -08001712 FlushAllRegs(cu);
1713 LIR* branch = OpTestSuspend(cu, NULL);
1714 LIR* ret_lab = NewLIR0(cu, kPseudoTargetLabel);
1715 LIR* target = RawLIR(cu, cu->current_dalvik_offset, kPseudoSuspendTarget,
1716 reinterpret_cast<uintptr_t>(ret_lab), cu->current_dalvik_offset);
buzbeecbd6d442012-11-17 14:11:25 -08001717 branch->target = target;
buzbeefa57c472012-11-21 12:06:18 -08001718 InsertGrowableList(cu, &cu->suspend_launchpads, reinterpret_cast<uintptr_t>(target));
buzbee31a4a6f2012-02-28 15:36:15 -08001719}
1720
buzbeefead2932012-03-30 14:02:01 -07001721/* Check if we need to check for pending suspend request */
buzbee02031b12012-11-23 09:41:35 -08001722void Codegen::GenSuspendTestAndBranch(CompilationUnit* cu, int opt_flags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07001723{
buzbeefa57c472012-11-21 12:06:18 -08001724 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1725 OpUnconditionalBranch(cu, target);
Bill Buzbeea114add2012-05-03 15:00:40 -07001726 return;
1727 }
buzbeefa57c472012-11-21 12:06:18 -08001728 OpTestSuspend(cu, target);
1729 LIR* launch_pad =
1730 RawLIR(cu, cu->current_dalvik_offset, kPseudoSuspendTarget,
1731 reinterpret_cast<uintptr_t>(target), cu->current_dalvik_offset);
1732 FlushAllRegs(cu);
1733 OpUnconditionalBranch(cu, launch_pad);
1734 InsertGrowableList(cu, &cu->suspend_launchpads, reinterpret_cast<uintptr_t>(launch_pad));
buzbeefead2932012-03-30 14:02:01 -07001735}
1736
buzbee31a4a6f2012-02-28 15:36:15 -08001737} // namespace art