blob: d347a4f121d1d951968a95e9710f36b4f62a2361 [file] [log] [blame]
Nicolas Geoffray9cf35522014-06-09 18:40:10 +01001/*
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"
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000022#include "parallel_move_resolver.h"
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010023#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
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000059class 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:
Nicolas Geoffray412f10c2014-06-19 10:00:34 +010072 void Exchange32(CpuRegister reg, int mem);
73 void Exchange32(int mem1, int mem2);
74 void Exchange64(CpuRegister reg, int mem);
75 void Exchange64(int mem1, int mem2);
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +000076
77 CodeGeneratorX86_64* const codegen_;
78
79 DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
80};
81
Nicolas Geoffray9cf35522014-06-09 18:40:10 +010082class LocationsBuilderX86_64 : public HGraphVisitor {
83 public:
84 LocationsBuilderX86_64(HGraph* graph, CodeGeneratorX86_64* codegen)
85 : HGraphVisitor(graph), codegen_(codegen) {}
86
87#define DECLARE_VISIT_INSTRUCTION(name) \
88 virtual void Visit##name(H##name* instr);
89
90 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
91
92#undef DECLARE_VISIT_INSTRUCTION
93
94 private:
95 CodeGeneratorX86_64* const codegen_;
96 InvokeDexCallingConventionVisitor parameter_visitor_;
97
98 DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
99};
100
101class InstructionCodeGeneratorX86_64 : public HGraphVisitor {
102 public:
103 InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
104
105#define DECLARE_VISIT_INSTRUCTION(name) \
106 virtual void Visit##name(H##name* instr);
107
108 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
109
110#undef DECLARE_VISIT_INSTRUCTION
111
112 void LoadCurrentMethod(CpuRegister reg);
113
114 X86_64Assembler* GetAssembler() const { return assembler_; }
115
116 private:
117 X86_64Assembler* const assembler_;
118 CodeGeneratorX86_64* const codegen_;
119
120 DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
121};
122
123class CodeGeneratorX86_64 : public CodeGenerator {
124 public:
125 explicit CodeGeneratorX86_64(HGraph* graph);
126 virtual ~CodeGeneratorX86_64() {}
127
128 virtual void ComputeFrameSize(size_t number_of_spill_slots) OVERRIDE;
129 virtual void GenerateFrameEntry() OVERRIDE;
130 virtual void GenerateFrameExit() OVERRIDE;
131 virtual void Bind(Label* label) OVERRIDE;
132 virtual void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
133
134 virtual size_t GetWordSize() const OVERRIDE {
135 return kX86_64WordSize;
136 }
137
138 virtual HGraphVisitor* GetLocationBuilder() OVERRIDE {
139 return &location_builder_;
140 }
141
142 virtual HGraphVisitor* GetInstructionVisitor() OVERRIDE {
143 return &instruction_visitor_;
144 }
145
146 virtual X86_64Assembler* GetAssembler() OVERRIDE {
147 return &assembler_;
148 }
149
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000150 ParallelMoveResolverX86_64* GetMoveResolver() {
151 return &move_resolver_;
152 }
153
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100154 int32_t GetStackSlot(HLocal* local) const;
155 virtual Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
156
157 virtual size_t GetNumberOfRegisters() const OVERRIDE {
158 return kNumberOfRegIds;
159 }
160
161 virtual size_t GetNumberOfCoreRegisters() const OVERRIDE {
162 return kNumberOfCpuRegisters;
163 }
164
165 virtual size_t GetNumberOfFloatingPointRegisters() const OVERRIDE {
166 return kNumberOfFloatRegisters;
167 }
168
169 virtual void SetupBlockedRegisters(bool* blocked_registers) const OVERRIDE;
170 virtual ManagedRegister AllocateFreeRegister(
171 Primitive::Type type, bool* blocked_registers) const OVERRIDE;
172 virtual void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
173 virtual void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
174
Nicolas Geoffray412f10c2014-06-19 10:00:34 +0100175 virtual InstructionSet GetInstructionSet() const OVERRIDE {
176 return InstructionSet::kX86_64;
177 }
178
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100179 private:
180 // Helper method to move a value between two locations.
181 void Move(Location destination, Location source);
182
183 LocationsBuilderX86_64 location_builder_;
184 InstructionCodeGeneratorX86_64 instruction_visitor_;
Nicolas Geoffrayecb2f9b2014-06-13 08:59:59 +0000185 ParallelMoveResolverX86_64 move_resolver_;
Nicolas Geoffray9cf35522014-06-09 18:40:10 +0100186 X86_64Assembler assembler_;
187
188 DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
189};
190
191} // namespace x86_64
192} // namespace art
193
194#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_