blob: 3c4b1114735a7f254da6e4409e84650f9d65aaf2 [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
Brian Carlstrom265091e2013-01-30 14:08:26 -080017#include "compiler/dex/quick/codegen_util.h"
buzbee395116c2013-02-27 14:30:25 -080018#include "compiler/dex/compiler_ir.h"
Brian Carlstrom641ce032013-01-31 15:21:37 -080019#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;
Ian Rogers1212a022013-03-04 10:48:41 -0800224 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx,
225 *cu->dex_file,
226 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;
Ian Rogers1212a022013-03-04 10:48:41 -0800248 if (cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx,
249 *cu->dex_file,
250 type_idx)) {
buzbeefa57c472012-11-21 12:06:18 -0800251 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
Brian Carlstrom265091e2013-01-30 14:08:26 -0800357 DexCompilationUnit m_unit(cu);
buzbee31a4a6f2012-02-28 15:36:15 -0800358
buzbeefa57c472012-11-21 12:06:18 -0800359 bool fast_path =
Ian Rogers1212a022013-03-04 10:48:41 -0800360 cu->compiler_driver->ComputeStaticFieldInfo(field_idx, &m_unit,
361 field_offset, ssb_index,
362 is_referrers_class, is_volatile,
363 true);
buzbeefa57c472012-11-21 12:06:18 -0800364 if (fast_path && !SLOW_FIELD_PATH) {
365 DCHECK_GE(field_offset, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700366 int rBase;
buzbeefa57c472012-11-21 12:06:18 -0800367 if (is_referrers_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700368 // Fast path, static storage base is this method's class
buzbeefa57c472012-11-21 12:06:18 -0800369 RegLocation rl_method = LoadCurrMethod(cu);
370 rBase = AllocTemp(cu);
371 LoadWordDisp(cu, rl_method.low_reg,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800372 mirror::AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
buzbeefa57c472012-11-21 12:06:18 -0800373 if (IsTemp(cu, rl_method.low_reg)) {
374 FreeTemp(cu, rl_method.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700375 }
buzbee31a4a6f2012-02-28 15:36:15 -0800376 } else {
Ian Rogersaed07162013-03-15 12:00:53 -0700377 // Medium path, static storage base in a different class which requires checks that the other
378 // class is initialized.
379 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
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);
Ian Rogersaed07162013-03-15 12:00:53 -0700383 // Using fixed register to sync with possible call to runtime support.
buzbeefa57c472012-11-21 12:06:18 -0800384 int r_method = TargetReg(kArg1);
385 LockTemp(cu, r_method);
386 LoadCurrMethodDirect(cu, r_method);
buzbee52a77fc2012-11-20 19:50:46 -0800387 rBase = TargetReg(kArg0);
buzbeefa57c472012-11-21 12:06:18 -0800388 LockTemp(cu, rBase);
389 LoadWordDisp(cu, r_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800390 mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700391 rBase);
buzbeefa57c472012-11-21 12:06:18 -0800392 LoadWordDisp(cu, rBase,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800393 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800394 sizeof(int32_t*) * ssb_index, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700395 // rBase now points at appropriate static storage base (Class*)
396 // or NULL if not initialized. Check for NULL and call helper if NULL.
397 // TUNING: fast path should fall through
buzbeefa57c472012-11-21 12:06:18 -0800398 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, rBase, 0, NULL);
399 LoadConstant(cu, TargetReg(kArg0), ssb_index);
400 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssb_index, true);
401 if (cu->instruction_set == kMips) {
buzbeef0504cd2012-11-13 16:31:10 -0800402 // For Arm, kRet0 = kArg0 = rBase, for Mips, we need to copy
buzbeefa57c472012-11-21 12:06:18 -0800403 OpRegCopy(cu, rBase, TargetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700404 }
buzbeefa57c472012-11-21 12:06:18 -0800405 LIR* skip_target = NewLIR0(cu, kPseudoTargetLabel);
406 branch_over->target = skip_target;
407 FreeTemp(cu, r_method);
buzbee31a4a6f2012-02-28 15:36:15 -0800408 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700409 // rBase now holds static storage base
buzbeefa57c472012-11-21 12:06:18 -0800410 if (is_long_or_double) {
411 rl_src = LoadValueWide(cu, rl_src, kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700412 } else {
buzbeefa57c472012-11-21 12:06:18 -0800413 rl_src = LoadValue(cu, rl_src, kAnyReg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700414 }
buzbeefa57c472012-11-21 12:06:18 -0800415 if (is_volatile) {
416 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700417 }
buzbeefa57c472012-11-21 12:06:18 -0800418 if (is_long_or_double) {
419 StoreBaseDispWide(cu, rBase, field_offset, rl_src.low_reg,
420 rl_src.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700421 } else {
buzbeefa57c472012-11-21 12:06:18 -0800422 StoreWordDisp(cu, rBase, field_offset, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700423 }
buzbeefa57c472012-11-21 12:06:18 -0800424 if (is_volatile) {
425 GenMemBarrier(cu, kStoreLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700426 }
buzbee6a791b22013-02-07 05:35:08 -0800427 if (is_object && !IsConstantNullRef(cu, rl_src)) {
buzbeefa57c472012-11-21 12:06:18 -0800428 MarkGCCard(cu, rl_src.low_reg, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700429 }
buzbeefa57c472012-11-21 12:06:18 -0800430 FreeTemp(cu, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700431 } else {
buzbeefa57c472012-11-21 12:06:18 -0800432 FlushAllRegs(cu); // Everything to home locations
433 int setter_offset = is_long_or_double ? ENTRYPOINT_OFFSET(pSet64Static) :
434 (is_object ? ENTRYPOINT_OFFSET(pSetObjStatic)
Bill Buzbeea114add2012-05-03 15:00:40 -0700435 : ENTRYPOINT_OFFSET(pSet32Static));
buzbeefa57c472012-11-21 12:06:18 -0800436 CallRuntimeHelperImmRegLocation(cu, setter_offset, field_idx, rl_src, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700437 }
buzbee31a4a6f2012-02-28 15:36:15 -0800438}
439
buzbee02031b12012-11-23 09:41:35 -0800440void Codegen::GenSget(CompilationUnit* cu, uint32_t field_idx, RegLocation rl_dest,
441 bool is_long_or_double, bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800442{
buzbeefa57c472012-11-21 12:06:18 -0800443 int field_offset;
444 int ssb_index;
445 bool is_volatile;
446 bool is_referrers_class;
buzbee31a4a6f2012-02-28 15:36:15 -0800447
Brian Carlstrom265091e2013-01-30 14:08:26 -0800448 DexCompilationUnit m_unit(cu);
buzbee31a4a6f2012-02-28 15:36:15 -0800449
buzbeefa57c472012-11-21 12:06:18 -0800450 bool fast_path =
Ian Rogers1212a022013-03-04 10:48:41 -0800451 cu->compiler_driver->ComputeStaticFieldInfo(field_idx, &m_unit,
452 field_offset, ssb_index,
453 is_referrers_class, is_volatile,
454 false);
buzbeefa57c472012-11-21 12:06:18 -0800455 if (fast_path && !SLOW_FIELD_PATH) {
456 DCHECK_GE(field_offset, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700457 int rBase;
buzbeefa57c472012-11-21 12:06:18 -0800458 if (is_referrers_class) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700459 // Fast path, static storage base is this method's class
buzbeefa57c472012-11-21 12:06:18 -0800460 RegLocation rl_method = LoadCurrMethod(cu);
461 rBase = AllocTemp(cu);
462 LoadWordDisp(cu, rl_method.low_reg,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800463 mirror::AbstractMethod::DeclaringClassOffset().Int32Value(), rBase);
buzbee31a4a6f2012-02-28 15:36:15 -0800464 } else {
Ian Rogersaed07162013-03-15 12:00:53 -0700465 // Medium path, static storage base in a different class which requires checks that the other
466 // class is initialized
467 // TODO: remove initialized check now that we are initializing classes in the compiler driver.
buzbeefa57c472012-11-21 12:06:18 -0800468 DCHECK_GE(ssb_index, 0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700469 // May do runtime call so everything to home locations.
buzbeefa57c472012-11-21 12:06:18 -0800470 FlushAllRegs(cu);
Ian Rogersaed07162013-03-15 12:00:53 -0700471 // Using fixed register to sync with possible call to runtime support.
buzbeefa57c472012-11-21 12:06:18 -0800472 int r_method = TargetReg(kArg1);
473 LockTemp(cu, r_method);
474 LoadCurrMethodDirect(cu, r_method);
buzbee52a77fc2012-11-20 19:50:46 -0800475 rBase = TargetReg(kArg0);
buzbeefa57c472012-11-21 12:06:18 -0800476 LockTemp(cu, rBase);
477 LoadWordDisp(cu, r_method,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800478 mirror::AbstractMethod::DexCacheInitializedStaticStorageOffset().Int32Value(),
Bill Buzbeea114add2012-05-03 15:00:40 -0700479 rBase);
buzbeefa57c472012-11-21 12:06:18 -0800480 LoadWordDisp(cu, rBase,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800481 mirror::Array::DataOffset(sizeof(mirror::Object*)).Int32Value() +
buzbeefa57c472012-11-21 12:06:18 -0800482 sizeof(int32_t*) * ssb_index, rBase);
Bill Buzbeea114add2012-05-03 15:00:40 -0700483 // rBase now points at appropriate static storage base (Class*)
484 // or NULL if not initialized. Check for NULL and call helper if NULL.
485 // TUNING: fast path should fall through
buzbeefa57c472012-11-21 12:06:18 -0800486 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, rBase, 0, NULL);
487 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeStaticStorage), ssb_index, true);
488 if (cu->instruction_set == kMips) {
buzbeef0504cd2012-11-13 16:31:10 -0800489 // For Arm, kRet0 = kArg0 = rBase, for Mips, we need to copy
buzbeefa57c472012-11-21 12:06:18 -0800490 OpRegCopy(cu, rBase, TargetReg(kRet0));
buzbeeb046e162012-10-30 15:48:42 -0700491 }
buzbeefa57c472012-11-21 12:06:18 -0800492 LIR* skip_target = NewLIR0(cu, kPseudoTargetLabel);
493 branch_over->target = skip_target;
494 FreeTemp(cu, r_method);
buzbee31a4a6f2012-02-28 15:36:15 -0800495 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700496 // rBase now holds static storage base
buzbeefa57c472012-11-21 12:06:18 -0800497 RegLocation rl_result = EvalLoc(cu, rl_dest, kAnyReg, true);
498 if (is_volatile) {
499 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700500 }
buzbeefa57c472012-11-21 12:06:18 -0800501 if (is_long_or_double) {
502 LoadBaseDispWide(cu, rBase, field_offset, rl_result.low_reg,
503 rl_result.high_reg, INVALID_SREG);
Bill Buzbeea114add2012-05-03 15:00:40 -0700504 } else {
buzbeefa57c472012-11-21 12:06:18 -0800505 LoadWordDisp(cu, rBase, field_offset, rl_result.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700506 }
buzbeefa57c472012-11-21 12:06:18 -0800507 FreeTemp(cu, rBase);
508 if (is_long_or_double) {
509 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700510 } else {
buzbeefa57c472012-11-21 12:06:18 -0800511 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700512 }
513 } else {
buzbeefa57c472012-11-21 12:06:18 -0800514 FlushAllRegs(cu); // Everything to home locations
515 int getterOffset = is_long_or_double ? ENTRYPOINT_OFFSET(pGet64Static) :
516 (is_object ? ENTRYPOINT_OFFSET(pGetObjStatic)
Bill Buzbeea114add2012-05-03 15:00:40 -0700517 : ENTRYPOINT_OFFSET(pGet32Static));
buzbeefa57c472012-11-21 12:06:18 -0800518 CallRuntimeHelperImm(cu, getterOffset, field_idx, true);
519 if (is_long_or_double) {
520 RegLocation rl_result = GetReturnWide(cu, rl_dest.fp);
521 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700522 } else {
buzbeefa57c472012-11-21 12:06:18 -0800523 RegLocation rl_result = GetReturn(cu, rl_dest.fp);
524 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700525 }
526 }
buzbee31a4a6f2012-02-28 15:36:15 -0800527}
528
529
530// Debugging routine - if null target, branch to DebugMe
buzbee02031b12012-11-23 09:41:35 -0800531void Codegen::GenShowTarget(CompilationUnit* cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800532{
buzbeefa57c472012-11-21 12:06:18 -0800533 DCHECK_NE(cu->instruction_set, kX86) << "unimplemented GenShowTarget";
534 LIR* branch_over = OpCmpImmBranch(cu, kCondNe, TargetReg(kInvokeTgt), 0, NULL);
535 LoadWordDisp(cu, TargetReg(kSelf), ENTRYPOINT_OFFSET(pDebugMe), TargetReg(kInvokeTgt));
536 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
537 branch_over->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -0800538}
539
buzbee02031b12012-11-23 09:41:35 -0800540void Codegen::HandleSuspendLaunchPads(CompilationUnit *cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800541{
buzbeefa57c472012-11-21 12:06:18 -0800542 LIR** suspend_label = reinterpret_cast<LIR**>(cu->suspend_launchpads.elem_list);
543 int num_elems = cu->suspend_launchpads.num_used;
544 int helper_offset = ENTRYPOINT_OFFSET(pTestSuspendFromCode);
545 for (int i = 0; i < num_elems; i++) {
546 ResetRegPool(cu);
547 ResetDefTracking(cu);
548 LIR* lab = suspend_label[i];
549 LIR* resume_lab = reinterpret_cast<LIR*>(lab->operands[0]);
550 cu->current_dalvik_offset = lab->operands[1];
551 AppendLIR(cu, lab);
552 int r_tgt = CallHelperSetup(cu, helper_offset);
553 CallHelper(cu, r_tgt, helper_offset, true /* MarkSafepointPC */);
554 OpUnconditionalBranch(cu, resume_lab);
Bill Buzbeea114add2012-05-03 15:00:40 -0700555 }
buzbee31a4a6f2012-02-28 15:36:15 -0800556}
557
buzbee02031b12012-11-23 09:41:35 -0800558void Codegen::HandleIntrinsicLaunchPads(CompilationUnit *cu)
buzbeefc9e6fa2012-03-23 15:14:29 -0700559{
buzbeefa57c472012-11-21 12:06:18 -0800560 LIR** intrinsic_label = reinterpret_cast<LIR**>(cu->intrinsic_launchpads.elem_list);
561 int num_elems = cu->intrinsic_launchpads.num_used;
562 for (int i = 0; i < num_elems; i++) {
563 ResetRegPool(cu);
564 ResetDefTracking(cu);
565 LIR* lab = intrinsic_label[i];
buzbeecbd6d442012-11-17 14:11:25 -0800566 CallInfo* info = reinterpret_cast<CallInfo*>(lab->operands[0]);
buzbeefa57c472012-11-21 12:06:18 -0800567 cu->current_dalvik_offset = info->offset;
568 AppendLIR(cu, lab);
buzbee52a77fc2012-11-20 19:50:46 -0800569 // NOTE: GenInvoke handles MarkSafepointPC
buzbeefa57c472012-11-21 12:06:18 -0800570 GenInvoke(cu, info);
571 LIR* resume_lab = reinterpret_cast<LIR*>(lab->operands[2]);
572 if (resume_lab != NULL) {
573 OpUnconditionalBranch(cu, resume_lab);
buzbeefc9e6fa2012-03-23 15:14:29 -0700574 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700575 }
buzbeefc9e6fa2012-03-23 15:14:29 -0700576}
577
buzbee02031b12012-11-23 09:41:35 -0800578void Codegen::HandleThrowLaunchPads(CompilationUnit *cu)
buzbee31a4a6f2012-02-28 15:36:15 -0800579{
buzbeefa57c472012-11-21 12:06:18 -0800580 LIR** throw_label = reinterpret_cast<LIR**>(cu->throw_launchpads.elem_list);
581 int num_elems = cu->throw_launchpads.num_used;
582 for (int i = 0; i < num_elems; i++) {
583 ResetRegPool(cu);
584 ResetDefTracking(cu);
585 LIR* lab = throw_label[i];
586 cu->current_dalvik_offset = lab->operands[1];
587 AppendLIR(cu, lab);
588 int func_offset = 0;
Bill Buzbeea114add2012-05-03 15:00:40 -0700589 int v1 = lab->operands[2];
590 int v2 = lab->operands[3];
buzbeefa57c472012-11-21 12:06:18 -0800591 bool target_x86 = (cu->instruction_set == kX86);
Bill Buzbeea114add2012-05-03 15:00:40 -0700592 switch (lab->operands[0]) {
593 case kThrowNullPointer:
buzbeefa57c472012-11-21 12:06:18 -0800594 func_offset = ENTRYPOINT_OFFSET(pThrowNullPointerFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700595 break;
buzbee4ef3e452012-12-14 13:35:28 -0800596 case kThrowConstantArrayBounds: // v1 is length reg (for Arm/Mips), v2 constant index
597 // v1 holds the constant array index. Mips/Arm uses v2 for length, x86 reloads.
598 if (target_x86) {
599 OpRegMem(cu, kOpMov, TargetReg(kArg1), v1, mirror::Array::LengthOffset().Int32Value());
600 } else {
601 OpRegCopy(cu, TargetReg(kArg1), v1);
602 }
603 // Make sure the following LoadConstant doesn't mess with kArg1.
604 LockTemp(cu, TargetReg(kArg1));
605 LoadConstant(cu, TargetReg(kArg0), v2);
606 func_offset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
607 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700608 case kThrowArrayBounds:
buzbeef0504cd2012-11-13 16:31:10 -0800609 // Move v1 (array index) to kArg0 and v2 (array length) to kArg1
buzbee52a77fc2012-11-20 19:50:46 -0800610 if (v2 != TargetReg(kArg0)) {
buzbeefa57c472012-11-21 12:06:18 -0800611 OpRegCopy(cu, TargetReg(kArg0), v1);
612 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700613 // x86 leaves the array pointer in v2, so load the array length that the handler expects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800614 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700615 } else {
buzbeefa57c472012-11-21 12:06:18 -0800616 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700617 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700618 } else {
buzbee52a77fc2012-11-20 19:50:46 -0800619 if (v1 == TargetReg(kArg1)) {
buzbeef0504cd2012-11-13 16:31:10 -0800620 // Swap v1 and v2, using kArg2 as a temp
buzbeefa57c472012-11-21 12:06:18 -0800621 OpRegCopy(cu, TargetReg(kArg2), v1);
622 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700623 // x86 leaves the array pointer in v2; load the array length that the handler expects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800624 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700625 } else {
buzbeefa57c472012-11-21 12:06:18 -0800626 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700627 }
buzbeefa57c472012-11-21 12:06:18 -0800628 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2));
Bill Buzbeea114add2012-05-03 15:00:40 -0700629 } else {
buzbeefa57c472012-11-21 12:06:18 -0800630 if (target_x86) {
buzbeeb046e162012-10-30 15:48:42 -0700631 // x86 leaves the array pointer in v2; load the array length that the handler expects
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800632 OpRegMem(cu, kOpMov, TargetReg(kArg1), v2, mirror::Array::LengthOffset().Int32Value());
buzbeeb046e162012-10-30 15:48:42 -0700633 } else {
buzbeefa57c472012-11-21 12:06:18 -0800634 OpRegCopy(cu, TargetReg(kArg1), v2);
buzbeeb046e162012-10-30 15:48:42 -0700635 }
buzbeefa57c472012-11-21 12:06:18 -0800636 OpRegCopy(cu, TargetReg(kArg0), v1);
Bill Buzbeea114add2012-05-03 15:00:40 -0700637 }
buzbee31a4a6f2012-02-28 15:36:15 -0800638 }
buzbeefa57c472012-11-21 12:06:18 -0800639 func_offset = ENTRYPOINT_OFFSET(pThrowArrayBoundsFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700640 break;
641 case kThrowDivZero:
buzbeefa57c472012-11-21 12:06:18 -0800642 func_offset = ENTRYPOINT_OFFSET(pThrowDivZeroFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700643 break;
Bill Buzbeea114add2012-05-03 15:00:40 -0700644 case kThrowNoSuchMethod:
buzbeefa57c472012-11-21 12:06:18 -0800645 OpRegCopy(cu, TargetReg(kArg0), v1);
646 func_offset =
Bill Buzbeea114add2012-05-03 15:00:40 -0700647 ENTRYPOINT_OFFSET(pThrowNoSuchMethodFromCode);
648 break;
649 case kThrowStackOverflow:
buzbeefa57c472012-11-21 12:06:18 -0800650 func_offset = ENTRYPOINT_OFFSET(pThrowStackOverflowFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700651 // Restore stack alignment
buzbeefa57c472012-11-21 12:06:18 -0800652 if (target_x86) {
653 OpRegImm(cu, kOpAdd, TargetReg(kSp), cu->frame_size);
buzbeeb046e162012-10-30 15:48:42 -0700654 } else {
buzbeefa57c472012-11-21 12:06:18 -0800655 OpRegImm(cu, kOpAdd, TargetReg(kSp), (cu->num_core_spills + cu->num_fp_spills) * 4);
buzbeeb046e162012-10-30 15:48:42 -0700656 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700657 break;
658 default:
659 LOG(FATAL) << "Unexpected throw kind: " << lab->operands[0];
buzbee31a4a6f2012-02-28 15:36:15 -0800660 }
buzbeefa57c472012-11-21 12:06:18 -0800661 ClobberCalleeSave(cu);
662 int r_tgt = CallHelperSetup(cu, func_offset);
663 CallHelper(cu, r_tgt, func_offset, true /* MarkSafepointPC */);
Bill Buzbeea114add2012-05-03 15:00:40 -0700664 }
buzbee31a4a6f2012-02-28 15:36:15 -0800665}
666
buzbee02031b12012-11-23 09:41:35 -0800667void Codegen::GenIGet(CompilationUnit* cu, uint32_t field_idx, int opt_flags, OpSize size,
668 RegLocation rl_dest, RegLocation rl_obj, bool is_long_or_double,
669 bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800670{
buzbeefa57c472012-11-21 12:06:18 -0800671 int field_offset;
672 bool is_volatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800673
buzbeefa57c472012-11-21 12:06:18 -0800674 bool fast_path = FastInstance(cu, field_idx, field_offset, is_volatile, false);
buzbee31a4a6f2012-02-28 15:36:15 -0800675
buzbeefa57c472012-11-21 12:06:18 -0800676 if (fast_path && !SLOW_FIELD_PATH) {
677 RegLocation rl_result;
678 RegisterClass reg_class = oat_reg_class_by_size(size);
679 DCHECK_GE(field_offset, 0);
680 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
681 if (is_long_or_double) {
682 DCHECK(rl_dest.wide);
683 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
684 if (cu->instruction_set == kX86) {
685 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
686 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
687 LoadBaseDispWide(cu, rl_obj.low_reg, field_offset, rl_result.low_reg,
688 rl_result.high_reg, rl_obj.s_reg_low);
689 if (is_volatile) {
690 GenMemBarrier(cu, kLoadLoad);
buzbeeb046e162012-10-30 15:48:42 -0700691 }
692 } else {
buzbeefa57c472012-11-21 12:06:18 -0800693 int reg_ptr = AllocTemp(cu);
694 OpRegRegImm(cu, kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
695 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
buzbeee6285f92012-12-06 15:57:46 -0800696 LoadBaseDispWide(cu, reg_ptr, 0, rl_result.low_reg, rl_result.high_reg, INVALID_SREG);
buzbeefa57c472012-11-21 12:06:18 -0800697 if (is_volatile) {
698 GenMemBarrier(cu, kLoadLoad);
buzbeeb046e162012-10-30 15:48:42 -0700699 }
buzbeefa57c472012-11-21 12:06:18 -0800700 FreeTemp(cu, reg_ptr);
Bill Buzbeea114add2012-05-03 15:00:40 -0700701 }
buzbeefa57c472012-11-21 12:06:18 -0800702 StoreValueWide(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800703 } else {
buzbeefa57c472012-11-21 12:06:18 -0800704 rl_result = EvalLoc(cu, rl_dest, reg_class, true);
705 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
706 LoadBaseDisp(cu, rl_obj.low_reg, field_offset, rl_result.low_reg,
707 kWord, rl_obj.s_reg_low);
708 if (is_volatile) {
709 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700710 }
buzbeefa57c472012-11-21 12:06:18 -0800711 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800712 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700713 } else {
buzbeefa57c472012-11-21 12:06:18 -0800714 int getterOffset = is_long_or_double ? ENTRYPOINT_OFFSET(pGet64Instance) :
715 (is_object ? ENTRYPOINT_OFFSET(pGetObjInstance)
Bill Buzbeea114add2012-05-03 15:00:40 -0700716 : ENTRYPOINT_OFFSET(pGet32Instance));
buzbeefa57c472012-11-21 12:06:18 -0800717 CallRuntimeHelperImmRegLocation(cu, getterOffset, field_idx, rl_obj, true);
718 if (is_long_or_double) {
719 RegLocation rl_result = GetReturnWide(cu, rl_dest.fp);
720 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700721 } else {
buzbeefa57c472012-11-21 12:06:18 -0800722 RegLocation rl_result = GetReturn(cu, rl_dest.fp);
723 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700724 }
725 }
buzbee31a4a6f2012-02-28 15:36:15 -0800726}
727
buzbee02031b12012-11-23 09:41:35 -0800728void Codegen::GenIPut(CompilationUnit* cu, uint32_t field_idx, int opt_flags, OpSize size,
729 RegLocation rl_src, RegLocation rl_obj, bool is_long_or_double,
730 bool is_object)
buzbee31a4a6f2012-02-28 15:36:15 -0800731{
buzbeefa57c472012-11-21 12:06:18 -0800732 int field_offset;
733 bool is_volatile;
buzbee31a4a6f2012-02-28 15:36:15 -0800734
buzbeefa57c472012-11-21 12:06:18 -0800735 bool fast_path = FastInstance(cu, field_idx, field_offset, is_volatile,
Bill Buzbeea114add2012-05-03 15:00:40 -0700736 true);
buzbeefa57c472012-11-21 12:06:18 -0800737 if (fast_path && !SLOW_FIELD_PATH) {
738 RegisterClass reg_class = oat_reg_class_by_size(size);
739 DCHECK_GE(field_offset, 0);
740 rl_obj = LoadValue(cu, rl_obj, kCoreReg);
741 if (is_long_or_double) {
742 int reg_ptr;
743 rl_src = LoadValueWide(cu, rl_src, kAnyReg);
744 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
745 reg_ptr = AllocTemp(cu);
746 OpRegRegImm(cu, kOpAdd, reg_ptr, rl_obj.low_reg, field_offset);
747 if (is_volatile) {
748 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700749 }
buzbeefa57c472012-11-21 12:06:18 -0800750 StoreBaseDispWide(cu, reg_ptr, 0, rl_src.low_reg, rl_src.high_reg);
751 if (is_volatile) {
752 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700753 }
buzbeefa57c472012-11-21 12:06:18 -0800754 FreeTemp(cu, reg_ptr);
buzbee31a4a6f2012-02-28 15:36:15 -0800755 } else {
buzbeefa57c472012-11-21 12:06:18 -0800756 rl_src = LoadValue(cu, rl_src, reg_class);
757 GenNullCheck(cu, rl_obj.s_reg_low, rl_obj.low_reg, opt_flags);
758 if (is_volatile) {
759 GenMemBarrier(cu, kStoreStore);
Bill Buzbeea114add2012-05-03 15:00:40 -0700760 }
buzbeefa57c472012-11-21 12:06:18 -0800761 StoreBaseDisp(cu, rl_obj.low_reg, field_offset, rl_src.low_reg, kWord);
762 if (is_volatile) {
763 GenMemBarrier(cu, kLoadLoad);
Bill Buzbeea114add2012-05-03 15:00:40 -0700764 }
buzbee6a791b22013-02-07 05:35:08 -0800765 if (is_object && !IsConstantNullRef(cu, rl_src)) {
buzbeefa57c472012-11-21 12:06:18 -0800766 MarkGCCard(cu, rl_src.low_reg, rl_obj.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700767 }
buzbee31a4a6f2012-02-28 15:36:15 -0800768 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700769 } else {
buzbeefa57c472012-11-21 12:06:18 -0800770 int setter_offset = is_long_or_double ? ENTRYPOINT_OFFSET(pSet64Instance) :
771 (is_object ? ENTRYPOINT_OFFSET(pSetObjInstance)
Bill Buzbeea114add2012-05-03 15:00:40 -0700772 : ENTRYPOINT_OFFSET(pSet32Instance));
buzbeefa57c472012-11-21 12:06:18 -0800773 CallRuntimeHelperImmRegLocationRegLocation(cu, setter_offset, field_idx, rl_obj, rl_src, true);
Bill Buzbeea114add2012-05-03 15:00:40 -0700774 }
buzbee31a4a6f2012-02-28 15:36:15 -0800775}
776
buzbee02031b12012-11-23 09:41:35 -0800777void Codegen::GenConstClass(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800778{
buzbeefa57c472012-11-21 12:06:18 -0800779 RegLocation rl_method = LoadCurrMethod(cu);
780 int res_reg = AllocTemp(cu);
781 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Ian Rogers1212a022013-03-04 10:48:41 -0800782 if (!cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx,
buzbeefa57c472012-11-21 12:06:18 -0800783 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700784 type_idx)) {
785 // Call out to helper which resolves type and verifies access.
buzbeef0504cd2012-11-13 16:31:10 -0800786 // Resolved type returned in kRet0.
buzbeefa57c472012-11-21 12:06:18 -0800787 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
788 type_idx, rl_method.low_reg, true);
789 RegLocation rl_result = GetReturn(cu, false);
790 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700791 } else {
792 // We're don't need access checks, load type from dex cache
793 int32_t dex_cache_offset =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800794 mirror::AbstractMethod::DexCacheResolvedTypesOffset().Int32Value();
buzbeefa57c472012-11-21 12:06:18 -0800795 LoadWordDisp(cu, rl_method.low_reg, dex_cache_offset, res_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700796 int32_t offset_of_type =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800797 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
Bill Buzbeea114add2012-05-03 15:00:40 -0700798 * type_idx);
buzbeefa57c472012-11-21 12:06:18 -0800799 LoadWordDisp(cu, res_reg, offset_of_type, rl_result.low_reg);
Ian Rogers1212a022013-03-04 10:48:41 -0800800 if (!cu->compiler_driver->CanAssumeTypeIsPresentInDexCache(*cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700801 type_idx) || SLOW_TYPE_PATH) {
802 // Slow path, at runtime test if type is null and if so initialize
buzbeefa57c472012-11-21 12:06:18 -0800803 FlushAllRegs(cu);
804 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, rl_result.low_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700805 // Resolved, store and hop over following code
buzbeefa57c472012-11-21 12:06:18 -0800806 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700807 /*
808 * Because we have stores of the target value on two paths,
809 * clobber temp tracking for the destination using the ssa name
810 */
buzbeefa57c472012-11-21 12:06:18 -0800811 ClobberSReg(cu, rl_dest.s_reg_low);
812 LIR* branch2 = OpUnconditionalBranch(cu,0);
Bill Buzbeea114add2012-05-03 15:00:40 -0700813 // TUNING: move slow path to end & remove unconditional branch
buzbeefa57c472012-11-21 12:06:18 -0800814 LIR* target1 = NewLIR0(cu, kPseudoTargetLabel);
buzbeef0504cd2012-11-13 16:31:10 -0800815 // Call out to helper, which will return resolved type in kArg0
buzbeefa57c472012-11-21 12:06:18 -0800816 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx,
817 rl_method.low_reg, true);
818 RegLocation rl_result = GetReturn(cu, false);
819 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700820 /*
821 * Because we have stores of the target value on two paths,
822 * clobber temp tracking for the destination using the ssa name
823 */
buzbeefa57c472012-11-21 12:06:18 -0800824 ClobberSReg(cu, rl_dest.s_reg_low);
Bill Buzbeea114add2012-05-03 15:00:40 -0700825 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -0800826 LIR* target2 = NewLIR0(cu, kPseudoTargetLabel);
buzbeecbd6d442012-11-17 14:11:25 -0800827 branch1->target = target1;
828 branch2->target = target2;
buzbee31a4a6f2012-02-28 15:36:15 -0800829 } else {
Bill Buzbeea114add2012-05-03 15:00:40 -0700830 // Fast path, we're done - just store result
buzbeefa57c472012-11-21 12:06:18 -0800831 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800832 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700833 }
buzbee31a4a6f2012-02-28 15:36:15 -0800834}
Ian Rogersab2b55d2012-03-18 00:06:11 -0700835
buzbee02031b12012-11-23 09:41:35 -0800836void Codegen::GenConstString(CompilationUnit* cu, uint32_t string_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800837{
Bill Buzbeea114add2012-05-03 15:00:40 -0700838 /* NOTE: Most strings should be available at compile time */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800839 int32_t offset_of_string = mirror::Array::DataOffset(sizeof(mirror::String*)).Int32Value() +
840 (sizeof(mirror::String*) * string_idx);
Ian Rogers1212a022013-03-04 10:48:41 -0800841 if (!cu->compiler_driver->CanAssumeStringIsPresentInDexCache(
buzbeefa57c472012-11-21 12:06:18 -0800842 *cu->dex_file, string_idx) || SLOW_STRING_PATH) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700843 // slow path, resolve string if not in dex cache
buzbeefa57c472012-11-21 12:06:18 -0800844 FlushAllRegs(cu);
845 LockCallTemps(cu); // Using explicit registers
846 LoadCurrMethodDirect(cu, TargetReg(kArg2));
847 LoadWordDisp(cu, TargetReg(kArg2),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800848 mirror::AbstractMethod::DexCacheStringsOffset().Int32Value(), TargetReg(kArg0));
buzbeef0504cd2012-11-13 16:31:10 -0800849 // Might call out to helper, which will return resolved string in kRet0
buzbeefa57c472012-11-21 12:06:18 -0800850 int r_tgt = CallHelperSetup(cu, ENTRYPOINT_OFFSET(pResolveStringFromCode));
851 LoadWordDisp(cu, TargetReg(kArg0), offset_of_string, TargetReg(kRet0));
852 LoadConstant(cu, TargetReg(kArg1), string_idx);
853 if (cu->instruction_set == kThumb2) {
854 OpRegImm(cu, kOpCmp, TargetReg(kRet0), 0); // Is resolved?
855 GenBarrier(cu);
buzbeeb046e162012-10-30 15:48:42 -0700856 // For testing, always force through helper
857 if (!EXERCISE_SLOWEST_STRING_PATH) {
buzbee02031b12012-11-23 09:41:35 -0800858 OpIT(cu, kCondEq, "T");
buzbeeb046e162012-10-30 15:48:42 -0700859 }
buzbeefa57c472012-11-21 12:06:18 -0800860 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .eq
861 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt); // .eq, helper(Method*, string_idx)
862 MarkSafepointPC(cu, call_inst);
863 FreeTemp(cu, r_tgt);
864 } else if (cu->instruction_set == kMips) {
865 LIR* branch = OpCmpImmBranch(cu, kCondNe, TargetReg(kRet0), 0, NULL);
866 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .eq
867 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt);
868 MarkSafepointPC(cu, call_inst);
869 FreeTemp(cu, r_tgt);
870 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
buzbeeb046e162012-10-30 15:48:42 -0700871 branch->target = target;
872 } else {
buzbeefa57c472012-11-21 12:06:18 -0800873 DCHECK_EQ(cu->instruction_set, kX86);
874 CallRuntimeHelperRegReg(cu, ENTRYPOINT_OFFSET(pResolveStringFromCode), TargetReg(kArg2), TargetReg(kArg1), true);
buzbee31a4a6f2012-02-28 15:36:15 -0800875 }
buzbeefa57c472012-11-21 12:06:18 -0800876 GenBarrier(cu);
877 StoreValue(cu, rl_dest, GetReturn(cu, false));
Bill Buzbeea114add2012-05-03 15:00:40 -0700878 } else {
buzbeefa57c472012-11-21 12:06:18 -0800879 RegLocation rl_method = LoadCurrMethod(cu);
880 int res_reg = AllocTemp(cu);
881 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
882 LoadWordDisp(cu, rl_method.low_reg,
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800883 mirror::AbstractMethod::DexCacheStringsOffset().Int32Value(), res_reg);
buzbeefa57c472012-11-21 12:06:18 -0800884 LoadWordDisp(cu, res_reg, offset_of_string, rl_result.low_reg);
885 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700886 }
buzbee31a4a6f2012-02-28 15:36:15 -0800887}
888
889/*
890 * Let helper function take care of everything. Will
891 * call Class::NewInstanceFromCode(type_idx, method);
892 */
buzbee02031b12012-11-23 09:41:35 -0800893void Codegen::GenNewInstance(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest)
buzbee31a4a6f2012-02-28 15:36:15 -0800894{
buzbeefa57c472012-11-21 12:06:18 -0800895 FlushAllRegs(cu); /* Everything to home location */
Bill Buzbeea114add2012-05-03 15:00:40 -0700896 // alloc will always check for resolution, do we also need to verify
897 // access because the verifier was unable to?
buzbeefa57c472012-11-21 12:06:18 -0800898 int func_offset;
Ian Rogers1212a022013-03-04 10:48:41 -0800899 if (cu->compiler_driver->CanAccessInstantiableTypeWithoutChecks(
buzbeefa57c472012-11-21 12:06:18 -0800900 cu->method_idx, *cu->dex_file, type_idx)) {
901 func_offset = ENTRYPOINT_OFFSET(pAllocObjectFromCode);
Bill Buzbeea114add2012-05-03 15:00:40 -0700902 } else {
buzbeefa57c472012-11-21 12:06:18 -0800903 func_offset = ENTRYPOINT_OFFSET(pAllocObjectFromCodeWithAccessCheck);
Bill Buzbeea114add2012-05-03 15:00:40 -0700904 }
buzbeefa57c472012-11-21 12:06:18 -0800905 CallRuntimeHelperImmMethod(cu, func_offset, type_idx, true);
906 RegLocation rl_result = GetReturn(cu, false);
907 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -0800908}
909
buzbee02031b12012-11-23 09:41:35 -0800910void Codegen::GenThrow(CompilationUnit* cu, RegLocation rl_src)
Ian Rogersab2b55d2012-03-18 00:06:11 -0700911{
buzbeefa57c472012-11-21 12:06:18 -0800912 FlushAllRegs(cu);
913 CallRuntimeHelperRegLocation(cu, ENTRYPOINT_OFFSET(pDeliverException), rl_src, true);
Ian Rogersab2b55d2012-03-18 00:06:11 -0700914}
915
buzbee02031b12012-11-23 09:41:35 -0800916void Codegen::GenInstanceof(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_dest,
917 RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -0800918{
buzbeefa57c472012-11-21 12:06:18 -0800919 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700920 // May generate a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -0800921 LockCallTemps(cu);
922 LoadCurrMethodDirect(cu, TargetReg(kArg1)); // kArg1 <= current Method*
923 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Ian Rogers1212a022013-03-04 10:48:41 -0800924 if (!cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx,
buzbeefa57c472012-11-21 12:06:18 -0800925 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -0700926 type_idx)) {
927 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbeef0504cd2012-11-13 16:31:10 -0800928 // returns Class* in kArg0
buzbeefa57c472012-11-21 12:06:18 -0800929 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbee8320f382012-09-11 16:29:42 -0700930 type_idx, true);
buzbeefa57c472012-11-21 12:06:18 -0800931 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
932 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -0700933 } else {
buzbeefa57c472012-11-21 12:06:18 -0800934 // Load dex cache entry into class_reg (kArg2)
935 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
936 LoadWordDisp(cu, TargetReg(kArg1),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800937 mirror::AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -0700938 int32_t offset_of_type =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800939 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() + (sizeof(mirror::Class*)
Bill Buzbeea114add2012-05-03 15:00:40 -0700940 * type_idx);
buzbeefa57c472012-11-21 12:06:18 -0800941 LoadWordDisp(cu, class_reg, offset_of_type, class_reg);
Ian Rogers1212a022013-03-04 10:48:41 -0800942 if (!cu->compiler_driver->CanAssumeTypeIsPresentInDexCache(
buzbeefa57c472012-11-21 12:06:18 -0800943 *cu->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -0700944 // Need to test presence of type in dex cache at runtime
buzbeefa57c472012-11-21 12:06:18 -0800945 LIR* hop_branch = OpCmpImmBranch(cu, kCondNe, class_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700946 // Not resolved
buzbeef0504cd2012-11-13 16:31:10 -0800947 // Call out to helper, which will return resolved type in kRet0
buzbeefa57c472012-11-21 12:06:18 -0800948 CallRuntimeHelperImm(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, true);
949 OpRegCopy(cu, TargetReg(kArg2), TargetReg(kRet0)); // Align usage with fast path
950 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); /* reload Ref */
Bill Buzbeea114add2012-05-03 15:00:40 -0700951 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -0800952 LIR* hop_target = NewLIR0(cu, kPseudoTargetLabel);
953 hop_branch->target = hop_target;
buzbee31a4a6f2012-02-28 15:36:15 -0800954 }
Bill Buzbeea114add2012-05-03 15:00:40 -0700955 }
buzbeef0504cd2012-11-13 16:31:10 -0800956 /* kArg0 is ref, kArg2 is class. If ref==null, use directly as bool result */
buzbeefa57c472012-11-21 12:06:18 -0800957 RegLocation rl_result = GetReturn(cu, false);
958 if (cu->instruction_set == kMips) {
959 LoadConstant(cu, rl_result.low_reg, 0); // store false result for if branch is taken
buzbeeb046e162012-10-30 15:48:42 -0700960 }
buzbeefa57c472012-11-21 12:06:18 -0800961 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, TargetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -0700962 /* load object->klass_ */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -0800963 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
964 LoadWordDisp(cu, TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
buzbeef0504cd2012-11-13 16:31:10 -0800965 /* kArg0 is ref, kArg1 is ref->klass_, kArg2 is class */
buzbeefa57c472012-11-21 12:06:18 -0800966 LIR* call_inst;
buzbeeb046e162012-10-30 15:48:42 -0700967 LIR* branchover = NULL;
buzbeefa57c472012-11-21 12:06:18 -0800968 if (cu->instruction_set == kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -0700969 /* Uses conditional nullification */
buzbeefa57c472012-11-21 12:06:18 -0800970 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
971 OpRegReg(cu, kOpCmp, TargetReg(kArg1), TargetReg(kArg2)); // Same?
buzbee02031b12012-11-23 09:41:35 -0800972 OpIT(cu, kCondEq, "EE"); // if-convert the test
buzbeefa57c472012-11-21 12:06:18 -0800973 LoadConstant(cu, TargetReg(kArg0), 1); // .eq case - load true
974 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
975 call_inst = OpReg(cu, kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
976 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700977 } else {
978 /* Uses branchovers */
buzbeefa57c472012-11-21 12:06:18 -0800979 LoadConstant(cu, rl_result.low_reg, 1); // assume true
980 branchover = OpCmpBranch(cu, kCondEq, TargetReg(kArg1), TargetReg(kArg2), NULL);
981 if (cu->instruction_set != kX86) {
982 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
983 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2)); // .ne case - arg0 <= class
984 call_inst = OpReg(cu, kOpBlx, r_tgt); // .ne case: helper(class, ref->class)
985 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -0700986 } else {
buzbeefa57c472012-11-21 12:06:18 -0800987 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg2));
988 call_inst = OpThreadMem(cu, kOpBlx, ENTRYPOINT_OFFSET(pInstanceofNonTrivialFromCode));
buzbeeb046e162012-10-30 15:48:42 -0700989 }
990 }
buzbeefa57c472012-11-21 12:06:18 -0800991 MarkSafepointPC(cu, call_inst);
992 ClobberCalleeSave(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -0700993 /* branch targets here */
buzbeefa57c472012-11-21 12:06:18 -0800994 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
995 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -0700996 branch1->target = target;
buzbeefa57c472012-11-21 12:06:18 -0800997 if (cu->instruction_set != kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -0700998 branchover->target = target;
999 }
buzbee31a4a6f2012-02-28 15:36:15 -08001000}
1001
buzbee02031b12012-11-23 09:41:35 -08001002void Codegen::GenCheckCast(CompilationUnit* cu, uint32_t type_idx, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -08001003{
buzbeefa57c472012-11-21 12:06:18 -08001004 FlushAllRegs(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001005 // May generate a call - use explicit registers
buzbeefa57c472012-11-21 12:06:18 -08001006 LockCallTemps(cu);
1007 LoadCurrMethodDirect(cu, TargetReg(kArg1)); // kArg1 <= current Method*
1008 int class_reg = TargetReg(kArg2); // kArg2 will hold the Class*
Ian Rogers1212a022013-03-04 10:48:41 -08001009 if (!cu->compiler_driver->CanAccessTypeWithoutChecks(cu->method_idx,
buzbeefa57c472012-11-21 12:06:18 -08001010 *cu->dex_file,
Bill Buzbeea114add2012-05-03 15:00:40 -07001011 type_idx)) {
1012 // Check we have access to type_idx and if not throw IllegalAccessError,
buzbeef0504cd2012-11-13 16:31:10 -08001013 // returns Class* in kRet0
Bill Buzbeea114add2012-05-03 15:00:40 -07001014 // InitializeTypeAndVerifyAccess(idx, method)
buzbeefa57c472012-11-21 12:06:18 -08001015 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeAndVerifyAccessFromCode),
buzbee52a77fc2012-11-20 19:50:46 -08001016 type_idx, TargetReg(kArg1), true);
buzbeefa57c472012-11-21 12:06:18 -08001017 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001018 } else {
buzbeefa57c472012-11-21 12:06:18 -08001019 // Load dex cache entry into class_reg (kArg2)
1020 LoadWordDisp(cu, TargetReg(kArg1),
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001021 mirror::AbstractMethod::DexCacheResolvedTypesOffset().Int32Value(), class_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001022 int32_t offset_of_type =
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001023 mirror::Array::DataOffset(sizeof(mirror::Class*)).Int32Value() +
1024 (sizeof(mirror::Class*) * type_idx);
buzbeefa57c472012-11-21 12:06:18 -08001025 LoadWordDisp(cu, class_reg, offset_of_type, class_reg);
Ian Rogers1212a022013-03-04 10:48:41 -08001026 if (!cu->compiler_driver->CanAssumeTypeIsPresentInDexCache(
buzbeefa57c472012-11-21 12:06:18 -08001027 *cu->dex_file, type_idx)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001028 // Need to test presence of type in dex cache at runtime
buzbeefa57c472012-11-21 12:06:18 -08001029 LIR* hop_branch = OpCmpImmBranch(cu, kCondNe, class_reg, 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001030 // Not resolved
buzbeef0504cd2012-11-13 16:31:10 -08001031 // Call out to helper, which will return resolved type in kArg0
Bill Buzbeea114add2012-05-03 15:00:40 -07001032 // InitializeTypeFromCode(idx, method)
buzbeefa57c472012-11-21 12:06:18 -08001033 CallRuntimeHelperImmReg(cu, ENTRYPOINT_OFFSET(pInitializeTypeFromCode), type_idx, TargetReg(kArg1),
buzbee8320f382012-09-11 16:29:42 -07001034 true);
buzbeefa57c472012-11-21 12:06:18 -08001035 OpRegCopy(cu, class_reg, TargetReg(kRet0)); // Align usage with fast path
Bill Buzbeea114add2012-05-03 15:00:40 -07001036 // Rejoin code paths
buzbeefa57c472012-11-21 12:06:18 -08001037 LIR* hop_target = NewLIR0(cu, kPseudoTargetLabel);
1038 hop_branch->target = hop_target;
buzbee31a4a6f2012-02-28 15:36:15 -08001039 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001040 }
buzbeefa57c472012-11-21 12:06:18 -08001041 // At this point, class_reg (kArg2) has class
1042 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0)); // kArg0 <= ref
Bill Buzbeea114add2012-05-03 15:00:40 -07001043 /* Null is OK - continue */
buzbeefa57c472012-11-21 12:06:18 -08001044 LIR* branch1 = OpCmpImmBranch(cu, kCondEq, TargetReg(kArg0), 0, NULL);
Bill Buzbeea114add2012-05-03 15:00:40 -07001045 /* load object->klass_ */
Ian Rogers2dd0e2c2013-01-24 12:42:14 -08001046 DCHECK_EQ(mirror::Object::ClassOffset().Int32Value(), 0);
1047 LoadWordDisp(cu, TargetReg(kArg0), mirror::Object::ClassOffset().Int32Value(), TargetReg(kArg1));
buzbeef0504cd2012-11-13 16:31:10 -08001048 /* kArg1 now contains object->klass_ */
buzbeeb046e162012-10-30 15:48:42 -07001049 LIR* branch2;
buzbeefa57c472012-11-21 12:06:18 -08001050 if (cu->instruction_set == kThumb2) {
1051 int r_tgt = LoadHelper(cu, ENTRYPOINT_OFFSET(pCheckCastFromCode));
1052 OpRegReg(cu, kOpCmp, TargetReg(kArg1), class_reg);
1053 branch2 = OpCondBranch(cu, kCondEq, NULL); /* If eq, trivial yes */
1054 OpRegCopy(cu, TargetReg(kArg0), TargetReg(kArg1));
1055 OpRegCopy(cu, TargetReg(kArg1), TargetReg(kArg2));
1056 ClobberCalleeSave(cu);
1057 LIR* call_inst = OpReg(cu, kOpBlx, r_tgt);
1058 MarkSafepointPC(cu, call_inst);
1059 FreeTemp(cu, r_tgt);
buzbeeb046e162012-10-30 15:48:42 -07001060 } else {
buzbeefa57c472012-11-21 12:06:18 -08001061 branch2 = OpCmpBranch(cu, kCondEq, TargetReg(kArg1), class_reg, NULL);
1062 CallRuntimeHelperRegReg(cu, ENTRYPOINT_OFFSET(pCheckCastFromCode), TargetReg(kArg1), TargetReg(kArg2), true);
buzbeeb046e162012-10-30 15:48:42 -07001063 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001064 /* branch target here */
buzbeefa57c472012-11-21 12:06:18 -08001065 LIR* target = NewLIR0(cu, kPseudoTargetLabel);
Bill Buzbeea114add2012-05-03 15:00:40 -07001066 branch1->target = target;
1067 branch2->target = target;
buzbee31a4a6f2012-02-28 15:36:15 -08001068}
1069
buzbee02031b12012-11-23 09:41:35 -08001070void Codegen::GenLong3Addr(CompilationUnit* cu, OpKind first_op, OpKind second_op,
1071 RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001072{
buzbeefa57c472012-11-21 12:06:18 -08001073 RegLocation rl_result;
1074 if (cu->instruction_set == kThumb2) {
buzbeeb046e162012-10-30 15:48:42 -07001075 /*
1076 * NOTE: This is the one place in the code in which we might have
1077 * as many as six live temporary registers. There are 5 in the normal
1078 * set for Arm. Until we have spill capabilities, temporarily add
1079 * lr to the temp set. It is safe to do this locally, but note that
1080 * lr is used explicitly elsewhere in the code generator and cannot
1081 * normally be used as a general temp register.
1082 */
buzbeefa57c472012-11-21 12:06:18 -08001083 MarkTemp(cu, TargetReg(kLr)); // Add lr to the temp pool
1084 FreeTemp(cu, TargetReg(kLr)); // and make it available
buzbeeb046e162012-10-30 15:48:42 -07001085 }
buzbeefa57c472012-11-21 12:06:18 -08001086 rl_src1 = LoadValueWide(cu, rl_src1, kCoreReg);
1087 rl_src2 = LoadValueWide(cu, rl_src2, kCoreReg);
1088 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001089 // The longs may overlap - use intermediate temp if so
buzbeefa57c472012-11-21 12:06:18 -08001090 if ((rl_result.low_reg == rl_src1.high_reg) || (rl_result.low_reg == rl_src2.high_reg)){
1091 int t_reg = AllocTemp(cu);
1092 OpRegRegReg(cu, first_op, t_reg, rl_src1.low_reg, rl_src2.low_reg);
1093 OpRegRegReg(cu, second_op, rl_result.high_reg, rl_src1.high_reg, rl_src2.high_reg);
1094 OpRegCopy(cu, rl_result.low_reg, t_reg);
1095 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001096 } else {
buzbeefa57c472012-11-21 12:06:18 -08001097 OpRegRegReg(cu, first_op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
1098 OpRegRegReg(cu, second_op, rl_result.high_reg, rl_src1.high_reg,
1099 rl_src2.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001100 }
1101 /*
buzbeefa57c472012-11-21 12:06:18 -08001102 * NOTE: If rl_dest refers to a frame variable in a large frame, the
buzbee52a77fc2012-11-20 19:50:46 -08001103 * following StoreValueWide might need to allocate a temp register.
Bill Buzbeea114add2012-05-03 15:00:40 -07001104 * To further work around the lack of a spill capability, explicitly
buzbeefa57c472012-11-21 12:06:18 -08001105 * free any temps from rl_src1 & rl_src2 that aren't still live in rl_result.
Bill Buzbeea114add2012-05-03 15:00:40 -07001106 * Remove when spill is functional.
1107 */
buzbeefa57c472012-11-21 12:06:18 -08001108 FreeRegLocTemps(cu, rl_result, rl_src1);
1109 FreeRegLocTemps(cu, rl_result, rl_src2);
1110 StoreValueWide(cu, rl_dest, rl_result);
1111 if (cu->instruction_set == kThumb2) {
1112 Clobber(cu, TargetReg(kLr));
1113 UnmarkTemp(cu, TargetReg(kLr)); // Remove lr from the temp pool
buzbeeb046e162012-10-30 15:48:42 -07001114 }
buzbee31a4a6f2012-02-28 15:36:15 -08001115}
1116
1117
buzbeea5954be2013-02-07 10:41:40 -08001118void Codegen::GenShiftOpLong(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
buzbee02031b12012-11-23 09:41:35 -08001119 RegLocation rl_src1, RegLocation rl_shift)
buzbee31a4a6f2012-02-28 15:36:15 -08001120{
buzbeea5954be2013-02-07 10:41:40 -08001121 int func_offset = -1; // Make gcc happy
buzbee31a4a6f2012-02-28 15:36:15 -08001122
buzbee408ad162012-06-06 16:45:18 -07001123 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001124 case Instruction::SHL_LONG:
1125 case Instruction::SHL_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001126 func_offset = ENTRYPOINT_OFFSET(pShlLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001127 break;
1128 case Instruction::SHR_LONG:
1129 case Instruction::SHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001130 func_offset = ENTRYPOINT_OFFSET(pShrLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001131 break;
1132 case Instruction::USHR_LONG:
1133 case Instruction::USHR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001134 func_offset = ENTRYPOINT_OFFSET(pUshrLong);
Bill Buzbeea114add2012-05-03 15:00:40 -07001135 break;
1136 default:
1137 LOG(FATAL) << "Unexpected case";
Bill Buzbeea114add2012-05-03 15:00:40 -07001138 }
buzbeefa57c472012-11-21 12:06:18 -08001139 FlushAllRegs(cu); /* Send everything to home location */
1140 CallRuntimeHelperRegLocationRegLocation(cu, func_offset, rl_src1, rl_shift, false);
1141 RegLocation rl_result = GetReturnWide(cu, false);
1142 StoreValueWide(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -08001143}
1144
1145
buzbeea5954be2013-02-07 10:41:40 -08001146void Codegen::GenArithOpInt(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
buzbee02031b12012-11-23 09:41:35 -08001147 RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001148{
Bill Buzbeea114add2012-05-03 15:00:40 -07001149 OpKind op = kOpBkpt;
buzbeefa57c472012-11-21 12:06:18 -08001150 bool is_div_rem = false;
1151 bool check_zero = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001152 bool unary = false;
buzbeefa57c472012-11-21 12:06:18 -08001153 RegLocation rl_result;
1154 bool shift_op = false;
buzbee408ad162012-06-06 16:45:18 -07001155 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001156 case Instruction::NEG_INT:
1157 op = kOpNeg;
1158 unary = true;
1159 break;
1160 case Instruction::NOT_INT:
1161 op = kOpMvn;
1162 unary = true;
1163 break;
1164 case Instruction::ADD_INT:
1165 case Instruction::ADD_INT_2ADDR:
1166 op = kOpAdd;
1167 break;
1168 case Instruction::SUB_INT:
1169 case Instruction::SUB_INT_2ADDR:
1170 op = kOpSub;
1171 break;
1172 case Instruction::MUL_INT:
1173 case Instruction::MUL_INT_2ADDR:
1174 op = kOpMul;
1175 break;
1176 case Instruction::DIV_INT:
1177 case Instruction::DIV_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001178 check_zero = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001179 op = kOpDiv;
buzbeefa57c472012-11-21 12:06:18 -08001180 is_div_rem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001181 break;
buzbeef0504cd2012-11-13 16:31:10 -08001182 /* NOTE: returns in kArg1 */
Bill Buzbeea114add2012-05-03 15:00:40 -07001183 case Instruction::REM_INT:
1184 case Instruction::REM_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001185 check_zero = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001186 op = kOpRem;
buzbeefa57c472012-11-21 12:06:18 -08001187 is_div_rem = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001188 break;
1189 case Instruction::AND_INT:
1190 case Instruction::AND_INT_2ADDR:
1191 op = kOpAnd;
1192 break;
1193 case Instruction::OR_INT:
1194 case Instruction::OR_INT_2ADDR:
1195 op = kOpOr;
1196 break;
1197 case Instruction::XOR_INT:
1198 case Instruction::XOR_INT_2ADDR:
1199 op = kOpXor;
1200 break;
1201 case Instruction::SHL_INT:
1202 case Instruction::SHL_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001203 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001204 op = kOpLsl;
1205 break;
1206 case Instruction::SHR_INT:
1207 case Instruction::SHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001208 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001209 op = kOpAsr;
1210 break;
1211 case Instruction::USHR_INT:
1212 case Instruction::USHR_INT_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001213 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001214 op = kOpLsr;
1215 break;
1216 default:
buzbeecbd6d442012-11-17 14:11:25 -08001217 LOG(FATAL) << "Invalid word arith op: " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001218 }
buzbeefa57c472012-11-21 12:06:18 -08001219 if (!is_div_rem) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001220 if (unary) {
buzbeefa57c472012-11-21 12:06:18 -08001221 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1222 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1223 OpRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg);
buzbee31a4a6f2012-02-28 15:36:15 -08001224 } else {
buzbeefa57c472012-11-21 12:06:18 -08001225 if (shift_op) {
1226 int t_reg = INVALID_REG;
1227 if (cu->instruction_set == kX86) {
buzbeeb046e162012-10-30 15:48:42 -07001228 // X86 doesn't require masking and must use ECX
buzbeefa57c472012-11-21 12:06:18 -08001229 t_reg = TargetReg(kCount); // rCX
1230 LoadValueDirectFixed(cu, rl_src2, t_reg);
buzbeeb046e162012-10-30 15:48:42 -07001231 } else {
buzbeefa57c472012-11-21 12:06:18 -08001232 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1233 t_reg = AllocTemp(cu);
1234 OpRegRegImm(cu, kOpAnd, t_reg, rl_src2.low_reg, 31);
buzbeeb046e162012-10-30 15:48:42 -07001235 }
buzbeefa57c472012-11-21 12:06:18 -08001236 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1237 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1238 OpRegRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg, t_reg);
1239 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001240 } else {
buzbeefa57c472012-11-21 12:06:18 -08001241 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1242 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1243 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1244 OpRegRegReg(cu, op, rl_result.low_reg, rl_src1.low_reg, rl_src2.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001245 }
buzbee31a4a6f2012-02-28 15:36:15 -08001246 }
buzbeefa57c472012-11-21 12:06:18 -08001247 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001248 } else {
buzbeefa57c472012-11-21 12:06:18 -08001249 if (cu->instruction_set == kMips) {
1250 rl_src1 = LoadValue(cu, rl_src1, kCoreReg);
1251 rl_src2 = LoadValue(cu, rl_src2, kCoreReg);
1252 if (check_zero) {
1253 GenImmedCheck(cu, kCondEq, rl_src2.low_reg, 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001254 }
buzbeefa57c472012-11-21 12:06:18 -08001255 rl_result = GenDivRem(cu, rl_dest, rl_src1.low_reg, rl_src2.low_reg, op == kOpDiv);
jeffhao4f8f04a2012-10-02 18:10:35 -07001256 } else {
buzbeefa57c472012-11-21 12:06:18 -08001257 int func_offset = ENTRYPOINT_OFFSET(pIdivmod);
1258 FlushAllRegs(cu); /* Send everything to home location */
1259 LoadValueDirectFixed(cu, rl_src2, TargetReg(kArg1));
1260 int r_tgt = CallHelperSetup(cu, func_offset);
1261 LoadValueDirectFixed(cu, rl_src1, TargetReg(kArg0));
1262 if (check_zero) {
1263 GenImmedCheck(cu, kCondEq, TargetReg(kArg1), 0, kThrowDivZero);
buzbeeb046e162012-10-30 15:48:42 -07001264 }
1265 // NOTE: callout here is not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001266 CallHelper(cu, r_tgt, func_offset, false /* not a safepoint */ );
buzbeeb046e162012-10-30 15:48:42 -07001267 if (op == kOpDiv)
buzbeefa57c472012-11-21 12:06:18 -08001268 rl_result = GetReturn(cu, false);
buzbeeb046e162012-10-30 15:48:42 -07001269 else
buzbeefa57c472012-11-21 12:06:18 -08001270 rl_result = GetReturnAlt(cu);
jeffhao4f8f04a2012-10-02 18:10:35 -07001271 }
buzbeefa57c472012-11-21 12:06:18 -08001272 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001273 }
buzbee31a4a6f2012-02-28 15:36:15 -08001274}
1275
1276/*
1277 * The following are the first-level codegen routines that analyze the format
1278 * of each bytecode then either dispatch special purpose codegen routines
1279 * or produce corresponding Thumb instructions directly.
1280 */
1281
buzbeeaad94382012-11-21 07:40:50 -08001282static bool IsPowerOfTwo(int x)
buzbee31a4a6f2012-02-28 15:36:15 -08001283{
Bill Buzbeea114add2012-05-03 15:00:40 -07001284 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001285}
1286
1287// Returns true if no more than two bits are set in 'x'.
buzbeeaad94382012-11-21 07:40:50 -08001288static bool IsPopCountLE2(unsigned int x)
buzbee31a4a6f2012-02-28 15:36:15 -08001289{
Bill Buzbeea114add2012-05-03 15:00:40 -07001290 x &= x - 1;
1291 return (x & (x - 1)) == 0;
buzbee31a4a6f2012-02-28 15:36:15 -08001292}
1293
1294// Returns the index of the lowest set bit in 'x'.
buzbeeaad94382012-11-21 07:40:50 -08001295static int LowestSetBit(unsigned int x) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001296 int bit_posn = 0;
1297 while ((x & 0xf) == 0) {
1298 bit_posn += 4;
1299 x >>= 4;
1300 }
1301 while ((x & 1) == 0) {
1302 bit_posn++;
1303 x >>= 1;
1304 }
1305 return bit_posn;
buzbee31a4a6f2012-02-28 15:36:15 -08001306}
1307
buzbeefa57c472012-11-21 12:06:18 -08001308// Returns true if it added instructions to 'cu' to divide 'rl_src' by 'lit'
1309// and store the result in 'rl_dest'.
1310static bool HandleEasyDivide(CompilationUnit* cu, Instruction::Code dalvik_opcode,
1311 RegLocation rl_src, RegLocation rl_dest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001312{
buzbeefa57c472012-11-21 12:06:18 -08001313 if ((lit < 2) || ((cu->instruction_set != kThumb2) && !IsPowerOfTwo(lit))) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001314 return false;
buzbee0f79d722012-11-01 15:35:27 -07001315 }
buzbee02031b12012-11-23 09:41:35 -08001316 Codegen* cg = cu->cg.get();
buzbeeb046e162012-10-30 15:48:42 -07001317 // No divide instruction for Arm, so check for more special cases
buzbeefa57c472012-11-21 12:06:18 -08001318 if ((cu->instruction_set == kThumb2) && !IsPowerOfTwo(lit)) {
buzbee02031b12012-11-23 09:41:35 -08001319 return cg->SmallLiteralDivide(cu, dalvik_opcode, rl_src, rl_dest, lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001320 }
buzbee52a77fc2012-11-20 19:50:46 -08001321 int k = LowestSetBit(lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001322 if (k >= 30) {
1323 // Avoid special cases.
1324 return false;
1325 }
buzbeefa57c472012-11-21 12:06:18 -08001326 bool div = (dalvik_opcode == Instruction::DIV_INT_LIT8 ||
1327 dalvik_opcode == Instruction::DIV_INT_LIT16);
buzbee02031b12012-11-23 09:41:35 -08001328 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001329 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001330 if (div) {
buzbeefa57c472012-11-21 12:06:18 -08001331 int t_reg = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001332 if (lit == 2) {
1333 // Division by 2 is by far the most common division by constant.
buzbee02031b12012-11-23 09:41:35 -08001334 cg->OpRegRegImm(cu, kOpLsr, t_reg, rl_src.low_reg, 32 - k);
1335 cg->OpRegRegReg(cu, kOpAdd, t_reg, t_reg, rl_src.low_reg);
1336 cg->OpRegRegImm(cu, kOpAsr, rl_result.low_reg, t_reg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001337 } else {
buzbee02031b12012-11-23 09:41:35 -08001338 cg->OpRegRegImm(cu, kOpAsr, t_reg, rl_src.low_reg, 31);
1339 cg->OpRegRegImm(cu, kOpLsr, t_reg, t_reg, 32 - k);
1340 cg->OpRegRegReg(cu, kOpAdd, t_reg, t_reg, rl_src.low_reg);
1341 cg->OpRegRegImm(cu, kOpAsr, rl_result.low_reg, t_reg, k);
buzbee31a4a6f2012-02-28 15:36:15 -08001342 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001343 } else {
buzbeefa57c472012-11-21 12:06:18 -08001344 int t_reg1 = AllocTemp(cu);
1345 int t_reg2 = AllocTemp(cu);
Bill Buzbeea114add2012-05-03 15:00:40 -07001346 if (lit == 2) {
buzbee02031b12012-11-23 09:41:35 -08001347 cg->OpRegRegImm(cu, kOpLsr, t_reg1, rl_src.low_reg, 32 - k);
1348 cg->OpRegRegReg(cu, kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1349 cg->OpRegRegImm(cu, kOpAnd, t_reg2, t_reg2, lit -1);
1350 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Bill Buzbeea114add2012-05-03 15:00:40 -07001351 } else {
buzbee02031b12012-11-23 09:41:35 -08001352 cg->OpRegRegImm(cu, kOpAsr, t_reg1, rl_src.low_reg, 31);
1353 cg->OpRegRegImm(cu, kOpLsr, t_reg1, t_reg1, 32 - k);
1354 cg->OpRegRegReg(cu, kOpAdd, t_reg2, t_reg1, rl_src.low_reg);
1355 cg->OpRegRegImm(cu, kOpAnd, t_reg2, t_reg2, lit - 1);
1356 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg2, t_reg1);
Bill Buzbeea114add2012-05-03 15:00:40 -07001357 }
1358 }
buzbee02031b12012-11-23 09:41:35 -08001359 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001360 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001361}
1362
buzbeefa57c472012-11-21 12:06:18 -08001363// Returns true if it added instructions to 'cu' to multiply 'rl_src' by 'lit'
1364// and store the result in 'rl_dest'.
1365static bool HandleEasyMultiply(CompilationUnit* cu, RegLocation rl_src,
1366 RegLocation rl_dest, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001367{
Bill Buzbeea114add2012-05-03 15:00:40 -07001368 // Can we simplify this multiplication?
buzbeefa57c472012-11-21 12:06:18 -08001369 bool power_of_two = false;
1370 bool pop_count_le2 = false;
1371 bool power_of_two_minus_one = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001372 if (lit < 2) {
1373 // Avoid special cases.
1374 return false;
buzbee52a77fc2012-11-20 19:50:46 -08001375 } else if (IsPowerOfTwo(lit)) {
buzbeefa57c472012-11-21 12:06:18 -08001376 power_of_two = true;
buzbee52a77fc2012-11-20 19:50:46 -08001377 } else if (IsPopCountLE2(lit)) {
buzbeefa57c472012-11-21 12:06:18 -08001378 pop_count_le2 = true;
buzbee52a77fc2012-11-20 19:50:46 -08001379 } else if (IsPowerOfTwo(lit + 1)) {
buzbeefa57c472012-11-21 12:06:18 -08001380 power_of_two_minus_one = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001381 } else {
1382 return false;
1383 }
buzbee02031b12012-11-23 09:41:35 -08001384 Codegen* cg = cu->cg.get();
1385 rl_src = cg->LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001386 RegLocation rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
1387 if (power_of_two) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001388 // Shift.
buzbee02031b12012-11-23 09:41:35 -08001389 cg->OpRegRegImm(cu, kOpLsl, rl_result.low_reg, rl_src.low_reg, LowestSetBit(lit));
buzbeefa57c472012-11-21 12:06:18 -08001390 } else if (pop_count_le2) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001391 // Shift and add and shift.
buzbeefa57c472012-11-21 12:06:18 -08001392 int first_bit = LowestSetBit(lit);
1393 int second_bit = LowestSetBit(lit ^ (1 << first_bit));
buzbee02031b12012-11-23 09:41:35 -08001394 cg->GenMultiplyByTwoBitMultiplier(cu, rl_src, rl_result, lit, first_bit, second_bit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001395 } else {
1396 // Reverse subtract: (src << (shift + 1)) - src.
buzbeefa57c472012-11-21 12:06:18 -08001397 DCHECK(power_of_two_minus_one);
buzbee52a77fc2012-11-20 19:50:46 -08001398 // TUNING: rsb dst, src, src lsl#LowestSetBit(lit + 1)
buzbeefa57c472012-11-21 12:06:18 -08001399 int t_reg = AllocTemp(cu);
buzbee02031b12012-11-23 09:41:35 -08001400 cg->OpRegRegImm(cu, kOpLsl, t_reg, rl_src.low_reg, LowestSetBit(lit + 1));
1401 cg->OpRegRegReg(cu, kOpSub, rl_result.low_reg, t_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001402 }
buzbee02031b12012-11-23 09:41:35 -08001403 cg->StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001404 return true;
buzbee31a4a6f2012-02-28 15:36:15 -08001405}
1406
buzbeea5954be2013-02-07 10:41:40 -08001407void Codegen::GenArithOpIntLit(CompilationUnit* cu, Instruction::Code opcode,
buzbee02031b12012-11-23 09:41:35 -08001408 RegLocation rl_dest, RegLocation rl_src, int lit)
buzbee31a4a6f2012-02-28 15:36:15 -08001409{
buzbeefa57c472012-11-21 12:06:18 -08001410 RegLocation rl_result;
buzbeecbd6d442012-11-17 14:11:25 -08001411 OpKind op = static_cast<OpKind>(0); /* Make gcc happy */
buzbeefa57c472012-11-21 12:06:18 -08001412 int shift_op = false;
1413 bool is_div = false;
buzbee31a4a6f2012-02-28 15:36:15 -08001414
buzbee408ad162012-06-06 16:45:18 -07001415 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001416 case Instruction::RSUB_INT_LIT8:
1417 case Instruction::RSUB_INT: {
buzbeefa57c472012-11-21 12:06:18 -08001418 rl_src = LoadValue(cu, rl_src, kCoreReg);
buzbeefa57c472012-11-21 12:06:18 -08001419 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
buzbeec7d1f912013-02-07 15:22:39 -08001420 if (cu->instruction_set == kThumb2) {
1421 OpRegRegImm(cu, kOpRsub, rl_result.low_reg, rl_src.low_reg, lit);
1422 } else {
1423 OpRegReg(cu, kOpNeg, rl_result.low_reg, rl_src.low_reg);
1424 OpRegImm(cu, kOpAdd, rl_result.low_reg, lit);
1425 }
buzbeefa57c472012-11-21 12:06:18 -08001426 StoreValue(cu, rl_dest, rl_result);
buzbeea5954be2013-02-07 10:41:40 -08001427 return;
buzbee31a4a6f2012-02-28 15:36:15 -08001428 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001429
buzbeee6285f92012-12-06 15:57:46 -08001430 case Instruction::SUB_INT:
1431 case Instruction::SUB_INT_2ADDR:
1432 lit = -lit;
1433 // Intended fallthrough
1434 case Instruction::ADD_INT:
1435 case Instruction::ADD_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001436 case Instruction::ADD_INT_LIT8:
1437 case Instruction::ADD_INT_LIT16:
1438 op = kOpAdd;
1439 break;
buzbeee6285f92012-12-06 15:57:46 -08001440 case Instruction::MUL_INT:
1441 case Instruction::MUL_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001442 case Instruction::MUL_INT_LIT8:
1443 case Instruction::MUL_INT_LIT16: {
buzbeefa57c472012-11-21 12:06:18 -08001444 if (HandleEasyMultiply(cu, rl_src, rl_dest, lit)) {
buzbeea5954be2013-02-07 10:41:40 -08001445 return;
Bill Buzbeea114add2012-05-03 15:00:40 -07001446 }
1447 op = kOpMul;
1448 break;
buzbee31a4a6f2012-02-28 15:36:15 -08001449 }
buzbeee6285f92012-12-06 15:57:46 -08001450 case Instruction::AND_INT:
1451 case Instruction::AND_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001452 case Instruction::AND_INT_LIT8:
1453 case Instruction::AND_INT_LIT16:
1454 op = kOpAnd;
1455 break;
buzbeee6285f92012-12-06 15:57:46 -08001456 case Instruction::OR_INT:
1457 case Instruction::OR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001458 case Instruction::OR_INT_LIT8:
1459 case Instruction::OR_INT_LIT16:
1460 op = kOpOr;
1461 break;
buzbeee6285f92012-12-06 15:57:46 -08001462 case Instruction::XOR_INT:
1463 case Instruction::XOR_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001464 case Instruction::XOR_INT_LIT8:
1465 case Instruction::XOR_INT_LIT16:
1466 op = kOpXor;
1467 break;
1468 case Instruction::SHL_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001469 case Instruction::SHL_INT:
buzbeee6285f92012-12-06 15:57:46 -08001470 case Instruction::SHL_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001471 lit &= 31;
buzbeefa57c472012-11-21 12:06:18 -08001472 shift_op = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001473 op = kOpLsl;
1474 break;
1475 case Instruction::SHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001476 case Instruction::SHR_INT:
buzbeee6285f92012-12-06 15:57:46 -08001477 case Instruction::SHR_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 = kOpAsr;
1481 break;
1482 case Instruction::USHR_INT_LIT8:
buzbee2a83e8f2012-07-13 16:42:30 -07001483 case Instruction::USHR_INT:
buzbeee6285f92012-12-06 15:57:46 -08001484 case Instruction::USHR_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 = kOpLsr;
1488 break;
1489
buzbeee6285f92012-12-06 15:57:46 -08001490 case Instruction::DIV_INT:
1491 case Instruction::DIV_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001492 case Instruction::DIV_INT_LIT8:
1493 case Instruction::DIV_INT_LIT16:
buzbeee6285f92012-12-06 15:57:46 -08001494 case Instruction::REM_INT:
1495 case Instruction::REM_INT_2ADDR:
Bill Buzbeea114add2012-05-03 15:00:40 -07001496 case Instruction::REM_INT_LIT8:
jeffhao4f8f04a2012-10-02 18:10:35 -07001497 case Instruction::REM_INT_LIT16: {
Bill Buzbeea114add2012-05-03 15:00:40 -07001498 if (lit == 0) {
buzbeefa57c472012-11-21 12:06:18 -08001499 GenImmedCheck(cu, kCondAl, 0, 0, kThrowDivZero);
buzbeea5954be2013-02-07 10:41:40 -08001500 return;
Bill Buzbeea114add2012-05-03 15:00:40 -07001501 }
buzbeefa57c472012-11-21 12:06:18 -08001502 if (HandleEasyDivide(cu, opcode, rl_src, rl_dest, lit)) {
buzbeea5954be2013-02-07 10:41:40 -08001503 return;
Bill Buzbeea114add2012-05-03 15:00:40 -07001504 }
buzbee408ad162012-06-06 16:45:18 -07001505 if ((opcode == Instruction::DIV_INT_LIT8) ||
buzbeee6285f92012-12-06 15:57:46 -08001506 (opcode == Instruction::DIV_INT) ||
1507 (opcode == Instruction::DIV_INT_2ADDR) ||
buzbee408ad162012-06-06 16:45:18 -07001508 (opcode == Instruction::DIV_INT_LIT16)) {
buzbeefa57c472012-11-21 12:06:18 -08001509 is_div = true;
Bill Buzbeea114add2012-05-03 15:00:40 -07001510 } else {
buzbeefa57c472012-11-21 12:06:18 -08001511 is_div = false;
Bill Buzbeea114add2012-05-03 15:00:40 -07001512 }
buzbeefa57c472012-11-21 12:06:18 -08001513 if (cu->instruction_set == kMips) {
1514 rl_src = LoadValue(cu, rl_src, kCoreReg);
1515 rl_result = GenDivRemLit(cu, rl_dest, rl_src.low_reg, lit, is_div);
jeffhao4f8f04a2012-10-02 18:10:35 -07001516 } else {
buzbeefa57c472012-11-21 12:06:18 -08001517 FlushAllRegs(cu); /* Everything to home location */
1518 LoadValueDirectFixed(cu, rl_src, TargetReg(kArg0));
1519 Clobber(cu, TargetReg(kArg0));
1520 int func_offset = ENTRYPOINT_OFFSET(pIdivmod);
1521 CallRuntimeHelperRegImm(cu, func_offset, TargetReg(kArg0), lit, false);
1522 if (is_div)
1523 rl_result = GetReturn(cu, false);
buzbeeb046e162012-10-30 15:48:42 -07001524 else
buzbeefa57c472012-11-21 12:06:18 -08001525 rl_result = GetReturnAlt(cu);
jeffhao4f8f04a2012-10-02 18:10:35 -07001526 }
buzbeefa57c472012-11-21 12:06:18 -08001527 StoreValue(cu, rl_dest, rl_result);
buzbeea5954be2013-02-07 10:41:40 -08001528 return;
jeffhao4f8f04a2012-10-02 18:10:35 -07001529 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001530 default:
buzbeee6285f92012-12-06 15:57:46 -08001531 LOG(FATAL) << "Unexpected opcode " << opcode;
Bill Buzbeea114add2012-05-03 15:00:40 -07001532 }
buzbeefa57c472012-11-21 12:06:18 -08001533 rl_src = LoadValue(cu, rl_src, kCoreReg);
1534 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001535 // Avoid shifts by literal 0 - no support in Thumb. Change to copy
buzbeefa57c472012-11-21 12:06:18 -08001536 if (shift_op && (lit == 0)) {
1537 OpRegCopy(cu, rl_result.low_reg, rl_src.low_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001538 } else {
buzbeefa57c472012-11-21 12:06:18 -08001539 OpRegRegImm(cu, op, rl_result.low_reg, rl_src.low_reg, lit);
Bill Buzbeea114add2012-05-03 15:00:40 -07001540 }
buzbeefa57c472012-11-21 12:06:18 -08001541 StoreValue(cu, rl_dest, rl_result);
buzbee31a4a6f2012-02-28 15:36:15 -08001542}
1543
buzbeea5954be2013-02-07 10:41:40 -08001544void Codegen::GenArithOpLong(CompilationUnit* cu, Instruction::Code opcode, RegLocation rl_dest,
buzbee02031b12012-11-23 09:41:35 -08001545 RegLocation rl_src1, RegLocation rl_src2)
buzbee31a4a6f2012-02-28 15:36:15 -08001546{
buzbeefa57c472012-11-21 12:06:18 -08001547 RegLocation rl_result;
1548 OpKind first_op = kOpBkpt;
1549 OpKind second_op = kOpBkpt;
1550 bool call_out = false;
1551 bool check_zero = false;
1552 int func_offset;
1553 int ret_reg = TargetReg(kRet0);
buzbee31a4a6f2012-02-28 15:36:15 -08001554
buzbee408ad162012-06-06 16:45:18 -07001555 switch (opcode) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001556 case Instruction::NOT_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001557 rl_src2 = LoadValueWide(cu, rl_src2, kCoreReg);
1558 rl_result = EvalLoc(cu, rl_dest, kCoreReg, true);
Bill Buzbeea114add2012-05-03 15:00:40 -07001559 // Check for destructive overlap
buzbeefa57c472012-11-21 12:06:18 -08001560 if (rl_result.low_reg == rl_src2.high_reg) {
1561 int t_reg = AllocTemp(cu);
1562 OpRegCopy(cu, t_reg, rl_src2.high_reg);
1563 OpRegReg(cu, kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1564 OpRegReg(cu, kOpMvn, rl_result.high_reg, t_reg);
1565 FreeTemp(cu, t_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001566 } else {
buzbeefa57c472012-11-21 12:06:18 -08001567 OpRegReg(cu, kOpMvn, rl_result.low_reg, rl_src2.low_reg);
1568 OpRegReg(cu, kOpMvn, rl_result.high_reg, rl_src2.high_reg);
Bill Buzbeea114add2012-05-03 15:00:40 -07001569 }
buzbeefa57c472012-11-21 12:06:18 -08001570 StoreValueWide(cu, rl_dest, rl_result);
buzbeea5954be2013-02-07 10:41:40 -08001571 return;
Bill Buzbeea114add2012-05-03 15:00:40 -07001572 case Instruction::ADD_LONG:
1573 case Instruction::ADD_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001574 if (cu->instruction_set != kThumb2) {
buzbeea5954be2013-02-07 10:41:40 -08001575 GenAddLong(cu, rl_dest, rl_src1, rl_src2);
1576 return;
buzbeeb046e162012-10-30 15:48:42 -07001577 }
buzbeefa57c472012-11-21 12:06:18 -08001578 first_op = kOpAdd;
1579 second_op = kOpAdc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001580 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001581 case Instruction::SUB_LONG:
1582 case Instruction::SUB_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001583 if (cu->instruction_set != kThumb2) {
buzbeea5954be2013-02-07 10:41:40 -08001584 GenSubLong(cu, rl_dest, rl_src1, rl_src2);
1585 return;
buzbeeb046e162012-10-30 15:48:42 -07001586 }
buzbeefa57c472012-11-21 12:06:18 -08001587 first_op = kOpSub;
1588 second_op = kOpSbc;
Bill Buzbeea114add2012-05-03 15:00:40 -07001589 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001590 case Instruction::MUL_LONG:
1591 case Instruction::MUL_LONG_2ADDR:
buzbee4ef3e452012-12-14 13:35:28 -08001592 if (cu->instruction_set == kThumb2) {
1593 GenMulLong(cu, rl_dest, rl_src1, rl_src2);
buzbeea5954be2013-02-07 10:41:40 -08001594 return;
buzbee4ef3e452012-12-14 13:35:28 -08001595 } else {
1596 call_out = true;
1597 ret_reg = TargetReg(kRet0);
1598 func_offset = ENTRYPOINT_OFFSET(pLmul);
1599 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001600 break;
1601 case Instruction::DIV_LONG:
1602 case Instruction::DIV_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001603 call_out = true;
1604 check_zero = true;
1605 ret_reg = TargetReg(kRet0);
1606 func_offset = ENTRYPOINT_OFFSET(pLdiv);
Bill Buzbeea114add2012-05-03 15:00:40 -07001607 break;
1608 case Instruction::REM_LONG:
1609 case Instruction::REM_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001610 call_out = true;
1611 check_zero = true;
1612 func_offset = ENTRYPOINT_OFFSET(pLdivmod);
buzbeef0504cd2012-11-13 16:31:10 -08001613 /* NOTE - for Arm, result is in kArg2/kArg3 instead of kRet0/kRet1 */
buzbeefa57c472012-11-21 12:06:18 -08001614 ret_reg = (cu->instruction_set == kThumb2) ? TargetReg(kArg2) : TargetReg(kRet0);
Bill Buzbeea114add2012-05-03 15:00:40 -07001615 break;
1616 case Instruction::AND_LONG_2ADDR:
1617 case Instruction::AND_LONG:
buzbeefa57c472012-11-21 12:06:18 -08001618 if (cu->instruction_set == kX86) {
1619 return GenAndLong(cu, rl_dest, rl_src1, rl_src2);
buzbeeb046e162012-10-30 15:48:42 -07001620 }
buzbeefa57c472012-11-21 12:06:18 -08001621 first_op = kOpAnd;
1622 second_op = kOpAnd;
Bill Buzbeea114add2012-05-03 15:00:40 -07001623 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001624 case Instruction::OR_LONG:
1625 case Instruction::OR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001626 if (cu->instruction_set == kX86) {
buzbeea5954be2013-02-07 10:41:40 -08001627 GenOrLong(cu, rl_dest, rl_src1, rl_src2);
1628 return;
buzbeeb046e162012-10-30 15:48:42 -07001629 }
buzbeefa57c472012-11-21 12:06:18 -08001630 first_op = kOpOr;
1631 second_op = kOpOr;
Bill Buzbeea114add2012-05-03 15:00:40 -07001632 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001633 case Instruction::XOR_LONG:
1634 case Instruction::XOR_LONG_2ADDR:
buzbeefa57c472012-11-21 12:06:18 -08001635 if (cu->instruction_set == kX86) {
buzbeea5954be2013-02-07 10:41:40 -08001636 GenXorLong(cu, rl_dest, rl_src1, rl_src2);
1637 return;
buzbeeb046e162012-10-30 15:48:42 -07001638 }
buzbeefa57c472012-11-21 12:06:18 -08001639 first_op = kOpXor;
1640 second_op = kOpXor;
Bill Buzbeea114add2012-05-03 15:00:40 -07001641 break;
Bill Buzbeea114add2012-05-03 15:00:40 -07001642 case Instruction::NEG_LONG: {
buzbeea5954be2013-02-07 10:41:40 -08001643 GenNegLong(cu, rl_dest, rl_src2);
1644 return;
buzbee31a4a6f2012-02-28 15:36:15 -08001645 }
Bill Buzbeea114add2012-05-03 15:00:40 -07001646 default:
1647 LOG(FATAL) << "Invalid long arith op";
1648 }
buzbeefa57c472012-11-21 12:06:18 -08001649 if (!call_out) {
1650 GenLong3Addr(cu, first_op, second_op, rl_dest, rl_src1, rl_src2);
Bill Buzbeea114add2012-05-03 15:00:40 -07001651 } else {
buzbeefa57c472012-11-21 12:06:18 -08001652 FlushAllRegs(cu); /* Send everything to home location */
1653 if (check_zero) {
1654 LoadValueDirectWideFixed(cu, rl_src2, TargetReg(kArg2), TargetReg(kArg3));
1655 int r_tgt = CallHelperSetup(cu, func_offset);
1656 GenDivZeroCheck(cu, TargetReg(kArg2), TargetReg(kArg3));
1657 LoadValueDirectWideFixed(cu, rl_src1, TargetReg(kArg0), TargetReg(kArg1));
buzbee8320f382012-09-11 16:29:42 -07001658 // NOTE: callout here is not a safepoint
buzbeefa57c472012-11-21 12:06:18 -08001659 CallHelper(cu, r_tgt, func_offset, false /* not safepoint */);
buzbee31a4a6f2012-02-28 15:36:15 -08001660 } else {
buzbeefa57c472012-11-21 12:06:18 -08001661 CallRuntimeHelperRegLocationRegLocation(cu, func_offset,
1662 rl_src1, rl_src2, false);
buzbee31a4a6f2012-02-28 15:36:15 -08001663 }
buzbeef0504cd2012-11-13 16:31:10 -08001664 // Adjust return regs in to handle case of rem returning kArg2/kArg3
buzbeefa57c472012-11-21 12:06:18 -08001665 if (ret_reg == TargetReg(kRet0))
1666 rl_result = GetReturnWide(cu, false);
Bill Buzbeea114add2012-05-03 15:00:40 -07001667 else
buzbeefa57c472012-11-21 12:06:18 -08001668 rl_result = GetReturnWideAlt(cu);
1669 StoreValueWide(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001670 }
buzbee31a4a6f2012-02-28 15:36:15 -08001671}
1672
buzbeea5954be2013-02-07 10:41:40 -08001673void Codegen::GenConversionCall(CompilationUnit* cu, int func_offset,
buzbee02031b12012-11-23 09:41:35 -08001674 RegLocation rl_dest, RegLocation rl_src)
buzbee31a4a6f2012-02-28 15:36:15 -08001675{
Bill Buzbeea114add2012-05-03 15:00:40 -07001676 /*
1677 * Don't optimize the register usage since it calls out to support
1678 * functions
1679 */
buzbeefa57c472012-11-21 12:06:18 -08001680 FlushAllRegs(cu); /* Send everything to home location */
1681 if (rl_src.wide) {
1682 LoadValueDirectWideFixed(cu, rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0),
1683 rl_src.fp ? TargetReg(kFArg1) : TargetReg(kArg1));
buzbee408ad162012-06-06 16:45:18 -07001684 } else {
buzbeefa57c472012-11-21 12:06:18 -08001685 LoadValueDirectFixed(cu, rl_src, rl_src.fp ? TargetReg(kFArg0) : TargetReg(kArg0));
Bill Buzbeea114add2012-05-03 15:00:40 -07001686 }
buzbeefa57c472012-11-21 12:06:18 -08001687 CallRuntimeHelperRegLocation(cu, func_offset, rl_src, false);
1688 if (rl_dest.wide) {
1689 RegLocation rl_result;
1690 rl_result = GetReturnWide(cu, rl_dest.fp);
1691 StoreValueWide(cu, rl_dest, rl_result);
buzbee408ad162012-06-06 16:45:18 -07001692 } else {
buzbeefa57c472012-11-21 12:06:18 -08001693 RegLocation rl_result;
1694 rl_result = GetReturn(cu, rl_dest.fp);
1695 StoreValue(cu, rl_dest, rl_result);
Bill Buzbeea114add2012-05-03 15:00:40 -07001696 }
buzbee31a4a6f2012-02-28 15:36:15 -08001697}
1698
buzbee31a4a6f2012-02-28 15:36:15 -08001699/* Check if we need to check for pending suspend request */
buzbee02031b12012-11-23 09:41:35 -08001700void Codegen::GenSuspendTest(CompilationUnit* cu, int opt_flags)
buzbee31a4a6f2012-02-28 15:36:15 -08001701{
buzbeefa57c472012-11-21 12:06:18 -08001702 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
Bill Buzbeea114add2012-05-03 15:00:40 -07001703 return;
1704 }
buzbeefa57c472012-11-21 12:06:18 -08001705 FlushAllRegs(cu);
1706 LIR* branch = OpTestSuspend(cu, NULL);
1707 LIR* ret_lab = NewLIR0(cu, kPseudoTargetLabel);
1708 LIR* target = RawLIR(cu, cu->current_dalvik_offset, kPseudoSuspendTarget,
1709 reinterpret_cast<uintptr_t>(ret_lab), cu->current_dalvik_offset);
buzbeecbd6d442012-11-17 14:11:25 -08001710 branch->target = target;
buzbeefa57c472012-11-21 12:06:18 -08001711 InsertGrowableList(cu, &cu->suspend_launchpads, reinterpret_cast<uintptr_t>(target));
buzbee31a4a6f2012-02-28 15:36:15 -08001712}
1713
buzbeefead2932012-03-30 14:02:01 -07001714/* Check if we need to check for pending suspend request */
buzbee02031b12012-11-23 09:41:35 -08001715void Codegen::GenSuspendTestAndBranch(CompilationUnit* cu, int opt_flags, LIR* target)
buzbeefead2932012-03-30 14:02:01 -07001716{
buzbeefa57c472012-11-21 12:06:18 -08001717 if (NO_SUSPEND || (opt_flags & MIR_IGNORE_SUSPEND_CHECK)) {
1718 OpUnconditionalBranch(cu, target);
Bill Buzbeea114add2012-05-03 15:00:40 -07001719 return;
1720 }
buzbeefa57c472012-11-21 12:06:18 -08001721 OpTestSuspend(cu, target);
1722 LIR* launch_pad =
1723 RawLIR(cu, cu->current_dalvik_offset, kPseudoSuspendTarget,
1724 reinterpret_cast<uintptr_t>(target), cu->current_dalvik_offset);
1725 FlushAllRegs(cu);
1726 OpUnconditionalBranch(cu, launch_pad);
1727 InsertGrowableList(cu, &cu->suspend_launchpads, reinterpret_cast<uintptr_t>(launch_pad));
buzbeefead2932012-03-30 14:02:01 -07001728}
1729
buzbee31a4a6f2012-02-28 15:36:15 -08001730} // namespace art