blob: 2a5ae7d751d4c9115fbe0a9fe055d22a2db56ab2 [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
20#include "instruction_set.h"
21#include "memory_region.h"
22#include "nodes.h"
23#include "utils/assembler.h"
24
25namespace art {
26
27class CodeAllocator {
28 public:
29 CodeAllocator() { }
30 virtual ~CodeAllocator() { }
31
32 virtual uint8_t* Allocate(size_t size) = 0;
33
34 private:
35 DISALLOW_COPY_AND_ASSIGN(CodeAllocator);
36};
37
38class CodeGenerator : public HGraphVisitor {
39 public:
40 // Compiles the graph to executable instructions. Returns whether the compilation
41 // succeeded.
42 static bool CompileGraph(
43 HGraph* graph, InstructionSet instruction_set, CodeAllocator* allocator);
44
45 Assembler* assembler() const { return assembler_; }
46
47 // Visit functions for instruction classes.
48#define DECLARE_VISIT_INSTRUCTION(name) \
49 virtual void Visit##name(H##name* instr) = 0;
50
51 FOR_EACH_INSTRUCTION(DECLARE_VISIT_INSTRUCTION)
52
53#undef DECLARE_VISIT_INSTRUCTION
54
55 protected:
56 CodeGenerator(Assembler* assembler, HGraph* graph)
57 : HGraphVisitor(graph), assembler_(assembler), block_labels_(graph->arena(), 0) {
58 block_labels_.SetSize(graph->blocks()->Size());
59 }
60
61 Label* GetLabelOf(HBasicBlock* block) const;
62 bool GoesToNextBlock(HGoto* got) const;
63
64 private:
65 virtual void GenerateFrameEntry() = 0;
66 virtual void GenerateFrameExit() = 0;
67 virtual void Bind(Label* label) = 0;
68
69 void Compile(CodeAllocator* allocator);
70 void CompileBlock(HBasicBlock* block);
71
72 Assembler* const assembler_;
73
74 // Labels for each block that will be compiled.
75 GrowableArray<Label> block_labels_;
76
77 DISALLOW_COPY_AND_ASSIGN(CodeGenerator);
78};
79
80} // namespace art
81
82#endif // ART_COMPILER_OPTIMIZING_CODE_GENERATOR_H_