-
Notifications
You must be signed in to change notification settings - Fork 0
/
table.c
279 lines (247 loc) · 4.64 KB
/
table.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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include "globals.h"
#include "table.h"
#include "func.h"
Node* newNode(long value,char* key,instruction type,instruction entry)
{
Node* tmp=NULL;
char* tmp_key=NULL;
tmp = (Node*)malloc_with_check(sizeof(Node));
if(tmp == NULL)
{
printf("Out of memory");
exit(1);
}
tmp_key = (char*)malloc(sizeof(char)*(strlen(key)+1));
if(tmp_key == NULL)
{
printf("Out of memory");
exit(1);
}
strcpy(tmp_key,key);
tmp->entry=entry;
tmp->value = value;
tmp->key = tmp_key;
tmp->type = type;
tmp->next = NULL;
return tmp;
}
void destroy(Node* head)
{
if(head==NULL)
return;
while(head!=NULL)
{
Node* tmp = head;
free(tmp->key);
tmp->key = NULL;
head = head->next;
free(tmp);
tmp = NULL;
}
}
void printTable(Node* head)
{
while(head!=NULL)
{
printf("%ld %s %d %d\n",head->value,head->key,head->type,head->entry);
head = head->next;
}
}
void printTableByType(Node* head,instruction type)
{
while(head!=NULL)
{
if(head->type == type || head->entry ==type)
printf("%ld %s %d %d\n",head->value,head->key,head->type,head->entry);
head = head->next;
}
}
Node* insertToEnd(Node* head,char* key,long value,instruction type,instruction entry)
{
if(head==NULL)
return newNode(value,key,type,entry);
else{
Node* tmp = head;
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next = newNode(value,key,type,entry);
}
return head;
}
bool isLabelInTable(Node* head,char* label)
{
if(head==NULL)
return FALSE;
else{
while(head!=NULL)
{
if(!strcmp(label,head->key))
return TRUE;
head = head->next;
}
return FALSE;
}
}
instruction getLabelType(Node* head,char* label)
{
instruction tmp = ERROR_INST;
if(head==NULL)
return FALSE;
else{
while(head!=NULL)
{
if(!strcmp(label,head->key))
{
tmp=head->type;
return tmp;
}
head = head->next;
}
return tmp;
}
}
long getLabelValue(Node* head,char* label)
{
unsigned long tmp = 0;
if(head==NULL)
return FALSE;
else{
while(head!=NULL)
{
if(!strcmp(label,head->key))
{
tmp=head->value;
return tmp;
}
head = head->next;
}
return tmp;
}
}
bool isLabelInTableType(Node* head,char* label,instruction type)
{
if(head==NULL)
return FALSE;
else{
while(head!=NULL)
{
if(!strcmp(label,head->key) && (head->type == type || head->entry == type))
return TRUE;
head = head->next;
}
return FALSE;
}
}
/*ANOTHER----------------------------------------------- TABLE */
MachineNode* newMWNode(long adress,long mw ,ARE are,char* label,operandType opType)
{
MachineNode* tmp=NULL;
char* tmpLabel=NULL;
tmp = (MachineNode*)malloc_with_check(sizeof(MachineNode));
if(label!=NULL)
{
tmpLabel=(char*)malloc_with_check(sizeof(char)*strlen(label)+1);
strcpy(tmpLabel,label);
}
tmp->adress = adress;
tmp->machineWord =(unsigned long)mw;
tmp->are = are;
if(opType)
tmp->opType=opType;
else
tmp->opType=NONE_OPERAND;
tmp->next = NULL;
tmp->label = tmpLabel;
return tmp;
}
void destroyMW(MachineNode* head)
{
if(head==NULL)
return;
while(head!=NULL)
{
MachineNode* tmp = head;
if(tmp->label!=NULL)
free(tmp->label);
tmp->label=NULL;
head = head->next;
free(tmp);
tmp = NULL;
}
}
void printMWTable(MachineNode* head)
{
while(head->next!=NULL)
{
printf("%04d %03x %d %s %d\n",(int)head->adress,(unsigned int)(head->machineWord&0xFFF),head->are,head->label,head->opType);
head = head->next;
}
}
void printMWTableToFile(MachineNode* head,FILE* f,long* ICF,long* DCF)
{
char are;
fprintf(f," %ld %ld\n",*ICF-100,*DCF);
while(head->next!=NULL)
{
switch(head->are)
{
case A:
are='A';
break;
case R:
are='R';
break;
case E:
are='E';
break;
case NONE_ARE:
are='!';
break;
}
fprintf(f,"%04d %03x %c\n",(int)head->adress,(unsigned int)(head->machineWord&0xFFF),are);
head = head->next;
}
}
MachineNode* insertMWToEnd(MachineNode* head,long adress,long mw,ARE are,char* label,operandType opType)
{
if(head==NULL)
{
return newMWNode(adress,mw,are,label,opType);
}
else{
MachineNode* tmp = head;
while(tmp->next!=NULL)
tmp=tmp->next;
tmp->next = newMWNode(adress,mw,are,label,opType);
}
return head;
}
bool changeNodeAtAdress(MachineNode** head,long adress,long mw,ARE are,char* label,operandType opType)
{
MachineNode** tmp =NULL;
if((*head)==NULL)
return FALSE;
else{
tmp=head;
while((*tmp)!=NULL)
{
if(adress==(*tmp)->adress)
{
if(mw)
(*tmp)->machineWord=mw;
if(are)
(*tmp)->are=are;
if(label)
strcpy((*tmp)->label,label);
if(opType)
(*tmp)->opType=opType;
return TRUE;
}
(*tmp) = (*tmp)->next;
}
return FALSE;
}
}