blob: 2d26922dca060f12a1770171b9d455ea2843f8a0 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "codegen_mips.h"
Ian Rogersd582fa42014-11-05 23:46:43 -080018
19#include "arch/mips/instruction_set_features_mips.h"
Nikola Veljkovic2d873b62015-02-20 17:21:15 +010020#include "arch/mips/entrypoints_direct_mips.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080021#include "base/logging.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070022#include "dex/quick/mir_to_lir-inl.h"
buzbeeb5860fb2014-06-21 15:31:01 -070023#include "dex/reg_storage_eq.h"
Andreas Gampe0b9203e2015-01-22 20:39:27 -080024#include "driver/compiler_driver.h"
Brian Carlstrom7940e442013-07-12 13:46:57 -070025#include "mips_lir.h"
26
27namespace art {
28
29/* This file contains codegen for the MIPS32 ISA. */
buzbee2700f7e2014-03-07 09:46:20 -080030LIR* MipsMir2Lir::OpFpRegCopy(RegStorage r_dest, RegStorage r_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070031 int opcode;
32 /* must be both DOUBLE or both not DOUBLE */
buzbee091cc402014-03-31 10:14:40 -070033 DCHECK_EQ(r_dest.IsDouble(), r_src.IsDouble());
34 if (r_dest.IsDouble()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070035 opcode = kMipsFmovd;
36 } else {
buzbee091cc402014-03-31 10:14:40 -070037 if (r_dest.IsSingle()) {
38 if (r_src.IsSingle()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070039 opcode = kMipsFmovs;
40 } else {
41 /* note the operands are swapped for the mtc1 instr */
buzbee2700f7e2014-03-07 09:46:20 -080042 RegStorage t_opnd = r_src;
Brian Carlstrom7940e442013-07-12 13:46:57 -070043 r_src = r_dest;
44 r_dest = t_opnd;
45 opcode = kMipsMtc1;
46 }
47 } else {
buzbee091cc402014-03-31 10:14:40 -070048 DCHECK(r_src.IsSingle());
Brian Carlstrom7940e442013-07-12 13:46:57 -070049 opcode = kMipsMfc1;
50 }
51 }
buzbee2700f7e2014-03-07 09:46:20 -080052 LIR* res = RawLIR(current_dalvik_offset_, opcode, r_src.GetReg(), r_dest.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -070053 if (!(cu_->disable_opt & (1 << kSafeOptimizations)) && r_dest == r_src) {
54 res->flags.is_nop = true;
55 }
56 return res;
57}
58
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070059bool MipsMir2Lir::InexpensiveConstantInt(int32_t value) {
Andreas Gampeab1eb0d2015-02-13 19:23:55 -080060 // For encodings, see LoadConstantNoClobber below.
61 return ((value == 0) || IsUint<16>(value) || IsInt<16>(value));
Brian Carlstrom7940e442013-07-12 13:46:57 -070062}
63
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070064bool MipsMir2Lir::InexpensiveConstantFloat(int32_t value) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070065 UNUSED(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -070066 return false; // TUNING
67}
68
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070069bool MipsMir2Lir::InexpensiveConstantLong(int64_t value) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070070 UNUSED(value);
Brian Carlstrom7940e442013-07-12 13:46:57 -070071 return false; // TUNING
72}
73
Brian Carlstrom2ce745c2013-07-17 17:44:30 -070074bool MipsMir2Lir::InexpensiveConstantDouble(int64_t value) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070075 UNUSED(value);
Brian Carlstrom7934ac22013-07-26 10:54:15 -070076 return false; // TUNING
Brian Carlstrom7940e442013-07-12 13:46:57 -070077}
78
79/*
80 * Load a immediate using a shortcut if possible; otherwise
81 * grab from the per-translation literal pool. If target is
82 * a high register, build constant into a low register and copy.
83 *
84 * No additional register clobbering operation performed. Use this version when
85 * 1) r_dest is freshly returned from AllocTemp or
86 * 2) The codegen is under fixed register usage
87 */
buzbee2700f7e2014-03-07 09:46:20 -080088LIR* MipsMir2Lir::LoadConstantNoClobber(RegStorage r_dest, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -070089 LIR *res;
90
buzbee2700f7e2014-03-07 09:46:20 -080091 RegStorage r_dest_save = r_dest;
buzbee091cc402014-03-31 10:14:40 -070092 int is_fp_reg = r_dest.IsFloat();
Brian Carlstrom7940e442013-07-12 13:46:57 -070093 if (is_fp_reg) {
buzbee091cc402014-03-31 10:14:40 -070094 DCHECK(r_dest.IsSingle());
Brian Carlstrom7940e442013-07-12 13:46:57 -070095 r_dest = AllocTemp();
96 }
97
98 /* See if the value can be constructed cheaply */
99 if (value == 0) {
buzbee2700f7e2014-03-07 09:46:20 -0800100 res = NewLIR2(kMipsMove, r_dest.GetReg(), rZERO);
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800101 } else if (IsUint<16>(value)) {
102 // Use OR with (unsigned) immediate to encode 16b unsigned int.
buzbee2700f7e2014-03-07 09:46:20 -0800103 res = NewLIR3(kMipsOri, r_dest.GetReg(), rZERO, value);
Andreas Gampeab1eb0d2015-02-13 19:23:55 -0800104 } else if (IsInt<16>(value)) {
105 // Use ADD with (signed) immediate to encode 16b signed int.
buzbee2700f7e2014-03-07 09:46:20 -0800106 res = NewLIR3(kMipsAddiu, r_dest.GetReg(), rZERO, value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700107 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800108 res = NewLIR2(kMipsLui, r_dest.GetReg(), value >> 16);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700109 if (value & 0xffff)
buzbee2700f7e2014-03-07 09:46:20 -0800110 NewLIR3(kMipsOri, r_dest.GetReg(), r_dest.GetReg(), value);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700111 }
112
113 if (is_fp_reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800114 NewLIR2(kMipsMtc1, r_dest.GetReg(), r_dest_save.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700115 FreeTemp(r_dest);
116 }
117
118 return res;
119}
120
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700121LIR* MipsMir2Lir::OpUnconditionalBranch(LIR* target) {
Brian Carlstromdf629502013-07-17 22:39:56 -0700122 LIR* res = NewLIR1(kMipsB, 0 /* offset to be patched during assembly*/);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700123 res->target = target;
124 return res;
125}
126
buzbee2700f7e2014-03-07 09:46:20 -0800127LIR* MipsMir2Lir::OpReg(OpKind op, RegStorage r_dest_src) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 MipsOpCode opcode = kMipsNop;
129 switch (op) {
130 case kOpBlx:
131 opcode = kMipsJalr;
132 break;
133 case kOpBx:
Andreas Gampe8d365912015-01-13 11:32:32 -0800134 return NewLIR2(kMipsJalr, rZERO, r_dest_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700135 break;
136 default:
137 LOG(FATAL) << "Bad case in OpReg";
138 }
buzbee2700f7e2014-03-07 09:46:20 -0800139 return NewLIR2(opcode, rRA, r_dest_src.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700140}
141
buzbee2700f7e2014-03-07 09:46:20 -0800142LIR* MipsMir2Lir::OpRegImm(OpKind op, RegStorage r_dest_src1, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700143 LIR *res;
144 bool neg = (value < 0);
145 int abs_value = (neg) ? -value : value;
146 bool short_form = (abs_value & 0xff) == abs_value;
147 MipsOpCode opcode = kMipsNop;
148 switch (op) {
149 case kOpAdd:
150 return OpRegRegImm(op, r_dest_src1, r_dest_src1, value);
151 break;
152 case kOpSub:
153 return OpRegRegImm(op, r_dest_src1, r_dest_src1, value);
154 break;
155 default:
156 LOG(FATAL) << "Bad case in OpRegImm";
157 break;
158 }
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700159 if (short_form) {
buzbee2700f7e2014-03-07 09:46:20 -0800160 res = NewLIR2(opcode, r_dest_src1.GetReg(), abs_value);
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700161 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800162 RegStorage r_scratch = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700163 res = LoadConstant(r_scratch, value);
164 if (op == kOpCmp)
buzbee2700f7e2014-03-07 09:46:20 -0800165 NewLIR2(opcode, r_dest_src1.GetReg(), r_scratch.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700166 else
buzbee2700f7e2014-03-07 09:46:20 -0800167 NewLIR3(opcode, r_dest_src1.GetReg(), r_dest_src1.GetReg(), r_scratch.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700168 }
169 return res;
170}
171
buzbee2700f7e2014-03-07 09:46:20 -0800172LIR* MipsMir2Lir::OpRegRegReg(OpKind op, RegStorage r_dest, RegStorage r_src1, RegStorage r_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700173 MipsOpCode opcode = kMipsNop;
174 switch (op) {
175 case kOpAdd:
176 opcode = kMipsAddu;
177 break;
178 case kOpSub:
179 opcode = kMipsSubu;
180 break;
181 case kOpAnd:
182 opcode = kMipsAnd;
183 break;
184 case kOpMul:
185 opcode = kMipsMul;
186 break;
187 case kOpOr:
188 opcode = kMipsOr;
189 break;
190 case kOpXor:
191 opcode = kMipsXor;
192 break;
193 case kOpLsl:
194 opcode = kMipsSllv;
195 break;
196 case kOpLsr:
197 opcode = kMipsSrlv;
198 break;
199 case kOpAsr:
200 opcode = kMipsSrav;
201 break;
202 case kOpAdc:
203 case kOpSbc:
204 LOG(FATAL) << "No carry bit on MIPS";
205 break;
206 default:
207 LOG(FATAL) << "bad case in OpRegRegReg";
208 break;
209 }
buzbee2700f7e2014-03-07 09:46:20 -0800210 return NewLIR3(opcode, r_dest.GetReg(), r_src1.GetReg(), r_src2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700211}
212
buzbee2700f7e2014-03-07 09:46:20 -0800213LIR* MipsMir2Lir::OpRegRegImm(OpKind op, RegStorage r_dest, RegStorage r_src1, int value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 LIR *res;
215 MipsOpCode opcode = kMipsNop;
216 bool short_form = true;
217
218 switch (op) {
219 case kOpAdd:
220 if (IS_SIMM16(value)) {
221 opcode = kMipsAddiu;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700222 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700223 short_form = false;
224 opcode = kMipsAddu;
225 }
226 break;
227 case kOpSub:
228 if (IS_SIMM16((-value))) {
229 value = -value;
230 opcode = kMipsAddiu;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700231 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700232 short_form = false;
233 opcode = kMipsSubu;
234 }
235 break;
236 case kOpLsl:
Andreas Gampe8ebdc2b2015-01-14 12:09:25 -0800237 DCHECK(value >= 0 && value <= 31);
238 opcode = kMipsSll;
239 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700240 case kOpLsr:
Andreas Gampe8ebdc2b2015-01-14 12:09:25 -0800241 DCHECK(value >= 0 && value <= 31);
242 opcode = kMipsSrl;
243 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700244 case kOpAsr:
Andreas Gampe8ebdc2b2015-01-14 12:09:25 -0800245 DCHECK(value >= 0 && value <= 31);
246 opcode = kMipsSra;
247 break;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700248 case kOpAnd:
249 if (IS_UIMM16((value))) {
250 opcode = kMipsAndi;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700251 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700252 short_form = false;
253 opcode = kMipsAnd;
254 }
255 break;
256 case kOpOr:
257 if (IS_UIMM16((value))) {
258 opcode = kMipsOri;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700259 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700260 short_form = false;
261 opcode = kMipsOr;
262 }
263 break;
264 case kOpXor:
265 if (IS_UIMM16((value))) {
266 opcode = kMipsXori;
Brian Carlstromf69863b2013-07-17 21:53:13 -0700267 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700268 short_form = false;
269 opcode = kMipsXor;
270 }
271 break;
272 case kOpMul:
273 short_form = false;
274 opcode = kMipsMul;
275 break;
276 default:
277 LOG(FATAL) << "Bad case in OpRegRegImm";
278 break;
279 }
280
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700281 if (short_form) {
buzbee2700f7e2014-03-07 09:46:20 -0800282 res = NewLIR3(opcode, r_dest.GetReg(), r_src1.GetReg(), value);
Brian Carlstrom9b7085a2013-07-18 15:15:21 -0700283 } else {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700284 if (r_dest != r_src1) {
285 res = LoadConstant(r_dest, value);
buzbee2700f7e2014-03-07 09:46:20 -0800286 NewLIR3(opcode, r_dest.GetReg(), r_src1.GetReg(), r_dest.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700287 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800288 RegStorage r_scratch = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700289 res = LoadConstant(r_scratch, value);
buzbee2700f7e2014-03-07 09:46:20 -0800290 NewLIR3(opcode, r_dest.GetReg(), r_src1.GetReg(), r_scratch.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700291 }
292 }
293 return res;
294}
295
buzbee2700f7e2014-03-07 09:46:20 -0800296LIR* MipsMir2Lir::OpRegReg(OpKind op, RegStorage r_dest_src1, RegStorage r_src2) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700297 MipsOpCode opcode = kMipsNop;
298 LIR *res;
299 switch (op) {
300 case kOpMov:
301 opcode = kMipsMove;
302 break;
303 case kOpMvn:
buzbee2700f7e2014-03-07 09:46:20 -0800304 return NewLIR3(kMipsNor, r_dest_src1.GetReg(), r_src2.GetReg(), rZERO);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700305 case kOpNeg:
buzbee2700f7e2014-03-07 09:46:20 -0800306 return NewLIR3(kMipsSubu, r_dest_src1.GetReg(), rZERO, r_src2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700307 case kOpAdd:
308 case kOpAnd:
309 case kOpMul:
310 case kOpOr:
311 case kOpSub:
312 case kOpXor:
313 return OpRegRegReg(op, r_dest_src1, r_dest_src1, r_src2);
314 case kOp2Byte:
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800315 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsMipsInstructionSetFeatures()
Ian Rogersd582fa42014-11-05 23:46:43 -0800316 ->IsMipsIsaRevGreaterThanEqual2()) {
317 res = NewLIR2(kMipsSeb, r_dest_src1.GetReg(), r_src2.GetReg());
318 } else {
319 res = OpRegRegImm(kOpLsl, r_dest_src1, r_src2, 24);
320 OpRegRegImm(kOpAsr, r_dest_src1, r_dest_src1, 24);
321 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700322 return res;
323 case kOp2Short:
Andreas Gampe0b9203e2015-01-22 20:39:27 -0800324 if (cu_->compiler_driver->GetInstructionSetFeatures()->AsMipsInstructionSetFeatures()
Ian Rogersd582fa42014-11-05 23:46:43 -0800325 ->IsMipsIsaRevGreaterThanEqual2()) {
326 res = NewLIR2(kMipsSeh, r_dest_src1.GetReg(), r_src2.GetReg());
327 } else {
328 res = OpRegRegImm(kOpLsl, r_dest_src1, r_src2, 16);
329 OpRegRegImm(kOpAsr, r_dest_src1, r_dest_src1, 16);
330 }
Brian Carlstrom7940e442013-07-12 13:46:57 -0700331 return res;
332 case kOp2Char:
Andreas Gampe8ebdc2b2015-01-14 12:09:25 -0800333 return NewLIR3(kMipsAndi, r_dest_src1.GetReg(), r_src2.GetReg(), 0xFFFF);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700334 default:
335 LOG(FATAL) << "Bad case in OpRegReg";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700336 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700337 }
buzbee2700f7e2014-03-07 09:46:20 -0800338 return NewLIR2(opcode, r_dest_src1.GetReg(), r_src2.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700339}
340
buzbee2700f7e2014-03-07 09:46:20 -0800341LIR* MipsMir2Lir::OpMovRegMem(RegStorage r_dest, RegStorage r_base, int offset,
342 MoveType move_type) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700343 UNUSED(r_dest, r_base, offset, move_type);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800344 UNIMPLEMENTED(FATAL);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700345 UNREACHABLE();
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800346}
347
buzbee2700f7e2014-03-07 09:46:20 -0800348LIR* MipsMir2Lir::OpMovMemReg(RegStorage r_base, int offset, RegStorage r_src, MoveType move_type) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700349 UNUSED(r_base, offset, r_src, move_type);
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800350 UNIMPLEMENTED(FATAL);
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700351 UNREACHABLE();
Razvan A Lupusoru2c498d12014-01-29 16:02:57 -0800352}
353
buzbee2700f7e2014-03-07 09:46:20 -0800354LIR* MipsMir2Lir::OpCondRegReg(OpKind op, ConditionCode cc, RegStorage r_dest, RegStorage r_src) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700355 UNUSED(op, cc, r_dest, r_src);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800356 LOG(FATAL) << "Unexpected use of OpCondRegReg for MIPS";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700357 UNREACHABLE();
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800358}
359
buzbee2700f7e2014-03-07 09:46:20 -0800360LIR* MipsMir2Lir::LoadConstantWide(RegStorage r_dest, int64_t value) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700361 LIR *res;
Douglas Leung2db3e262014-06-25 16:02:55 -0700362 if (!r_dest.IsPair()) {
363 // Form 64-bit pair
364 r_dest = Solo64ToPair64(r_dest);
365 }
buzbee2700f7e2014-03-07 09:46:20 -0800366 res = LoadConstantNoClobber(r_dest.GetLow(), Low32Bits(value));
367 LoadConstantNoClobber(r_dest.GetHigh(), High32Bits(value));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700368 return res;
369}
370
371/* Load value from base + scaled index. */
buzbee2700f7e2014-03-07 09:46:20 -0800372LIR* MipsMir2Lir::LoadBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700373 int scale, OpSize size) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700374 LIR *first = NULL;
375 LIR *res;
376 MipsOpCode opcode = kMipsNop;
buzbee2700f7e2014-03-07 09:46:20 -0800377 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700378
buzbee091cc402014-03-31 10:14:40 -0700379 if (r_dest.IsFloat()) {
380 DCHECK(r_dest.IsSingle());
buzbeefd698e62014-04-27 19:33:22 -0700381 DCHECK((size == k32) || (size == kSingle) || (size == kReference));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700382 size = kSingle;
383 } else {
384 if (size == kSingle)
buzbee695d13a2014-04-19 13:32:20 -0700385 size = k32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700386 }
387
388 if (!scale) {
buzbee2700f7e2014-03-07 09:46:20 -0800389 first = NewLIR3(kMipsAddu, t_reg.GetReg() , r_base.GetReg(), r_index.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700390 } else {
391 first = OpRegRegImm(kOpLsl, t_reg, r_index, scale);
buzbee2700f7e2014-03-07 09:46:20 -0800392 NewLIR3(kMipsAddu, t_reg.GetReg() , r_base.GetReg(), t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700393 }
394
395 switch (size) {
396 case kSingle:
397 opcode = kMipsFlwc1;
398 break;
buzbee695d13a2014-04-19 13:32:20 -0700399 case k32:
400 case kReference:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700401 opcode = kMipsLw;
402 break;
403 case kUnsignedHalf:
404 opcode = kMipsLhu;
405 break;
406 case kSignedHalf:
407 opcode = kMipsLh;
408 break;
409 case kUnsignedByte:
410 opcode = kMipsLbu;
411 break;
412 case kSignedByte:
413 opcode = kMipsLb;
414 break;
415 default:
416 LOG(FATAL) << "Bad case in LoadBaseIndexed";
417 }
418
buzbee2700f7e2014-03-07 09:46:20 -0800419 res = NewLIR3(opcode, r_dest.GetReg(), 0, t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700420 FreeTemp(t_reg);
421 return (first) ? first : res;
422}
423
424/* store value base base + scaled index. */
buzbee2700f7e2014-03-07 09:46:20 -0800425LIR* MipsMir2Lir::StoreBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src,
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700426 int scale, OpSize size) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700427 LIR *first = NULL;
428 MipsOpCode opcode = kMipsNop;
buzbee2700f7e2014-03-07 09:46:20 -0800429 RegStorage t_reg = AllocTemp();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700430
buzbee091cc402014-03-31 10:14:40 -0700431 if (r_src.IsFloat()) {
432 DCHECK(r_src.IsSingle());
buzbeefd698e62014-04-27 19:33:22 -0700433 DCHECK((size == k32) || (size == kSingle) || (size == kReference));
Brian Carlstrom7940e442013-07-12 13:46:57 -0700434 size = kSingle;
435 } else {
436 if (size == kSingle)
buzbee695d13a2014-04-19 13:32:20 -0700437 size = k32;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700438 }
439
440 if (!scale) {
buzbee2700f7e2014-03-07 09:46:20 -0800441 first = NewLIR3(kMipsAddu, t_reg.GetReg() , r_base.GetReg(), r_index.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700442 } else {
443 first = OpRegRegImm(kOpLsl, t_reg, r_index, scale);
buzbee2700f7e2014-03-07 09:46:20 -0800444 NewLIR3(kMipsAddu, t_reg.GetReg() , r_base.GetReg(), t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700445 }
446
447 switch (size) {
448 case kSingle:
449 opcode = kMipsFswc1;
450 break;
buzbee695d13a2014-04-19 13:32:20 -0700451 case k32:
452 case kReference:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700453 opcode = kMipsSw;
454 break;
455 case kUnsignedHalf:
456 case kSignedHalf:
457 opcode = kMipsSh;
458 break;
459 case kUnsignedByte:
460 case kSignedByte:
461 opcode = kMipsSb;
462 break;
463 default:
464 LOG(FATAL) << "Bad case in StoreBaseIndexed";
465 }
buzbee2700f7e2014-03-07 09:46:20 -0800466 NewLIR3(opcode, r_src.GetReg(), 0, t_reg.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700467 return first;
468}
469
buzbee2700f7e2014-03-07 09:46:20 -0800470// FIXME: don't split r_dest into 2 containers.
471LIR* MipsMir2Lir::LoadBaseDispBody(RegStorage r_base, int displacement, RegStorage r_dest,
Douglas Leung2db3e262014-06-25 16:02:55 -0700472 OpSize size) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700473/*
474 * Load value from base + displacement. Optionally perform null check
475 * on base (which must have an associated s_reg and MIR). If not
476 * performing null check, incoming MIR can be null. IMPORTANT: this
477 * code must not allocate any new temps. If a new register is needed
478 * and base and dest are the same, spill some other register to
479 * rlp and then restore.
480 */
Brian Carlstrom7940e442013-07-12 13:46:57 -0700481 LIR *res;
482 LIR *load = NULL;
483 LIR *load2 = NULL;
484 MipsOpCode opcode = kMipsNop;
485 bool short_form = IS_SIMM16(displacement);
Douglas Leung2db3e262014-06-25 16:02:55 -0700486 bool pair = r_dest.IsPair();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700487
488 switch (size) {
buzbee695d13a2014-04-19 13:32:20 -0700489 case k64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700490 case kDouble:
Douglas Leung2db3e262014-06-25 16:02:55 -0700491 if (!pair) {
492 // Form 64-bit pair
493 r_dest = Solo64ToPair64(r_dest);
494 pair = 1;
495 }
buzbee091cc402014-03-31 10:14:40 -0700496 if (r_dest.IsFloat()) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700497 DCHECK_EQ(r_dest.GetLowReg(), r_dest.GetHighReg() - 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700498 opcode = kMipsFlwc1;
Douglas Leung2db3e262014-06-25 16:02:55 -0700499 } else {
500 opcode = kMipsLw;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700501 }
502 short_form = IS_SIMM16_2WORD(displacement);
503 DCHECK_EQ((displacement & 0x3), 0);
504 break;
buzbee695d13a2014-04-19 13:32:20 -0700505 case k32:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700506 case kSingle:
buzbee695d13a2014-04-19 13:32:20 -0700507 case kReference:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700508 opcode = kMipsLw;
buzbee091cc402014-03-31 10:14:40 -0700509 if (r_dest.IsFloat()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700510 opcode = kMipsFlwc1;
buzbee091cc402014-03-31 10:14:40 -0700511 DCHECK(r_dest.IsSingle());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700512 }
513 DCHECK_EQ((displacement & 0x3), 0);
514 break;
515 case kUnsignedHalf:
516 opcode = kMipsLhu;
517 DCHECK_EQ((displacement & 0x1), 0);
518 break;
519 case kSignedHalf:
520 opcode = kMipsLh;
521 DCHECK_EQ((displacement & 0x1), 0);
522 break;
523 case kUnsignedByte:
524 opcode = kMipsLbu;
525 break;
526 case kSignedByte:
527 opcode = kMipsLb;
528 break;
529 default:
530 LOG(FATAL) << "Bad case in LoadBaseIndexedBody";
531 }
532
533 if (short_form) {
534 if (!pair) {
buzbee2700f7e2014-03-07 09:46:20 -0800535 load = res = NewLIR3(opcode, r_dest.GetReg(), displacement, r_base.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700536 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700537 load = res = NewLIR3(opcode, r_dest.GetLowReg(), displacement + LOWORD_OFFSET, r_base.GetReg());
538 load2 = NewLIR3(opcode, r_dest.GetHighReg(), displacement + HIWORD_OFFSET, r_base.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700539 }
540 } else {
541 if (pair) {
buzbee2700f7e2014-03-07 09:46:20 -0800542 RegStorage r_tmp = AllocTemp();
543 res = OpRegRegImm(kOpAdd, r_tmp, r_base, displacement);
Douglas Leung2db3e262014-06-25 16:02:55 -0700544 load = NewLIR3(opcode, r_dest.GetLowReg(), LOWORD_OFFSET, r_tmp.GetReg());
545 load2 = NewLIR3(opcode, r_dest.GetHighReg(), HIWORD_OFFSET, r_tmp.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700546 FreeTemp(r_tmp);
547 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800548 RegStorage r_tmp = (r_base == r_dest) ? AllocTemp() : r_dest;
549 res = OpRegRegImm(kOpAdd, r_tmp, r_base, displacement);
550 load = NewLIR3(opcode, r_dest.GetReg(), 0, r_tmp.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700551 if (r_tmp != r_dest)
552 FreeTemp(r_tmp);
553 }
554 }
555
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100556 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800557 DCHECK_EQ(r_base, rs_rMIPS_SP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700558 AnnotateDalvikRegAccess(load, (displacement + (pair ? LOWORD_OFFSET : 0)) >> 2,
559 true /* is_load */, pair /* is64bit */);
560 if (pair) {
561 AnnotateDalvikRegAccess(load2, (displacement + HIWORD_OFFSET) >> 2,
562 true /* is_load */, pair /* is64bit */);
563 }
564 }
565 return load;
566}
567
Andreas Gampede686762014-06-24 18:42:06 +0000568LIR* MipsMir2Lir::LoadBaseDisp(RegStorage r_base, int displacement, RegStorage r_dest,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000569 OpSize size, VolatileKind is_volatile) {
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700570 if (UNLIKELY(is_volatile == kVolatile && (size == k64 || size == kDouble))) {
571 // Do atomic 64-bit load.
572 return GenAtomic64Load(r_base, displacement, r_dest);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000573 }
574
buzbee695d13a2014-04-19 13:32:20 -0700575 // TODO: base this on target.
576 if (size == kWord) {
577 size = k32;
578 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000579 LIR* load;
Douglas Leung2db3e262014-06-25 16:02:55 -0700580 load = LoadBaseDispBody(r_base, displacement, r_dest, size);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000581
582 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700583 GenMemBarrier(kLoadAny);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000584 }
585
586 return load;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700587}
588
Vladimir Marko455759b2014-05-06 20:49:36 +0100589// FIXME: don't split r_dest into 2 containers.
buzbee2700f7e2014-03-07 09:46:20 -0800590LIR* MipsMir2Lir::StoreBaseDispBody(RegStorage r_base, int displacement,
Douglas Leung2db3e262014-06-25 16:02:55 -0700591 RegStorage r_src, OpSize size) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700592 LIR *res;
593 LIR *store = NULL;
594 LIR *store2 = NULL;
595 MipsOpCode opcode = kMipsNop;
596 bool short_form = IS_SIMM16(displacement);
buzbee091cc402014-03-31 10:14:40 -0700597 bool pair = r_src.IsPair();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700598
599 switch (size) {
buzbee695d13a2014-04-19 13:32:20 -0700600 case k64:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700601 case kDouble:
Douglas Leung2db3e262014-06-25 16:02:55 -0700602 if (!pair) {
603 // Form 64-bit pair
604 r_src = Solo64ToPair64(r_src);
605 pair = 1;
606 }
buzbee091cc402014-03-31 10:14:40 -0700607 if (r_src.IsFloat()) {
Douglas Leung2db3e262014-06-25 16:02:55 -0700608 DCHECK_EQ(r_src.GetLowReg(), r_src.GetHighReg() - 1);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700609 opcode = kMipsFswc1;
Douglas Leung2db3e262014-06-25 16:02:55 -0700610 } else {
611 opcode = kMipsSw;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700612 }
613 short_form = IS_SIMM16_2WORD(displacement);
614 DCHECK_EQ((displacement & 0x3), 0);
615 break;
buzbee695d13a2014-04-19 13:32:20 -0700616 case k32:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700617 case kSingle:
buzbee695d13a2014-04-19 13:32:20 -0700618 case kReference:
Brian Carlstrom7940e442013-07-12 13:46:57 -0700619 opcode = kMipsSw;
buzbee091cc402014-03-31 10:14:40 -0700620 if (r_src.IsFloat()) {
Brian Carlstrom7940e442013-07-12 13:46:57 -0700621 opcode = kMipsFswc1;
buzbee091cc402014-03-31 10:14:40 -0700622 DCHECK(r_src.IsSingle());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700623 }
624 DCHECK_EQ((displacement & 0x3), 0);
625 break;
626 case kUnsignedHalf:
627 case kSignedHalf:
628 opcode = kMipsSh;
629 DCHECK_EQ((displacement & 0x1), 0);
630 break;
631 case kUnsignedByte:
632 case kSignedByte:
633 opcode = kMipsSb;
634 break;
635 default:
buzbee2700f7e2014-03-07 09:46:20 -0800636 LOG(FATAL) << "Bad case in StoreBaseDispBody";
Brian Carlstrom7940e442013-07-12 13:46:57 -0700637 }
638
639 if (short_form) {
640 if (!pair) {
buzbee2700f7e2014-03-07 09:46:20 -0800641 store = res = NewLIR3(opcode, r_src.GetReg(), displacement, r_base.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700642 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700643 store = res = NewLIR3(opcode, r_src.GetLowReg(), displacement + LOWORD_OFFSET, r_base.GetReg());
644 store2 = NewLIR3(opcode, r_src.GetHighReg(), displacement + HIWORD_OFFSET, r_base.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700645 }
646 } else {
buzbee2700f7e2014-03-07 09:46:20 -0800647 RegStorage r_scratch = AllocTemp();
648 res = OpRegRegImm(kOpAdd, r_scratch, r_base, displacement);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700649 if (!pair) {
buzbee2700f7e2014-03-07 09:46:20 -0800650 store = NewLIR3(opcode, r_src.GetReg(), 0, r_scratch.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700651 } else {
Douglas Leung2db3e262014-06-25 16:02:55 -0700652 store = NewLIR3(opcode, r_src.GetLowReg(), LOWORD_OFFSET, r_scratch.GetReg());
653 store2 = NewLIR3(opcode, r_src.GetHighReg(), HIWORD_OFFSET, r_scratch.GetReg());
Brian Carlstrom7940e442013-07-12 13:46:57 -0700654 }
655 FreeTemp(r_scratch);
656 }
657
Vladimir Marko8dea81c2014-06-06 14:50:36 +0100658 if (mem_ref_type_ == ResourceMask::kDalvikReg) {
Ian Rogersb28c1c02014-11-08 11:21:21 -0800659 DCHECK_EQ(r_base, rs_rMIPS_SP);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700660 AnnotateDalvikRegAccess(store, (displacement + (pair ? LOWORD_OFFSET : 0)) >> 2,
661 false /* is_load */, pair /* is64bit */);
662 if (pair) {
663 AnnotateDalvikRegAccess(store2, (displacement + HIWORD_OFFSET) >> 2,
664 false /* is_load */, pair /* is64bit */);
665 }
666 }
667
668 return res;
669}
670
Andreas Gampede686762014-06-24 18:42:06 +0000671LIR* MipsMir2Lir::StoreBaseDisp(RegStorage r_base, int displacement, RegStorage r_src,
Andreas Gampe3c12c512014-06-24 18:46:29 +0000672 OpSize size, VolatileKind is_volatile) {
673 if (is_volatile == kVolatile) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700674 // Ensure that prior accesses become visible to other threads first.
675 GenMemBarrier(kAnyStore);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000676 }
677
Andreas Gampe3c12c512014-06-24 18:46:29 +0000678 LIR* store;
Douglas Leungd9cb8ae2014-07-09 14:28:35 -0700679 if (UNLIKELY(is_volatile == kVolatile && (size == k64 || size == kDouble))) {
680 // Do atomic 64-bit load.
681 store = GenAtomic64Store(r_base, displacement, r_src);
682 } else {
683 // TODO: base this on target.
684 if (size == kWord) {
685 size = k32;
686 }
687 store = StoreBaseDispBody(r_base, displacement, r_src, size);
688 }
Andreas Gampe3c12c512014-06-24 18:46:29 +0000689
690 if (UNLIKELY(is_volatile == kVolatile)) {
Hans Boehm48f5c472014-06-27 14:50:10 -0700691 // Preserve order with respect to any subsequent volatile loads.
692 // We need StoreLoad, but that generally requires the most expensive barrier.
693 GenMemBarrier(kAnyAny);
Andreas Gampe3c12c512014-06-24 18:46:29 +0000694 }
695
696 return store;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700697}
698
buzbee2700f7e2014-03-07 09:46:20 -0800699LIR* MipsMir2Lir::OpMem(OpKind op, RegStorage r_base, int disp) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700700 UNUSED(op, r_base, disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700701 LOG(FATAL) << "Unexpected use of OpMem for MIPS";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700702 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700703}
704
Brian Carlstrom2ce745c2013-07-17 17:44:30 -0700705LIR* MipsMir2Lir::OpCondBranch(ConditionCode cc, LIR* target) {
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700706 UNUSED(cc, target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700707 LOG(FATAL) << "Unexpected use of OpCondBranch for MIPS";
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700708 UNREACHABLE();
Brian Carlstrom7940e442013-07-12 13:46:57 -0700709}
710
Andreas Gampe98430592014-07-27 19:44:50 -0700711LIR* MipsMir2Lir::InvokeTrampoline(OpKind op, RegStorage r_tgt, QuickEntrypointEnum trampoline) {
Nikola Veljkovic2d873b62015-02-20 17:21:15 +0100712 if (IsDirectEntrypoint(trampoline)) {
713 // Reserve argument space on stack (for $a0-$a3) for
714 // entrypoints that directly reference native implementations.
715 // This is not safe in general, as it violates the frame size
716 // of the Quick method, but it is used here only for calling
717 // native functions, outside of the runtime.
718 OpRegImm(kOpSub, rs_rSP, 16);
719 LIR* retVal = OpReg(op, r_tgt);
720 OpRegImm(kOpAdd, rs_rSP, 16);
721 return retVal;
722 }
723
Andreas Gampe98430592014-07-27 19:44:50 -0700724 return OpReg(op, r_tgt);
725}
726
Brian Carlstrom7940e442013-07-12 13:46:57 -0700727} // namespace art