blob: f656453559f713d78483c415fc49ec411b2362a8 [file] [log] [blame]
Dragos Sbirlea0e260a32013-06-21 09:20:34 -07001/*
2 * Copyright (C) 2013 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_SEA_IR_CODE_GEN_H_
18#define ART_COMPILER_SEA_IR_CODE_GEN_H_
19
Dragos Sbirleab40eddf2013-07-31 13:37:31 -070020#include "llvm/Analysis/Verifier.h"
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070021#include "llvm/IR/IRBuilder.h"
22#include "llvm/IR/LLVMContext.h"
23#include "llvm/IR/Module.h"
24#include "llvm/Analysis/Verifier.h"
25#include "visitor.h"
26
27namespace sea_ir {
28// Abstracts away the containers we use to map SEA IR objects to LLVM IR objects.
29class CodeGenData {
30 public:
31 explicit CodeGenData(): context_(&llvm::getGlobalContext()), module_("sea_ir", *context_),
32 builder_(*context_), function_(), blocks_(), values_() { }
33 // Returns the llvm::BasicBlock* corresponding to the sea_ir::Region with id @region_id.
34 llvm::BasicBlock* GetBlock(int region_id) {
35 std::map<int, llvm::BasicBlock*>::iterator block_it = blocks_.find(region_id);
36 DCHECK(block_it != blocks_.end());
37 return block_it->second;
38 }
39 // Returns the llvm::BasicBlock* corresponding top the sea_ir::Region @region.
40 llvm::BasicBlock* GetBlock(Region* region) {
41 return GetBlock(region->Id());
42 }
43 // Records @block as corresponding to the sea_ir::Region with id @region_id.
44 void AddBlock(int region_id, llvm::BasicBlock* block) {
45 blocks_.insert(std::pair<int, llvm::BasicBlock*>(region_id, block));
46 }
47 // Records @block as corresponding to the sea_ir::Region with @region.
48 void AddBlock(Region* region, llvm::BasicBlock* block) {
49 AddBlock(region->Id(), block);
50 }
51
52 llvm::Value* GetValue(int instruction_id) {
53 std::map<int, llvm::Value*>::iterator value_it = values_.find(instruction_id);
54 DCHECK(value_it != values_.end());
55 return value_it->second;
56 }
57 // Returns the llvm::Value* corresponding to the output of @instruction.
58 llvm::Value* GetValue(InstructionNode* instruction) {
59 return GetValue(instruction->Id());
60 }
61 // Records @value as corresponding to the sea_ir::InstructionNode with id @instruction_id.
62 void AddValue(int instruction_id, llvm::Value* value) {
63 values_.insert(std::pair<int, llvm::Value*>(instruction_id, value));
64 }
65 // Records @value as corresponding to the sea_ir::InstructionNode @instruction.
66 void AddValue(InstructionNode* instruction, llvm::Value* value) {
67 AddValue(instruction->Id(), value);
68 }
69
70 llvm::LLVMContext* const context_;
71 llvm::Module module_;
72 llvm::IRBuilder<> builder_;
73 llvm::Function* function_;
74
75 private:
76 std::map<int, llvm::BasicBlock*> blocks_;
77 std::map<int, llvm::Value*> values_;
78};
79
80class CodeGenPassVisitor: public IRVisitor {
81 public:
82 explicit CodeGenPassVisitor(CodeGenData* cgd): llvm_data_(cgd) { }
83 CodeGenPassVisitor(): llvm_data_(new CodeGenData()) { }
84 // Initialize any data structure needed before the start of visiting.
85 virtual void Initialize(SeaGraph* graph);
86 CodeGenData* GetData() {
87 return llvm_data_;
88 }
89 void Write(std::string file) {
90 llvm_data_->module_.dump();
91 llvm::verifyFunction(*llvm_data_->function_);
92 }
93
94 protected:
95 CodeGenData* const llvm_data_;
96};
97
98class CodeGenPrepassVisitor: public CodeGenPassVisitor {
99 public:
100 void Visit(SeaGraph* graph);
101 void Visit(SignatureNode* region);
102 void Visit(Region* region);
103 void Visit(InstructionNode* instruction) { }
104 void Visit(ConstInstructionNode* instruction) { }
105 void Visit(ReturnInstructionNode* instruction) { }
106 void Visit(IfNeInstructionNode* instruction) { }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700107 // void Visit(AddIntLitInstructionNode* instruction) { }
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700108 void Visit(MoveResultInstructionNode* instruction) { }
109 void Visit(InvokeStaticInstructionNode* instruction) { }
110 void Visit(AddIntInstructionNode* instruction) { }
111 void Visit(GotoInstructionNode* instruction) { }
112 void Visit(IfEqzInstructionNode* instruction) { }
113 void Visit(PhiInstructionNode* region);
114};
115
116class CodeGenPostpassVisitor: public CodeGenPassVisitor {
117 public:
118 explicit CodeGenPostpassVisitor(CodeGenData* code_gen_data): CodeGenPassVisitor(code_gen_data) { }
119 void Visit(SeaGraph* graph);
120 void Visit(SignatureNode* region);
121 void Visit(Region* region);
122 void Visit(InstructionNode* region) { }
123 void Visit(ConstInstructionNode* instruction) { }
124 void Visit(ReturnInstructionNode* instruction) { }
125 void Visit(IfNeInstructionNode* instruction) { }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700126 // void Visit(AddIntLitInstructionNode* instruction) { }
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700127 void Visit(MoveResultInstructionNode* instruction) { }
128 void Visit(InvokeStaticInstructionNode* instruction) { }
129 void Visit(AddIntInstructionNode* instruction) { }
130 void Visit(GotoInstructionNode* instruction) { }
131 void Visit(IfEqzInstructionNode* instruction) { }
132 void Visit(PhiInstructionNode* region);
133};
134
135class CodeGenVisitor: public CodeGenPassVisitor {
136 public:
137 explicit CodeGenVisitor(CodeGenData* code_gen_data): CodeGenPassVisitor(code_gen_data) { }
138 void Visit(SeaGraph* graph);
139 void Visit(SignatureNode* region);
140 void Visit(Region* region);
141 void Visit(InstructionNode* region);
142 void Visit(ConstInstructionNode* instruction);
143 void Visit(ReturnInstructionNode* instruction);
144 void Visit(IfNeInstructionNode* instruction);
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700145 // void Visit(AddIntLitInstructionNode* instruction);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700146 void Visit(MoveResultInstructionNode* instruction);
147 void Visit(InvokeStaticInstructionNode* instruction);
148 void Visit(AddIntInstructionNode* instruction);
149 void Visit(GotoInstructionNode* instruction);
150 void Visit(IfEqzInstructionNode* instruction);
151 void Visit(PhiInstructionNode* region) { }
152};
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700153} // namespace sea_ir
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700154#endif // ART_COMPILER_SEA_IR_CODE_GEN_H_