blob: e95bb210b6c9e121d5f2d011e5ee4af01c3fbf36 [file] [log] [blame]
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +00001/*
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_H_
18#define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_
19
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000020#include "globals.h"
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000021#include "instruction_set.h"
22#include "memory_region.h"
23#include "nodes.h"
24#include "utils/assembler.h"
25
26namespace art {
27
28class CodeAllocator {
29 public:
30 CodeAllocator() { }
31 virtual ~CodeAllocator() { }
32
33 virtual uint8_t* Allocate(size_t size) = 0;
34
35 private:
36 DISALLOW_COPY_AND_ASSIGN(CodeAllocator);
37};
38
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000039/**
40 * A Location is an abstraction over the potential location
41 * of an instruction. It could be in register or stack.
42 */
43class Location : public ValueObject {
44 public:
45 template<typename T>
46 T reg() const { return static_cast<T>(reg_); }
47
48 Location() : reg_(kInvalid) { }
49 explicit Location(uword reg) : reg_(reg) { }
50
51 static Location RegisterLocation(uword reg) {
52 return Location(reg);
53 }
54
55 bool IsValid() const { return reg_ != kInvalid; }
56
57 Location(const Location& other) : reg_(other.reg_) { }
58
59 Location& operator=(const Location& other) {
60 reg_ = other.reg_;
61 return *this;
62 }
63
64 private:
65 // The target register for that location.
66 // TODO: Support stack location.
67 uword reg_;
68 static const uword kInvalid = -1;
69};
70
71/**
72 * The code generator computes LocationSummary for each instruction so that
73 * the instruction itself knows what code to generate: where to find the inputs
74 * and where to place the result.
75 *
76 * The intent is to have the code for generating the instruction independent of
77 * register allocation. A register allocator just has to provide a LocationSummary.
78 */
79class LocationSummary : public ArenaObject {
80 public:
81 explicit LocationSummary(HInstruction* instruction)
Nicolas Geoffray787c3072014-03-17 10:20:19 +000082 : inputs(instruction->GetBlock()->GetGraph()->GetArena(), instruction->InputCount()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000083 inputs.SetSize(instruction->InputCount());
84 for (int i = 0; i < instruction->InputCount(); i++) {
85 inputs.Put(i, Location());
86 }
87 }
88
89 void SetInAt(uint32_t at, Location location) {
90 inputs.Put(at, location);
91 }
92
93 Location InAt(uint32_t at) const {
94 return inputs.Get(at);
95 }
96
97 void SetOut(Location location) {
98 output = Location(location);
99 }
100
101 Location Out() const { return output; }
102
103 private:
104 GrowableArray<Location> inputs;
105 Location output;
106
107 DISALLOW_COPY_AND_ASSIGN(LocationSummary);
108};
109
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000110class CodeGenerator : public ArenaObject {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000111 public:
112 // Compiles the graph to executable instructions. Returns whether the compilation
113 // succeeded.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000114 void Compile(CodeAllocator* allocator);
115 static CodeGenerator* Create(ArenaAllocator* allocator,
116 HGraph* graph,
117 InstructionSet instruction_set);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000118
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000119 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000120
121 Label* GetLabelOf(HBasicBlock* block) const;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000122 bool GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000123
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000124 virtual void GenerateFrameEntry() = 0;
125 virtual void GenerateFrameExit() = 0;
126 virtual void Bind(Label* label) = 0;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000127 virtual void Move(HInstruction* instruction, Location location) = 0;
128 virtual void Push(HInstruction* instruction, Location location) = 0;
129 virtual HGraphVisitor* GetLocationBuilder() = 0;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000130 virtual HGraphVisitor* GetInstructionVisitor() = 0;
131 virtual Assembler* GetAssembler() = 0;
132
133 uint32_t GetFrameSize() const { return frame_size_; }
134 void SetFrameSize(uint32_t size) { frame_size_ = size; }
135
136 void BuildMappingTable(std::vector<uint8_t>* vector) const { }
137 void BuildVMapTable(std::vector<uint8_t>* vector) const { }
138 void BuildNativeGCMap(std::vector<uint8_t>* vector) const { }
139
140 protected:
141 explicit CodeGenerator(HGraph* graph)
142 : frame_size_(0),
143 graph_(graph),
144 block_labels_(graph->GetArena(), 0) {
145 block_labels_.SetSize(graph->GetBlocks()->Size());
146 }
147 ~CodeGenerator() { }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000148
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000149 private:
150 void InitLocations(HInstruction* instruction);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000151 void CompileBlock(HBasicBlock* block);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000152 void CompileEntryBlock();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000153
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000154 // Frame size required for this method.
155 uint32_t frame_size_;
156
157 HGraph* const graph_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000158
159 // Labels for each block that will be compiled.
160 GrowableArray<Label> block_labels_;
161
162 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
163};
164
165} // namespace art
166
167#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_