-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfg.h
89 lines (67 loc) · 2.38 KB
/
cfg.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
#pragma once
#include <nlohmann/json.hpp>
#include <iostream>
#include <vector>
#include <unordered_set>
#include <unordered_map>
#define TERMNI_SIZE 3
using json = nlohmann::json;
static std::string terminator[3] = {"jmp", "br", "ret"};
struct Block{
int id;
bool fall_through;
std::unordered_set<int> predcessors;
std::unordered_set<int> successors;
std::vector<json> instrs;
Block(int new_id): id(new_id), fall_through(true){}
//Getters
size_t size(){ return instrs.size(); }
bool empty(){ return instrs.empty(); }
//Setters
void clear();
void setID(int new_id) { id = new_id; }
//Member functions
void LinkTo(Block &successor);
};
class BasicBlocks{
using iterator = std::vector<Block>::iterator;
using const_iter = std::vector<Block>::const_iterator;
public:
std::string function_name;
std::vector<Block> blocks;
BasicBlocks(const json &input);
//STL section
iterator begin(){ return blocks.begin(); }
iterator end(){ return blocks.end(); }
const_iter begin() const{ return blocks.begin(); }
const_iter end() const{ return blocks.end(); }
size_t size(){return blocks.size(); }
Block& operator[](unsigned int i){ return blocks[i]; }
//getters
json &getArgs();
bool hasArgs();
//Other member functions
json Dump();
private:
std::string return_type;
json args;
void AddInst(std::vector<Block> &blocks, Block ¤t_block, const json &inst);
};
class CFG{
public:
std::unordered_map<std::string, int> label_2_block;
BasicBlocks blocks;
CFG(const BasicBlocks &blocks): blocks(blocks){}
void ExtractCFG();
void Dump();
private:
std::unordered_map<std::string, std::vector<int>> fwd_decl_preds; //forward declared predcessors
void AddInst(std::vector<Block> &blocks, Block ¤t_block, const json &inst,
std::unordered_map<std::string, int> &label_2_block,
std::unordered_map<std::string, std::vector<int>> &fwd_decl_preds);
void Link2NewLabel(BasicBlocks &blocks, Block ¤t_block, const std::string label,
std::unordered_map<std::string, std::vector<int>> &fwd_decl_preds);
void LinkJumps(BasicBlocks &blocks, Block ¤t_block, const json &labels,
std::unordered_map<std::string, int> &label_2_block,
std::unordered_map<std::string, std::vector<int>> &fwd_decl_preds);
};