-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateCode.c
634 lines (575 loc) · 23.4 KB
/
generateCode.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
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
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
#ifndef _GENERATE_CODE_
#define _GENERATE_CODE_
#include "structures.h"
#include "show.h"
#include "generateCode.h"
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#define LOCAL 0
#define PARAM 1
#define GLOBAL 2
char insideFunction = FALSE;
char* currentMethod;
int tabs = 0;
int varIndex = 0;
char alreadyOneParam = FALSE;
char returnValue[100];
int function_type;
int counter4jumps;
//Check if ID is Local
char checkifIDIsLocal(char* id,char* methodID,Table* main) {
char returnFlag = PARAM;
Table* table = getMethodTable(main, methodID);
TableNode* local;
local = table->table->next;
while(local != NULL) {
if(local->isParam == FALSE)
returnFlag = LOCAL;
if((local->n_type == TABLE_DECL) && strcmp(id,local->id->id) == 0) {
return returnFlag;
}
local = local->next;
}
return GLOBAL;
}
char* generateCode(Node* ast,Table* main){
listID* aux;
char value1[100];
char value2[100];
char value3[100];
if(ast!=NULL){
//printf("LOLADA:%s\n",NODE_STRING[ast->n_type]);
//printf("DEBUG:%s\n\t%s\n",NODE_STRING[ast->n_type],NODE_TYPE_NAMES[ast->type]);
if(ast->n_type == NODE_PROGRAM){
printf("declare i32 @printf(i8* noalias nocapture, ...)\n");
printf("declare noalias i8* @calloc(i32, i32) nounwind\n");
printf("declare i32 @atoi(i8*) nounwind readonly\n");
printf("@.printInt = private constant [4 x i8] c\"%%d\\0A\\00\"\n");
printf("@.true = private constant [6 x i8] c\"true\\0A\\00\"\n");
printf("@.false = private constant [7 x i8] c\"false\\0A\\00\"\n");
printf("@.printBool = private constant [2 x i8*] [i8* getelementptr inbounds ([7 x i8]* @.false, i32 0, i32 0),i8* getelementptr inbounds ([6 x i8]* @.true, i32 0, i32 0)]\n\n");
printf("%%.ArrayInt = type { i32, i32* }\n");
printf("%%.ArrayBool = type { i32, i1* }\n");
counter4jumps = 0;
}
else if(ast->n_type == NODE_METHODDECL){
currentMethod = ast->id->id;
varIndex = 0;
insideFunction = TRUE;
tabs++;
if(strcmp(ast->id->id,"main")==0){
function_type = TYPE_INT;
//change this to include the name of the variable defined by the user...
if(ast->n1->n1!=NULL)
printf("\n\ndefine i32 @main(i32 %%..argc, i8** %%%s){\n",ast->n1->n1->id->id);
else
printf("\n\ndefine i32 @main(i32 %%..argc, i8** argv){\n");
}
else{
function_type = ast->type;
printf("\ndefine %s @%s(",SYMBOLS_TYPE_SIZE[ast->type],ast->id->id);
generateCode(ast->n1->n1,main);
if(ast->n1->n1==NULL)
printf("){\n");
}
alreadyOneParam = FALSE;
generateCode(ast->n2->n1,main);
printf("\n");
generateCode(ast->n3->n1,main);
printf("\n");
printTabs(tabs);
if(function_type == TYPE_INT)
printf("ret i32 0\n");
else if(function_type == TYPE_BOOL)
printf("ret i1 0\n");
else if(function_type == TYPE_INT_ARRAY)
printf("ret %%.ArrayInt {i32 0, i32* null}\n");
else if(function_type == TYPE_BOOL_ARRAY)
printf("ret %%.ArrayBool {i32 0, i1* null}\n");
else if(function_type == TYPE_VOID)
printf("ret void\n");
printf("}\n\n");
tabs--;
insideFunction = FALSE;
}
else if(ast->n_type == NODE_VARDECL){
if(insideFunction == FALSE ){
aux = ast->id;
while(aux!=NULL){
if(ast->type == TYPE_INT || ast->type == TYPE_BOOL)
printf("@%s = global %s 0\n",aux->id,SYMBOLS_TYPE_SIZE[ast->type]);
else{
if(ast->type == TYPE_INT_ARRAY)
printf("@%s = global %s {i32 0, i32* null}\n",aux->id,SYMBOLS_TYPE_SIZE[ast->type]);
else if(ast->type == TYPE_BOOL_ARRAY)
printf("@%s = global %s {i32 0, i1* null}\n",aux->id,SYMBOLS_TYPE_SIZE[ast->type]);
}
aux = aux->next;
}
}
else{
aux = ast->id;
while(aux!=NULL){
printTabs(tabs);
printf("%%%s = alloca %s\n",aux->id,SYMBOLS_TYPE_SIZE[ast->type]);
aux = aux->next;
}
}
}
else if(ast->n_type == NODE_PARAMDECL){
if(alreadyOneParam == TRUE)
printf(",");
alreadyOneParam = TRUE;
printf("%s %%.%s",SYMBOLS_TYPE_SIZE[ast->type],ast->id->id);
generateCode(ast->next,main);
if(ast->next == NULL)
printf("){\n");
printf("%%%s = alloca %s\n",ast->id->id,SYMBOLS_TYPE_SIZE[ast->type]);
printf("store %s %%.%s, %s* %%%s",SYMBOLS_TYPE_SIZE[ast->type],ast->id->id,SYMBOLS_TYPE_SIZE[ast->type],ast->id->id);
return returnValue;
}
else if(ast->n_type == NODE_PLUS || ast->n_type == NODE_MINUS || ast->n_type == NODE_MUL || ast->n_type == NODE_DIV || ast->n_type == NODE_MOD){
strcpy(value1,generateCode(ast->n1,main));
strcpy(value2,generateCode(ast->n2,main));
printTabs(tabs);
varIndex++;
printf("%%..%d = %s %s %s , %s\n\n",varIndex,CODE_OPERATOR_STRING[ast->n_type],SYMBOLS_TYPE_SIZE[ast->n1->type],value1,value2); //<result> = add/sub/mul/sdiv <ty> <op1>, <op2>
sprintf(returnValue,"%%..%d",varIndex);
return returnValue;
}
else if(ast->n_type == NODE_AND){
strcpy(value1,generateCode(ast->n1,main));
int tmp = counter4jumps;
int temp2;
//se 1ºparam é true, executar merdas
printTabs(tabs);
varIndex++;
temp2 = varIndex;
//alloc temporary space
printf("%%..%d = alloca i1\n",varIndex);
//store th 1º value
printTabs(tabs);
printf("store i1 %s, i1* %%..%d\n",value1,varIndex);
//verify if it need to run the second
printTabs(tabs);
varIndex++;
printf("%%..%d = icmp eq i1 1, %s\n", varIndex, value1);
printTabs(tabs);
printf("br i1 %%..%d, label %%and.do%d, label %%and.end%d\n",varIndex,tmp,tmp);
printf("and.do%d:\n",tmp);
counter4jumps++;
strcpy(value2,generateCode(ast->n2,main));
printTabs(tabs);
printf("store i1 %s, i1* %%..%d\n",value2,temp2);
printTabs(tabs);
printf("br label %%and.end%d\n",tmp);
printf("and.end%d:\n",tmp);
varIndex++;
printTabs(tabs);
printf("%%..%d = load i1* %%..%d\n",varIndex,temp2);
sprintf(returnValue,"%%..%d",varIndex);
return returnValue;
}
else if(ast->n_type == NODE_OR){
strcpy(value1,generateCode(ast->n1,main));
int tmp = counter4jumps;
int temp2;
//se 1ºparam é true, executar merdas
printTabs(tabs);
varIndex++;
temp2 = varIndex;
//alloc temporary space
printf("%%..%d = alloca i1\n",varIndex);
//store th 1º value
printTabs(tabs);
printf("store i1 %s, i1* %%..%d\n",value1,varIndex);
//verify if it need to run the second
printTabs(tabs);
varIndex++;
printf("%%..%d = icmp eq i1 0, %s\n", varIndex, value1);
printTabs(tabs);
printf("br i1 %%..%d, label %%and.do%d, label %%and.end%d\n",varIndex,tmp,tmp);
printf("and.do%d:\n",tmp);
counter4jumps++;
strcpy(value2,generateCode(ast->n2,main));
printTabs(tabs);
printf("store i1 %s, i1* %%..%d\n",value2,temp2);
printTabs(tabs);
printf("br label %%and.end%d\n",tmp);
printf("and.end%d:\n",tmp);
varIndex++;
printTabs(tabs);
printf("%%..%d = load i1* %%..%d\n",varIndex,temp2);
sprintf(returnValue,"%%..%d",varIndex);
return returnValue;
}
else if(ast->n_type == NODE_INTLIT){
//is a decimal
if(*(ast->value)!='0'){
sprintf(returnValue,"%d",atoi(ast->value));
return returnValue;
}
int len = strlen(ast->value);
//if it is a hexadecimal
if(len>0 && *(ast->value+1) == 'x'){
sprintf(returnValue,"%ld",strtol((ast->value+2),NULL,16));
return returnValue;
}
sprintf(returnValue,"%ld",strtol(ast->value,NULL,8));
return returnValue;
}
else if(ast->n_type == NODE_BOOLLIT){
if(strcmp(ast->value,"true")==0)
sprintf(returnValue,"%d",1);
else
sprintf(returnValue,"%d",0);
return returnValue;
}
else if(ast->n_type == NODE_UNARYMINUS){
strcpy(value1,generateCode(ast->n1,main));
printTabs(tabs);
varIndex++;
//just multiply by -1
printf("%%..%d = mul %s -1 , %s\n",varIndex,SYMBOLS_TYPE_SIZE[ast->type],value1);
sprintf(returnValue,"%%..%d",varIndex);
return returnValue;
}
else if(ast->n_type == NODE_UNARYPLUS){
//this doesn't do anything
return generateCode(ast->n1,main);
}
else if(ast->n_type == NODE_NOT){
strcpy(value1,generateCode(ast->n1,main));
printTabs(tabs);
varIndex++;
//MAKING A NOT LIKE A BOSS!
printf("%%..%d = sub i1 1, %s\n",varIndex,value1);
sprintf(returnValue,"%%..%d",varIndex);
return returnValue;
}
else if(ast->n_type == NODE_GREATER || ast->n_type == NODE_LESS || ast->n_type == NODE_GREATEREQUAL || ast->n_type == NODE_LESSEQUAL || ast->n_type == NODE_EQUAL || ast->n_type == NODE_DIFFERENT){
strcpy(value1,generateCode(ast->n1,main));
strcpy(value2,generateCode(ast->n2,main));
printTabs(tabs);
varIndex++;
printf("%%..%d = icmp %s %s %s, %s\n",varIndex,CODE_OPERATOR_STRING[ast->n_type],SYMBOLS_TYPE_SIZE[ast->n1->type] ,value1,value2);
sprintf(returnValue,"%%..%d",varIndex);
return returnValue;
}
else if(ast->n_type == NODE_ID){
printTabs(tabs);
char result = checkifIDIsLocal(ast->value,currentMethod,main);
//printf("\n\n;DEBUG: %s(%s)\n\n",SYMBOLS_TYPE_NAMES[ast->type],ast->value);
if(result != GLOBAL){
varIndex++;
printf("%%..%d = load %s* %%%s\n",varIndex,SYMBOLS_TYPE_SIZE[ast->type],ast->value);
}
else{
varIndex++;
printf("%%..%d = load %s* @%s\n",varIndex,SYMBOLS_TYPE_SIZE[ast->type],ast->value);
}
sprintf(returnValue,"%%..%d",varIndex);
return returnValue;
}
else if(ast->n_type == NODE_LOADARRAY){
printf(";LOADARRAY\n");
//array
strcpy(value1,generateCode(ast->n1,main));
//index
strcpy(value2,generateCode(ast->n2,main));
varIndex++;
printTabs(tabs);
printf("%%..%d = extractvalue %s %s, 1\n", varIndex,SYMBOLS_TYPE_SIZE[ast->n1->type], value1);
if(ast->n1->type == TYPE_BOOL_ARRAY){
varIndex++;
printTabs(tabs);
printf("%%..%d = getelementptr i1* %%..%d, i32 %s\n",varIndex,varIndex-1,value2);
varIndex++;
printTabs(tabs);
printf("%%..%d = load i1* %%..%d\n",varIndex,varIndex-1);
}else if(ast->n1->type == TYPE_INT_ARRAY){
varIndex++;
printTabs(tabs);
printf("%%..%d = getelementptr i32* %%..%d, i32 %s\n",varIndex,varIndex-1,value2);
varIndex++;
printTabs(tabs);
printf("%%..%d = load i32* %%..%d\n",varIndex,varIndex-1);
}
sprintf(returnValue,"%%..%d",varIndex);
return returnValue;
}
else if(ast->n_type == NODE_STORE){
char tmp;
printf(";STORE\n");
strcpy(value1,generateCode(ast->n3,main));
printTabs(tabs);
//store i32 3, i32* %ptr
tmp = checkifIDIsLocal(ast->n1->value,currentMethod,main);
if(tmp!=GLOBAL){
printf("store %s %s, %s* %%%s\n\n",SYMBOLS_TYPE_SIZE[ast->n1->type],value1,SYMBOLS_TYPE_SIZE[ast->n1->type],ast->n1->value);
}
else{
printf("store %s %s, %s* @%s\n\n",SYMBOLS_TYPE_SIZE[ast->n1->type],value1,SYMBOLS_TYPE_SIZE[ast->n1->type],ast->n1->value);
}
}
else if(ast->n_type == NODE_STOREARRAY){
printf(";STOREARRAY\n");
//array
printf(";ARRAY\n");
strcpy(value1,generateCode(ast->n1,main));
//index
printf(";INDEX\n");
strcpy(value2,generateCode(ast->n2,main));
//value
printf(";VALUE\n");
strcpy(value3,generateCode(ast->n3,main));
//varIndex++;
//printTabs(tabs);
//printf("%%..%d = load %s* %s%s\n", varNumber++, llvmType1, varDeclSymbol, stmt->id);
//get the array
printf(";STOREFOFINHO\n");
varIndex++;
printTabs(tabs);
printf("%%..%d = extractvalue %s %s, 1\n", varIndex, SYMBOLS_TYPE_SIZE[ast->n1->type], value1);
//get the index
varIndex++;
printTabs(tabs);
printf("%%..%d = getelementptr %s* %%..%d, i32 %s\n", varIndex, SYMBOLS_TYPE_SIZE[ast->n3->type], varIndex -1, value2);
printTabs(tabs);
printf("store %s %s, %s* %%..%d\n", SYMBOLS_TYPE_SIZE[ast->n3->type], value3, SYMBOLS_TYPE_SIZE[ast->n3->type], varIndex);
}
else if(ast->n_type == NODE_NEWINT){
printf(";NEWINTARRAY\n");
strcpy(value1,generateCode(ast->n1,main));
varIndex++;
printTabs(tabs);
//reserve the space
printf("%%..%d = call noalias i8* @calloc(i32 %s, i32 4) nounwind\n\n", varIndex, value1);
varIndex++;
printTabs(tabs);
//cast to int
printf("%%..%d = bitcast i8* %%..%d to i32*\n", varIndex, varIndex -1);
varIndex++;
printTabs(tabs);
//insert size
printf("%%..%d = insertvalue %%.ArrayInt undef, i32 %s, 0\n", varIndex, value1);
varIndex++;
printTabs(tabs);
//insert array
printf("%%..%d = insertvalue %%.ArrayInt %%..%d, i32* %%..%d, 1\n", varIndex, varIndex -1, varIndex -2);
sprintf(returnValue,"%%..%d",varIndex);
return returnValue;
}
else if(ast->n_type == NODE_NEWBOOL){
printf(";NEWBOOLARRAY\n");
strcpy(value1,generateCode(ast->n1,main));
varIndex++;
printTabs(tabs);
//reserve the space
printf("%%..%d = call noalias i8* @calloc(i32 %s, i32 1) nounwind\n\n", varIndex, value1);
varIndex++;
printTabs(tabs);
//cast to bool
printf("%%..%d = bitcast i8* %%..%d to i1*\n", varIndex, varIndex -1);
varIndex++;
printTabs(tabs);
//insert size
printf("%%..%d = insertvalue %%.ArrayBool undef, i32 %s, 0\n", varIndex, value1);
varIndex++;
printTabs(tabs);
//insert array
printf("%%..%d = insertvalue %%.ArrayBool %%..%d, i1* %%..%d, 1\n", varIndex, varIndex -1, varIndex -2);
sprintf(returnValue,"%%..%d",varIndex);
return returnValue;
}
else if(ast->n_type == NODE_PRINT){
printf(";PRINT\n");
strcpy(value1,generateCode(ast->n1,main));
printTabs(tabs);
varIndex++;
if(ast->n1->type == TYPE_BOOL){
//convert to int
printf("%%..%d = zext i1 %s to i32\n", varIndex,value1);
varIndex++;
//get the rigth string
printTabs(tabs);
printf("%%..%d = getelementptr inbounds [2 x i8*]* @.printBool, i32 0, i32 %%..%d\n", varIndex, varIndex -1);
varIndex++;
//load it
printTabs(tabs);
printf("%%..%d = load i8** %%..%d\n", varIndex, varIndex -1);
printTabs(tabs);
printf("call i32 (i8*, ...)* @printf(i8* %%..%d)\n\n", varIndex);
varIndex++;
}else
printf("call i32 (i8*, ...)* @printf(i8* getelementptr inbounds ([4 x i8]* @.printInt, i32 0, i32 0), i32 %s)\n\n",value1);
}
else if(ast->n_type == NODE_CALL){
printTabs(tabs);
//%retval = call i32 @test(i32 %argc)
Node* aux;
char flagzinhafofinha = FALSE;
callParams* params = (callParams*)calloc(sizeof(callParams),1);
callParams* current = params;
//UPS!!!!
assert(params!=NULL);
//params
aux = ast->n1;
while(aux!=NULL){
current->next = (callParams*)calloc(sizeof(callParams),1);
//UPS
assert(current->next!=NULL);
current = current->next;
current->next = NULL;
strcpy(current->name,generateCode(aux,main));
current->type = aux->type;
aux = aux->next;
}
varIndex++;
printf("%%..%d = call %s @%s(",varIndex,SYMBOLS_TYPE_SIZE[ast->type],ast->id->id);
flagzinhafofinha = FALSE;
current = params;
params = params->next;
free(current);
while(params!=NULL){
if(flagzinhafofinha==TRUE)
printf(",");
else
flagzinhafofinha = TRUE;
printf(" %s %s ",SYMBOLS_TYPE_SIZE[params->type],params->name);
current = params;
params = params->next;
free(current);
}
printf(")\n\n");
sprintf(returnValue,"%%..%d",varIndex);
return returnValue;
}
else if(ast->n_type == NODE_RETURN){
printf(";RETURN\n");
if(ast->n1!=NULL){
strcpy(value1,generateCode(ast->n1,main));
printTabs(tabs);
printf("ret %s %s\n",SYMBOLS_TYPE_SIZE[function_type],value1);
}
else{
if(function_type == TYPE_INT)
printf("ret i32 0\n");
else if(function_type == TYPE_BOOL)
printf("ret i1 0\n");
else if(function_type == TYPE_VOID)
printf("ret void\n");
else if(function_type == TYPE_INT_ARRAY)
printf("ret %%.ArrayInt {i32 0, i32* null}\n");
else if(function_type == TYPE_BOOL_ARRAY)
printf("ret %%.ArrayBool {i32 0, i1* null}\n");
}
}
else if(ast->n_type == NODE_LENGTH){
printf(";.LENGTH\n");
//array
if(ast->n1->type != TYPE_STRING_ARRAY){
strcpy(value1,generateCode(ast->n1,main));
varIndex++;
printTabs(tabs);
printf("%%..%d = extractvalue %s %s, 0\n", varIndex,SYMBOLS_TYPE_SIZE[ast->n1->type], value1);
sprintf(returnValue,"%%..%d",varIndex);
}
else{
varIndex++;
printTabs(tabs);
printf("%%..%d = sub i32 %%..argc , 1\n",varIndex);
sprintf(returnValue,"%%..%d",varIndex);
}
return returnValue;
}
else if(ast->n_type == NODE_PARSEARGS){
printf(";PARSEARGS\n");
//array
strcpy(value2,generateCode(ast->n2,main));
//get the array Index (+1 because of the program name)
varIndex++;
printTabs(tabs);
printf("%%..%d = add i32 1 , %s\n",varIndex,value2);
//get the final pointer
varIndex++;
printTabs(tabs);
printf("%%..%d = getelementptr inbounds i8** %%%s, i32 %%..%d\n",varIndex,ast->n1->value,varIndex-1);
//get the value
varIndex++;
printTabs(tabs);
printf("%%..%d = load i8** %%..%d\n",varIndex,varIndex-1);
varIndex++;
printTabs(tabs);
printf("%%..%d = call i32 @atoi(i8* %%..%d) nounwind readonly\n",varIndex,varIndex-1);
sprintf(returnValue,"%%..%d",varIndex);
return returnValue;
}
else if(ast->n_type == NODE_COMPOUNDSTAT){
tabs++;
generateCode(ast->n1,main);
tabs--;
}
else if(ast->n_type == NODE_IFELSE){
//check statement
strcpy(value1,generateCode(ast->n1,main));
int tmp = counter4jumps;
counter4jumps++;
int temp2;
//se param is true, do stuff
varIndex++;
temp2 = varIndex;
printTabs(tabs);
printf("%%..%d = icmp eq i1 1, %s\n", varIndex, value1);
printTabs(tabs);
//if
//varIndex++;
printf("br i1 %%..%d, label %%if.do%d, label %%if.else%d\n",varIndex,tmp,tmp);
printf("if.do%d:\n",tmp);
tabs++;
generateCode(ast->n2,main);
printTabs(tabs);
printf("br label %%if.end%d\n",tmp);
tabs--;
//else
printf("if.else%d:\n",tmp);
tabs++;
generateCode(ast->n3,main);
tabs--;
printTabs(tabs);
printf("br label %%if.end%d\n",tmp);
printf("if.end%d:\n",tmp);
}
else if(ast->n_type == NODE_WHILE){
//check statement
int tmp = counter4jumps;
counter4jumps++;
int temp2;
printf("br label %%while.do%d\n",tmp);
printf("while.do%d:\n",tmp);
strcpy(value1,generateCode(ast->n1,main));
//se param is true, do stuff
varIndex++;
temp2 = varIndex;
printTabs(tabs);
printf("%%..%d = icmp eq i1 1, %s\n", varIndex, value1);
printTabs(tabs);
//if
//varIndex++;
printf("br i1 %%..%d, label %%while.continue%d, label %%while.end%d\n",varIndex,tmp,tmp);
printf("while.continue%d:\n",tmp);
tabs++;
generateCode(ast->n2,main);
printTabs(tabs);
tabs--;
printTabs(tabs);
printf("br label %%while.do%d\n",tmp);
printf("while.end%d:\n",tmp);
}
generateCode(ast->next,main);
}
return NULL;
}
#endif