forked from SEL4PROJ/chronos4.2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsymexec_value.c
302 lines (289 loc) · 9.93 KB
/
symexec_value.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
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "symexec.h"
FILE *dbgF;
/*** PARAMETER VALUE OPERATION ***/
void setNewPara(void *para) {
static int paraCount = 0;
char name[4];
int dbg = 1;
sprintf(name,"V%d",paraCount);
paraCount++;
para = calloc(strlen(name)+1,sizeof(char);
strcpy(para,name);
if (dbg) fprintf(dbgF,"\nNew para: %s",name);
}
/*** INDUCTION VALUE OPERATION ***/
int updateInitVal(biv_p biVar, char* initVal) {
int dbg = 0;
}
int cmpBIV(biv_p inVar1, biv_p inVar2) {
if (cmpReg(
if (inVar1->stride != inVar2->stride) return 1;
if (strcmp(inVar1->regName, inVar2->regName)!=0) return 1;
return 0;
}
int cpyBIV(biv_p varDst, biv_p varSrc) {
varDst->insn = varSrc->insn;
setReg(varDst->initValue,varSrc->initValue.type,varSrc->initValue.value);
strcpy(varDst->opr,varSrc->opr);
varDst->stride = varSrc->stride;
}
void printBIV(FILE *fp, biv_p biVar) {
fprintf(fp," BIV (%s,%s,%d) ",biVar->regName,biVar->init,biVar->stride);
fflush(fp);
}
/*** EXPRESSION VALUE OPERATION ***/
void computeExpr(char *opr, expr_p *expr1, expr_p *expr2) {
int dbg = 0;
poly_expr_t *cur1, *cur2, *prev1, *prev2, *num;
int i,flag, disFlag, fin;
int tmp;
/*Decide whether we need to follow distribution rule*/
if (dbg) {
//fprintf(dbgAddr,"\nFlag 0");fflush(dbgAddr);
fprintf(dbgAddr,"\n E1: ");
printExpression(dbgAddr,expr1);
fprintf(dbgAddr," %s E2: ",opr);
printExpression(dbgAddr,expr2);
}
if (opr[0]=='+' || opr[0]=='-') disFlag = 0;
else disFlag = 1;
/*1 expr -> not distributive opr*/
for (cur1 = expr1; cur1; cur1 = cur1->next) cur1->added = 0;
for (cur2 = expr2; cur2; cur2 = cur2->next) cur2->added = 0;
cur1 = expr1;cur2 = expr2;
if (cur1 && cur2 && (cur1->next==NULL && cur2->next==NULL) ) disFlag = 0;
if (disFlag == 0) {/*Non distributive opr like +/-*/
/*1st phase: find expr2's regName similar to expr1's regName
*add their coefficient
*/
while (cur1!=NULL) {
cur2 = expr2;
flag = 0;
fin = 1;
while (cur2!=NULL) { //find similar regName in cur2
if (cur2->added!=0) { //already processed, somehow
cur2 = cur2->next; continue;
}
if (cur2->coef==0) {
cur2->added=2;//ignore 0:x
cur2 = cur2->next; continue;
}
fin = 0;
if (strcmp(cur1->regName,cur2->regName)==0
&& cur2->regName[0]=='$' && cur1->regName[0]=='$'){
flag = 1; break;
}
else if (cur1->regName[0]=='1' && cur2->regName[0]=='1') {
flag = 2; break;
}
cur2 = cur2->next;
}
if (fin==1) break; //has finish searching all regName in expr2
if (dbg) {
//fprintf(dbgAddr,"\nFlag 0");fflush(dbgAddr);
fprintf(dbgAddr,"\n C1: ");
printExpression(dbgAddr,cur1);
fprintf(dbgAddr," %s C2: ",opr);
printExpression(dbgAddr,cur2);
}
switch (flag) {
case 1: /*expr format: c1:$5 + c2:$5*/
/*two induction var: $1 + $1 */
cur1->added=0;
cur2->added=2;
if (strcmp(opr,"+")==0) {
cur1->coef += cur2->coef;
}
else if (strcmp(opr,"-")==0) {
cur1->coef -= cur2->coef;
}
else {
fprintf(dbgAddr,"\nUnknown opr 1: %s %s %s",
cur1->regName,opr,cur2->regName);fflush(stdout);
}
break;
case 2:/*expr format: 4:1 + - * << / 10:1*/
cur1->added=0;
cur2->added=2;
/*two numerical value 1+1 */
if (strcmp(opr,"+")==0) {
cur1->coef += cur2->coef;
}
else if (strcmp(opr,"-")==0) {
cur1->coef -= cur2->coef;
}
else if (strcmp(opr,"*")==0) {
cur1->coef *= cur2->coef;
}
else if (strcmp(opr,"<<")==0) {
for (i=0;i<cur2->coef;i++) cur1->coef*=2;
}
else {
fprintf(dbgAddr,"\nUnknown opr 2: %s %s %s",
cur1->regName,opr,cur2->regName);fflush(stdout);
}
break;
default:
break;
}
cur1 = cur1->next;
}
/*Phase 2: process regName in cur2 with no-equivalent regName in cur1*/
for (cur1=expr1; cur1->next; cur1=cur1->next);//move to end of expr1
while (1) {
cur2 = expr2;
while (cur2!=NULL && cur2->added!=0) cur2=cur2->next;
if (cur2) cur2->added = 1;
else break;//finish, no unprocessed cur2
if (strcmp(opr,"+")==0 || strcmp(opr,"-")==0) {
//$1 +/- num -> do nothing
cur2->added = 3;
cur1->next = cur2;
cur1 = cur2;
}
else {//opr = * , <<
if (cur2->regName[0]=='$' || cur2->regName[0]=='T') {
tmp = cur1->coef;
cur1->coef = cur2->coef;
cur2->coef = tmp;
sprintf(cur1->regName,"%s",cur2->regName);
}
cur2->added = 2;
if (strcmp(opr,"*")==0) {
cur1->coef *= cur2->coef;
}
else if (strcmp(opr,"<<")==0) {
for (i=0;i<cur2->coef;i++) cur1->coef*=2;
}
else {
fprintf(dbgAddr,"\nUnknown %d:%s %s %d:%s",cur1->coef,
cur1->regName,opr,cur2->coef,cur2->regName);
}
}
}
}
else { /*distributive opr like * / <<: e.g. ($5 + 1) << 2 */
if (expr2->next==NULL) {// ($5 + 1) << 2
cur1 = expr1;
cur2 = expr2;
flag = 0;
}
else {// 2 << ($5 + 1), actually mean ($5 + 1) << 2
cur2 = expr1;
cur1 = expr2;
if (dbg) {
fprintf(dbgAddr,"\nReverse expr ");
printExpression(dbgAddr,expr1);
printExpression(dbgAddr,expr2);
}
flag = 1;
}
/*assume no opr like ($5+1) * ($4+3), they are not affine function*/
while (cur2!=NULL) {
if (cur2->regName[0]=='$') {
printf("\nNot affine function");
printExpression(stdout,expr1);
printf(" %s ",opr);
printExpression(stdout,expr2);
fflush(stdout);
exit(1);
}
cur2 = cur2->next;
}
//now cur1: ($5+1) << cur2: 2:1
if (flag) cur2 = expr1;
else cur2 = expr2;
while (cur1!=NULL) {
cur1->added = 2; cur2->added = 2;
if (strcmp(opr,"*")==0) {
cur1->coef *= cur2->coef;
}
else if (strcmp(opr,"/")==0) {
cur1->coef /= cur2->coef;
}
else if (strcmp(opr,"<<")==0) {
for (i=0;i<cur2->coef;i++) cur1->coef*=2;
}
else {
fprintf(dbgAddr,"\nUnknown opr 5: %s %s %s",
cur1->regName,opr,cur2->regName);fflush(stdout);
}
cur1 = cur1->next;
}
if (flag) {// 2 << ($5+1) = 4$5 + 4, copy expr2 to expr1
expr1->coef = expr2->coef;
sprintf(expr1->regName,"%s",expr2->regName);
expr1->added = 0;
expr2->added = 2;
}
}
/*Copy the remaining of expr2 to expr1*/
cur1 = expr1;prev1 = NULL;
while (cur1!=NULL) {prev1= cur1;cur1 = cur1->next;}//find end of cur1
cur2 = expr2;
while (cur2!=NULL) {
if (cur2->added==3) {//copied to expr1
//cannot free
cur2 = cur2->next;
}
else if (cur2->added==2) {//merged to expr1
/*already merge to expr1, free it*/
prev2 = cur2;
cur2 = cur2->next;
if (prev2!=NULL) free(prev2); //NOTE: memory leak
}
else {//cur2->added==1 || cur2->added==0 -> copy forward
cur1 = cur2;
cur2 = cur2->next;
if (prev1!=NULL) {
prev1->next = cur1;
prev1 = cur1;
prev1->next = NULL;
}
else { //expr1 = NULL ???
expr1 = cur2;
prev1 = expr1;
}
}
}
if (dbg) {
fprintf(dbgAddr,"\n = ");
printExpression(dbgAddr,expr1);
}
}
void cpyExpr(expr_p exprDst, expr_p exprSrc) {
expr_p srcNode, dstNode, prvNode;
srcNode = exprSrc;
dstNode = exprDst;
while (dstNode && srcNode) {
dstNode->coef = srcNode->coef;
strcpy(dstNode->var,srcNode->var);
prvNode = dstNode;
dstNode = dstNode->next;
srcNode = srcNode->next;
}
while (srcNode) {
dstNode = malloc(sizeof(expr_s));
dstNode->next = NULL;
dstNode->coef = srcNode->coef;
strcpy(dstNode->var,srcNode->var);
prvNode->next = dstNode;
prvNode = dstNode;
srcNode = srcNode->next;
}
while (dstNode) {
prvNode = dstNode;
dstNode = dstNode->next;
free(prvNode);
}
}
void freeExpr(expr_p expr) {
expr_p prvNode, curNode;
prvNode = NULL;
curNode = expr;
while (curNode) {
prvNode = curNode;
curNode = curNode->next;
if (prvNode) free(prvNode);
}
}