Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 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_H_ |
| 18 | #define ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_ |
| 19 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 20 | #include "globals.h" |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 21 | #include "instruction_set.h" |
| 22 | #include "memory_region.h" |
| 23 | #include "nodes.h" |
| 24 | #include "utils/assembler.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 28 | class DexCompilationUnit; |
| 29 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 30 | class 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 Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 41 | struct PcInfo { |
| 42 | uint32_t dex_pc; |
| 43 | uintptr_t native_pc; |
| 44 | }; |
| 45 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 46 | /** |
| 47 | * A Location is an abstraction over the potential location |
| 48 | * of an instruction. It could be in register or stack. |
| 49 | */ |
| 50 | class Location : public ValueObject { |
| 51 | public: |
| 52 | template<typename T> |
| 53 | T reg() const { return static_cast<T>(reg_); } |
| 54 | |
| 55 | Location() : reg_(kInvalid) { } |
| 56 | explicit Location(uword reg) : reg_(reg) { } |
| 57 | |
| 58 | static Location RegisterLocation(uword reg) { |
| 59 | return Location(reg); |
| 60 | } |
| 61 | |
| 62 | bool IsValid() const { return reg_ != kInvalid; } |
| 63 | |
| 64 | Location(const Location& other) : reg_(other.reg_) { } |
| 65 | |
| 66 | Location& operator=(const Location& other) { |
| 67 | reg_ = other.reg_; |
| 68 | return *this; |
| 69 | } |
| 70 | |
| 71 | private: |
| 72 | // The target register for that location. |
| 73 | // TODO: Support stack location. |
| 74 | uword reg_; |
| 75 | static const uword kInvalid = -1; |
| 76 | }; |
| 77 | |
| 78 | /** |
| 79 | * The code generator computes LocationSummary for each instruction so that |
| 80 | * the instruction itself knows what code to generate: where to find the inputs |
| 81 | * and where to place the result. |
| 82 | * |
| 83 | * The intent is to have the code for generating the instruction independent of |
| 84 | * register allocation. A register allocator just has to provide a LocationSummary. |
| 85 | */ |
| 86 | class LocationSummary : public ArenaObject { |
| 87 | public: |
| 88 | explicit LocationSummary(HInstruction* instruction) |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 89 | : inputs(instruction->GetBlock()->GetGraph()->GetArena(), instruction->InputCount()), |
| 90 | temps(instruction->GetBlock()->GetGraph()->GetArena(), 0) { |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 91 | inputs.SetSize(instruction->InputCount()); |
| 92 | for (int i = 0; i < instruction->InputCount(); i++) { |
| 93 | inputs.Put(i, Location()); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | void SetInAt(uint32_t at, Location location) { |
| 98 | inputs.Put(at, location); |
| 99 | } |
| 100 | |
| 101 | Location InAt(uint32_t at) const { |
| 102 | return inputs.Get(at); |
| 103 | } |
| 104 | |
| 105 | void SetOut(Location location) { |
| 106 | output = Location(location); |
| 107 | } |
| 108 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 109 | void AddTemp(Location location) { |
| 110 | temps.Add(location); |
| 111 | } |
| 112 | |
| 113 | Location GetTemp(uint32_t at) const { |
| 114 | return temps.Get(at); |
| 115 | } |
| 116 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 117 | Location Out() const { return output; } |
| 118 | |
| 119 | private: |
| 120 | GrowableArray<Location> inputs; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 121 | GrowableArray<Location> temps; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 122 | Location output; |
| 123 | |
| 124 | DISALLOW_COPY_AND_ASSIGN(LocationSummary); |
| 125 | }; |
| 126 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 127 | class CodeGenerator : public ArenaObject { |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 128 | public: |
| 129 | // Compiles the graph to executable instructions. Returns whether the compilation |
| 130 | // succeeded. |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 131 | void Compile(CodeAllocator* allocator); |
| 132 | static CodeGenerator* Create(ArenaAllocator* allocator, |
| 133 | HGraph* graph, |
| 134 | InstructionSet instruction_set); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 135 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 136 | HGraph* GetGraph() const { return graph_; } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 137 | |
| 138 | Label* GetLabelOf(HBasicBlock* block) const; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 139 | bool GoesToNextBlock(HBasicBlock* current, HBasicBlock* next) const; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 140 | |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 141 | virtual void GenerateFrameEntry() = 0; |
| 142 | virtual void GenerateFrameExit() = 0; |
| 143 | virtual void Bind(Label* label) = 0; |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 144 | virtual void Move(HInstruction* instruction, Location location) = 0; |
| 145 | virtual void Push(HInstruction* instruction, Location location) = 0; |
| 146 | virtual HGraphVisitor* GetLocationBuilder() = 0; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 147 | virtual HGraphVisitor* GetInstructionVisitor() = 0; |
| 148 | virtual Assembler* GetAssembler() = 0; |
| 149 | |
| 150 | uint32_t GetFrameSize() const { return frame_size_; } |
| 151 | void SetFrameSize(uint32_t size) { frame_size_ = size; } |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 152 | uint32_t GetCoreSpillMask() const { return core_spill_mask_; } |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 153 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 154 | void RecordPcInfo(uint32_t dex_pc) { |
| 155 | struct PcInfo pc_info; |
| 156 | pc_info.dex_pc = dex_pc; |
| 157 | pc_info.native_pc = GetAssembler()->CodeSize(); |
| 158 | pc_infos_.Add(pc_info); |
| 159 | } |
| 160 | |
| 161 | void BuildMappingTable(std::vector<uint8_t>* vector) const; |
| 162 | void BuildVMapTable(std::vector<uint8_t>* vector) const; |
Nicolas Geoffray | 92cf83e | 2014-03-18 17:59:20 +0000 | [diff] [blame] | 163 | void BuildNativeGCMap( |
| 164 | std::vector<uint8_t>* vector, const DexCompilationUnit& dex_compilation_unit) const; |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 165 | |
| 166 | protected: |
| 167 | explicit CodeGenerator(HGraph* graph) |
| 168 | : frame_size_(0), |
| 169 | graph_(graph), |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 170 | block_labels_(graph->GetArena(), 0), |
| 171 | pc_infos_(graph->GetArena(), 32) { |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 172 | block_labels_.SetSize(graph->GetBlocks()->Size()); |
| 173 | } |
| 174 | ~CodeGenerator() { } |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 175 | |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 176 | // Frame size required for this method. |
| 177 | uint32_t frame_size_; |
| 178 | uint32_t core_spill_mask_; |
| 179 | |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 180 | private: |
| 181 | void InitLocations(HInstruction* instruction); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 182 | void CompileBlock(HBasicBlock* block); |
Nicolas Geoffray | bab4ed7 | 2014-03-11 17:53:17 +0000 | [diff] [blame] | 183 | void CompileEntryBlock(); |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 184 | |
Nicolas Geoffray | 787c307 | 2014-03-17 10:20:19 +0000 | [diff] [blame] | 185 | HGraph* const graph_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 186 | |
| 187 | // Labels for each block that will be compiled. |
| 188 | GrowableArray<Label> block_labels_; |
Nicolas Geoffray | 8ccc3f5 | 2014-03-19 10:34:11 +0000 | [diff] [blame] | 189 | GrowableArray<PcInfo> pc_infos_; |
Nicolas Geoffray | d4dd255 | 2014-02-28 10:23:58 +0000 | [diff] [blame] | 190 | |
| 191 | DISALLOW_COPY_AND_ASSIGN(CodeGenerator); |
| 192 | }; |
| 193 | |
| 194 | } // namespace art |
| 195 | |
| 196 | #endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_ |