-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconstprop.c
More file actions
208 lines (189 loc) · 5.93 KB
/
constprop.c
File metadata and controls
208 lines (189 loc) · 5.93 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
/*
********************************************************************************
CONSTPROP.C : IMPLEMENT THE DOWNSTREAM CONSTANT PROPOGATION OPTIMIZATION HERE
*********************************************************************************
*/
#include "constprop.h"
refConst *lastNode, *headNode;
/*
*************************************************************************************
YOUR CODE IS TO BE FILLED IN THE GIVEN TODO BLANKS. YOU CAN CHOOSE TO USE ALL
UTILITY FUNCTIONS OR NONE. YOU CAN ADD NEW FUNCTIONS. BUT DO NOT FORGET TO
DECLARE THEM IN THE HEADER FILE.
**************************************************************************************
*/
/*
***********************************************************************
FUNCTION TO FREE THE CONSTANTS-ASSOCIATED VARIABLES LIST
************************************************************************
*/
void FreeConstList()
{
refConst *tmp;
while (headNode != NULL)
{
tmp = headNode;
headNode = headNode->next;
free(tmp);
}
}
/*
*************************************************************************
FUNCTION TO ADD A CONSTANT VALUE AND THE ASSOCIATED VARIABLE TO THE LIST
**************************************************************************
*/
void UpdateConstList(char *name, long val)
{
refConst *node = malloc(sizeof(refConst));
if (node == NULL)
return;
node->name = name;
node->val = val;
node->next = NULL;
if (headNode == NULL)
{
lastNode = node;
headNode = node;
}
else
{
lastNode->next = node;
lastNode = node;
}
}
/*
*****************************************************************************
FUNCTION TO LOOKUP IF A CONSTANT ASSOCIATED VARIABLE IS ALREADY IN THE LIST
******************************************************************************
*/
refConst *LookupConstList(char *name)
{
refConst *node;
node = headNode;
while (node != NULL)
{
if (!strcmp(name, node->name))
return node;
node = node->next;
}
return NULL;
}
/*
**********************************************************************************************************************************
YOU CAN MAKE CHANGES/ADD AUXILLIARY FUNCTIONS BELOW THIS LINE
**********************************************************************************************************************************
*/
/*
************************************************************************************
THIS FUNCTION IS MEANT TO UPDATE THE CONSTANT LIST WITH THE ASSOCIATED VARIABLE
AND CONSTANT VALUE WHEN ONE IS SEEN. IT SHOULD ALSO PROPOGATE THE CONSTANTS WHEN
WHEN APPLICABLE. YOU CAN ADD A NEW FUNCTION IF YOU WISH TO MODULARIZE BETTER.
*************************************************************************************
*/
void TrackConst(NodeList *statements)
{
Node *node;
while (statements != NULL)
{
node = statements->node;
Node *expression;
refConst *c;
if (node->stmtCode == ASSIGN)
expression = node->right;
else if (node->stmtCode == RETURN)
expression = node->left;
if (expression->exprCode == CONSTANT)
UpdateConstList(node->name, expression->value);
else if (expression->exprCode == VARIABLE || expression->exprCode == PARAMETER)
{
c = LookupConstList(expression->name);
if (c != NULL)
{
expression->left = NULL;
expression->exprCode = CONSTANT;
free(expression->name);
expression->name = NULL;
expression->value = c->val;
}
}
else if (expression->exprCode == OPERATION)
{
if (expression->opCode == NEGATE)
{
c = LookupConstList(expression->left->name);
if (c != NULL)
{
expression->exprCode = CONSTANT;
expression->opCode = O_NONE;
free(expression->name);
expression->name = NULL;
expression->value = -c->val;
FreeVariable(expression->left);
expression->left = NULL;
}
}
else if (expression->opCode == FUNCTIONCALL)
{
NodeList *arg = expression->arguments;
while (arg != NULL)
{
c = LookupConstList(arg->node->name);
if (c != NULL)
{
arg->node->exprCode = CONSTANT;
arg->node->opCode = O_NONE;
free(arg->node->name);
arg->node->name = NULL;
arg->node->value = c->val;
}
arg = arg->next;
}
}
else
{
if (expression->left->exprCode == VARIABLE || expression->left->exprCode == PARAMETER)
{
c = LookupConstList(expression->left->name);
if (c != NULL)
{
expression->left->left = NULL;
expression->left->exprCode = CONSTANT;
expression->left->opCode = O_NONE;
free(expression->left->name);
expression->left->name = NULL;
expression->left->value = c->val;
}
}
if (expression->right->exprCode == VARIABLE || expression->right->exprCode == PARAMETER)
{
c = LookupConstList(expression->right->name);
if (c != NULL)
{
expression->right->left = NULL;
expression->right->exprCode = CONSTANT;
expression->right->opCode = O_NONE;
free(expression->right->name);
expression->right->name = NULL;
expression->right->value = c->val;
}
}
}
}
statements = statements->next;
}
}
bool ConstProp(NodeList *worklist)
{
while (worklist != NULL)
{
TrackConst(worklist->node->statements);
worklist = worklist->next;
FreeConstList();
}
return madeChange;
}
/*
**********************************************************************************************************************************
YOU CAN MAKE CHANGES/ADD AUXILLIARY FUNCTIONS ABOVE THIS LINE
**********************************************************************************************************************************
*/