blob: babb114c7bf7379877beeb2071a0f38cf485bf46 [file] [log] [blame]
Shih-wei Liaoe94d9b22012-05-22 09:01:24 -07001/*
2 * Copyright (C) 2012 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#include "target_lir_emitter.h"
18
19#include "target_lir_info.h"
20#include "target_lir_opcodes.h"
21
22#include "intrinsic_helper.h"
23#include "lir_function.h"
24
25#include <llvm/Function.h>
26
27namespace art {
28namespace greenland {
29
30TargetLIREmitter::TargetLIREmitter(const llvm::Function& func,
31 const OatCompilationUnit& cunit,
32 DexLang::Context& dex_lang_ctx,
33 TargetLIRInfo& target_lir_info)
34 : func_(func), cunit_(cunit), dex_lang_ctx_(dex_lang_ctx.IncRef()),
35 info_(target_lir_info), lir_func_() {
36 return;
37}
38
39TargetLIREmitter::~TargetLIREmitter() {
40 dex_lang_ctx_.DecRef();
41 return;
42}
43
44bool TargetLIREmitter::visitBasicBlock(const llvm::BasicBlock& bb) {
45 // Place the corresponding block label to the output
46 llvm::DenseMap<const llvm::BasicBlock*, LIR*>::const_iterator label_iter =
47 block_labels_.find(&bb);
48
49 DCHECK(label_iter != block_labels_.end());
50 lir_func_.push_back(label_iter->second);
51
52 // Now, iterate over and process all instructions within the basic block
53 for (llvm::BasicBlock::const_iterator inst_iter = bb.begin(),
54 inst_end = bb.end(); inst_iter != inst_end; inst_iter++) {
55 const llvm::Instruction& inst = *inst_iter;
56 switch (inst.getOpcode()) {
57#define VISIT(OPCODE, CLASS) \
58 case llvm::Instruction::OPCODE: { \
59 if (!visit ## CLASS(static_cast<const llvm::CLASS&>(inst))) { \
60 return false; \
61 } \
62 break; \
63 }
64
65 VISIT(Ret, ReturnInst);
66 VISIT(Br, BranchInst);
67 VISIT(ICmp, ICmpInst);
68 VISIT(IntToPtr, IntToPtrInst);
69 VISIT(Call, CallInst);
70
71#undef VISIT
72 default : {
73 LOG(INFO) << "Unhandled instruction hit!";
74 inst.dump();
75 return false;
76 }
77 }
78 }
79
80 return true;
81}
82
83bool TargetLIREmitter::visitReturnInst(const llvm::ReturnInst& inst) {
84 inst.dump();
85 return true;
86}
87
88bool TargetLIREmitter::visitBranchInst(const llvm::BranchInst& inst) {
89 inst.dump();
90 return true;
91}
92
93bool TargetLIREmitter::visitICmpInst(const llvm::ICmpInst& inst) {
94 inst.dump();
95 return true;
96}
97
98bool TargetLIREmitter::visitIntToPtrInst(const llvm::IntToPtrInst& inst) {
99 inst.dump();
100 return true;
101}
102
103bool TargetLIREmitter::visitCallInst(const llvm::CallInst& inst) {
104 // The callee must be a DexLang intrinsic
105 return visitDexLangIntrinsics(inst);
106}
107
108bool TargetLIREmitter::visitDexLangIntrinsics(const llvm::CallInst& inst) {
109 const llvm::Function* callee = inst.getCalledFunction();
110 IntrinsicHelper::IntrinsicId intr_id =
111 dex_lang_ctx_.GetIntrinsicHelper().GetIntrinsicId(callee);
112
113 if (intr_id == IntrinsicHelper::UnknownId) {
114 LOG(INFO) << "Unexpected call instruction to '"
115 << callee->getName().str() << "'";
116 return false;
117 }
118
119 //const IntrinsicHelper::IntrinsicInfo& intr_info =
120 // IntrinsicHelper::GetInfo(intr_id);
121
122
123 return true;
124}
125
126LIRFunction* TargetLIREmitter::Emit() {
127 if (EmitBasicBlockLabels() &&
128 EmitEntrySequence() &&
129 EmitInstructions() &&
130 EmitExitSequence()) {
131 return &lir_func_;
132 }
133
134 return NULL;
135}
136
137bool TargetLIREmitter::EmitBasicBlockLabels() {
138 for (llvm::Function::const_iterator bb_iter = func_.begin(),
139 bb_end = func_.end(); bb_iter != bb_end; bb_iter++) {
140 LIR* lir = lir_func_.CreateLIR(info_.GetLIRDesc(opcode::kBlockLabel));
141 CHECK(block_labels_.insert(std::make_pair(bb_iter, lir)).second);
142 }
143 return true;
144}
145
146bool TargetLIREmitter::EmitEntrySequence() {
147 // Flush all function arguments to the virtual registers
148 return true;
149}
150
151bool TargetLIREmitter::EmitInstructions() {
152 // Iterator over all basic blocks
153 for (llvm::Function::const_iterator bb_iter = func_.begin(),
154 bb_end = func_.end(); bb_iter != bb_end; bb_iter++) {
155 if (!visitBasicBlock(*bb_iter)) {
156 return false;
157 }
158 }
159 return true;
160}
161
162bool TargetLIREmitter::EmitExitSequence() {
163 return true;
164}
165
166} // namespace greenland
167} // namespace art