-
Notifications
You must be signed in to change notification settings - Fork 0
/
jmpList.c
176 lines (158 loc) · 3.81 KB
/
jmpList.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
#include "jmpList.h"
#include <stdlib.h>
#include <stdio.h>
IfJmpList* jumpingList=NULL;
IfJmpList* lastCell=NULL;
void addMainJL() {
IfJmpList* cell = jumpingList;
IfJmpList* pred = NULL;
if(jumpingList==NULL) {
jumpingList = malloc(sizeof(IfJmpList));
cell = jumpingList;
} else {
while(cell->next != NULL) {
cell = cell->next;
}
cell->next = malloc(sizeof(IfJmpList));
pred=cell;
cell = cell->next;
}
cell->statementLine = 1;
cell->jumpingLine = -1;
cell->next = NULL;
cell->pred = pred;
lastCell = cell;
}
void updateMainJL(int location) {
if (jumpingList != NULL) {
jumpingList->jumpingLine=location;
}
}
void addStatementJL(int statementLine) {
printf("Adding statement %d\n", statementLine);
IfJmpList* cell = jumpingList;
IfJmpList* pred = NULL;
if(jumpingList==NULL) {
jumpingList=malloc(sizeof(IfJmpList));
cell = jumpingList;
} else {
while(cell->next != NULL) {
cell = cell->next;
}
cell->next = malloc(sizeof(IfJmpList));
pred = cell;
cell = cell->next;
}
cell->statementLine = statementLine;
cell->jumpingLine = -1;
cell->next=NULL;
cell->pred =pred;
lastCell = cell;
}
void addJumpingJL(int jumpingLine) {
printf("Adding Jumping %d\n", jumpingLine);
IfJmpList* cell = jumpingList;
IfJmpList* pred = NULL;
if(jumpingList==NULL) {
jumpingList=malloc(sizeof(IfJmpList));
cell = jumpingList;
} else {
while(cell->next != NULL) {
cell = cell->next;
}
cell->next = malloc(sizeof(IfJmpList));
pred = cell;
cell = cell->next;
}
cell->statementLine=-1;
cell->jumpingLine = jumpingLine;
cell->next = NULL;
cell->pred = pred;
lastCell = cell;
}
void updateStatementLine(int statementLine) {
IfJmpList* cell = jumpingList;
if(jumpingList->next == NULL) {
jumpingList->statementLine == statementLine;
} else {
while(cell->next != NULL && cell->statementLine != -1) {
cell = cell->next;
}
cell->statementLine = statementLine;
}
}
void updateStatementLineFromBottom(int statementLine) {
IfJmpList* cell = lastCell;
if(cell->pred == NULL) {
cell->statementLine == statementLine;
} else {
while(cell->pred != NULL && cell->statementLine != -1) {
cell = cell->pred;
}
cell->statementLine = statementLine;
}
}
void updateJumpingJLFromBottom(int jumpingLine) {
IfJmpList* cell =lastCell;
if(cell->pred == NULL) {
cell->jumpingLine == jumpingLine;
} else {
while(cell->pred != NULL && cell->jumpingLine != -1) {
cell = cell->pred;
}
cell->jumpingLine = jumpingLine;
}
}
void updateJumpingJL(int jumpingLine) {
IfJmpList* cell = jumpingList;
if(jumpingList->next== NULL) {
jumpingList->jumpingLine == jumpingLine;
} else {
while(cell->next != NULL && cell->jumpingLine != -1) {
cell = cell->next;
}
// while(cell->next != NULL && cell->next->jumpingLine == -1) {
// cell = cell->next;
// }
cell->jumpingLine = jumpingLine;
}
}
void clearLast() {
IfJmpList* cell = jumpingList;
if(cell!=NULL && cell->next != NULL) {
while(cell->next->next != NULL) {
cell = cell->next;
}
}
cell->next=NULL;
}
void displayJL() {
printf("\n----------------------------------\n");
IfJmpList* cell = jumpingList;
int i=0;
while(cell!=NULL) {
printf("%d: statement: %d ; jumping %d\n",i, cell->statementLine, cell->jumpingLine);
cell = cell->next;
i++;
}
}
IfJmpList* getJmpList() {
return jumpingList;
}
/*
* Should we jump on line line ?
* Returns the line to jump to if so, -1 otherwise.
*/
int isThereAJump(int line) {
IfJmpList* head = jumpingList;
IfJmpList* cur = head;
int res;
while(cur != NULL && cur->statementLine!= line) {
cur = cur->next;
}
if(cur != NULL)
res = cur->jumpingLine;
else
res = -1;
return res;
}