blob: 0ebd4d02d40cb8b5388ae46524fd10b3ab4669ce [file] [log] [blame]
Dragos Sbirlea7467ee02013-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#include "dex_file.h"
18#include "dex_instruction.h"
19
20#ifndef SEA_IR_H_
21#define SEA_IR_H_
22
23#include <set>
24#include <map>
25
26namespace sea_ir {
27
28
29class SeaNode {
30 public:
31 explicit SeaNode(const art::Instruction* in):id_(GetNewId()), instruction_(in), successors_() {};
32 explicit SeaNode():id_(GetNewId()), instruction_(NULL) {};
33 void AddSuccessor(SeaNode* successor);
34 const art::Instruction* GetInstruction() {
35 DCHECK(NULL != instruction_);
36 return instruction_;
37 }
38 std::string StringId() const;
39 // Returns a dot language formatted string representing the node and
40 // (by convention) outgoing edges, so that the composition of theToDot() of all nodes
41 // builds a complete dot graph (without prolog and epilog though).
42 virtual std::string ToDot() const;
43 virtual ~SeaNode(){};
44
45 protected:
46 // Returns the id of the current block as string
47
48 static int GetNewId() {
49 return current_max_node_id_++;
50 }
51
52
53 private:
54 const int id_;
55 const art::Instruction* const instruction_;
56 std::vector<sea_ir::SeaNode*> successors_;
57 static int current_max_node_id_;
58};
59
60
61
62class Region : public SeaNode {
63 public:
64 explicit Region():SeaNode() {}
65 void AddChild(sea_ir::SeaNode* instruction);
66 SeaNode* GetLastChild() const;
67
68 // Returns a dot language formatted string representing the node and
69 // (by convention) outgoing edges, so that the composition of theToDot() of all nodes
70 // builds a complete dot graph (without prolog and epilog though).
71 virtual std::string ToDot() const;
72
73 private:
74 std::vector<sea_ir::SeaNode*> instructions_;
75};
76
77
78
79class SeaGraph {
80 public:
81 static SeaGraph* GetCurrentGraph();
82 void CompileMethod(const art::DexFile::CodeItem* code_item,
83 uint32_t class_def_idx, uint32_t method_idx, const art::DexFile& dex_file);
84 // Returns a string representation of the region and its Instruction children
85 void DumpSea(std::string filename) const;
86 /*** Static helper functions follow: ***/
87 static int ParseInstruction(const uint16_t* code_ptr,
88 art::DecodedInstruction* decoded_instruction);
89 static bool IsInstruction(const uint16_t* code_ptr);
90
91 private:
92 // Registers the parameter as a child region of the SeaGraph instance
93 void AddRegion(Region* r);
94 // Returns new region and registers it with the SeaGraph instance
95 Region* GetNewRegion();
96 static SeaGraph graph_;
97 std::vector<Region*> regions_;
98};
99
100
101} // end namespace sea_ir
102#endif