blob: a03e5f2e04f02b9f47909f4950f486a80bfe72b5 [file] [log] [blame]
Brian Carlstrom7940e442013-07-12 13:46:57 -07001/*
2 * Copyright (C) 2011 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 Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_COMPILER_DEX_QUICK_X86_CODEGEN_X86_H_
18#define ART_COMPILER_DEX_QUICK_X86_CODEGEN_X86_H_
Brian Carlstrom7940e442013-07-12 13:46:57 -070019
20#include "dex/compiler_internals.h"
21#include "x86_lir.h"
22
23namespace art {
24
Ian Rogerse2143c02014-03-28 08:47:16 -070025class X86Mir2Lir FINAL : public Mir2Lir {
Brian Carlstrom7940e442013-07-12 13:46:57 -070026 public:
Brian Carlstrom7940e442013-07-12 13:46:57 -070027 X86Mir2Lir(CompilationUnit* cu, MIRGraph* mir_graph, ArenaAllocator* arena);
28
29 // Required for target - codegen helpers.
buzbee11b63d12013-08-27 07:34:17 -070030 bool SmallLiteralDivRem(Instruction::Code dalvik_opcode, bool is_div, RegLocation rl_src,
buzbee2700f7e2014-03-07 09:46:20 -080031 RegLocation rl_dest, int lit);
Ian Rogerse2143c02014-03-28 08:47:16 -070032 bool EasyMultiply(RegLocation rl_src, RegLocation rl_dest, int lit) OVERRIDE;
Dave Allisonb373e092014-02-20 16:06:36 -080033 LIR* CheckSuspendUsingLoad() OVERRIDE;
Ian Rogersdd7624d2014-03-14 17:43:00 -070034 RegStorage LoadHelper(ThreadOffset<4> offset);
Vladimir Marko3bf7c602014-05-07 14:55:43 +010035 LIR* LoadBaseDisp(RegStorage r_base, int displacement, RegStorage r_dest,
36 OpSize size) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -080037 LIR* LoadBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_dest, int scale,
Vladimir Marko3bf7c602014-05-07 14:55:43 +010038 OpSize size) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -080039 LIR* LoadBaseIndexedDisp(RegStorage r_base, RegStorage r_index, int scale, int displacement,
Vladimir Marko3bf7c602014-05-07 14:55:43 +010040 RegStorage r_dest, OpSize size) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -080041 LIR* LoadConstantNoClobber(RegStorage r_dest, int value);
42 LIR* LoadConstantWide(RegStorage r_dest, int64_t value);
Vladimir Marko3bf7c602014-05-07 14:55:43 +010043 LIR* StoreBaseDisp(RegStorage r_base, int displacement, RegStorage r_src,
44 OpSize size) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -080045 LIR* StoreBaseIndexed(RegStorage r_base, RegStorage r_index, RegStorage r_src, int scale,
Vladimir Marko3bf7c602014-05-07 14:55:43 +010046 OpSize size) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -080047 LIR* StoreBaseIndexedDisp(RegStorage r_base, RegStorage r_index, int scale, int displacement,
Vladimir Marko3bf7c602014-05-07 14:55:43 +010048 RegStorage r_src, OpSize size) OVERRIDE;
buzbee2700f7e2014-03-07 09:46:20 -080049 void MarkGCCard(RegStorage val_reg, RegStorage tgt_addr_reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070050
51 // Required for target - register utilities.
buzbee2700f7e2014-03-07 09:46:20 -080052 RegStorage AllocTypedTemp(bool fp_hint, int reg_class);
Bill Buzbee00e1ec62014-02-27 23:44:13 +000053 RegStorage AllocTypedTempWide(bool fp_hint, int reg_class);
buzbee2700f7e2014-03-07 09:46:20 -080054 RegStorage TargetReg(SpecialTargetRegister reg);
55 RegStorage GetArgMappingToPhysicalReg(int arg_num);
Brian Carlstrom7940e442013-07-12 13:46:57 -070056 RegLocation GetReturnAlt();
57 RegLocation GetReturnWideAlt();
58 RegLocation LocCReturn();
59 RegLocation LocCReturnDouble();
60 RegLocation LocCReturnFloat();
61 RegLocation LocCReturnWide();
buzbee091cc402014-03-31 10:14:40 -070062 uint64_t GetRegMaskCommon(RegStorage reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070063 void AdjustSpillMask();
Vladimir Marko31c2aac2013-12-09 16:31:19 +000064 void ClobberCallerSave();
Brian Carlstrom7940e442013-07-12 13:46:57 -070065 void FreeCallTemps();
66 void FreeRegLocTemps(RegLocation rl_keep, RegLocation rl_free);
67 void LockCallTemps();
buzbee091cc402014-03-31 10:14:40 -070068 void MarkPreservedSingle(int v_reg, RegStorage reg);
69 void MarkPreservedDouble(int v_reg, RegStorage reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -070070 void CompilerInitializeRegAlloc();
71
72 // Required for target - miscellaneous.
buzbeeb48819d2013-09-14 16:15:25 -070073 void AssembleLIR();
74 int AssignInsnOffsets();
75 void AssignOffsets();
buzbee0d829482013-10-11 15:24:55 -070076 AssemblerStatus AssembleInstructions(CodeOffset start_addr);
Brian Carlstrom7940e442013-07-12 13:46:57 -070077 void DumpResourceMask(LIR* lir, uint64_t mask, const char* prefix);
buzbeeb48819d2013-09-14 16:15:25 -070078 void SetupTargetResourceMasks(LIR* lir, uint64_t flags);
Brian Carlstrom7940e442013-07-12 13:46:57 -070079 const char* GetTargetInstFmt(int opcode);
80 const char* GetTargetInstName(int opcode);
81 std::string BuildInsnString(const char* fmt, LIR* lir, unsigned char* base_addr);
82 uint64_t GetPCUseDefEncoding();
83 uint64_t GetTargetInstFlags(int opcode);
84 int GetInsnSize(LIR* lir);
85 bool IsUnconditionalBranch(LIR* lir);
86
87 // Required for target - Dalvik-level generators.
buzbee2700f7e2014-03-07 09:46:20 -080088 void GenArithImmOpLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
89 RegLocation rl_src2);
90 void GenArrayGet(int opt_flags, OpSize size, RegLocation rl_array, RegLocation rl_index,
91 RegLocation rl_dest, int scale);
Brian Carlstrom7940e442013-07-12 13:46:57 -070092 void GenArrayPut(int opt_flags, OpSize size, RegLocation rl_array,
Ian Rogersa9a82542013-10-04 11:17:26 -070093 RegLocation rl_index, RegLocation rl_src, int scale, bool card_mark);
Brian Carlstrom7940e442013-07-12 13:46:57 -070094 void GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
Ian Rogersa9a82542013-10-04 11:17:26 -070095 RegLocation rl_src1, RegLocation rl_shift);
buzbee2700f7e2014-03-07 09:46:20 -080096 void GenMulLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
97 RegLocation rl_src2);
98 void GenAddLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
99 RegLocation rl_src2);
100 void GenAndLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
101 RegLocation rl_src2);
102 void GenArithOpDouble(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
Brian Carlstrom7940e442013-07-12 13:46:57 -0700103 RegLocation rl_src2);
buzbee2700f7e2014-03-07 09:46:20 -0800104 void GenArithOpFloat(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
105 RegLocation rl_src2);
106 void GenCmpFP(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
107 RegLocation rl_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700108 void GenConversion(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src);
Vladimir Marko1c282e22013-11-21 14:49:47 +0000109 bool GenInlinedCas(CallInfo* info, bool is_long, bool is_object);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700110 bool GenInlinedMinMaxInt(CallInfo* info, bool is_min);
111 bool GenInlinedSqrt(CallInfo* info);
Vladimir Markoe508a202013-11-04 15:24:22 +0000112 bool GenInlinedPeek(CallInfo* info, OpSize size);
113 bool GenInlinedPoke(CallInfo* info, OpSize size);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700114 void GenNegLong(RegLocation rl_dest, RegLocation rl_src);
buzbee2700f7e2014-03-07 09:46:20 -0800115 void GenOrLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
116 RegLocation rl_src2);
117 void GenSubLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
118 RegLocation rl_src2);
119 void GenXorLong(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_src1,
120 RegLocation rl_src2);
buzbee2700f7e2014-03-07 09:46:20 -0800121 // TODO: collapse reg_lo, reg_hi
122 RegLocation GenDivRem(RegLocation rl_dest, RegStorage reg_lo, RegStorage reg_hi, bool is_div);
123 RegLocation GenDivRemLit(RegLocation rl_dest, RegStorage reg_lo, int lit, bool is_div);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700124 void GenCmpLong(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2);
Mingyao Yange643a172014-04-08 11:02:52 -0700125 void GenDivZeroCheckWide(RegStorage reg);
Mingyao Yang80365d92014-04-18 12:10:58 -0700126 void GenArrayBoundsCheck(RegStorage index, RegStorage array_base, int32_t len_offset);
127 void GenArrayBoundsCheck(int32_t index, RegStorage array_base, int32_t len_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700128 void GenEntrySequence(RegLocation* ArgLocs, RegLocation rl_method);
129 void GenExitSequence();
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800130 void GenSpecialExitSequence();
buzbee0d829482013-10-11 15:24:55 -0700131 void GenFillArrayData(DexOffset table_offset, RegLocation rl_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700132 void GenFusedFPCmpBranch(BasicBlock* bb, MIR* mir, bool gt_bias, bool is_double);
133 void GenFusedLongCmpBranch(BasicBlock* bb, MIR* mir);
134 void GenSelect(BasicBlock* bb, MIR* mir);
135 void GenMemBarrier(MemBarrierKind barrier_kind);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700136 void GenMoveException(RegLocation rl_dest);
buzbee2700f7e2014-03-07 09:46:20 -0800137 void GenMultiplyByTwoBitMultiplier(RegLocation rl_src, RegLocation rl_result, int lit,
138 int first_bit, int second_bit);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700139 void GenNegDouble(RegLocation rl_dest, RegLocation rl_src);
140 void GenNegFloat(RegLocation rl_dest, RegLocation rl_src);
buzbee0d829482013-10-11 15:24:55 -0700141 void GenPackedSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src);
142 void GenSparseSwitch(MIR* mir, DexOffset table_offset, RegLocation rl_src);
Razvan A Lupusoru3bc01742014-02-06 13:18:43 -0800143
Mark Mendelle02d48f2014-01-15 11:19:23 -0800144 /*
145 * @brief Generate a two address long operation with a constant value
146 * @param rl_dest location of result
147 * @param rl_src constant source operand
148 * @param op Opcode to be generated
149 */
150 void GenLongImm(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op);
151 /*
152 * @brief Generate a three address long operation with a constant value
153 * @param rl_dest location of result
154 * @param rl_src1 source operand
155 * @param rl_src2 constant source operand
156 * @param op Opcode to be generated
157 */
buzbee2700f7e2014-03-07 09:46:20 -0800158 void GenLongLongImm(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
159 Instruction::Code op);
Mark Mendelle02d48f2014-01-15 11:19:23 -0800160
161 /**
162 * @brief Generate a long arithmetic operation.
163 * @param rl_dest The destination.
164 * @param rl_src1 First operand.
165 * @param rl_src2 Second operand.
166 * @param op The DEX opcode for the operation.
167 * @param is_commutative The sources can be swapped if needed.
168 */
buzbee2700f7e2014-03-07 09:46:20 -0800169 void GenLongArith(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
170 Instruction::Code op, bool is_commutative);
Mark Mendelle02d48f2014-01-15 11:19:23 -0800171
172 /**
173 * @brief Generate a two operand long arithmetic operation.
174 * @param rl_dest The destination.
175 * @param rl_src Second operand.
176 * @param op The DEX opcode for the operation.
177 */
178 void GenLongArith(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op);
179
180 /**
181 * @brief Generate a long operation.
182 * @param rl_dest The destination. Must be in a register
183 * @param rl_src The other operand. May be in a register or in memory.
184 * @param op The DEX opcode for the operation.
185 */
186 void GenLongRegOrMemOp(RegLocation rl_dest, RegLocation rl_src, Instruction::Code op);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700187
Mark Mendelldf8ee2e2014-01-27 16:37:47 -0800188 /**
189 * @brief Implement instanceof a final class with x86 specific code.
190 * @param use_declaring_class 'true' if we can use the class itself.
191 * @param type_idx Type index to use if use_declaring_class is 'false'.
192 * @param rl_dest Result to be set to 0 or 1.
193 * @param rl_src Object to be tested.
194 */
buzbee2700f7e2014-03-07 09:46:20 -0800195 void GenInstanceofFinal(bool use_declaring_class, uint32_t type_idx, RegLocation rl_dest,
196 RegLocation rl_src);
Mark Mendell6607d972014-02-10 06:54:18 -0800197 /*
198 *
199 * @brief Implement Set up instanceof a class with x86 specific code.
200 * @param needs_access_check 'true' if we must check the access.
201 * @param type_known_final 'true' if the type is known to be a final class.
202 * @param type_known_abstract 'true' if the type is known to be an abstract class.
203 * @param use_declaring_class 'true' if the type can be loaded off the current Method*.
204 * @param can_assume_type_is_in_dex_cache 'true' if the type is known to be in the cache.
205 * @param type_idx Type index to use if use_declaring_class is 'false'.
206 * @param rl_dest Result to be set to 0 or 1.
207 * @param rl_src Object to be tested.
208 */
209 void GenInstanceofCallingHelper(bool needs_access_check, bool type_known_final,
210 bool type_known_abstract, bool use_declaring_class,
211 bool can_assume_type_is_in_dex_cache,
buzbee2700f7e2014-03-07 09:46:20 -0800212 uint32_t type_idx, RegLocation rl_dest, RegLocation rl_src);
Mark Mendell6607d972014-02-10 06:54:18 -0800213
Brian Carlstrom7940e442013-07-12 13:46:57 -0700214 // Single operation generators.
215 LIR* OpUnconditionalBranch(LIR* target);
buzbee2700f7e2014-03-07 09:46:20 -0800216 LIR* OpCmpBranch(ConditionCode cond, RegStorage src1, RegStorage src2, LIR* target);
217 LIR* OpCmpImmBranch(ConditionCode cond, RegStorage reg, int check_value, LIR* target);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700218 LIR* OpCondBranch(ConditionCode cc, LIR* target);
buzbee2700f7e2014-03-07 09:46:20 -0800219 LIR* OpDecAndBranch(ConditionCode c_code, RegStorage reg, LIR* target);
220 LIR* OpFpRegCopy(RegStorage r_dest, RegStorage r_src);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700221 LIR* OpIT(ConditionCode cond, const char* guide);
Dave Allison3da67a52014-04-02 17:03:45 -0700222 void OpEndIT(LIR* it);
buzbee2700f7e2014-03-07 09:46:20 -0800223 LIR* OpMem(OpKind op, RegStorage r_base, int disp);
224 LIR* OpPcRelLoad(RegStorage reg, LIR* target);
225 LIR* OpReg(OpKind op, RegStorage r_dest_src);
buzbee7a11ab02014-04-28 20:02:38 -0700226 void OpRegCopy(RegStorage r_dest, RegStorage r_src);
buzbee2700f7e2014-03-07 09:46:20 -0800227 LIR* OpRegCopyNoInsert(RegStorage r_dest, RegStorage r_src);
228 LIR* OpRegImm(OpKind op, RegStorage r_dest_src1, int value);
229 LIR* OpRegMem(OpKind op, RegStorage r_dest, RegStorage r_base, int offset);
230 LIR* OpRegMem(OpKind op, RegStorage r_dest, RegLocation value);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800231 LIR* OpMemReg(OpKind op, RegLocation rl_dest, int value);
buzbee2700f7e2014-03-07 09:46:20 -0800232 LIR* OpRegReg(OpKind op, RegStorage r_dest_src1, RegStorage r_src2);
233 LIR* OpMovRegMem(RegStorage r_dest, RegStorage r_base, int offset, MoveType move_type);
234 LIR* OpMovMemReg(RegStorage r_base, int offset, RegStorage r_src, MoveType move_type);
235 LIR* OpCondRegReg(OpKind op, ConditionCode cc, RegStorage r_dest, RegStorage r_src);
236 LIR* OpRegRegImm(OpKind op, RegStorage r_dest, RegStorage r_src1, int value);
237 LIR* OpRegRegReg(OpKind op, RegStorage r_dest, RegStorage r_src1, RegStorage r_src2);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700238 LIR* OpTestSuspend(LIR* target);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700239 LIR* OpThreadMem(OpKind op, ThreadOffset<4> thread_offset);
buzbee2700f7e2014-03-07 09:46:20 -0800240 LIR* OpVldm(RegStorage r_base, int count);
241 LIR* OpVstm(RegStorage r_base, int count);
242 void OpLea(RegStorage r_base, RegStorage reg1, RegStorage reg2, int scale, int offset);
243 void OpRegCopyWide(RegStorage dest, RegStorage src);
Ian Rogersdd7624d2014-03-14 17:43:00 -0700244 void OpTlsCmp(ThreadOffset<4> offset, int val);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700245
buzbee091cc402014-03-31 10:14:40 -0700246 void OpRegThreadMem(OpKind op, RegStorage r_dest, ThreadOffset<4> thread_offset);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700247 void SpillCoreRegs();
248 void UnSpillCoreRegs();
249 static const X86EncodingMap EncodingMap[kX86Last];
250 bool InexpensiveConstantInt(int32_t value);
251 bool InexpensiveConstantFloat(int32_t value);
252 bool InexpensiveConstantLong(int64_t value);
253 bool InexpensiveConstantDouble(int64_t value);
254
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800255 /*
256 * @brief x86 specific codegen for int operations.
257 * @param opcode Operation to perform.
258 * @param rl_dest Destination for the result.
259 * @param rl_lhs Left hand operand.
260 * @param rl_rhs Right hand operand.
261 */
buzbee2700f7e2014-03-07 09:46:20 -0800262 void GenArithOpInt(Instruction::Code opcode, RegLocation rl_dest, RegLocation rl_lhs,
263 RegLocation rl_rhs);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800264
Mark Mendell55d0eac2014-02-06 11:02:52 -0800265 /*
266 * @brief Dump a RegLocation using printf
267 * @param loc Register location to dump
268 */
269 static void DumpRegLocation(RegLocation loc);
270
271 /*
272 * @brief Load the Method* of a dex method into the register.
Jeff Hao49161ce2014-03-12 11:05:25 -0700273 * @param target_method The MethodReference of the method to be invoked.
Mark Mendell55d0eac2014-02-06 11:02:52 -0800274 * @param type How the method will be invoked.
275 * @param register that will contain the code address.
276 * @note register will be passed to TargetReg to get physical register.
277 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700278 void LoadMethodAddress(const MethodReference& target_method, InvokeType type,
Mark Mendell55d0eac2014-02-06 11:02:52 -0800279 SpecialTargetRegister symbolic_reg);
280
281 /*
282 * @brief Load the Class* of a Dex Class type into the register.
283 * @param type How the method will be invoked.
284 * @param register that will contain the code address.
285 * @note register will be passed to TargetReg to get physical register.
286 */
287 void LoadClassType(uint32_t type_idx, SpecialTargetRegister symbolic_reg);
288
289 /*
290 * @brief Generate a relative call to the method that will be patched at link time.
Jeff Hao49161ce2014-03-12 11:05:25 -0700291 * @param target_method The MethodReference of the method to be invoked.
Mark Mendell55d0eac2014-02-06 11:02:52 -0800292 * @param type How the method will be invoked.
293 * @returns Call instruction
294 */
Jeff Hao49161ce2014-03-12 11:05:25 -0700295 LIR * CallWithLinkerFixup(const MethodReference& target_method, InvokeType type);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800296
297 /*
298 * @brief Handle x86 specific literals
299 */
300 void InstallLiteralPools();
301
Mark Mendellae9fd932014-02-10 16:14:35 -0800302 /*
303 * @brief Generate the debug_frame CFI information.
304 * @returns pointer to vector containing CFE information
305 */
306 static std::vector<uint8_t>* ReturnCommonCallFrameInformation();
307
308 /*
309 * @brief Generate the debug_frame FDE information.
310 * @returns pointer to vector containing CFE information
311 */
312 std::vector<uint8_t>* ReturnCallFrameInformation();
313
Brian Carlstrom7940e442013-07-12 13:46:57 -0700314 private:
Vladimir Marko057c74a2013-12-03 15:20:45 +0000315 void EmitPrefix(const X86EncodingMap* entry);
316 void EmitOpcode(const X86EncodingMap* entry);
317 void EmitPrefixAndOpcode(const X86EncodingMap* entry);
318 void EmitDisp(uint8_t base, int disp);
319 void EmitModrmDisp(uint8_t reg_or_opcode, uint8_t base, int disp);
320 void EmitModrmSibDisp(uint8_t reg_or_opcode, uint8_t base, uint8_t index, int scale, int disp);
321 void EmitImm(const X86EncodingMap* entry, int imm);
Vladimir Markoa8b4caf2013-10-24 15:08:57 +0100322 void EmitOpRegOpcode(const X86EncodingMap* entry, uint8_t reg);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700323 void EmitOpReg(const X86EncodingMap* entry, uint8_t reg);
324 void EmitOpMem(const X86EncodingMap* entry, uint8_t base, int disp);
buzbee2700f7e2014-03-07 09:46:20 -0800325 void EmitOpArray(const X86EncodingMap* entry, uint8_t base, uint8_t index, int scale, int disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700326 void EmitMemReg(const X86EncodingMap* entry, uint8_t base, int disp, uint8_t reg);
Mark Mendell343adb52013-12-18 06:02:17 -0800327 void EmitMemImm(const X86EncodingMap* entry, uint8_t base, int disp, int32_t imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700328 void EmitRegMem(const X86EncodingMap* entry, uint8_t reg, uint8_t base, int disp);
329 void EmitRegArray(const X86EncodingMap* entry, uint8_t reg, uint8_t base, uint8_t index,
330 int scale, int disp);
331 void EmitArrayReg(const X86EncodingMap* entry, uint8_t base, uint8_t index, int scale, int disp,
332 uint8_t reg);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400333 void EmitArrayImm(const X86EncodingMap* entry, uint8_t base, uint8_t index, int scale, int disp,
334 int32_t imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700335 void EmitRegThread(const X86EncodingMap* entry, uint8_t reg, int disp);
336 void EmitRegReg(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2);
337 void EmitRegRegImm(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2, int32_t imm);
Mark Mendell4708dcd2014-01-22 09:05:18 -0800338 void EmitRegRegImmRev(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2, int32_t imm);
buzbee2700f7e2014-03-07 09:46:20 -0800339 void EmitRegMemImm(const X86EncodingMap* entry, uint8_t reg1, uint8_t base, int disp,
340 int32_t imm);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400341 void EmitMemRegImm(const X86EncodingMap* entry, uint8_t base, int disp, uint8_t reg1, int32_t imm);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700342 void EmitRegImm(const X86EncodingMap* entry, uint8_t reg, int imm);
343 void EmitThreadImm(const X86EncodingMap* entry, int disp, int imm);
344 void EmitMovRegImm(const X86EncodingMap* entry, uint8_t reg, int imm);
345 void EmitShiftRegImm(const X86EncodingMap* entry, uint8_t reg, int imm);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400346 void EmitShiftMemImm(const X86EncodingMap* entry, uint8_t base, int disp, int imm);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800347 void EmitShiftMemCl(const X86EncodingMap* entry, uint8_t base, int displacement, uint8_t cl);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700348 void EmitShiftRegCl(const X86EncodingMap* entry, uint8_t reg, uint8_t cl);
349 void EmitRegCond(const X86EncodingMap* entry, uint8_t reg, uint8_t condition);
Mark Mendell2637f2e2014-04-30 10:10:47 -0400350 void EmitMemCond(const X86EncodingMap* entry, uint8_t base, int displacement, uint8_t condition);
Razvan A Lupusorubd288c22013-12-20 17:27:23 -0800351
352 /**
353 * @brief Used for encoding conditional register to register operation.
354 * @param entry The entry in the encoding map for the opcode.
355 * @param reg1 The first physical register.
356 * @param reg2 The second physical register.
357 * @param condition The condition code for operation.
358 */
359 void EmitRegRegCond(const X86EncodingMap* entry, uint8_t reg1, uint8_t reg2, uint8_t condition);
360
Mark Mendell2637f2e2014-04-30 10:10:47 -0400361 /**
362 * @brief Used for encoding conditional register to memory operation.
363 * @param entry The entry in the encoding map for the opcode.
364 * @param reg1 The first physical register.
365 * @param base The memory base register.
366 * @param displacement The memory displacement.
367 * @param condition The condition code for operation.
368 */
369 void EmitRegMemCond(const X86EncodingMap* entry, uint8_t reg1, uint8_t base, int displacement, uint8_t condition);
370
Brian Carlstrom7940e442013-07-12 13:46:57 -0700371 void EmitJmp(const X86EncodingMap* entry, int rel);
372 void EmitJcc(const X86EncodingMap* entry, int rel, uint8_t cc);
373 void EmitCallMem(const X86EncodingMap* entry, uint8_t base, int disp);
Mark Mendell55d0eac2014-02-06 11:02:52 -0800374 void EmitCallImmediate(const X86EncodingMap* entry, int disp);
Brian Carlstrom7940e442013-07-12 13:46:57 -0700375 void EmitCallThread(const X86EncodingMap* entry, int disp);
376 void EmitPcRel(const X86EncodingMap* entry, uint8_t reg, int base_or_table, uint8_t index,
377 int scale, int table_or_disp);
378 void EmitMacro(const X86EncodingMap* entry, uint8_t reg, int offset);
379 void EmitUnimplemented(const X86EncodingMap* entry, LIR* lir);
Mark Mendell412d4f82013-12-18 13:32:36 -0800380 void GenFusedLongCmpImmBranch(BasicBlock* bb, RegLocation rl_src1,
381 int64_t val, ConditionCode ccode);
Bill Buzbeed61ba4b2014-01-13 21:44:01 +0000382 void GenConstWide(RegLocation rl_dest, int64_t value);
Mark Mendelle02d48f2014-01-15 11:19:23 -0800383
Razvan A Lupusoru99ad7232014-02-25 17:41:08 -0800384 static bool ProvidesFullMemoryBarrier(X86OpCode opcode);
385
Mark Mendelle02d48f2014-01-15 11:19:23 -0800386 /*
Mark Mendell4028a6c2014-02-19 20:06:20 -0800387 * @brief generate inline code for fast case of Strng.indexOf.
388 * @param info Call parameters
389 * @param zero_based 'true' if the index into the string is 0.
390 * @returns 'true' if the call was inlined, 'false' if a regular call needs to be
391 * generated.
392 */
393 bool GenInlinedIndexOf(CallInfo* info, bool zero_based);
394
395 /*
Mark Mendelle02d48f2014-01-15 11:19:23 -0800396 * @brief Return the correct x86 opcode for the Dex operation
397 * @param op Dex opcode for the operation
398 * @param loc Register location of the operand
399 * @param is_high_op 'true' if this is an operation on the high word
400 * @param value Immediate value for the operation. Used for byte variants
401 * @returns the correct x86 opcode to perform the operation
402 */
403 X86OpCode GetOpcode(Instruction::Code op, RegLocation loc, bool is_high_op, int32_t value);
404
405 /*
406 * @brief Return the correct x86 opcode for the Dex operation
407 * @param op Dex opcode for the operation
408 * @param dest location of the destination. May be register or memory.
409 * @param rhs Location for the rhs of the operation. May be in register or memory.
410 * @param is_high_op 'true' if this is an operation on the high word
411 * @returns the correct x86 opcode to perform the operation
412 * @note at most one location may refer to memory
413 */
414 X86OpCode GetOpcode(Instruction::Code op, RegLocation dest, RegLocation rhs,
415 bool is_high_op);
416
417 /*
418 * @brief Is this operation a no-op for this opcode and value
419 * @param op Dex opcode for the operation
420 * @param value Immediate value for the operation.
421 * @returns 'true' if the operation will have no effect
422 */
423 bool IsNoOp(Instruction::Code op, int32_t value);
424
Mark Mendell2bf31e62014-01-23 12:13:40 -0800425 /**
426 * @brief Calculate magic number and shift for a given divisor
427 * @param divisor divisor number for calculation
428 * @param magic hold calculated magic number
429 * @param shift hold calculated shift
430 */
431 void CalculateMagicAndShift(int divisor, int& magic, int& shift);
432
433 /*
434 * @brief Generate an integer div or rem operation.
435 * @param rl_dest Destination Location.
436 * @param rl_src1 Numerator Location.
437 * @param rl_src2 Divisor Location.
438 * @param is_div 'true' if this is a division, 'false' for a remainder.
439 * @param check_zero 'true' if an exception should be generated if the divisor is 0.
440 */
buzbee2700f7e2014-03-07 09:46:20 -0800441 RegLocation GenDivRem(RegLocation rl_dest, RegLocation rl_src1, RegLocation rl_src2,
442 bool is_div, bool check_zero);
Mark Mendell2bf31e62014-01-23 12:13:40 -0800443
444 /*
445 * @brief Generate an integer div or rem operation by a literal.
446 * @param rl_dest Destination Location.
447 * @param rl_src Numerator Location.
448 * @param lit Divisor.
449 * @param is_div 'true' if this is a division, 'false' for a remainder.
450 */
451 RegLocation GenDivRemLit(RegLocation rl_dest, RegLocation rl_src, int lit, bool is_div);
Mark Mendell4708dcd2014-01-22 09:05:18 -0800452
453 /*
454 * Generate code to implement long shift operations.
455 * @param opcode The DEX opcode to specify the shift type.
456 * @param rl_dest The destination.
457 * @param rl_src The value to be shifted.
458 * @param shift_amount How much to shift.
459 * @returns the RegLocation of the result.
460 */
461 RegLocation GenShiftImmOpLong(Instruction::Code opcode, RegLocation rl_dest,
462 RegLocation rl_src, int shift_amount);
463 /*
464 * Generate an imul of a register by a constant or a better sequence.
465 * @param dest Destination Register.
466 * @param src Source Register.
467 * @param val Constant multiplier.
468 */
buzbee2700f7e2014-03-07 09:46:20 -0800469 void GenImulRegImm(RegStorage dest, RegStorage src, int val);
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800470
Mark Mendell4708dcd2014-01-22 09:05:18 -0800471 /*
472 * Generate an imul of a memory location by a constant or a better sequence.
473 * @param dest Destination Register.
474 * @param sreg Symbolic register.
475 * @param displacement Displacement on stack of Symbolic Register.
476 * @param val Constant multiplier.
477 */
buzbee2700f7e2014-03-07 09:46:20 -0800478 void GenImulMemImm(RegStorage dest, int sreg, int displacement, int val);
Mark Mendell766e9292014-01-27 07:55:47 -0800479
480 /*
481 * @brief Compare memory to immediate, and branch if condition true.
482 * @param cond The condition code that when true will branch to the target.
483 * @param temp_reg A temporary register that can be used if compare memory is not
484 * supported by the architecture.
485 * @param base_reg The register holding the base address.
486 * @param offset The offset from the base.
487 * @param check_value The immediate to compare to.
488 */
buzbee2700f7e2014-03-07 09:46:20 -0800489 LIR* OpCmpMemImmBranch(ConditionCode cond, RegStorage temp_reg, RegStorage base_reg,
Mark Mendell766e9292014-01-27 07:55:47 -0800490 int offset, int check_value, LIR* target);
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800491
Mark Mendellfeb2b4e2014-01-28 12:59:49 -0800492 /*
493 * Can this operation be using core registers without temporaries?
494 * @param rl_lhs Left hand operand.
495 * @param rl_rhs Right hand operand.
496 * @returns 'true' if the operation can proceed without needing temporary regs.
497 */
498 bool IsOperationSafeWithoutTemps(RegLocation rl_lhs, RegLocation rl_rhs);
Mark Mendell67c39c42014-01-31 17:28:00 -0800499
Razvan A Lupusoru614c2b42014-01-28 17:05:21 -0800500 /**
501 * @brief Generates inline code for conversion of long to FP by using x87/
502 * @param rl_dest The destination of the FP.
503 * @param rl_src The source of the long.
504 * @param is_double 'true' if dealing with double, 'false' for float.
505 */
506 void GenLongToFP(RegLocation rl_dest, RegLocation rl_src, bool is_double);
507
Mark Mendell67c39c42014-01-31 17:28:00 -0800508 /*
509 * @brief Perform MIR analysis before compiling method.
510 * @note Invokes Mir2LiR::Materialize after analysis.
511 */
512 void Materialize();
513
514 /*
515 * @brief Analyze MIR before generating code, to prepare for the code generation.
516 */
517 void AnalyzeMIR();
518
519 /*
520 * @brief Analyze one basic block.
521 * @param bb Basic block to analyze.
522 */
523 void AnalyzeBB(BasicBlock * bb);
524
525 /*
526 * @brief Analyze one extended MIR instruction
527 * @param opcode MIR instruction opcode.
528 * @param bb Basic block containing instruction.
529 * @param mir Extended instruction to analyze.
530 */
531 void AnalyzeExtendedMIR(int opcode, BasicBlock * bb, MIR *mir);
532
533 /*
534 * @brief Analyze one MIR instruction
535 * @param opcode MIR instruction opcode.
536 * @param bb Basic block containing instruction.
537 * @param mir Instruction to analyze.
538 */
539 void AnalyzeMIR(int opcode, BasicBlock * bb, MIR *mir);
540
541 /*
542 * @brief Analyze one MIR float/double instruction
543 * @param opcode MIR instruction opcode.
544 * @param bb Basic block containing instruction.
545 * @param mir Instruction to analyze.
546 */
547 void AnalyzeFPInstruction(int opcode, BasicBlock * bb, MIR *mir);
548
549 /*
550 * @brief Analyze one use of a double operand.
551 * @param rl_use Double RegLocation for the operand.
552 */
553 void AnalyzeDoubleUse(RegLocation rl_use);
554
555 // Information derived from analysis of MIR
556
Mark Mendell55d0eac2014-02-06 11:02:52 -0800557 // The compiler temporary for the code address of the method.
558 CompilerTemp *base_of_code_;
559
Mark Mendell67c39c42014-01-31 17:28:00 -0800560 // Have we decided to compute a ptr to code and store in temporary VR?
561 bool store_method_addr_;
562
Mark Mendell55d0eac2014-02-06 11:02:52 -0800563 // Have we used the stored method address?
564 bool store_method_addr_used_;
565
566 // Instructions to remove if we didn't use the stored method address.
567 LIR* setup_method_address_[2];
568
569 // Instructions needing patching with Method* values.
570 GrowableArray<LIR*> method_address_insns_;
571
572 // Instructions needing patching with Class Type* values.
573 GrowableArray<LIR*> class_type_address_insns_;
574
575 // Instructions needing patching with PC relative code addresses.
576 GrowableArray<LIR*> call_method_insns_;
Mark Mendellae9fd932014-02-10 16:14:35 -0800577
578 // Prologue decrement of stack pointer.
579 LIR* stack_decrement_;
580
581 // Epilogue increment of stack pointer.
582 LIR* stack_increment_;
Brian Carlstrom7940e442013-07-12 13:46:57 -0700583};
584
585} // namespace art
586
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700587#endif // ART_COMPILER_DEX_QUICK_X86_CODEGEN_X86_H_