-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathflowgraph.c
91 lines (82 loc) · 2.12 KB
/
flowgraph.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
81
82
83
84
85
86
87
88
89
90
91
#include <stdio.h>
#include "util.h"
#include "symbol.h"
#include "temp.h"
#include "tree.h"
#include "absyn.h"
#include "assem.h"
#include "frame.h"
#include "graph.h"
#include "flowgraph.h"
#include "errormsg.h"
#include "table.h"
Temp_tempList FG_def(G_node n) {
//your code here.
if (!n) return NULL;
AS_instr inst = G_nodeInfo(n);
switch (inst->kind) {
case I_OPER: return inst->u.OPER.dst;
case I_LABEL: break;
case I_MOVE: return inst->u.MOVE.dst;
}
return NULL;
}
Temp_tempList FG_use(G_node n) {
//your code here.
if (!n) return NULL;
AS_instr inst = G_nodeInfo(n);
switch (inst->kind) {
case I_OPER: return inst->u.OPER.src;
case I_LABEL: break;
case I_MOVE: return inst->u.MOVE.src;
}
return NULL;
}
bool FG_isMove(G_node n) {
//your code here.
if (!n) return FALSE;
AS_instr inst = G_nodeInfo(n);
switch (inst->kind) {
case I_OPER: break;
case I_LABEL: break;
case I_MOVE: return TRUE;
}
return FALSE;
}
static void FG_addJumpEdges(TAB_table t, G_node n)
{
AS_instr i = (AS_instr)G_nodeInfo(n);
if (!i->u.OPER.jumps) return;
Temp_labelList tl = i->u.OPER.jumps->labels;
G_node neighbour = NULL;
for (; tl; tl = tl->tail) {
neighbour = (G_node)TAB_look(t, tl->head);
if (neighbour && !G_goesTo(n, neighbour)) G_addEdge(n, neighbour);
}
}
G_graph FG_AssemFlowGraph(AS_instrList il) {
//your code here.
G_graph g = G_Graph();
G_node current = NULL, prev = NULL;
G_nodeList nodes = NULL;
TAB_table tb = TAB_empty();
AS_instr i;
for (; il; il = il->tail) {
i = il->head;
current = G_Node(g, i);
if (prev)
G_addEdge(prev, current);
prev = current;
switch (i->kind) {
case I_LABEL: TAB_enter(tb, i->u.LABEL.label, current); break;
case I_MOVE: break;
case I_OPER: nodes = G_NodeList(current, nodes); break;
default: assert(0 && "invalid instr kind");
}
}
for (; nodes; nodes = nodes->tail) {
current = nodes->head;
FG_addJumpEdges(tb, current);
}
return g;
}