-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcodegen_functions.c
2288 lines (1807 loc) · 79.9 KB
/
codegen_functions.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
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Codegen header */
#include "codegen_functions.h"
/* Includes */
#include <stdio.h>
#include <stdlib.h>
/* Terminals define */
#ifdef DEBUG_MODE
#include "debug.y.tab.h"
#else
#include "y.tab.h"
#endif
/* Non Terminals defines */
#include "parser.defs.h"
/* Token Structure */
#include "token_struct.h"
/* Utils */
#include "utils.h"
/* Generated Code */
InstructionQueue *header_instruction_queue;
/* Main Generated Code */
InstructionQueue *main_instruction_queue;
/* Global Symbol Table */
SymbolTable *global_symbol_table;
/* *** Counter for operations *** */
/* Short Circuit 'and' counter */
int sc_and_counter;
/* Short Circuit 'or' counter */
int sc_or_counter;
/* Loop 'while' counter */
int loop_while_counter;
/* Loop 'for' counter */
int loop_for_counter;
/* Conditional 'if' counter */
int cond_if_counter;
/* Counter for multiple values 'return' */
int multiple_return_counter;
/**
* Pop local varibles, this include, the instruction pop, and the logical (symbol table) pop too.
*
* @param symbol_table The symbol table which is being popped.
* @return true if there's no error on execution and false otherwise.
*/
bool popSymbolTable(SymbolTable *symbol_table){
int i;
int no_variables;
/* Check if we have a valid symbol table */
if(symbol_table == NULL){
printError("INVALID SYMBOL TABLE!");
return false;
}
/* Include the instruction to pop local variables */
addInstructionMainQueueFormated(mips_pop_local, (symbol_table->shift_address));
/* Get how many variables we have */
no_variables = symbol_table->length;
/* Remove each variable shift from all scopes */
for(i = 0; i < no_variables; i++){
fprintf(stderr, "POP >> LINE: %d \n", __LINE__);
symbolTablePopVar(symbol_table);
}
/* Return success */
return true;
}
/**
* Assign a list of names all values on stack.
*
* @param list_names a valid list of *ONLY* token names.
* @return true if there's no error on execution and false otherwise.
*/
bool assignStackHelper(TokenList *list_names, SymbolTable *actual_symbol_table, int exp_executed){
int i;
TokenNode *token_name;
SymbolNode *symbol_node;
/* Execute list of names and assign values for each symbol name */
for(i = 1; i <= list_names->length; i++){
/* Get token name */
token_name = listGetTokenByIndex(list_names, i);
/* Check if token name is not null */
if(token_name == NULL){
printError("INVALID TOKEN NAME!");
return false;
}
/* Add header of the instruction */
addInstructionMainQueueFormated(mips_marker_assign, token_name->lex_str, i);
/* $a0 = top */
addInstructionMainQueue(mips_top_a0);
/* Get the symbol node */
symbol_node = symbolTableGetSymbolNodeByName(actual_symbol_table, token_name->lex_str);
/* If token name isn't inside the actual symbol table so this t_name is global */
if(symbol_node == NULL){
symbol_node = symbolTableGetSymbolNodeByName(global_symbol_table, token_name->lex_str);
}
/* If this symbol doesn't have a valid root symbol table */
else if(symbol_node->root_symbol_table == NULL){
printError("INVALID SYMBOL NODE ROOT TOKEN!");
return false;
}
/* If this symbol has a valid root symbol table, and the type of this symbol table is SP - Stack Pointer */
else if(symbol_node->root_symbol_table->register_type == REGISTER_TYPE_SP){
/* Update address temporarily, because stack contains a lot of new values */
//symbol_node->symbol_address += ((list_names->length - i + 1) * 4);
// WTF?
// WHY??
// COMMON, PLEASE TEACHER JUST ACCEPT!
// IT'S CLEARLY THAT I'M GETTING CRAZY!
// PLS! STOP THIS, PLS!
}
/* If this symbol is not on global symbol table, so need to add him first */
if(symbol_node == NULL){
/* Add the symbol to the global symbol table */
symbolTableAddSymbol(global_symbol_table, token_name->lex_str, NUMBER_TYPE);
/* Get the inserted symbol node */
symbol_node = symbolTableGetSymbolNodeByName(global_symbol_table, token_name->lex_str);
/* Check if symbol node is valid */
if(symbol_node == NULL){
printError("INVALID SYMBOL NODE!");
return false;
}
}
/* Store the correct store instruction */
instructionQueueEnqueueInstructionNode(main_instruction_queue, symbolNodeGetStoreInstruction(symbol_node));
/* pop */
addInstructionMainQueue(mips_pop);
/* Pop address on symbol table too. This assume that the symbol table has a lot of blank spaces */
if(exp_executed > 0){
fprintf(stderr, "POP >> LINE: %d \n", __LINE__);
symbolTablePopVar(actual_symbol_table);
exp_executed -= 1;
}
}
/* Return success */
return true;
}
/**
* Copy global variables into header instruction queue.
*
* @return true if there's no error on execution and false otherwise.
*/
bool copyGlobalVariables(){
int i;
/* Check if header instruction queue is null */
if(header_instruction_queue == NULL){
printError("HEADER INSTRUCTION QUEUE, NOT INITIALIZED!");
return false;
}
/* Check if global symbol table is null */
if(global_symbol_table == NULL){
printError("GLOBAL SYMBOL TABLE, NOT INITIALIZED!");
return false;
}
/* Enqueue global definition of every node in global symbol table */
for(i = 0; i < global_symbol_table->length; i++){
/* Check if instruction queue has failed */
if(!instructionQueueEnqueueInstructionNode( header_instruction_queue,
symbolNodeGetDefineInstruction(global_symbol_table->items[i]))){
return false;
}
}
/* Return success */
return true;
}
/**
* Generate code based on AST.
*
* @param root_token the root token node of the AST.
* @return true if there's no error on execution and false otherwise.
*/
bool cgenAllCode(TokenNode *root_token){
SymbolTable *main_symbol_table;
/* Initialize global operation counters */
sc_or_counter = 0;
sc_and_counter = 0;
cond_if_counter = 0;
loop_for_counter = 0;
loop_while_counter = 0;
multiple_return_counter = 0;
/* Initialize Header Instruction Queue */
header_instruction_queue = newInstructionQueue();
/* Check if the header instruction queue was corrected initialized */
if(header_instruction_queue == NULL){
printFatalError("CANNOT INITIALIZE HEADER INSTRUCTION QUEUE.");
exit(EXIT_FAILURE);
}
/* Initialize Main Instruction Queue */
main_instruction_queue = newInstructionQueue();
/* Check if the header instruction queue was corrected initialized */
if(main_instruction_queue == NULL){
printFatalError("CANNOT INITIALIZE MAIN INSTRUCTION QUEUE.");
exit(EXIT_FAILURE);
}
/* Initialize Global Symbol Table */
global_symbol_table = newGlobalSymbolTable();
/* Check if the global symbol was correct initialized */
if(global_symbol_table == NULL){
printFatalError("CANNOT CREATE GLOBAL SYMBOL TABLE.");
exit(EXIT_FAILURE);
}
/* Block token node */
TokenNode *block_token = listGetTokenByIndex(root_token->child_list, 1);
/* Check if the block token really exists */
if(block_token == NULL){
printFatalError("CANNOT GET BLOCK TOKEN NODE.");
exit(EXIT_FAILURE);
}
/* Create the main symbol table */
main_symbol_table = newSymbolTable(NULL, REGISTER_TYPE_SP);
/* Check if the main symbol table is working */
if(main_symbol_table == NULL){
printFatalError("INVALID MAIN SYMBOL TABLE! STOPPING NOW!");
exit(EXIT_FAILURE);
}
/* Add a logical symbol node on main symbol table */
symbolTableAddLogicSymbol(main_symbol_table);
/* Add header on header instruction queue */
instructionQueueEnqueueInstruction(header_instruction_queue, formatedInstruction(mips_header), false);
/* Add header on main instruction queue */
addInstructionMainQueue(mips_main);
//symbolTableAddSymbol(main_symbol_table, "$RA",NUMBER_TYPE); OTHER DUMMY DEBUG! -_-
/* Generate code for main application */
cgenBlockCode(block_token, main_symbol_table);
/* Pop Main Symbol table */
popSymbolTable(main_symbol_table);
/* Delete main symbol table */
deleteSymbolTable(&main_symbol_table);
/* Copy variable definitions to main-instruction_queue */
copyGlobalVariables();
/* Add footer of the main instruction queue */
addInstructionMainQueue(mips_footer);
/* Return success */
return true;
}
/**
* Generate code for function call.
*
* @param call_function_token Call function token.
* @param actual_symbol_table The actual or previous symbol table.
* @return true if there's no error on execution and false otherwise.
*/
bool cgenCallFunction(TokenNode *call_function_token, SymbolTable *actual_symbol_table){
int i;
int no_exp_executed;
TokenNode *exp_list;
TokenNode *token_name;
/* Check if command list token is null */
if(call_function_token == NULL){
printError("COMMAND LIST IS INVALID!");
return false;
}
/* Check if symbol table is null */
if(actual_symbol_table == NULL){
printError("INVALID SYMBOL TABLE!");
return false;
}
/* Get function name */
token_name = listGetTokenByIndex(call_function_token->child_list, 1);
/* Check if token_name is valid */
if(token_name == NULL){
printError("INVALID TOKEN NAME!");
return false;
}
/* Add the header of the function call */
addInstructionMainQueue(mips_start_function_call);
/* Push var */
fprintf(stderr, "PUSH >> LINE: %d \n", __LINE__);
symbolTablePushVar(actual_symbol_table);
/* Iniitalize list of expressions executed */
no_exp_executed = 0;
/* Check what is the type of the function call */
if(call_function_token->token_type == TI_CALL_FUNCTION_PAR){
/* Get the list of expressions */
exp_list = listGetTokenByIndex(call_function_token->child_list, 3);
/* Check if expression list is valid */
if(exp_list == NULL){
printError("CANNOT GET LIST OF EXPRESSIONS!");
return false;
}
/* Execute expression list and assign number of params processed */
no_exp_executed = cgenExpressionList(exp_list, actual_symbol_table);
}
/* Add the end of the function call */
addInstructionMainQueueFormated(mips_end_function_call, token_name->lex_str);
/* Pop All parameters */
for(i = 0; i < no_exp_executed; i++){
fprintf(stderr, "POP >> LINE: %d \n", __LINE__);
symbolTablePopVar(actual_symbol_table);
}
/* Pop var */
fprintf(stderr, "POP >> LINE: %d \n", __LINE__);
symbolTablePopVar(actual_symbol_table);
/* Return success */
return true;
}
/**
* Generate code for assign.
*
* @param assign_token Token with the assign operations
* @param actual_symbol_table The actual or previous symbol table.
* @return true if there's no error on execution and false otherwise.
*/
bool cgenAssign(TokenNode *assign_token, SymbolTable *actual_symbol_table){
int num_exp;
TokenList *list_names;
TokenNode *token_exp;
TokenNode *token_name;
TokenNode *list_exp_token;
TokenNode *list_names_token;
SymbolNode *symbol_node;
/* Check if command list token is null */
if(assign_token == NULL){
printError("ASSIGN TOKEN IS INVALID!");
return false;
}
/* Check if symbol table is null */
if(actual_symbol_table == NULL){
printError("INVALID SYMBOL TABLE!");
return false;
}
/* Get token list of names */
list_names_token = listGetTokenByIndex(assign_token->child_list, 1);
/* Check list of names token */
if(list_names_token == NULL){
printError("INVALID LIST OF NAMES!");
return false;
}
/* Get token list of expressions */
list_exp_token = listGetTokenByIndex(assign_token->child_list, 3);
/* Check if list of expressions token is not null */
if(list_exp_token == NULL){
printError("INVALID LIST OF EXPRESSIONS!");
return false;
}
/* Add header of an assign to the main instruction queue */
addInstructionMainQueue(mips_start_assign);
/* If list_names is a token name, we only have one operand */
if(list_names_token->token_type == T_NAME){
/* Get the actual token expression node */
token_exp = list_exp_token;
/* Add header of this instruction to main queue */
addInstructionMainQueueFormated(mips_marker_exp, 0);
/* CGEN(exp) */
cgenExpression(token_exp, actual_symbol_table);
/* Get the actual token name node */
token_name = list_names_token;
/* Add header of the instruction */
addInstructionMainQueueFormated(mips_marker_assign, token_name->lex_str, 0);
/* Get the symbol node */
symbol_node = symbolTableGetSymbolNodeByName(actual_symbol_table, token_name->lex_str);
printf("%s -- symbol name\n", token_name->lex_str);
/* If token name isn't inside the actual symbol table so this t_name is global */
if(symbol_node == NULL){
symbol_node = symbolTableGetSymbolNodeByName(global_symbol_table, token_name->lex_str);
printf("NOT FOUND LOCAL!\n");
}
/* If this symbol is not on global symbol table, so need to add him first, on global symbol table */
if(symbol_node == NULL){
printf("NOT FOUND GLOBAL!\n");
/* Add the symbol to the global symbol table */
symbolTableAddSymbol(global_symbol_table, token_name->lex_str, NUMBER_TYPE);
/* Get the inserted symbol node */
symbol_node = symbolTableGetSymbolNodeByName(global_symbol_table, token_name->lex_str);
/* Check if symbol node is valid */
if(symbol_node == NULL){
printError("INVALID SYMBOL NODE!");
return false;
}
}
/* Store the correct store instruction */
instructionQueueEnqueueInstructionNode(main_instruction_queue, symbolNodeGetStoreInstruction(symbol_node));
}
/* Otherwise we have a list of operands */
else{
/* Get list of names */
list_names = listGetTokensByType(list_names_token->child_list, T_NAME);
/* Check if the list of names is correct */
if(list_names == NULL){
printError("INVALID LIST OF NAMES!");
return false;
}
/* Execute list of expressions, and put reverse results on stack */
num_exp = cgenExpressionList(list_exp_token, actual_symbol_table);
/* Assign our list of names with our actual symbol table, the values stacked */
assignStackHelper(list_names, actual_symbol_table, num_exp);
}
/* Add footer of a assign instruction */
addInstructionMainQueue(mips_end_assign);
/* Return success */
return true;
}
/**
* Generate code for command block.
*
* @param command_block_token Command block token.
* @param actual_symbol_table The actual or previous symbol table.
* @return true if there's no error on execution and false otherwise.
*/
bool cgenCommandBlock(TokenNode *command_block_token, SymbolTable *actual_symbol_table){
TokenNode *block_token;
SymbolTable *new_symbol_table;
/* Check if command list token is null */
if(command_block_token == NULL){
printError("COMMAND BLOCK IS INVALID!");
return false;
}
/* Check if symbol table is null */
if(actual_symbol_table == NULL){
printError("INVALID SYMBOL TABLE!");
return false;
}
/* Get block token */
block_token = listGetTokenByIndex(command_block_token->child_list, 2);
/* Check if block token is not null */
if(block_token == NULL){
printError("ERROR INVALID BLOCK TOKEN!");
return false;
}
/* Create a new scope */
new_symbol_table = newSymbolTable(actual_symbol_table, REGISTER_TYPE_SP);
/* Check if the new symbol table is valid */
if(new_symbol_table == NULL){
printError("INVALID NEW SYMBOL TABLE!");
return false;
}
/* Generate Code for the T_DO [bloco] T_END */
cgenBlockCode(block_token, new_symbol_table);
/* Pop variables */
popSymbolTable(new_symbol_table);
/* Destroy new symbol table */
deleteSymbolTable(&new_symbol_table);
return true;
}
/**
* Generate code for while.
*
* @param while_token token with while structure.
* @param actual_symbol_table The actual or previous symbol table.
* @return true if there's no error on execution and false otherwise.
*/
bool cgenWhile(TokenNode *while_token, SymbolTable *previous_symbol_table){
int while_counter;
SymbolTable *new_symbol_table;
TokenNode *block_token;
TokenNode *exp_token;
/* Check if while token is null */
if(while_token == NULL){
printError("WHILE COMMAND IS INVALID!");
return false;
}
/* Check if symbol table is null */
if(previous_symbol_table == NULL){
printError("INVALID SYMBOL TABLE!");
return false;
}
/* Create a new symbol table */
new_symbol_table = newSymbolTable(previous_symbol_table, REGISTER_TYPE_SP);
/* Check the new symbol table */
if(new_symbol_table == NULL){
printError("CANNOT CREATE A NEW SYMBOL TABLE!");
return false;
}
/* Get expression */
exp_token = listGetTokenByIndex(while_token->child_list, 2);
/* Check if this expression is null */
if(exp_token == NULL){
printError("EXPRESSION TOKEN IS NULL!");
return false;
}
/* Get bloco */
block_token = listGetTokenByIndex(while_token->child_list, 4);
/* Check if block token is null */
if(block_token == NULL){
printError("BLOCK TOKEN IS NULL!");
return false;
}
/* Store locally the counter */
while_counter = loop_while_counter;
/* Increment while loop counter */
loop_while_counter += 1;
/* Add header message */
addInstructionMainQueueFormated(mips_start_while, while_counter);
/* CGEN(exp) */
cgenExpression(exp_token, previous_symbol_table);
/* Check if it's not nil */
addInstructionMainQueue(mips_check_a0_nil);
/* Check condition */
addInstructionMainQueueFormated(mips_check_while, while_counter);
/* CGEN(bloco) */
cgenBlockCode(block_token, new_symbol_table);
/* Pop local symbol table */
popSymbolTable(new_symbol_table);
/* Add while end */
addInstructionMainQueueFormated(mips_end_while, while_counter, while_counter);
/* Delete Scope */
deleteSymbolTable(&new_symbol_table);
/* Return succes */
return true;
}
/**
* Generate code for for.
*
* @param for_token token for.
* @param actual_symbol_table The actual or previous symbol table.
* @return true if there's no error on execution and false otherwise.
*/
bool cgenFor(TokenNode *for_token, SymbolTable *actual_symbol_table){
int count_for;
TokenNode *token_exp;
TokenNode *token_name;
TokenNode *token_block;
TokenNode *token_assign;
TokenNode *token_increment;
SymbolNode *symbol_node;
SymbolTable *new_symbol_table;
SymbolTable *new_symbol_table_iterator;
/* Check if for token is null */
if(for_token == NULL){
printError("IF COMMAND IS INVALID!");
return false;
}
/* Check if symbol table is null */
if(actual_symbol_table == NULL){
printError("INVALID SYMBOL TABLE!");
return false;
}
/* Get token_name */
token_name = listGetTokenByIndex(for_token->child_list, 2);
/* Check if token name is valid */
if(token_name == NULL){
printError("INVALID TOKEN NAME!");
return false;
}
/* Store the counter */
count_for = loop_for_counter;
/* Increment for counter */
loop_for_counter += 1;
/* Get assign expression token */
token_assign = listGetTokenByIndex(for_token->child_list, 4);
/* Check if this expression is valid */
if(token_assign == NULL){
printError("INVALID EXPRESSION TOKEN!");
return false;
}
/* Get assign expression token */
token_exp = listGetTokenByIndex(for_token->child_list, 6);
/* Check if this expression is valid */
if(token_exp == NULL){
printError("INVALID EXPRESSION TOKEN!");
return false;
}
/* Header of the for operation */
addInstructionMainQueue(mips_for_ini);
/* Add a symbol to symbol table */
fprintf(stderr, "PUSH >> LINE: %d \n", __LINE__);
symbolTablePushVar(actual_symbol_table);
/* Initialize a new symbol table just for iterator */
new_symbol_table_iterator = newSymbolTable(actual_symbol_table, REGISTER_TYPE_SP);
/* Check if new symbol table hasn't failed */
if(new_symbol_table_iterator == NULL){
printError("CANNOT CREATE A NEW SYMBOL TABLE!");
return false;
}
/* Initialize a new symbol table */
new_symbol_table = newSymbolTable(new_symbol_table_iterator, REGISTER_TYPE_SP);
/* Check if new symbol table hasn't failed */
if(new_symbol_table == NULL){
printError("CANNOT CREATE A NEW SYMBOL TABLE!");
return false;
}
/* Add our token name to the new symbol table */
symbolTableAddSymbol(new_symbol_table_iterator, token_name->lex_str, NUMBER_TYPE);
///// JUST A TEST
///// fprintf(stderr, "POP >> LINE: %d \n", __LINE__);
///// symbolTablePopVar(new_symbol_table_iterator);
///// INITIALIZING LOCAL VARIABLES WITH WRONG ADDRESS -_-
/* Get the symbol node */
symbol_node = symbolTableGetSymbolNodeByName(new_symbol_table_iterator, token_name->lex_str);
/* Check if symbol node is valid */
if(symbol_node == NULL){
printError("CANNOT RETRIEVE SYMBOL NODE!");
return false;
}
/* Add definition of this token */
instructionQueueEnqueueInstructionNode(main_instruction_queue, symbolNodeGetDefineInstruction(symbol_node));
/* Execute token assign */
cgenExpression(token_assign, new_symbol_table_iterator);
/* Assign the local iterator variable */
instructionQueueEnqueueInstructionNode(main_instruction_queue, symbolNodeGetStoreInstruction(symbol_node));
// --- CHECK THE TYPE OF THE FOR --- //
/* Move iterator from $a0 to $t4 */
addInstructionMainQueue(mips_move_a0_t4);
/* If our for has a non default increment, check the interval of the for */
if(for_token->token_type == TI_FOR_INC){
/* Execute for expression condition */
cgenExpression(token_exp, new_symbol_table);
/* Add check for the expression */
addInstructionMainQueue(mips_for_interval);
}
/* Otherwise we have only one type of interval */
else{
/* Load default type of interval for 'for' */
addInstructionMainQueueFormated(mips_static_number_load, " 1 ");
/* Move this value to $a2 */
addInstructionMainQueue(mips_move_a0_a2);
}
// --- CHECK THE TYPE OF THE FOR --- //
/* Add label of for begin */
addInstructionMainQueueFormated(mips_start_for, count_for);
/* Execute for expression condition */
cgenExpression(token_exp, new_symbol_table);
/* Move expression from $a0 to $t1 */
addInstructionMainQueue(mips_move_a0_t1);
/* Load value of the iterator */
instructionQueueEnqueueInstructionNode( main_instruction_queue,
symbolNodeGetLoadInstruction(symbol_node));
/* Check if it's not nil */
addInstructionMainQueue(mips_check_a0_nil);
/* Add check expression */
addInstructionMainQueueFormated(mips_for_check, count_for);
/* Block token and increment may vary so we first check what's the type of the 'for' */
if(for_token->token_type == TI_FOR_INC){
/* Get block token */
token_block = listGetTokenByIndex(for_token->child_list, 10);
}
else{
/* Get block token */
token_block = listGetTokenByIndex(for_token->child_list, 8);
}
/* Check if block is valid */
if(token_block == NULL){
printError("BLOCK TOKEN IS INVALID!");
return false;
}
/* Execute block code */
cgenBlockCode(token_block, new_symbol_table);
/* Check if the for have an increment expression */
if(for_token->token_type == TI_FOR_INC){
/* Get expression increment */
token_increment = listGetTokenByIndex(for_token->child_list, 8);
/* Check if this token is valid */
if(token_increment == NULL){
printError("INVALID TOKEN EXPRESSION FOR INCREMENT!");
return false;
}
/* Execute increment expression */
cgenExpression(token_increment, new_symbol_table);
}
else{
/* Add 1 into a0, this is the same as increment of 1 */
addInstructionMainQueueFormated(mips_static_number_load, " 1 ");
}
/* Store in $t1 returned by expression */
addInstructionMainQueue(mips_for_load_inc);
/* Load old value of the iterator */
instructionQueueEnqueueInstructionNode( main_instruction_queue,
symbolNodeGetLoadInstruction(symbol_node));
/* Increment the iterator */
addInstructionMainQueue(mips_for_inc);
/* Store the new value of the iterator */
instructionQueueEnqueueInstructionNode( main_instruction_queue,
symbolNodeGetStoreInstruction(symbol_node));
/* Pop local scope */
popSymbolTable(new_symbol_table);
/* Add the footer of the for instruction */
addInstructionMainQueueFormated(mips_end_for, count_for, count_for);
/* Pop local iterator */
popSymbolTable(new_symbol_table_iterator);
/* Pop var from symbol table */
fprintf(stderr, "POP >> LINE: %d \n", __LINE__);
symbolTablePopVar(actual_symbol_table);
/* Delte actual scope */
deleteSymbolTable(&new_symbol_table);
deleteSymbolTable(&new_symbol_table_iterator);
/* Return success */
return true;
}
/**
* Generate code for if.
*
* @param if_token If comand token structure.
* @param actual_symbol_table The actual or previous symbol table.
* @return true if there's no error on execution and false otherwise.
*/
bool cgenIf(TokenNode *if_token, SymbolTable *actual_symbol_table){
int i;
int if_counter;
TokenNode *exp_token;
TokenNode *block_token;
TokenNode *list_elseif;
TokenNode *token_node;
TokenList *exp_list;
TokenList *block_list;
SymbolTable *new_symbol_table;
/* Check if for token is null */
if(if_token == NULL){
printError("IF COMMAND IS INVALID!");
return false;
}
/* Check if symbol table is null */
if(actual_symbol_table == NULL){
printError("INVALID SYMBOL TABLE!");
return false;
}
/* Get expression */
exp_token = listGetTokenByIndex(if_token->child_list, 2);
/* Check if this expression is null */
if(exp_token == NULL){
printError("EXPRESSION TOKEN IS NULL!");
return false;
}
/* Get bloco */
block_token = listGetTokenByIndex(if_token->child_list, 4);
/* Check if block token is null */
if(block_token == NULL){
printError("BLOCK TOKEN IS NULL!");
return false;
}
/* Get list of elseif conditions */
list_elseif = listGetTokenByIndex(if_token->child_list, 5);
/* Check if block token is null */
if(list_elseif == NULL){
printError("INVALID TOKEN LIST ESLE IF!");
return false;
}
/* Store the counter locally */
if_counter = cond_if_counter;
/* Increment if counter */
cond_if_counter += 1;
/* Add header message */
addInstructionMainQueue(mips_start_if);
/* CGEN(exp) */
cgenExpression(exp_token, actual_symbol_table);
/* Check if it's not nil */
addInstructionMainQueue(mips_check_a0_nil);
/* Check condition */
addInstructionMainQueueFormated(mips_check_if, if_counter, 0);
/* Create a new escope */
new_symbol_table = newSymbolTable(actual_symbol_table, REGISTER_TYPE_SP);
/* Check if the new symbol table is correct */
if(new_symbol_table == NULL){
printError("FAILED TO CREATE LOCAL SYMBOL TABLE!");
return false;
}
/* CGEN(bloco) */
cgenBlockCode(block_token, new_symbol_table);
/* Pop local scope */
popSymbolTable(new_symbol_table);
/* Delete the temporary escope */
deleteSymbolTable(&new_symbol_table);
/* Add check for next if condition {else if / else} */
addInstructionMainQueueFormated(mips_next_if, if_counter, if_counter, 0);
/* Check if there are a list of else if */
if(list_elseif->token_type == TI_LIST_ELSEIF){
/* Get all expressions */
exp_list = newTokenList();
/* Check if the list of expression list is valid */
if(exp_list == NULL){
printError("EXPRESSION LIST IS INVALID!");
return false;
}
/* Pick all expressions in elseif */
for(i = 1; i <= list_elseif->child_list->length; i++){
/* Get the token node */
token_node = listGetTokenByIndex(list_elseif->child_list, i);
/* Check if this token node is valid or not */
if(token_node == NULL){
printError("INVALID TOKEN NODE!");
return false;
}
/* If this token is an expression add him to the token list */
if(IS_EXPRESSION(token_node->token_type)){
listAddToken(exp_list, token_node);
}
}
/* Get all blocks */
block_list = newTokenList();
/* Check if the list of blocks is valid */
if(block_list == NULL){
printError("BLOCK LIST IS INVALID!");