Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 1 | /* |
| 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_x86.h" |
| 18 | #include "dex/quick/mir_to_lir-inl.h" |
| 19 | #include "x86_lir.h" |
| 20 | |
| 21 | namespace art { |
| 22 | |
| 23 | /* This file contains codegen for the X86 ISA */ |
| 24 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 25 | LIR* X86Mir2Lir::OpFpRegCopy(int r_dest, int r_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 26 | int opcode; |
| 27 | /* must be both DOUBLE or both not DOUBLE */ |
| 28 | DCHECK_EQ(X86_DOUBLEREG(r_dest), X86_DOUBLEREG(r_src)); |
| 29 | if (X86_DOUBLEREG(r_dest)) { |
| 30 | opcode = kX86MovsdRR; |
| 31 | } else { |
| 32 | if (X86_SINGLEREG(r_dest)) { |
| 33 | if (X86_SINGLEREG(r_src)) { |
| 34 | opcode = kX86MovssRR; |
| 35 | } else { // Fpr <- Gpr |
| 36 | opcode = kX86MovdxrRR; |
| 37 | } |
| 38 | } else { // Gpr <- Fpr |
| 39 | DCHECK(X86_SINGLEREG(r_src)); |
| 40 | opcode = kX86MovdrxRR; |
| 41 | } |
| 42 | } |
| 43 | DCHECK_NE((EncodingMap[opcode].flags & IS_BINARY_OP), 0ULL); |
| 44 | LIR* res = RawLIR(current_dalvik_offset_, opcode, r_dest, r_src); |
| 45 | if (r_dest == r_src) { |
| 46 | res->flags.is_nop = true; |
| 47 | } |
| 48 | return res; |
| 49 | } |
| 50 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 51 | bool X86Mir2Lir::InexpensiveConstantInt(int32_t value) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 52 | return true; |
| 53 | } |
| 54 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 55 | bool X86Mir2Lir::InexpensiveConstantFloat(int32_t value) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 56 | return false; |
| 57 | } |
| 58 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 59 | bool X86Mir2Lir::InexpensiveConstantLong(int64_t value) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 60 | return true; |
| 61 | } |
| 62 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 63 | bool X86Mir2Lir::InexpensiveConstantDouble(int64_t value) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 64 | return false; // TUNING |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | /* |
| 68 | * Load a immediate using a shortcut if possible; otherwise |
| 69 | * grab from the per-translation literal pool. If target is |
| 70 | * a high register, build constant into a low register and copy. |
| 71 | * |
| 72 | * No additional register clobbering operation performed. Use this version when |
| 73 | * 1) r_dest is freshly returned from AllocTemp or |
| 74 | * 2) The codegen is under fixed register usage |
| 75 | */ |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 76 | LIR* X86Mir2Lir::LoadConstantNoClobber(int r_dest, int value) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 77 | int r_dest_save = r_dest; |
| 78 | if (X86_FPREG(r_dest)) { |
| 79 | if (value == 0) { |
| 80 | return NewLIR2(kX86XorpsRR, r_dest, r_dest); |
| 81 | } |
| 82 | DCHECK(X86_SINGLEREG(r_dest)); |
| 83 | r_dest = AllocTemp(); |
| 84 | } |
| 85 | |
| 86 | LIR *res; |
| 87 | if (value == 0) { |
| 88 | res = NewLIR2(kX86Xor32RR, r_dest, r_dest); |
| 89 | } else { |
| 90 | // Note, there is no byte immediate form of a 32 bit immediate move. |
| 91 | res = NewLIR2(kX86Mov32RI, r_dest, value); |
| 92 | } |
| 93 | |
| 94 | if (X86_FPREG(r_dest_save)) { |
| 95 | NewLIR2(kX86MovdxrRR, r_dest_save, r_dest); |
| 96 | FreeTemp(r_dest); |
| 97 | } |
| 98 | |
| 99 | return res; |
| 100 | } |
| 101 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 102 | LIR* X86Mir2Lir::OpUnconditionalBranch(LIR* target) { |
Brian Carlstrom | df62950 | 2013-07-17 22:39:56 -0700 | [diff] [blame] | 103 | LIR* res = NewLIR1(kX86Jmp8, 0 /* offset to be patched during assembly*/); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 104 | res->target = target; |
| 105 | return res; |
| 106 | } |
| 107 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 108 | LIR* X86Mir2Lir::OpCondBranch(ConditionCode cc, LIR* target) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 109 | LIR* branch = NewLIR2(kX86Jcc8, 0 /* offset to be patched */, |
| 110 | X86ConditionEncoding(cc)); |
| 111 | branch->target = target; |
| 112 | return branch; |
| 113 | } |
| 114 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 115 | LIR* X86Mir2Lir::OpReg(OpKind op, int r_dest_src) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 116 | X86OpCode opcode = kX86Bkpt; |
| 117 | switch (op) { |
| 118 | case kOpNeg: opcode = kX86Neg32R; break; |
| 119 | case kOpNot: opcode = kX86Not32R; break; |
Vladimir Marko | a8b4caf | 2013-10-24 15:08:57 +0100 | [diff] [blame] | 120 | case kOpRev: opcode = kX86Bswap32R; break; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 121 | case kOpBlx: opcode = kX86CallR; break; |
| 122 | default: |
| 123 | LOG(FATAL) << "Bad case in OpReg " << op; |
| 124 | } |
| 125 | return NewLIR1(opcode, r_dest_src); |
| 126 | } |
| 127 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 128 | LIR* X86Mir2Lir::OpRegImm(OpKind op, int r_dest_src1, int value) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 129 | X86OpCode opcode = kX86Bkpt; |
| 130 | bool byte_imm = IS_SIMM8(value); |
| 131 | DCHECK(!X86_FPREG(r_dest_src1)); |
| 132 | switch (op) { |
| 133 | case kOpLsl: opcode = kX86Sal32RI; break; |
| 134 | case kOpLsr: opcode = kX86Shr32RI; break; |
| 135 | case kOpAsr: opcode = kX86Sar32RI; break; |
| 136 | case kOpAdd: opcode = byte_imm ? kX86Add32RI8 : kX86Add32RI; break; |
| 137 | case kOpOr: opcode = byte_imm ? kX86Or32RI8 : kX86Or32RI; break; |
| 138 | case kOpAdc: opcode = byte_imm ? kX86Adc32RI8 : kX86Adc32RI; break; |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 139 | // case kOpSbb: opcode = kX86Sbb32RI; break; |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 140 | case kOpAnd: opcode = byte_imm ? kX86And32RI8 : kX86And32RI; break; |
| 141 | case kOpSub: opcode = byte_imm ? kX86Sub32RI8 : kX86Sub32RI; break; |
| 142 | case kOpXor: opcode = byte_imm ? kX86Xor32RI8 : kX86Xor32RI; break; |
| 143 | case kOpCmp: opcode = byte_imm ? kX86Cmp32RI8 : kX86Cmp32RI; break; |
| 144 | case kOpMov: return LoadConstantNoClobber(r_dest_src1, value); |
| 145 | case kOpMul: |
| 146 | opcode = byte_imm ? kX86Imul32RRI8 : kX86Imul32RRI; |
| 147 | return NewLIR3(opcode, r_dest_src1, r_dest_src1, value); |
| 148 | default: |
| 149 | LOG(FATAL) << "Bad case in OpRegImm " << op; |
| 150 | } |
| 151 | return NewLIR2(opcode, r_dest_src1, value); |
| 152 | } |
| 153 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 154 | LIR* X86Mir2Lir::OpRegReg(OpKind op, int r_dest_src1, int r_src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 155 | X86OpCode opcode = kX86Nop; |
| 156 | bool src2_must_be_cx = false; |
| 157 | switch (op) { |
| 158 | // X86 unary opcodes |
| 159 | case kOpMvn: |
| 160 | OpRegCopy(r_dest_src1, r_src2); |
| 161 | return OpReg(kOpNot, r_dest_src1); |
| 162 | case kOpNeg: |
| 163 | OpRegCopy(r_dest_src1, r_src2); |
| 164 | return OpReg(kOpNeg, r_dest_src1); |
Vladimir Marko | a8b4caf | 2013-10-24 15:08:57 +0100 | [diff] [blame] | 165 | case kOpRev: |
| 166 | OpRegCopy(r_dest_src1, r_src2); |
| 167 | return OpReg(kOpRev, r_dest_src1); |
| 168 | case kOpRevsh: |
| 169 | OpRegCopy(r_dest_src1, r_src2); |
| 170 | OpReg(kOpRev, r_dest_src1); |
| 171 | return OpRegImm(kOpAsr, r_dest_src1, 16); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 172 | // X86 binary opcodes |
| 173 | case kOpSub: opcode = kX86Sub32RR; break; |
| 174 | case kOpSbc: opcode = kX86Sbb32RR; break; |
| 175 | case kOpLsl: opcode = kX86Sal32RC; src2_must_be_cx = true; break; |
| 176 | case kOpLsr: opcode = kX86Shr32RC; src2_must_be_cx = true; break; |
| 177 | case kOpAsr: opcode = kX86Sar32RC; src2_must_be_cx = true; break; |
| 178 | case kOpMov: opcode = kX86Mov32RR; break; |
| 179 | case kOpCmp: opcode = kX86Cmp32RR; break; |
| 180 | case kOpAdd: opcode = kX86Add32RR; break; |
| 181 | case kOpAdc: opcode = kX86Adc32RR; break; |
| 182 | case kOpAnd: opcode = kX86And32RR; break; |
| 183 | case kOpOr: opcode = kX86Or32RR; break; |
| 184 | case kOpXor: opcode = kX86Xor32RR; break; |
| 185 | case kOp2Byte: |
| 186 | // Use shifts instead of a byte operand if the source can't be byte accessed. |
| 187 | if (r_src2 >= 4) { |
| 188 | NewLIR2(kX86Mov32RR, r_dest_src1, r_src2); |
| 189 | NewLIR2(kX86Sal32RI, r_dest_src1, 24); |
| 190 | return NewLIR2(kX86Sar32RI, r_dest_src1, 24); |
| 191 | } else { |
| 192 | opcode = kX86Movsx8RR; |
| 193 | } |
| 194 | break; |
| 195 | case kOp2Short: opcode = kX86Movsx16RR; break; |
| 196 | case kOp2Char: opcode = kX86Movzx16RR; break; |
| 197 | case kOpMul: opcode = kX86Imul32RR; break; |
| 198 | default: |
| 199 | LOG(FATAL) << "Bad case in OpRegReg " << op; |
| 200 | break; |
| 201 | } |
| 202 | CHECK(!src2_must_be_cx || r_src2 == rCX); |
| 203 | return NewLIR2(opcode, r_dest_src1, r_src2); |
| 204 | } |
| 205 | |
Razvan A Lupusoru | bd288c2 | 2013-12-20 17:27:23 -0800 | [diff] [blame^] | 206 | LIR* X86Mir2Lir::OpCondRegReg(OpKind op, ConditionCode cc, int r_dest, int r_src) { |
| 207 | // The only conditional reg to reg operation supported is Cmov |
| 208 | DCHECK_EQ(op, kOpCmov); |
| 209 | return NewLIR3(kX86Cmov32RRC, r_dest, r_src, X86ConditionEncoding(cc)); |
| 210 | } |
| 211 | |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 212 | LIR* X86Mir2Lir::OpRegMem(OpKind op, int r_dest, int rBase, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 213 | int offset) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 214 | X86OpCode opcode = kX86Nop; |
| 215 | switch (op) { |
| 216 | // X86 binary opcodes |
| 217 | case kOpSub: opcode = kX86Sub32RM; break; |
| 218 | case kOpMov: opcode = kX86Mov32RM; break; |
| 219 | case kOpCmp: opcode = kX86Cmp32RM; break; |
| 220 | case kOpAdd: opcode = kX86Add32RM; break; |
| 221 | case kOpAnd: opcode = kX86And32RM; break; |
| 222 | case kOpOr: opcode = kX86Or32RM; break; |
| 223 | case kOpXor: opcode = kX86Xor32RM; break; |
| 224 | case kOp2Byte: opcode = kX86Movsx8RM; break; |
| 225 | case kOp2Short: opcode = kX86Movsx16RM; break; |
| 226 | case kOp2Char: opcode = kX86Movzx16RM; break; |
| 227 | case kOpMul: |
| 228 | default: |
| 229 | LOG(FATAL) << "Bad case in OpRegMem " << op; |
| 230 | break; |
| 231 | } |
| 232 | return NewLIR3(opcode, r_dest, rBase, offset); |
| 233 | } |
| 234 | |
| 235 | LIR* X86Mir2Lir::OpRegRegReg(OpKind op, int r_dest, int r_src1, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 236 | int r_src2) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 237 | if (r_dest != r_src1 && r_dest != r_src2) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 238 | if (op == kOpAdd) { // lea special case, except can't encode rbp as base |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 239 | if (r_src1 == r_src2) { |
| 240 | OpRegCopy(r_dest, r_src1); |
| 241 | return OpRegImm(kOpLsl, r_dest, 1); |
| 242 | } else if (r_src1 != rBP) { |
| 243 | return NewLIR5(kX86Lea32RA, r_dest, r_src1 /* base */, |
| 244 | r_src2 /* index */, 0 /* scale */, 0 /* disp */); |
| 245 | } else { |
| 246 | return NewLIR5(kX86Lea32RA, r_dest, r_src2 /* base */, |
| 247 | r_src1 /* index */, 0 /* scale */, 0 /* disp */); |
| 248 | } |
| 249 | } else { |
| 250 | OpRegCopy(r_dest, r_src1); |
| 251 | return OpRegReg(op, r_dest, r_src2); |
| 252 | } |
| 253 | } else if (r_dest == r_src1) { |
| 254 | return OpRegReg(op, r_dest, r_src2); |
| 255 | } else { // r_dest == r_src2 |
| 256 | switch (op) { |
| 257 | case kOpSub: // non-commutative |
| 258 | OpReg(kOpNeg, r_dest); |
| 259 | op = kOpAdd; |
| 260 | break; |
| 261 | case kOpSbc: |
| 262 | case kOpLsl: case kOpLsr: case kOpAsr: case kOpRor: { |
| 263 | int t_reg = AllocTemp(); |
| 264 | OpRegCopy(t_reg, r_src1); |
| 265 | OpRegReg(op, t_reg, r_src2); |
| 266 | LIR* res = OpRegCopy(r_dest, t_reg); |
| 267 | FreeTemp(t_reg); |
| 268 | return res; |
| 269 | } |
| 270 | case kOpAdd: // commutative |
| 271 | case kOpOr: |
| 272 | case kOpAdc: |
| 273 | case kOpAnd: |
| 274 | case kOpXor: |
| 275 | break; |
| 276 | default: |
| 277 | LOG(FATAL) << "Bad case in OpRegRegReg " << op; |
| 278 | } |
| 279 | return OpRegReg(op, r_dest, r_src1); |
| 280 | } |
| 281 | } |
| 282 | |
| 283 | LIR* X86Mir2Lir::OpRegRegImm(OpKind op, int r_dest, int r_src, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 284 | int value) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 285 | if (op == kOpMul) { |
| 286 | X86OpCode opcode = IS_SIMM8(value) ? kX86Imul32RRI8 : kX86Imul32RRI; |
| 287 | return NewLIR3(opcode, r_dest, r_src, value); |
| 288 | } else if (op == kOpAnd) { |
| 289 | if (value == 0xFF && r_src < 4) { |
| 290 | return NewLIR2(kX86Movzx8RR, r_dest, r_src); |
| 291 | } else if (value == 0xFFFF) { |
| 292 | return NewLIR2(kX86Movzx16RR, r_dest, r_src); |
| 293 | } |
| 294 | } |
| 295 | if (r_dest != r_src) { |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 296 | if (false && op == kOpLsl && value >= 0 && value <= 3) { // lea shift special case |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 297 | // TODO: fix bug in LEA encoding when disp == 0 |
| 298 | return NewLIR5(kX86Lea32RA, r_dest, r5sib_no_base /* base */, |
| 299 | r_src /* index */, value /* scale */, 0 /* disp */); |
Brian Carlstrom | 7934ac2 | 2013-07-26 10:54:15 -0700 | [diff] [blame] | 300 | } else if (op == kOpAdd) { // lea add special case |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 301 | return NewLIR5(kX86Lea32RA, r_dest, r_src /* base */, |
| 302 | r4sib_no_index /* index */, 0 /* scale */, value /* disp */); |
| 303 | } |
| 304 | OpRegCopy(r_dest, r_src); |
| 305 | } |
| 306 | return OpRegImm(op, r_dest, value); |
| 307 | } |
| 308 | |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 309 | LIR* X86Mir2Lir::OpThreadMem(OpKind op, ThreadOffset thread_offset) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 310 | X86OpCode opcode = kX86Bkpt; |
| 311 | switch (op) { |
| 312 | case kOpBlx: opcode = kX86CallT; break; |
| 313 | default: |
| 314 | LOG(FATAL) << "Bad opcode: " << op; |
| 315 | break; |
| 316 | } |
Ian Rogers | 468532e | 2013-08-05 10:56:33 -0700 | [diff] [blame] | 317 | return NewLIR1(opcode, thread_offset.Int32Value()); |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 318 | } |
| 319 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 320 | LIR* X86Mir2Lir::OpMem(OpKind op, int rBase, int disp) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 321 | X86OpCode opcode = kX86Bkpt; |
| 322 | switch (op) { |
| 323 | case kOpBlx: opcode = kX86CallM; break; |
| 324 | default: |
| 325 | LOG(FATAL) << "Bad opcode: " << op; |
| 326 | break; |
| 327 | } |
| 328 | return NewLIR2(opcode, rBase, disp); |
| 329 | } |
| 330 | |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 331 | LIR* X86Mir2Lir::LoadConstantWide(int r_dest_lo, int r_dest_hi, int64_t value) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 332 | int32_t val_lo = Low32Bits(value); |
| 333 | int32_t val_hi = High32Bits(value); |
| 334 | LIR *res; |
| 335 | if (X86_FPREG(r_dest_lo)) { |
| 336 | DCHECK(X86_FPREG(r_dest_hi)); // ignore r_dest_hi |
| 337 | if (value == 0) { |
| 338 | return NewLIR2(kX86XorpsRR, r_dest_lo, r_dest_lo); |
| 339 | } else { |
| 340 | if (val_lo == 0) { |
| 341 | res = NewLIR2(kX86XorpsRR, r_dest_lo, r_dest_lo); |
| 342 | } else { |
| 343 | res = LoadConstantNoClobber(r_dest_lo, val_lo); |
| 344 | } |
| 345 | if (val_hi != 0) { |
| 346 | LoadConstantNoClobber(r_dest_hi, val_hi); |
| 347 | NewLIR2(kX86PsllqRI, r_dest_hi, 32); |
| 348 | NewLIR2(kX86OrpsRR, r_dest_lo, r_dest_hi); |
| 349 | } |
| 350 | } |
| 351 | } else { |
| 352 | res = LoadConstantNoClobber(r_dest_lo, val_lo); |
| 353 | LoadConstantNoClobber(r_dest_hi, val_hi); |
| 354 | } |
| 355 | return res; |
| 356 | } |
| 357 | |
| 358 | LIR* X86Mir2Lir::LoadBaseIndexedDisp(int rBase, int r_index, int scale, |
| 359 | int displacement, int r_dest, int r_dest_hi, OpSize size, |
| 360 | int s_reg) { |
| 361 | LIR *load = NULL; |
| 362 | LIR *load2 = NULL; |
| 363 | bool is_array = r_index != INVALID_REG; |
| 364 | bool pair = false; |
| 365 | bool is64bit = false; |
| 366 | X86OpCode opcode = kX86Nop; |
| 367 | switch (size) { |
| 368 | case kLong: |
| 369 | case kDouble: |
| 370 | is64bit = true; |
| 371 | if (X86_FPREG(r_dest)) { |
| 372 | opcode = is_array ? kX86MovsdRA : kX86MovsdRM; |
| 373 | if (X86_SINGLEREG(r_dest)) { |
| 374 | DCHECK(X86_FPREG(r_dest_hi)); |
| 375 | DCHECK_EQ(r_dest, (r_dest_hi - 1)); |
| 376 | r_dest = S2d(r_dest, r_dest_hi); |
| 377 | } |
| 378 | r_dest_hi = r_dest + 1; |
| 379 | } else { |
| 380 | pair = true; |
| 381 | opcode = is_array ? kX86Mov32RA : kX86Mov32RM; |
| 382 | } |
| 383 | // TODO: double store is to unaligned address |
| 384 | DCHECK_EQ((displacement & 0x3), 0); |
| 385 | break; |
| 386 | case kWord: |
| 387 | case kSingle: |
| 388 | opcode = is_array ? kX86Mov32RA : kX86Mov32RM; |
| 389 | if (X86_FPREG(r_dest)) { |
| 390 | opcode = is_array ? kX86MovssRA : kX86MovssRM; |
| 391 | DCHECK(X86_SINGLEREG(r_dest)); |
| 392 | } |
| 393 | DCHECK_EQ((displacement & 0x3), 0); |
| 394 | break; |
| 395 | case kUnsignedHalf: |
| 396 | opcode = is_array ? kX86Movzx16RA : kX86Movzx16RM; |
| 397 | DCHECK_EQ((displacement & 0x1), 0); |
| 398 | break; |
| 399 | case kSignedHalf: |
| 400 | opcode = is_array ? kX86Movsx16RA : kX86Movsx16RM; |
| 401 | DCHECK_EQ((displacement & 0x1), 0); |
| 402 | break; |
| 403 | case kUnsignedByte: |
| 404 | opcode = is_array ? kX86Movzx8RA : kX86Movzx8RM; |
| 405 | break; |
| 406 | case kSignedByte: |
| 407 | opcode = is_array ? kX86Movsx8RA : kX86Movsx8RM; |
| 408 | break; |
| 409 | default: |
| 410 | LOG(FATAL) << "Bad case in LoadBaseIndexedDispBody"; |
| 411 | } |
| 412 | |
| 413 | if (!is_array) { |
| 414 | if (!pair) { |
| 415 | load = NewLIR3(opcode, r_dest, rBase, displacement + LOWORD_OFFSET); |
| 416 | } else { |
| 417 | if (rBase == r_dest) { |
| 418 | load2 = NewLIR3(opcode, r_dest_hi, rBase, |
| 419 | displacement + HIWORD_OFFSET); |
| 420 | load = NewLIR3(opcode, r_dest, rBase, displacement + LOWORD_OFFSET); |
| 421 | } else { |
| 422 | load = NewLIR3(opcode, r_dest, rBase, displacement + LOWORD_OFFSET); |
| 423 | load2 = NewLIR3(opcode, r_dest_hi, rBase, |
| 424 | displacement + HIWORD_OFFSET); |
| 425 | } |
| 426 | } |
| 427 | if (rBase == rX86_SP) { |
| 428 | AnnotateDalvikRegAccess(load, (displacement + (pair ? LOWORD_OFFSET : 0)) >> 2, |
| 429 | true /* is_load */, is64bit); |
| 430 | if (pair) { |
| 431 | AnnotateDalvikRegAccess(load2, (displacement + HIWORD_OFFSET) >> 2, |
| 432 | true /* is_load */, is64bit); |
| 433 | } |
| 434 | } |
| 435 | } else { |
| 436 | if (!pair) { |
| 437 | load = NewLIR5(opcode, r_dest, rBase, r_index, scale, |
| 438 | displacement + LOWORD_OFFSET); |
| 439 | } else { |
| 440 | if (rBase == r_dest) { |
| 441 | load2 = NewLIR5(opcode, r_dest_hi, rBase, r_index, scale, |
| 442 | displacement + HIWORD_OFFSET); |
| 443 | load = NewLIR5(opcode, r_dest, rBase, r_index, scale, |
| 444 | displacement + LOWORD_OFFSET); |
| 445 | } else { |
| 446 | load = NewLIR5(opcode, r_dest, rBase, r_index, scale, |
| 447 | displacement + LOWORD_OFFSET); |
| 448 | load2 = NewLIR5(opcode, r_dest_hi, rBase, r_index, scale, |
| 449 | displacement + HIWORD_OFFSET); |
| 450 | } |
| 451 | } |
| 452 | } |
| 453 | |
| 454 | return load; |
| 455 | } |
| 456 | |
| 457 | /* Load value from base + scaled index. */ |
| 458 | LIR* X86Mir2Lir::LoadBaseIndexed(int rBase, |
| 459 | int r_index, int r_dest, int scale, OpSize size) { |
| 460 | return LoadBaseIndexedDisp(rBase, r_index, scale, 0, |
| 461 | r_dest, INVALID_REG, size, INVALID_SREG); |
| 462 | } |
| 463 | |
| 464 | LIR* X86Mir2Lir::LoadBaseDisp(int rBase, int displacement, |
| 465 | int r_dest, OpSize size, int s_reg) { |
| 466 | return LoadBaseIndexedDisp(rBase, INVALID_REG, 0, displacement, |
| 467 | r_dest, INVALID_REG, size, s_reg); |
| 468 | } |
| 469 | |
| 470 | LIR* X86Mir2Lir::LoadBaseDispWide(int rBase, int displacement, |
| 471 | int r_dest_lo, int r_dest_hi, int s_reg) { |
| 472 | return LoadBaseIndexedDisp(rBase, INVALID_REG, 0, displacement, |
| 473 | r_dest_lo, r_dest_hi, kLong, s_reg); |
| 474 | } |
| 475 | |
| 476 | LIR* X86Mir2Lir::StoreBaseIndexedDisp(int rBase, int r_index, int scale, |
| 477 | int displacement, int r_src, int r_src_hi, OpSize size, |
| 478 | int s_reg) { |
| 479 | LIR *store = NULL; |
| 480 | LIR *store2 = NULL; |
| 481 | bool is_array = r_index != INVALID_REG; |
| 482 | bool pair = false; |
| 483 | bool is64bit = false; |
| 484 | X86OpCode opcode = kX86Nop; |
| 485 | switch (size) { |
| 486 | case kLong: |
| 487 | case kDouble: |
| 488 | is64bit = true; |
| 489 | if (X86_FPREG(r_src)) { |
| 490 | opcode = is_array ? kX86MovsdAR : kX86MovsdMR; |
| 491 | if (X86_SINGLEREG(r_src)) { |
| 492 | DCHECK(X86_FPREG(r_src_hi)); |
| 493 | DCHECK_EQ(r_src, (r_src_hi - 1)); |
| 494 | r_src = S2d(r_src, r_src_hi); |
| 495 | } |
| 496 | r_src_hi = r_src + 1; |
| 497 | } else { |
| 498 | pair = true; |
| 499 | opcode = is_array ? kX86Mov32AR : kX86Mov32MR; |
| 500 | } |
| 501 | // TODO: double store is to unaligned address |
| 502 | DCHECK_EQ((displacement & 0x3), 0); |
| 503 | break; |
| 504 | case kWord: |
| 505 | case kSingle: |
| 506 | opcode = is_array ? kX86Mov32AR : kX86Mov32MR; |
| 507 | if (X86_FPREG(r_src)) { |
| 508 | opcode = is_array ? kX86MovssAR : kX86MovssMR; |
| 509 | DCHECK(X86_SINGLEREG(r_src)); |
| 510 | } |
| 511 | DCHECK_EQ((displacement & 0x3), 0); |
| 512 | break; |
| 513 | case kUnsignedHalf: |
| 514 | case kSignedHalf: |
| 515 | opcode = is_array ? kX86Mov16AR : kX86Mov16MR; |
| 516 | DCHECK_EQ((displacement & 0x1), 0); |
| 517 | break; |
| 518 | case kUnsignedByte: |
| 519 | case kSignedByte: |
| 520 | opcode = is_array ? kX86Mov8AR : kX86Mov8MR; |
| 521 | break; |
| 522 | default: |
| 523 | LOG(FATAL) << "Bad case in LoadBaseIndexedDispBody"; |
| 524 | } |
| 525 | |
| 526 | if (!is_array) { |
| 527 | if (!pair) { |
| 528 | store = NewLIR3(opcode, rBase, displacement + LOWORD_OFFSET, r_src); |
| 529 | } else { |
| 530 | store = NewLIR3(opcode, rBase, displacement + LOWORD_OFFSET, r_src); |
| 531 | store2 = NewLIR3(opcode, rBase, displacement + HIWORD_OFFSET, r_src_hi); |
| 532 | } |
| 533 | if (rBase == rX86_SP) { |
| 534 | AnnotateDalvikRegAccess(store, (displacement + (pair ? LOWORD_OFFSET : 0)) >> 2, |
| 535 | false /* is_load */, is64bit); |
| 536 | if (pair) { |
| 537 | AnnotateDalvikRegAccess(store2, (displacement + HIWORD_OFFSET) >> 2, |
| 538 | false /* is_load */, is64bit); |
| 539 | } |
| 540 | } |
| 541 | } else { |
| 542 | if (!pair) { |
| 543 | store = NewLIR5(opcode, rBase, r_index, scale, |
| 544 | displacement + LOWORD_OFFSET, r_src); |
| 545 | } else { |
| 546 | store = NewLIR5(opcode, rBase, r_index, scale, |
| 547 | displacement + LOWORD_OFFSET, r_src); |
| 548 | store2 = NewLIR5(opcode, rBase, r_index, scale, |
| 549 | displacement + HIWORD_OFFSET, r_src_hi); |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | return store; |
| 554 | } |
| 555 | |
| 556 | /* store value base base + scaled index. */ |
| 557 | LIR* X86Mir2Lir::StoreBaseIndexed(int rBase, int r_index, int r_src, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 558 | int scale, OpSize size) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 559 | return StoreBaseIndexedDisp(rBase, r_index, scale, 0, |
| 560 | r_src, INVALID_REG, size, INVALID_SREG); |
| 561 | } |
| 562 | |
| 563 | LIR* X86Mir2Lir::StoreBaseDisp(int rBase, int displacement, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 564 | int r_src, OpSize size) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 565 | return StoreBaseIndexedDisp(rBase, INVALID_REG, 0, |
| 566 | displacement, r_src, INVALID_REG, size, |
| 567 | INVALID_SREG); |
| 568 | } |
| 569 | |
| 570 | LIR* X86Mir2Lir::StoreBaseDispWide(int rBase, int displacement, |
Brian Carlstrom | 2ce745c | 2013-07-17 17:44:30 -0700 | [diff] [blame] | 571 | int r_src_lo, int r_src_hi) { |
Brian Carlstrom | 7940e44 | 2013-07-12 13:46:57 -0700 | [diff] [blame] | 572 | return StoreBaseIndexedDisp(rBase, INVALID_REG, 0, displacement, |
| 573 | r_src_lo, r_src_hi, kLong, INVALID_SREG); |
| 574 | } |
| 575 | |
| 576 | } // namespace art |