code_generator_x86_64.h revision 3c04974a90b0e03f4b509010bff49f0b2a3da57f
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 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);
76
77  CodeGeneratorX86_64* const codegen_;
78
79  DISALLOW_COPY_AND_ASSIGN(ParallelMoveResolverX86_64);
80};
81
82class 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_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
91
92#undef DECLARE_VISIT_INSTRUCTION
93
94  void HandleInvoke(HInvoke* invoke);
95
96 private:
97  CodeGeneratorX86_64* const codegen_;
98  InvokeDexCallingConventionVisitor parameter_visitor_;
99
100  DISALLOW_COPY_AND_ASSIGN(LocationsBuilderX86_64);
101};
102
103class InstructionCodeGeneratorX86_64 : public HGraphVisitor {
104 public:
105  InstructionCodeGeneratorX86_64(HGraph* graph, CodeGeneratorX86_64* codegen);
106
107#define DECLARE_VISIT_INSTRUCTION(name)     \
108  virtual void Visit##name(H##name* instr);
109
110  FOR_EACH_CONCRETE_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
111
112#undef DECLARE_VISIT_INSTRUCTION
113
114  void LoadCurrentMethod(CpuRegister reg);
115
116  X86_64Assembler* GetAssembler() const { return assembler_; }
117
118 private:
119  // Generate code for the given suspend check. If not null, `successor`
120  // is the block to branch to if the suspend check is not needed, and after
121  // the suspend call.
122  void GenerateSuspendCheck(HSuspendCheck* instruction, HBasicBlock* successor);
123
124  X86_64Assembler* const assembler_;
125  CodeGeneratorX86_64* const codegen_;
126
127  DISALLOW_COPY_AND_ASSIGN(InstructionCodeGeneratorX86_64);
128};
129
130class CodeGeneratorX86_64 : public CodeGenerator {
131 public:
132  explicit CodeGeneratorX86_64(HGraph* graph);
133  virtual ~CodeGeneratorX86_64() {}
134
135  virtual void GenerateFrameEntry() OVERRIDE;
136  virtual void GenerateFrameExit() OVERRIDE;
137  virtual void Bind(Label* label) OVERRIDE;
138  virtual void Move(HInstruction* instruction, Location location, HInstruction* move_for) OVERRIDE;
139  virtual void SaveCoreRegister(Location stack_location, uint32_t reg_id) OVERRIDE;
140  virtual void RestoreCoreRegister(Location stack_location, uint32_t reg_id) OVERRIDE;
141
142  virtual size_t GetWordSize() const OVERRIDE {
143    return kX86_64WordSize;
144  }
145
146  virtual size_t FrameEntrySpillSize() const OVERRIDE;
147
148  virtual HGraphVisitor* GetLocationBuilder() OVERRIDE {
149    return &location_builder_;
150  }
151
152  virtual HGraphVisitor* GetInstructionVisitor() OVERRIDE {
153    return &instruction_visitor_;
154  }
155
156  virtual X86_64Assembler* GetAssembler() OVERRIDE {
157    return &assembler_;
158  }
159
160  ParallelMoveResolverX86_64* GetMoveResolver() {
161    return &move_resolver_;
162  }
163
164  virtual Location GetStackLocation(HLoadLocal* load) const OVERRIDE;
165
166  virtual size_t GetNumberOfRegisters() const OVERRIDE {
167    return kNumberOfRegIds;
168  }
169
170  virtual size_t GetNumberOfCoreRegisters() const OVERRIDE {
171    return kNumberOfCpuRegisters;
172  }
173
174  virtual size_t GetNumberOfFloatingPointRegisters() const OVERRIDE {
175    return kNumberOfFloatRegisters;
176  }
177
178  virtual void SetupBlockedRegisters(bool* blocked_registers) const OVERRIDE;
179  virtual ManagedRegister AllocateFreeRegister(
180      Primitive::Type type, bool* blocked_registers) const OVERRIDE;
181  virtual void DumpCoreRegister(std::ostream& stream, int reg) const OVERRIDE;
182  virtual void DumpFloatingPointRegister(std::ostream& stream, int reg) const OVERRIDE;
183
184  virtual InstructionSet GetInstructionSet() const OVERRIDE {
185    return InstructionSet::kX86_64;
186  }
187
188  // Emit a write barrier.
189  void MarkGCCard(CpuRegister temp, CpuRegister card, CpuRegister object, CpuRegister value);
190
191  // Helper method to move a value between two locations.
192  void Move(Location destination, Location source);
193
194 private:
195  LocationsBuilderX86_64 location_builder_;
196  InstructionCodeGeneratorX86_64 instruction_visitor_;
197  ParallelMoveResolverX86_64 move_resolver_;
198  X86_64Assembler assembler_;
199
200  DISALLOW_COPY_AND_ASSIGN(CodeGeneratorX86_64);
201};
202
203}  // namespace x86_64
204}  // namespace art
205
206#endif  // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_X86_64_H_
207