blob: b1bc4dc2da5523f4f3598e01056d6443267f957b [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
Dragos Sbirleabfaf44f2013-08-06 15:41:44 -070017#ifndef ART_COMPILER_SEA_IR_CODE_GEN_CODE_GEN_H_
18#define ART_COMPILER_SEA_IR_CODE_GEN_CODE_GEN_H_
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070019
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"
Dragos Sbirleabfaf44f2013-08-06 15:41:44 -070025#include "sea_ir/ir/visitor.h"
Dragos Sbirlea0e260a32013-06-21 09:20:34 -070026
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) { }
Dragos Sbirlea6547fa92013-08-05 18:33:30 -0700104
105 void Visit(UnnamedConstInstructionNode* instruction) { }
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700106 void Visit(ConstInstructionNode* instruction) { }
107 void Visit(ReturnInstructionNode* instruction) { }
108 void Visit(IfNeInstructionNode* instruction) { }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700109 // void Visit(AddIntLitInstructionNode* instruction) { }
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700110 void Visit(MoveResultInstructionNode* instruction) { }
111 void Visit(InvokeStaticInstructionNode* instruction) { }
112 void Visit(AddIntInstructionNode* instruction) { }
113 void Visit(GotoInstructionNode* instruction) { }
114 void Visit(IfEqzInstructionNode* instruction) { }
115 void Visit(PhiInstructionNode* region);
116};
117
118class CodeGenPostpassVisitor: public CodeGenPassVisitor {
119 public:
120 explicit CodeGenPostpassVisitor(CodeGenData* code_gen_data): CodeGenPassVisitor(code_gen_data) { }
121 void Visit(SeaGraph* graph);
122 void Visit(SignatureNode* region);
123 void Visit(Region* region);
124 void Visit(InstructionNode* region) { }
Dragos Sbirlea6547fa92013-08-05 18:33:30 -0700125 void Visit(UnnamedConstInstructionNode* instruction) { }
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700126 void Visit(ConstInstructionNode* instruction) { }
127 void Visit(ReturnInstructionNode* instruction) { }
128 void Visit(IfNeInstructionNode* instruction) { }
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700129 // void Visit(AddIntLitInstructionNode* instruction) { }
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700130 void Visit(MoveResultInstructionNode* instruction) { }
131 void Visit(InvokeStaticInstructionNode* instruction) { }
132 void Visit(AddIntInstructionNode* instruction) { }
133 void Visit(GotoInstructionNode* instruction) { }
134 void Visit(IfEqzInstructionNode* instruction) { }
135 void Visit(PhiInstructionNode* region);
136};
137
138class CodeGenVisitor: public CodeGenPassVisitor {
139 public:
140 explicit CodeGenVisitor(CodeGenData* code_gen_data): CodeGenPassVisitor(code_gen_data) { }
141 void Visit(SeaGraph* graph);
142 void Visit(SignatureNode* region);
143 void Visit(Region* region);
144 void Visit(InstructionNode* region);
Dragos Sbirlea6547fa92013-08-05 18:33:30 -0700145 void Visit(UnnamedConstInstructionNode* instruction);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700146 void Visit(ConstInstructionNode* instruction);
147 void Visit(ReturnInstructionNode* instruction);
148 void Visit(IfNeInstructionNode* instruction);
Dragos Sbirlea0e260a32013-06-21 09:20:34 -0700149 void Visit(MoveResultInstructionNode* instruction);
150 void Visit(InvokeStaticInstructionNode* instruction);
151 void Visit(AddIntInstructionNode* instruction);
152 void Visit(GotoInstructionNode* instruction);
153 void Visit(IfEqzInstructionNode* instruction);
154 void Visit(PhiInstructionNode* region) { }
155};
Brian Carlstrom7934ac22013-07-26 10:54:15 -0700156} // namespace sea_ir
Dragos Sbirleabfaf44f2013-08-06 15:41:44 -0700157#endif // ART_COMPILER_SEA_IR_CODE_GEN_CODE_GEN_H_