blob: 63f8cbf4299ab56f10fa66f35b7c1785d8f8cad1 [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
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +000028class DexCompilationUnit;
29
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +000030class CodeAllocator {
31 public:
32 CodeAllocator() { }
33 virtual ~CodeAllocator() { }
34
35 virtual uint8_t* Allocate(size_t size) = 0;
36
37 private:
38 DISALLOW_COPY_AND_ASSIGN(CodeAllocator);
39};
40
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000041/**
42 * A Location is an abstraction over the potential location
43 * of an instruction. It could be in register or stack.
44 */
45class Location : public ValueObject {
46 public:
47 template<typename T>
48 T reg() const { return static_cast<T>(reg_); }
49
50 Location() : reg_(kInvalid) { }
51 explicit Location(uword reg) : reg_(reg) { }
52
53 static Location RegisterLocation(uword reg) {
54 return Location(reg);
55 }
56
57 bool IsValid() const { return reg_ != kInvalid; }
58
59 Location(const Location& other) : reg_(other.reg_) { }
60
61 Location& operator=(const Location& other) {
62 reg_ = other.reg_;
63 return *this;
64 }
65
66 private:
67 // The target register for that location.
68 // TODO: Support stack location.
69 uword reg_;
70 static const uword kInvalid = -1;
71};
72
73/**
74 * The code generator computes LocationSummary for each instruction so that
75 * the instruction itself knows what code to generate: where to find the inputs
76 * and where to place the result.
77 *
78 * The intent is to have the code for generating the instruction independent of
79 * register allocation. A register allocator just has to provide a LocationSummary.
80 */
81class LocationSummary : public ArenaObject {
82 public:
83 explicit LocationSummary(HInstruction* instruction)
Nicolas Geoffray787c3072014-03-17 10:20:19 +000084 : inputs(instruction->GetBlock()->GetGraph()->GetArena(), instruction->InputCount()) {
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +000085 inputs.SetSize(instruction->InputCount());
86 for (int i = 0; i < instruction->InputCount(); i++) {
87 inputs.Put(i, Location());
88 }
89 }
90
91 void SetInAt(uint32_t at, Location location) {
92 inputs.Put(at, location);
93 }
94
95 Location InAt(uint32_t at) const {
96 return inputs.Get(at);
97 }
98
99 void SetOut(Location location) {
100 output = Location(location);
101 }
102
103 Location Out() const { return output; }
104
105 private:
106 GrowableArray<Location> inputs;
107 Location output;
108
109 DISALLOW_COPY_AND_ASSIGN(LocationSummary);
110};
111
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000112class CodeGenerator : public ArenaObject {
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000113 public:
114 // Compiles the graph to executable instructions. Returns whether the compilation
115 // succeeded.
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000116 void Compile(CodeAllocator* allocator);
117 static CodeGenerator* Create(ArenaAllocator* allocator,
118 HGraph* graph,
119 InstructionSet instruction_set);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000120
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000121 HGraph* GetGraph() const { return graph_; }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000122
123 Label* GetLabelOf(HBasicBlock* block) const;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000124 bool GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000125
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000126 virtual void GenerateFrameEntry() = 0;
127 virtual void GenerateFrameExit() = 0;
128 virtual void Bind(Label* label) = 0;
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000129 virtual void Move(HInstruction* instruction, Location location) = 0;
130 virtual void Push(HInstruction* instruction, Location location) = 0;
131 virtual HGraphVisitor* GetLocationBuilder() = 0;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000132 virtual HGraphVisitor* GetInstructionVisitor() = 0;
133 virtual Assembler* GetAssembler() = 0;
134
135 uint32_t GetFrameSize() const { return frame_size_; }
136 void SetFrameSize(uint32_t size) { frame_size_ = size; }
137
138 void BuildMappingTable(std::vector<uint8_t>* vector) const { }
139 void BuildVMapTable(std::vector<uint8_t>* vector) const { }
Nicolas Geoffray92cf83e2014-03-18 17:59:20 +0000140 void BuildNativeGCMap(
141 std::vector<uint8_t>* vector, const DexCompilationUnit& dex_compilation_unit) const;
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000142
143 protected:
144 explicit CodeGenerator(HGraph* graph)
145 : frame_size_(0),
146 graph_(graph),
147 block_labels_(graph->GetArena(), 0) {
148 block_labels_.SetSize(graph->GetBlocks()->Size());
149 }
150 ~CodeGenerator() { }
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000151
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000152 private:
153 void InitLocations(HInstruction* instruction);
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000154 void CompileBlock(HBasicBlock* block);
Nicolas Geoffraybab4ed72014-03-11 17:53:17 +0000155 void CompileEntryBlock();
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000156
Nicolas Geoffray787c3072014-03-17 10:20:19 +0000157 // Frame size required for this method.
158 uint32_t frame_size_;
159
160 HGraph* const graph_;
Nicolas Geoffrayd4dd2552014-02-28 10:23:58 +0000161
162 // Labels for each block that will be compiled.
163 GrowableArray<Label> block_labels_;
164
165 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
166};
167
168} // namespace art
169
170#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_