Repository navigation
Expand file tree
/
Copy pathListe.c
More file actions
436 lines (357 loc) · 11.2 KB
/
Copy pathListe.c
File metadata and controls
436 lines (357 loc) · 11.2 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Liste.c
// Name: Eric Hübel, Mat.Nr: 54045, S-Nr: 86462, Gruppe: 041-62
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include "Compiler.h"
#include "Liste.h"
#include "Semroutinen.h"
#include "Lexer.h"
#include "Parser.h"
// globale Variablen
extern tBezEntry* List;
extern short curProcNum;
extern tProc* curProc;
extern size_int* ConstBlock;
extern int numElements;
extern tLabl* LablList; // Keller für Label -> für jmp
// globale Suche: gesamte Liste durchsuchen
// lokale Suche: gesamte Liste durchsuchen der aktuellen Prozedurnummer -> nur pLocalBez durchsuchen
// ===================== Konstantenblock ====================
void initConst(){
ConstBlock = NULL; // Start ohne Speicher
numElements = 0;
}
void addConst(size_int val){
numElements++;
size_int* newBlock = realloc(ConstBlock, sizeof(size_int) * (numElements));
if (newBlock == NULL){
printf("Konnte Speicher nicht reallokieren!\n");
exit(1);
}
ConstBlock = newBlock;
ConstBlock[numElements-1] = val; // -1, da Start bei 0
}
int searchConst(size_int val){
int index = 0;
while (index < numElements){
//DEBUG_PRINT("Index: %d\n", index);
if (ConstBlock[index] == (size_int) val){
#ifdef DEBUG
#if (VARSIZE == 8)
DEBUG_PRINT("Gefunden: %ld\n", ConstBlock[index]);
#else
DEBUG_PRINT("Gefunden: %d\n", ConstBlock[index]);
#endif
#endif
return index;
}
index++;
}
DEBUG_PRINT("Nichts gefunden!\n");
return -1;
}
void PrintConstBlock(){
for (int i = 0; i < numElements; i++) {
#ifdef DEBUG
#if (VARSIZE == 8)
DEBUG_PRINT("ConstBlock[%d] = %ld\n", i, ConstBlock[i]);
#else
DEBUG_PRINT("ConstBlock[%d] = %d\n", i, ConstBlock[i]);
#endif
#endif
}
}
void deleteConstBlock(){
free(ConstBlock);
numElements = 0;
}
// ============================= Liste =======================
tBezEntry* append(tBezEntry* head, tKz Kz, short indexProc, void* pObj, char* pName) {
tBezEntry* newEntry = (tBezEntry*)malloc(sizeof(tBezEntry)); // Speicher für neuen Listeneintrag allokieren
if (newEntry == NULL) { // Speicherallokierung erfolgreich?
printf("Konnte Speicher nicht allokieren!\n");
exit(1);
}
// Daten in neuen Eintrag kopieren
newEntry->indexProc = indexProc;
newEntry->Kz = Kz;
newEntry->pObj = (void*) pObj;
char* newName = malloc(strlen(pName)+1);
if (newName == NULL) {
printf("Konnte Speicher nicht allokieren!\n");
exit(1);
}
strcpy(newName, pName);
newEntry->pName = newName;
newEntry->next = NULL; // nächsten Eintrag auf NULL setzen, da Ende der Liste
// Überprüfen, ob die Liste leer ist
if (head == NULL) {
head = newEntry;
// Liste leer -> neuer Eintrag wird der erste Eintrag und damit der Kopf
return head;
}
// Liste nicht leer -> füge am Ende hinzu
tBezEntry* current = head; // starten am Anfang/Kopf
int index = 1;
while (current->next != NULL) { // vorhangeln in der Liste (current: aktueller Eintrag)
current = (tBezEntry*)current->next;
index++;
}
current->next = (struct tBezEntry*)newEntry;
return head; // Rückgabe des ursprünglichen Kopfes der Liste
}
tBezEntry* getFirst(void){
// Namensliste von akt. Prozedur bekommen
return curProc->pLocalBez;
}
tBezEntry* getNext(tBezEntry* current){
return (tBezEntry*)current->next;
}
tBezEntry* getLast(void){
tBezEntry* current = curProc->pLocalBez; // mit Kopf starten der Namensliste akt. Proz
tBezEntry* prev = NULL;
while(current != NULL){
prev = (tBezEntry*)current;
current = (tBezEntry*)current->next;
}
return prev;
}
tBezEntry* deleteLast(tBezEntry* head) {
if (head == NULL) {
DEBUG_PRINT("Liste leer\n");
return head;
}
tBezEntry* current = head; // starten am Anfang/Kopf
tBezEntry* PrevEntry = head;
int iter = 0;
while (1) { // durch Liste hangeln
iter++;
if (current->next == NULL) { // letztes Entry gefunden
PrevEntry->next = NULL; // Zeiger zum letzten Entry bei vorh. Entry entfernen
free(current->pName); // Pointer auf Name löschen
free(current->pObj); // Pointer auf Objekt löschen
free(current); // letztes Entry löschen
break; // Ende Schleife
}
PrevEntry = current; // vorheriges ListenEntry speichern
current = (tBezEntry*)current->next; // nächstes ListenEntry setzen
}
if (iter == 1) { //bei einer Iteration wurde also nur 1 Index gefunden, also leere Liste setzen
head = NULL;
}
return head;
}
tBezEntry* deleteFirst(tBezEntry* head) {
if (head == NULL) {
DEBUG_PRINT("Liste leer\n");
return head;
}
tBezEntry* newFirst = (tBezEntry*)head->next;
free(head->pName); // Pointer auf Name löschen
free(head->pObj); // Pointer auf Objekt löschen
free(head); // letztes Entry löschen
return newFirst;
}
void PrintProc(tProc* pProc){
//tProc* pProc = (tProc*)current->pObj;
DEBUG_PRINT("=PRC= KZ: %d | IdxProc: %u | SpzzVar: %d\n", pProc->Kz, pProc->IndexProc, pProc->SpzzVar);
}
void PrintList(tBezEntry* head) {
if (head == NULL) {
DEBUG_PRINT("Liste leer!\n");
return;
}
int index = 0;
tBezEntry* current = head;
while (current != NULL) { // vorhangeln durch die Liste bis zum letzten Entry und ausgeben
index++;
DEBUG_PRINT("Idx: %d | KZ: %d | idxProc: %u | pName: %s || ", index, current->Kz, current->indexProc, current->pName);
// je nach Obj richtig Infos printen
if (current->Kz == BEZ){
DEBUG_PRINT("=BEZ= Bez. ohne Objektbeschreibung!\n");
}
else if (current->Kz == PRC){
tProc* pProc = (tProc*)current->pObj;
DEBUG_PRINT("=PRC= KZ: %d | IdxProc: %u | SpzzVar: %d\n", pProc->Kz, pProc->IndexProc, pProc->SpzzVar);
}
else if (current->Kz == CST){
tConst* pConst = (tConst*)current->pObj;
DEBUG_PRINT("=CST= KZ: %d | Val: %d | Idx: %d\n", pConst->Kz, pConst->Val, pConst->Idx);
}
else if (current->Kz == VAR){
tVar* pVar = (tVar*)current->pObj;
DEBUG_PRINT("=VAR= KZ: %d | Dspl: %d\n", pVar->Kz, pVar->Dspl);
}
current = (tBezEntry*)current->next;
}
}
tBezEntry* ClearList(tBezEntry* head){
// ganze Liste löschen
tBezEntry* current = head;
if (head == NULL){
DEBUG_PRINT("Liste leer!\n");
return head;
}
//PrintList(head);
do {
current = getNext(head);
head = deleteFirst(head);
//head = deleteLast(head);
//PrintList(head);
}while (current != NULL);
return head;
}
// =============== Listeneinträge + Suchfkt. =======================
tBezEntry* createBezEntry(char* pBez){
// Namensliste von akt. Prozedur bekommen
List = curProc->pLocalBez;
// neuer Listeneintrag, noch keine Objektbeschr.
curProc->pLocalBez = append(List, BEZ, curProcNum, NULL, pBez);
tBezEntry* LastEntry = getLast(); // letzter Eintrag ist neuer Eintrag
return LastEntry;
}
tProc* createMainProc(){
tProc* mainProc = createProc(NULL);
DEBUG_PRINT("Prozedur %d erstellt:\n", mainProc->IndexProc);
PrintProc(mainProc);
return mainProc;
}
tProc* createProc(tProc* pParent){
// Prozedurenobjekt erstellen
tProc* newProc = (tProc*)malloc(sizeof(tProc));
if (newProc == NULL) {
printf("Konnte Speicher nicht allokieren!\n");
exit(1);
}
newProc->Kz = PRC;
curProcNum++;
newProc->IndexProc = curProcNum; // Prozedurnummer erhöhen
newProc->SpzzVar = 0; // wird immer um VarSize erhöht
newProc->pLocalBez = NULL; // unsere lokale Namensliste
newProc->pParent = pParent;
return newProc;
}
tVar* createVar(void){
// Variablenobjekt erstellen
tVar* newVar = (tVar*)malloc(sizeof(tVar));
if (newVar == NULL) {
printf("Konnte Speicher nicht allokieren!\n");
exit(1);
}
newVar->Kz = VAR;
// Berechnung des Displacements
// aktuellen Speicherzuordnungszähler aus akt. Prozedurbeschreibung bekommen + (Var.größe dazuaddieren)
newVar->Dspl = curProc->SpzzVar; // auf das letzte setzen
// Speicherzuordnungszähler in curProc erhöhen
curProc->SpzzVar = curProc->SpzzVar + VARSIZE;
return newVar;
}
tConst* createConst(size_int Val){
// Konstantenobjekt erstellen
tConst* newConst = (tConst*)malloc(sizeof(tConst));
if (newConst == NULL) {
printf("Konnte Speicher nicht allokieren!\n");
exit(1);
}
newConst->Kz = CST;
newConst->Idx = 0; // Initialwert später richtig setzen
newConst->Val = Val;
return newConst;
}
tBezEntry* addVarPObj(tBezEntry* pBezEntry, tVar* pVar){
//tVar* addVar = (tVar*)pBezEntry->pObj;
pBezEntry->Kz = VAR;
pBezEntry->pObj = pVar;
return pBezEntry;
}
tBezEntry* addConstPObj(tBezEntry* pBezEntry, tConst* pConst){
pBezEntry->Kz = CST;
pBezEntry->pObj = pConst;
return pBezEntry;
}
tBezEntry* addProcPObj(tBezEntry* pBezEntry, tProc* pProc){
pBezEntry->Kz = PRC;
pBezEntry->pObj = pProc;
return pBezEntry;
}
// lokale Suche nach Bezeichnernamen
tBezEntry* searchBez(tProc* pProc, char* pBez){
tBezEntry* current = pProc->pLocalBez;
while (current != NULL){
if (strcmp(current->pName, pBez) == 0){
DEBUG_PRINT("Gefunden: %s\n", current->pName);
return current; // was gefunden!!
}
current = getNext(current);
}
DEBUG_PRINT("Nichts gefunden!\n");
return NULL; // nichts gefunden
}
// globale Suche nach Bezeichnernamen
tBezEntry* searchBezGlobal(char* pBez){
tProc* currentProc = curProc; // bei akt. Proc starten
while (1){
//DEBUG_PRINT("PROC: %d\n", currentProc->IndexProc);
// Namensliste von currentProc bekommen
tBezEntry* current = (tBezEntry*)currentProc->pLocalBez;
while (current != NULL){
if (strcmp(current->pName, pBez) == 0){
DEBUG_PRINT("Gefunden: %s\n", current->pName);
return current; // was gefunden!!
}
//current = getNext(current);
current = (tBezEntry*)current->next;
}
// Abbruch
if (currentProc->pParent == NULL){
DEBUG_PRINT("Abbruch bei Proc: %d\n", currentProc->IndexProc);
break;
}
currentProc = currentProc->pParent;
}
DEBUG_PRINT("Nichts gefunden!\n");
return NULL; // nichts gefunden
}
// ============= Labelliste ============
tLabl* pushLabl(tLabl* head, tKz Kz, short iJmp) {
tLabl* newEntry = (tLabl*)malloc(sizeof(tLabl)); // Speicher für neuen Listeneintrag allokieren
if (newEntry == NULL) { // Speicherallokierung erfolgreich?
printf("Konnte Speicher nicht allokieren!\n");
exit(1);
}
// Daten in neuen Eintrag kopieren
newEntry->Kz = Kz;
newEntry->iJmp = iJmp;
newEntry->next = (struct tLabl*)head; // letzter erster Eintrag wird nächster
return newEntry; // Rückgabe des neuen Kopfes
}
tLabl* getTopLabl(void){
return LablList;
}
tLabl* getNextLabl(tLabl* current){
return (tLabl*)current->next;
}
tLabl* PopLabl(tLabl* head) {
if (head == NULL) {
DEBUG_PRINT("Labelliste leer\n");
return head;
}
tLabl* newHead = (tLabl*)head->next;
free(head);
return newHead;
}
void PrintLablList(tLabl* head){
if (head == NULL) {
DEBUG_PRINT("Labelliste leer!\n");
return;
}
tLabl* current = head;
int index = 0;
while (current != NULL){
index++;
DEBUG_PRINT("Index: %d | Kz: %d | iJmp: %d\n", index, current->Kz, current->iJmp);
current = (tLabl*)current->next;
}
}