code_generator_x86_64.h revision ecb2f9ba57b08ceac4204ddd6a0a88a0524f8741
1/* 2 * Copyright (C) 2014 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#ifndef ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_ 18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_ 19 20#include "code_generator.h" 21#include "nodes.h" 22#include "parallel_move_resolver.h" 23#include "utils/x86_64/assembler_x86_64.h" 24 25namespace art { 26namespace x86_64 { 27 28static constexpr size_t kX86_64WordSize = 8; 29 30static constexpr Register kParameterCoreRegisters[] = { RSI, RDX, RCX, R8, R9 }; 31 32static constexpr size_t kParameterCoreRegistersLength = arraysize(kParameterCoreRegisters); 33 34class InvokeDexCallingConvention : public CallingConvention<Register> { 35 public: 36 InvokeDexCallingConvention() 37 : CallingConvention(kParameterCoreRegisters, kParameterCoreRegistersLength) {} 38 39 private: 40 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConvention); 41}; 42 43class InvokeDexCallingConventionVisitor { 44 public: 45 InvokeDexCallingConventionVisitor() : gp_index_(0), stack_index_(0) {} 46 47 Location GetNextLocation(Primitive::Type type); 48 49 private: 50 InvokeDexCallingConvention calling_convention; 51 uint32_t gp_index_; 52 uint32_t stack_index_; 53 54 DISALLOW_COPY_AND_ASSIGN(InvokeDexCallingConventionVisitor); 55}; 56 57class CodeGeneratorX86_64; 58 59class ParallelMoveResolverX86_64 : public ParallelMoveResolver { 60 public: 61 ParallelMoveResolverX86_64(ArenaAllocator* allocator, CodeGeneratorX86_64* codegen) 62 : ParallelMoveResolver(allocator), codegen_(codegen) {} 63 64 virtual void EmitMove(size_t index) OVERRIDE; 65 virtual void EmitSwap(size_t index) OVERRIDE; 66 virtual void SpillScratch(int reg) OVERRIDE; 67 virtual void RestoreScratch(int reg) OVERRIDE; 68 69 X86_64Assembler* GetAssembler() const; 70 71 private: 72 void Exchange(CpuRegister reg, int mem); 73 void Exchange(int mem1, int mem2); 74 75 CodeGeneratorX86_64* const codegen_; 76 77 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64); 78}; 79 80class LocationsBuilderX86_64 : public HGraphVisitor { 81 public: 82 LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen) 83 : HGraphVisitor(graph), codegen_(codegen) {} 84 85#define DECLARE_VISIT_INSTRUCTION(name) \ 86 virtual void Visit##name(H##name* instr); 87 88 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) 89 90#undef DECLARE_VISIT_INSTRUCTION 91 92 private: 93 CodeGeneratorX86_64* const codegen_; 94 InvokeDexCallingConventionVisitor parameter_visitor_; 95 96 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64); 97}; 98 99class InstructionCodeGeneratorX86_64 : public HGraphVisitor { 100 public: 101 InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen); 102 103#define DECLARE_VISIT_INSTRUCTION(name) \ 104 virtual void Visit##name(H##name* instr); 105 106 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION) 107 108#undef DECLARE_VISIT_INSTRUCTION 109 110 void LoadCurrentMethod(CpuRegister reg); 111 112 X86_64Assembler* GetAssembler() const { return assembler_; } 113 114 private: 115 X86_64Assembler* const assembler_; 116 CodeGeneratorX86_64* const codegen_; 117 118 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64); 119}; 120 121class CodeGeneratorX86_64 : public CodeGenerator { 122 public: 123 explicit CodeGeneratorX86_64(HGraph* graph); 124 virtual ~CodeGeneratorX86_64() {} 125 126 virtual void ComputeFrameSize(size_t number_of_spill_slots) OVERRIDE; 127 virtual void GenerateFrameEntry() OVERRIDE; 128 virtual void GenerateFrameExit() OVERRIDE; 129 virtual void Bind(Label* label) OVERRIDE; 130 virtual void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE; 131 132 virtual size_t GetWordSize() const OVERRIDE { 133 return kX86_64WordSize; 134 } 135 136 virtual HGraphVisitor* GetLocationBuilder() OVERRIDE { 137 return &location_builder_; 138 } 139 140 virtual HGraphVisitor* GetInstructionVisitor() OVERRIDE { 141 return &instruction_visitor_; 142 } 143 144 virtual X86_64Assembler* GetAssembler() OVERRIDE { 145 return &assembler_; 146 } 147 148 ParallelMoveResolverX86_64* GetMoveResolver() { 149 return &move_resolver_; 150 } 151 152 int32_t GetStackSlot(HLocal* local) const; 153 virtual Location GetStackLocation(HLoadLocal* load) const OVERRIDE; 154 155 virtual size_t GetNumberOfRegisters() const OVERRIDE { 156 return kNumberOfRegIds; 157 } 158 159 virtual size_t GetNumberOfCoreRegisters() const OVERRIDE { 160 return kNumberOfCpuRegisters; 161 } 162 163 virtual size_t GetNumberOfFloatingPointRegisters() const OVERRIDE { 164 return kNumberOfFloatRegisters; 165 } 166 167 virtual void SetupBlockedRegisters(bool* blocked_registers) const OVERRIDE; 168 virtual ManagedRegister AllocateFreeRegister( 169 Primitive::Type type, bool* blocked_registers) const OVERRIDE; 170 virtual void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE; 171 virtual void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE; 172 173 private: 174 // Helper method to move a value between two locations. 175 void Move(Location destination, Location source); 176 177 LocationsBuilderX86_64 location_builder_; 178 InstructionCodeGeneratorX86_64 instruction_visitor_; 179 ParallelMoveResolverX86_64 move_resolver_; 180 X86_64Assembler assembler_; 181 182 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64); 183}; 184 185} // namespace x86_64 186} // namespace art 187 188#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_ 189