-
Notifications
You must be signed in to change notification settings - Fork 0
/
cfg.c
80 lines (60 loc) · 1.79 KB
/
cfg.c
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
#include <stdlib.h>
#include <stdint.h>
struct CFGBlock;
typedef struct {
struct CFGBlock *source;
struct CFGBlock *destination;
} CFGEdge;
typedef struct CFGBlock {
Instruction *instructions;
int numInstructions;
CFGEdge *successors;
int numSuccessors;
CFGEdge *predecessors;
int numPredecessors;
int blockID;
} CFGBlock;
typedef struct {
CFGBlock **cfgBlocks;
int numBlocks;
} ControlFlowGraph;
CFGBlock createCFGBlock(void) {
CFGBlock block;
block.instructions = NULL;
block.numInstructions = 0;
block.successors = NULL;
block.numSuccessors = 0;
block.predecessors = NULL;
block.numPredecessors = 0;
block.blockID = -1;
return block;
}
ControlFlowGraph createControlFlowGraph(void) {
ControlFlowGraph cfg;
cfg.cfgBlocks = NULL;
cfg.numBlocks = 0;
return cfg;
}
void addEdge(CFGBlock *source, CFGBlock *destination) {
source->successors = zRealloc(source->successors,
(source->numSuccessors + 1) * sizeof(CFGEdge));
source->successors[source->numSuccessors].source = source;
source->successors[source->numSuccessors].destination = destination;
source->numSuccessors++;
destination->predecessors =
zRealloc(destination->predecessors,
(destination->numPredecessors + 1) * sizeof(CFGEdge));
destination->predecessors[destination->numPredecessors].source = source;
destination->predecessors[destination->numPredecessors].destination =
destination;
destination->numPredecessors++;
}
void initializeCFG(ControlFlowGraph *cfg) {
cfg->cfgBlocks = zAlloc(2 * sizeof(CFGBlock));
cfg->cfgBlocks[0] = createCFGBlock();
cfg->cfgBlocks[1] = createCFGBlock();
cfg->cfgBlocks[0].blockID = 0;
cfg->cfgBlocks[1].blockID = 1;
addEdge(&cfg.cfgBlocks[0], &cfg.cfgBlocks[1]);
cfg->numBlocks = 2;
}