-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMIPSGenerator.java
More file actions
1328 lines (1196 loc) · 68.2 KB
/
MIPSGenerator.java
File metadata and controls
1328 lines (1196 loc) · 68.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
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
/*******************************************************************
* MIPS Code Generator for CS340 *
* *
* PROGRAMMER: Emily Culp *
* COURSE: CS340 *
* DATE: 12/10/2024 *
* REQUIREMENT: Final - Compiler *
* *
* DESCRIPTION: *
* The MIPSGenerator class is responsible for generating MIPS assembly code. *
* It handles register allocation, the creation of MIPS instructions, *
* and the management of a data section for variables and their initialization. *
* The class includes methods for allocating temporary and saved registers, *
* freeing registers, and adding variables to the data section with proper initialization. *
* It also maintains a symbol table for use in MIPS code generation. *
* *
* COPYRIGHT: This code is copyright (C) 2024 Emily Culp*
* and Dean Zeller. *
* *
* CREDITS: This code was written with the help of ChatGPT. *
* *
*******************************************************************/
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MIPSGenerator {
private Deque<String> tempRegisters;
private Deque<String> savedRegisters;
private Deque<String> tempFloatRegisters;
private Deque<String> savedFloatRegisters;
private Set<String> usedRegisters;
private Deque<String> freeRegisters;
private static List<String> mipsCode; // List to store MIPS instructions
private static int labelCounter = 0;
private int stackPointer = 0x7fffe000;
private Map<String, Integer> stackMap;
private int stackOffset = -4;
private Map<String, String> dataSection = new HashMap<>();
private Set<String> usedSavedRegisters;
private static SymbolTable symbolTable;
private Queue<String> availableRegisters = new LinkedList<>(Arrays.asList("$t0", "$t1", "$t2", "$t3", "$t4"));
private int registerCounter = 0;
private int currentRegister = 0;
public MIPSGenerator(SymbolTable symbolTable) {
// Initialize register pools
tempRegisters = new ArrayDeque<>(List.of("$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8", "$t9"));
savedRegisters = new ArrayDeque<>(List.of("$s0", "$s1", "$s2", "$s3", "$s4", "$s5", "$s6", "$s7"));
tempFloatRegisters = new ArrayDeque<>(List.of("$f0", "$f2", "$f4", "$f6", "$f8", "$f10", "$f12", "$f14", "$f16", "$f18"));
savedFloatRegisters = new ArrayDeque<>(List.of("$f20", "$f22", "$f24", "$f26", "$f28", "$f30"));
usedRegisters = new HashSet<>();
freeRegisters = new ArrayDeque<>(List.of("$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8", "$t9"));
mipsCode = new ArrayList<>(); // Initialize the list to store MIPS code
stackMap = new HashMap<>();
stackPointer = 0;
usedSavedRegisters = new HashSet<>();
this.symbolTable = symbolTable;
}
/**********************************************************
* METHOD: addToDataSection(String variableName, String initValue, String dataType) *
* DESCRIPTION: Adds a variable to the data section with its initialization value. *
* If the variable is already present, it will not be added again. The method handles *
* various data types (int, string, boolean, double) and provides default values for *
* uninitialized variables. *
* PARAMETERS: *
* String variableName - the name of the variable to add to the data section. *
* String initValue - the initialization value for the variable. *
* String dataType - the data type of the variable (int, string, boolean, double). *
* RETURN VALUE: void *
**********************************************************/
public void addToDataSection(String variableName, String initValue, String dataType) {
// Check if the variable is already in the data section
if (!dataSection.containsKey(variableName)) {
// If initValue contains an '=' sign, it's an assignment (e.g., "x = 5")
if (initValue.contains("=")) {
// Extract the value after '='
String[] parts = initValue.split("=");
initValue = parts[1].trim().replace(";", ""); // Remove any trailing semicolon
}
// Handle different data types and provide default initialization if necessary
if (initValue.isEmpty()) {
switch (dataType.toLowerCase()) {
case "int":
initValue = "0"; // Default to 0 for int
break;
case "string":
initValue = "\"\""; // Default to empty string for string
break;
case "boolean":
initValue = "0"; // Default to false (0) for boolean
break;
case "double":
initValue = "0.0"; // Default to 0.0 for double
break;
default:
System.out.println("Unknown data type: " + dataType);
return;
}
}
// Now add the variable to the data section based on its type
switch (dataType.toLowerCase()) {
case "int":
dataSection.put(variableName, variableName + ": .word " + initValue);
break;
case "string":
dataSection.put(variableName, variableName + ": .asciiz " + initValue);
break;
case "boolean":
int boolValue = Boolean.parseBoolean(initValue) ? 1 : 0;
dataSection.put(variableName, variableName + ": .word " + boolValue);
break;
case "double":
dataSection.put(variableName, variableName + ": .double " + initValue);
break;
default:
System.out.println("Unknown data type: " + dataType);
break;
}
} else {
System.out.println("Variable " + variableName + " already exists in the .data section.");
}
}
/**********************************************************
* METHOD: isVariableInDataSection(String variableName) *
* DESCRIPTION: Checks if a variable is already present in the data section. *
* PARAMETERS: *
* String variableName - the name of the variable to check. *
* RETURN VALUE: boolean - true if the variable is in the data section, false otherwise. *
**********************************************************/
public boolean isVariableInDataSection(String variableName) {
return dataSection.containsKey(variableName);
}
/**********************************************************
* METHOD: allocateTempRegister() *
* DESCRIPTION: Allocates a temporary register from the pool. If no registers are available, *
* it resets the register pool and attempts to allocate a register again. *
* PARAMETERS: none *
* RETURN VALUE: String - the name of the allocated temporary register. *
**********************************************************/
// Method to allocate a temporary register
public String allocateTempRegister() {
if (freeRegisters.isEmpty()) {
addMipsInstruction("# No available temporary registers, resetting register pool.");
resetRegisterPools();
}
if(freeRegisters.isEmpty()){
throw new RuntimeException("No available temporary registers even after reset.");
}
String reg = freeRegisters.poll(); // Get a free register
usedRegisters.add(reg); // Mark the register as used
addMipsInstruction("# Allocating temporary register: " + reg);
// printRegisterState();
return reg;
}
/**********************************************************
* METHOD: allocateSavedRegister() *
* DESCRIPTION: Allocates a saved register from the pool. If no registers are available, *
* it resets the register pool and attempts to allocate a register again. *
* PARAMETERS: none *
* RETURN VALUE: String - the name of the allocated saved register. *
**********************************************************/
// Method to allocate a saved register
public String allocateSavedRegister() {
for (String reg : savedRegisters) {
if (!usedRegisters.contains(reg)) {
usedRegisters.add(reg);
usedSavedRegisters.add(reg);
addMipsInstruction("# Allocating saved register: " + reg);
return reg;
}
}
addMipsInstruction("# No available saved registers, resetting register pool.");
resetRegisterPools();
for (String reg : savedRegisters) {
if (!usedRegisters.contains(reg)) {
usedRegisters.add(reg);
usedSavedRegisters.add(reg);
addMipsInstruction("# Allocating saved register after reset: " + reg);
return reg;
}
}
throw new RuntimeException("No available saved registers even after resetting.");
}
/**********************************************************
* METHOD: freeRegister() *
* DESCRIPTION: Frees a register that was previously allocated. If the register is in use, *
* it is removed from the used registers set and added back to the free registers pool. *
* PARAMETERS: *
* String reg - the name of the register to free. *
* RETURN VALUE: void *
**********************************************************/
// Method to free a register
public void freeRegister(String reg) {
if (usedRegisters.contains(reg)) {
usedRegisters.remove(reg);
if(tempRegisters.contains(reg) && !freeRegisters.contains(reg)){
freeRegisters.offerFirst(reg);
}
if(savedRegisters.contains(reg)){
usedSavedRegisters.remove(reg);
}
addMipsInstruction("# Register freed: " + reg);
// printRegisterState();
} else {
addMipsInstruction("# Warning: Attempted to free an unallocated register: " + reg);
}
}
/**********************************************************
* METHOD: resetRegisterPools() *
* DESCRIPTION: This method clears the usedRegisters and usedSavedRegisters
* sets and reinitializes the freeRegisters deque with the
* original temporary registers. It effectively resets the register
* allocation pools to their initial state*
* PARAMETERS: None*
* RETURN VALUE: void *
**********************************************************/
public void resetRegisterPools() {
// Reset the register pools to their initial states
tempRegisters = new ArrayDeque<>(List.of("$t0", "$t1", "$t2", "$t3", "$t4", "$t5", "$t6", "$t7", "$t8", "$t9"));
usedRegisters.clear(); // Clear the set of used registers
freeRegisters = new ArrayDeque<>(tempRegisters); // Reinitialize free registers from tempRegisters
usedSavedRegisters.clear();
// Optionally log or output a message for debugging
addMipsInstruction("# Reset all register pools");
// printRegisterState();
}
/**********************************************************
* METHOD: loadRegister(String register, Object operand) *
* DESCRIPTION: This method checks the type of the operand
* and generates the corresponding MIPS instruction to
* load the operand into the specified register. It the
* operand is a double, the method loads it from the data
* section, assuming it has already been stored. For strings,
* the address of the string is loaded, and then the
* string's value is fetched*
* PARAMETERS:
* String register - the target register to load the operand
* into (e.g., $t0, $f2)
* Object operand - the value to load into the register. This
* can be an integer, double, boolean, or string*
* EXCEPTION:
* Throws IllegalArgumentException if the operand is of an
* unsupported type
* RETURN VALUE: void *
**********************************************************/
// Method to load a register with an operand
public void loadRegister(String register, Object operand) {
if (operand == null) {
throw new IllegalArgumentException("Operand cannot be null");
}
if (operand instanceof Integer) {
// If the operand is an integer, load it directly
addMipsInstruction("li " + register + ", " + operand); // li = Load immediate
} else if (operand instanceof Double) {
System.out.println("Double operand: " +operand);
// If the operand is a double, load it directly from the data section
// Assuming the double value is already stored in the data section
String label = generateUniqueLabelForDouble((Double) operand);
addMipsInstruction("l.d " + register + ", " + label); // Load double into target register
} else if (operand instanceof Boolean) {
// If the operand is a boolean (true/false)
addMipsInstruction("li " + register + ", " + ((Boolean) operand ? "1" : "0"));
} else if (operand instanceof String) {
// If the operand is a string (assuming it is a label)
String addressRegister = allocateTempRegister();
addMipsInstruction("la " + addressRegister + ", " + operand); // Load address of the string
addMipsInstruction("lw " + register + ", " + stackOffset + "(" + addressRegister + ")"); // Load string address into register
freeRegister(addressRegister);
} else {
throw new IllegalArgumentException("Unsupported operand type: " + operand.getClass().getSimpleName());
}
}
/**********************************************************
* METHOD: generateUniqueLabelForDouble(double value) *
* DESCRIPTION: This method generates a unique label by appending
* the double value to the prefix double_, which is useful
* for referencing double values in the data section of the
* generated MIPS code*
* PARAMETERS: double value - the double value for which to generate
* the label*
* RETURN VALUE: String - A unique label string for the given double
* value, in the format double_<value>*
**********************************************************/
private String generateUniqueLabelForDouble(double value){
return "double_" + value;
}
/**********************************************************
* METHOD: loadImmediate(String reg, int value) *
* DESCRIPTION: This method generates a MIPS li (load immediate)
* instruction to load the specified integer value into the
* given register*
* PARAMETERS: String reg : The target register to load the value into (e.g., $t0).
* int value : The immediate value to load into the register
* RETURN VALUE: void*
**********************************************************/
public static void loadImmediate(String reg, int value){
addMipsInstruction("li " +reg+ ", " +value);
}
/**********************************************************
* METHOD: evaluateExpression(String expression, String endLabel) *
* DESCRIPTION: This method splits the input expression into operands
* and an operator, resolves the operands to registers, and generates MIPS
* code to perform the operation based on the operator. It supports basic
* arithmetic operations (+, -, *, /) and comparison operations
* (<, >, ==, !=, <=, >=). It then adds the result to the MIPS code,
* followed by a jump to the specified endLabel*
* PARAMETERS: String expression - the arithmetic expression to evaluate,
* formatted as "operand1 operator operand2" (e.g., "x+y")
* String endLabel - the label to jump to after evaluating the expression
* (used for conditional branches)
* RETURN VALUE: String - A string containing the MIPS code for evaluating the
* expression*
**********************************************************/
// Example evaluateExpression method (simplified)
public String evaluateExpression(String expression, String endLabel) {
// Split the expression into operands and operator (assuming basic "operand operator operand" format)
String[] tokens = expression.split(" ");
if (tokens.length != 3) {
throw new IllegalArgumentException("Invalid expression format.");
}
String operand1 = tokens[0]; // First operand (e.g., "y")
String operator = tokens[1]; // Operator (e.g., "+")
String operand2 = tokens[2]; // Second operand (e.g., "5")
// Allocate registers for operands and result
String reg1 = resolveToRegister(operand1); // Resolve the register for operand1
String reg2 = resolveToRegister(operand2); // Resolve the register for operand2
String regResult = allocateTempRegister(); // Allocate a temporary register for the result
// Perform the operation based on the operator
switch (operator) {
case "+":
mipsAdd(reg1, reg2, regResult);
break;
case "-":
mipsSub(reg1, reg2, regResult);
break;
case "*":
mipsMul(reg1, reg2, regResult);
break;
case "/":
mipsDiv(reg1, reg2, regResult);
break;
case "<":
addMipsInstruction("slt " + regResult + ", " + reg1 + ", " + reg2); // Set less than
addMipsInstruction("bne " + regResult + ", $zero, " + endLabel); // Branch if equal
break;
case ">":
addMipsInstruction("slt " + regResult + ", " + reg2 + ", " + reg1); // Set less than (reverse the operands)
addMipsInstruction("beq " + regResult + ", $zero, " + endLabel); // Branch if equal
break;
case "==":
addMipsInstruction("sub " + regResult + ", " + reg1 + ", " + reg2);
addMipsInstruction("beq " + regResult + ", $zero, " + endLabel); // Branch if equal
break;
case "!=":
addMipsInstruction("sub " + regResult + ", " + reg1 + ", " + reg2);
addMipsInstruction("bne " + regResult + ", $zero, " + endLabel); // Branch if not equal
break;
case "<=":
// For <=, check if greater than and jump if true
addMipsInstruction("slt " + regResult + ", " + reg2 + ", " + reg1); // Set less than
addMipsInstruction("beq " + regResult + ", $zero, " + endLabel); // Jump if greater (i.e., less than or equal)
break;
case ">=":
// For >=, check if less than and jump if true
addMipsInstruction("slt " + regResult + ", " + reg1 + ", " + reg2); // Set less than
addMipsInstruction("beq " + regResult + ", $zero, " + endLabel); // Jump if less (i.e., greater than or equal)
break;
default:
throw new IllegalArgumentException("Unsupported operator: " + operator);
}
mipsCode.add(endLabel + ":");
// Join the list into a single string with line breaks
return String.join("\n", mipsCode);
}
/**********************************************************
* METHOD: mipsAdd(String reg1, String reg2, String regResult) *
* DESCRIPTION: Generates MIPS assembly code to perform addition between two operands (either registers or an immediate value). *
* PARAMETERS: *
* String reg1 - The first operand (either a register or an integer literal). *
* String reg2 - The second operand (either a register or an integer literal). *
* String regResult - The register where the result of the addition will be stored. *
* RETURN VALUE: none *
**********************************************************/
public void mipsAdd(String reg1, String reg2, String regResult) {
System.out.println("mipsAdd called with reg1=" + reg1 + ", reg2=" + reg2 + ", regResult=" + regResult);
if (isRegister(reg1) && isRegister(reg2)) {
// Case 1: Both operands are registers
addMipsInstruction("add " + regResult + ", " + reg1 + ", " + reg2); // Integer addition
} else if (isInteger(reg2)) {
// Case 2: reg2 is an immediate value, so use addi
addMipsInstruction("addi " + regResult + ", " + reg1 + ", " + reg2); // Integer addition with immediate
} else {
System.out.println("Invalid operands for mipsAdd: reg1=" + reg1 + ", reg2=" + reg2);
}
}
/**********************************************************
* METHOD: mipsSub(String reg1, String reg2, String regResult) *
* DESCRIPTION: Generates MIPS assembly code to perform subtraction between two operands (either registers or an immediate value). *
* PARAMETERS: *
* String reg1 - The first operand (either a register or an integer literal). *
* String reg2 - The second operand (either a register or an integer literal). *
* String regResult - The register where the result of the subtraction will be stored. *
* RETURN VALUE: none *
**********************************************************/
public void mipsSub(String reg1, String reg2, String regResult) {
if (isRegister(reg1) && isRegister(reg2)) {
// Case 1: Both operands are registers (integers)
addMipsInstruction("sub " + regResult + ", " + reg1 + ", " + reg2); // Integer subtraction
} else if (isInteger(reg2)) {
// Case 2: reg2 is an immediate value, so use subi
addMipsInstruction("subi " + regResult + ", " + reg1 + ", " + reg2); // Integer subtraction with immediate
} else {
System.out.println("Invalid operands for mipsSub: reg1=" + reg1 + ", reg2=" + reg2);
}
}
/**********************************************************
* METHOD: mipsMul(String reg1, String reg2, String regResult) *
* DESCRIPTION: Generates MIPS assembly code to perform multiplication between two operands (either registers or an immediate value). *
* PARAMETERS: *
* String reg1 - The first operand (either a register or an integer literal). *
* String reg2 - The second operand (either a register or an integer literal). *
* String regResult - The register where the result of the multiplication will be stored. *
* RETURN VALUE: none *
**********************************************************/
public void mipsMul(String reg1, String reg2, String regResult) {
if (isRegister(reg1) && isRegister(reg2)) {
// Case 1: Both operands are registers (integers)
addMipsInstruction("mul " + regResult + ", " + reg1 + ", " + reg2); // Integer multiplication
} else if (isInteger(reg2)) {
// Case 2: reg2 is an immediate value, so use muli
addMipsInstruction("muli " + regResult + ", " + reg1 + ", " + reg2); // Integer multiplication with immediate
} else {
System.out.println("Invalid operands for mipsMul: reg1=" + reg1 + ", reg2=" + reg2);
}
}
/**********************************************************
* METHOD: mipsDiv(String reg1, String reg2, String regResult) *
* DESCRIPTION: Generates MIPS assembly code to perform division between two operands (either registers or an immediate value). Throws an ArithmeticException if attempting to divide by zero. *
* PARAMETERS: *
* String reg1 - The first operand (either a register or an integer literal). *
* String reg2 - The second operand (either a register or an integer literal). *
* String regResult - The register where the result of the division will be stored. *
* RETURN VALUE: none *
**********************************************************/
public void mipsDiv(String reg1, String reg2, String regResult) {
if(Integer.parseInt(reg2) == 0){
throw new ArithmeticException("Division by 0");
}
if (isRegister(reg1) && isRegister(reg2)) {
// Case 1: Both operands are registers (integers)
addMipsInstruction("div " + reg1 + ", " + reg2); // Perform integer division
addMipsInstruction("mflo " + regResult); // Move result to regResult (quotient)
} else if (isInteger(reg2)) {
// Case 2: reg2 is an immediate value, so perform division with immediate
addMipsInstruction("div " + reg1 + ", " + reg2); // Perform integer division
addMipsInstruction("mflo " + regResult); // Move result to regResult (quotient)
} else {
System.out.println("Invalid operands for mipsDiv: reg1=" + reg1 + ", reg2=" + reg2);
}
}
/**********************************************************
* METHOD: isRegister(String operand) *
* DESCRIPTION: Checks if a given operand is a register (starts with '$'). *
* PARAMETERS: *
* String operand - The operand to check. *
* RETURN VALUE: *
* boolean - true if the operand is a register, otherwise false. *
**********************************************************/
private boolean isRegister(String operand) {
return operand.startsWith("$"); // Check if the operand is a register (starts with '$')
}
/**********************************************************
* METHOD: resolveToRegister(String operand) *
* DESCRIPTION: Resolves an operand to a register. If the operand is an integer literal, it returns the literal. If the operand is a variable, it retrieves the corresponding register from the SymbolTable. *
* PARAMETERS: *
* String operand - The operand to resolve (either a register or a variable). *
* RETURN VALUE: *
* String - The resolved register or immediate value. *
* EXCEPTION:
* Throws IllegalArgumentException if the variable is not found in the SymbolTable. *
**********************************************************/
private String resolveToRegister(String operand) {
// Check if operand is an integer literal
if (isInteger(operand)) {
// If operand is a literal, return the literal value instead of a register
System.out.println("Using immediate value: " + operand);
return operand; // Return the literal as a string
} else {
// If operand is a variable, get its register from the SymbolTable
String variableRegister = symbolTable.getRegisterForVariable(operand);
if (variableRegister == null) {
System.out.println("Error: Variable '" + operand + "' not found in SymbolTable.");
throw new IllegalArgumentException("Variable '" + operand + "' not found in SymbolTable.");
}
System.out.println("Resolved variable '" + operand + "' to register " + variableRegister);
return variableRegister;
}
}
/**********************************************************
* METHOD: isInteger(String token) *
* DESCRIPTION: Checks if a given string represents an integer. *
* PARAMETERS: *
* String token - The string to check. *
* RETURN VALUE: *
* boolean - true if the string can be parsed as an integer, otherwise false. *
**********************************************************/
public boolean isInteger(String token) {
try {
Integer.parseInt(token);
return true;
} catch (NumberFormatException e) {
return false;
}
}
/**********************************************************
* METHOD: convertConditionToMips(String condition, String label, SymbolTable symbolTable) *
* DESCRIPTION: Converts a condition (e.g., "x < 5") into MIPS assembly code for comparison and branching. *
* PARAMETERS: *
* String condition - The condition to convert (e.g., "x < 5"). *
* String label - The label to jump to if the condition is true. *
* SymbolTable symbolTable - The SymbolTable used to resolve variable registers. *
* RETURN VALUE: *
* String - The generated MIPS assembly code as a string. *
**********************************************************/
public String convertConditionToMips(String condition, String label, SymbolTable symbolTable) {
// Split the condition into parts (e.g., "x < 5" -> ["x", "<", "5"])
String[] conditionParts = condition.split(" ");
String operator = conditionParts[1]; // Get the operator
String leftOperand = conditionParts[0]; // Left side of the condition
String rightOperand = conditionParts[2]; // Right side of the condition
// Get the register for the left operand (e.g., "x" -> $t0)
String leftRegister = symbolTable.getRegisterForVariable(leftOperand);
if (leftRegister == null) {
System.out.println("Error: No register found for variable '" + leftOperand + "'");
return "";
}
// We need a temporary register to store the comparison result
String tempRegister = "$t1"; // Temporary register for condition result
StringBuilder mipsCode = new StringBuilder();
// Generate MIPS code based on the operator
switch (operator) {
case "<":
mipsCode.append("slt ").append(tempRegister).append(", ").append(leftRegister).append(", ").append(rightOperand).append("\n");
mipsCode.append("bne ").append(tempRegister).append(", $zero, ").append(label).append("\n"); // Branch if true
break;
case ">":
mipsCode.append("sgt ").append(tempRegister).append(", ").append(leftRegister).append(", ").append(rightOperand).append("\n");
mipsCode.append("bne ").append(tempRegister).append(", $zero, ").append(label).append("\n"); // Branch if true
break;
case "<=":
mipsCode.append("sle ").append(tempRegister).append(", ").append(leftRegister).append(", ").append(rightOperand).append("\n");
mipsCode.append("bne ").append(tempRegister).append(", $zero, ").append(label).append("\n"); // Branch if true
break;
case ">=":
mipsCode.append("sge ").append(tempRegister).append(", ").append(leftRegister).append(", ").append(rightOperand).append("\n");
mipsCode.append("bne ").append(tempRegister).append(", $zero, ").append(label).append("\n"); // Branch if true
break;
case "==":
mipsCode.append("beq ").append(leftRegister).append(", ").append(rightOperand).append(", ").append(label).append("\n"); // Branch if equal
break;
case "!=":
mipsCode.append("bne ").append(leftRegister).append(", ").append(rightOperand).append(", ").append(label).append("\n"); // Branch if not equal
break;
default:
System.out.println("Error: Unsupported operator '" + operator + "'");
return "";
}
// Return the generated MIPS code
return mipsCode.toString();
}
/**********************************************************
* METHOD: generateIfElse(String condition, List<String> ifBodyTokens, List<String> elseBodyTokens) *
* DESCRIPTION: Generates MIPS assembly code for an if-else structure. The method evaluates a condition and generates MIPS for both the "if" and "else" blocks. *
* PARAMETERS: *
* - String condition: A string representing the condition to evaluate. *
* - List<String> ifBodyTokens: A list of strings representing the tokens for the "if" block. *
* - List<String> elseBodyTokens: A list of strings representing the tokens for the "else" block. *
* RETURN VALUE: None *
**********************************************************/
public void generateIfElse(String condition, List<String> ifBodyTokens, List<String> elseBodyTokens) {
addMipsInstruction(" ");
// Generate labels for the if-else structure
String ifLabel = "IF_BLOCK";
String elseLabel = "ELSE_BLOCK";
String endLabel = "END_IF_ELSE";
// Start generating MIPS code
addMipsInstruction("# If-Else Structure");
// Evaluate the condition expression
String conditionRegister = evaluateExpression(condition, endLabel); // Get the condition register
// Process the conditional logic based on operator (e.g., <, >, ==, !=)
// String operator = extractOperatorFromCondition(condition); // Extract operator from the condition
// String dataRegister = getDataRegisterForCondition(condition); // Get data register for condition evaluation
// String constantRegister = getConstantRegisterForCondition(condition); // Get constant register (if applicable)
// If the condition is false, jump to the else block
// addMipsInstruction("beq " + conditionRegister + ", $zero, " + elseLabel);
// If block
addMipsInstruction(ifLabel + ":");
processBodyTokens(ifBodyTokens); // Generate MIPS for if block
// Jump to the end label after if block
addMipsInstruction("j " + endLabel);
// Else block (only if else body tokens are not empty)
if (!elseBodyTokens.isEmpty()) {
addMipsInstruction(elseLabel + ":");
processBodyTokens(elseBodyTokens); // Generate MIPS for else block
}
// End of if-else structure
addMipsInstruction(endLabel + ":");
}
/**********************************************************
* METHOD: processBodyTokens(List<String> bodyTokens) *
* DESCRIPTION: Processes a list of body tokens and generates MIPS code for each token. It handles print statements, assignments, and nested if-else structures. *
* PARAMETERS: *
* - List<String> bodyTokens: A list of strings representing the tokens in the body of a control structure. *
* RETURN VALUE: None *
**********************************************************/
public void processBodyTokens(List<String> bodyTokens) {
for (String token : bodyTokens) {
if (token.startsWith("print")) {
// Example: print("Hello")
String message = extractPrintMessage(token);
addMipsInstruction("# Printing message: " + message);
generatePrint(message);
} else if (token.contains("=")) {
// Example: x = 10
String[] parts = token.split("=");
String variable = parts[0].trim();
String value = parts[1].trim();
addMipsInstruction("# Assigning value to variable: " + variable);
generateAssignmentInstruction(variable, value);
} else if (token.startsWith("if")) {
// Nested if-else handling
String condition = extractCondition(token);
List<String> nestedIfBodyTokens = extractIfBodyTokens(token);
List<String> nestedElseBodyTokens = extractElseBodyTokens(token);
generateIfElse(condition, nestedIfBodyTokens, nestedElseBodyTokens);
} else {
addMipsInstruction("# Unknown token: " + token);
}
}
}
/**********************************************************
* METHOD: extractPrintMessage(String token) *
* DESCRIPTION: Extracts the message to be printed from a print statement token. *
* PARAMETERS: *
* - String token: A string representing the print statement, e.g., print("message"). *
* RETURN VALUE: String - A string containing the message to be printed. *
**********************************************************/
private String extractPrintMessage(String token) {
// Assuming format: print("message")
int startIndex = token.indexOf("\"") + 1;
int endIndex = token.lastIndexOf("\"");
return token.substring(startIndex, endIndex);
}
/**********************************************************
* METHOD: generateAssignmentInstruction(String variable, String value) *
* DESCRIPTION: Generates MIPS assembly code to assign a value to a variable. The method handles integer assignment. *
* PARAMETERS: *
* - String variable: A string representing the variable name to which the value will be assigned. *
* - String value: A string representing the value to assign to the variable. *
* RETURN VALUE: None *
**********************************************************/
private void generateAssignmentInstruction(String variable, String value) {
// Assuming integer assignment
addMipsInstruction("li $t0, " + value); // Load immediate value into a temporary register
addMipsInstruction("sw $t0, " + variable); // Store the value into the variable's memory address
}
/**********************************************************
* METHOD: extractIfBodyTokens(String token) *
* DESCRIPTION: Extracts the tokens for the "if" block from a given if statement string. *
* PARAMETERS: *
* - String token: A string representing the if statement containing the body in curly braces. *
* RETURN VALUE: List<String> - A list of strings representing the tokens in the "if" body. *
**********************************************************/
private List<String> extractIfBodyTokens(String token) {
// Extracts tokens between `{` and `}` of the `if` block
int start = token.indexOf("{") + 1;
int end = token.indexOf("}");
String body = token.substring(start, end).trim();
return Arrays.asList(body.split(";")); // Assuming semicolon-separated statements
}
/**********************************************************
* METHOD: extractElseBodyTokens(String token) *
* DESCRIPTION: Extracts the tokens for the "else" block from a given if-else statement string. *
* PARAMETERS: *
* - String - token: A string representing the if-else statement containing the body in curly braces. *
* RETURN VALUE: List<String> - A list of strings representing the tokens in the "else" body. If no "else" block exists, returns an empty list. *
**********************************************************/
private List<String> extractElseBodyTokens(String token) {
// Extracts tokens for the `else` block, if it exists
int elseIndex = token.indexOf("else");
if (elseIndex == -1) return new ArrayList<>();
int start = token.indexOf("{", elseIndex) + 1;
int end = token.indexOf("}", elseIndex);
String body = token.substring(start, end).trim();
return Arrays.asList(body.split(";")); // Assuming semicolon-separated statements
}
/**********************************************************
* METHOD: generateWhileLoop(String condition, List<String> bodyTokens) *
* DESCRIPTION: Generates MIPS assembly code for a while loop. The method processes the loop condition and body tokens, generating the appropriate assembly instructions. *
* PARAMETERS: *
* - String condition: A string representing the loop condition. *
* - List<String> - bodyTokens: A list of strings representing the tokens in the body of the while loop. *
* RETURN VALUE: None *
**********************************************************/
// Generate MIPS code for a 'while' loop
// Method to generate MIPS code for a while loop
public void generateWhileLoop(String condition, List<String> bodyTokens) {
addMipsInstruction(" ");
if (bodyTokens == null || bodyTokens.isEmpty()) {
throw new IllegalArgumentException("Loop body tokens are empty.");
}
// Extract the loop variable and constant from the condition (e.g., "y < 5")
String loopVar = condition.split(" ")[0].trim(); // Loop variable (e.g., "y")
String operator = condition.split(" ")[1].trim(); // Condition operator (e.g., "<")
String constant = condition.split(" ")[2].trim(); // Constant value (e.g., "5")
// Retrieve the register for the loop variable (e.g., "y")
String dataRegister = assignRegister(loopVar);
// Start of the loop
String startLabel = "label_6";
String endLabel = "label_7";
addMipsInstruction(startLabel + ":");
// Condition check using the evaluateExpression method
addComment("Check condition for " + loopVar + " " + operator + " " + constant);
evaluateExpression(condition, endLabel);
// Loop body
// Inside the loop body processing
for (String bodyToken : bodyTokens) {
bodyToken = bodyToken.trim();
if (bodyToken.contains("=")) {
// Assignment statement with an arithmetic expression (e.g., y = y + 1)
String[] statementParts = bodyToken.split("=");
String leftSide = statementParts[0].trim();
String rightSide = statementParts[1].trim().replace(";", ""); // Remove semicolon
// Resolve the register for the left-hand side variable
String leftRegister = symbolTable.getRegisterForVariable(leftSide);
if (leftRegister == null) {
throw new IllegalStateException("Variable '" + leftSide + "' not found in symbol table");
}
// Handle cases like y = y + 1
if (rightSide.contains("+") || rightSide.contains("-")) {
// For expressions like "y + 1" or "y - 1"
String[] operands = rightSide.split("\\+|\\-");
String operand1 = operands[0].trim();
String operand2 = operands[1].trim();
// Get the registers for operands
String operand1Register = symbolTable.getRegisterForVariable(operand1);
String operand2Register = assignRegister(operand2); // Register for the constant value, e.g., 1
if (operand1Register == null) {
throw new IllegalStateException("Operand variable '" + operand1 + "' not found in symbol table");
}
String resultRegister = evaluateExpression(rightSide, endLabel);
// Store the result back into the left-hand side variable register
addMipsInstruction("move " + leftRegister + ", " + resultRegister);
} else {
// Handle simple assignments without arithmetic expressions
String resultRegister = evaluateExpression(rightSide, endLabel); // Evaluate the arithmetic expression
addMipsInstruction("move " + leftRegister + ", " + resultRegister);
}
} else if (bodyToken.equals("++") || bodyToken.equals("--")) {
handleIncrementOrDecrement(bodyToken);
} else if (bodyToken.startsWith("print")) {
handlePrintStatement(bodyToken);
} else {
processBodyToken(bodyToken);
}
}
// Store the updated value of the loop variable back to memory (if applicable)
addMipsInstruction("sw " + dataRegister + ", " + loopVar);
// Jump back to the start of the loop
addMipsInstruction("j " + startLabel);
// End of the loop
addMipsInstruction(endLabel + ":");
}
/**********************************************************
* METHOD: handlePrintStatement(String statement) *
* DESCRIPTION: Handles the print statement by extracting the variable name and generating MIPS code to print its value. *
* PARAMETERS: String statement - The print statement to handle, in the form of "print(variable)". The variable should be extracted and printed using its corresponding register. *
* RETURN VALUE: None *
**********************************************************/
private void handlePrintStatement(String statement) {
// Parse the print statement
String regex = "print\\s*\\(\\s*(\\w+)\\s*\\)";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(statement);
if (matcher.find()) {
String variableName = matcher.group(1);
String printRegister = symbolTable.getRegisterForVariable(variableName);
if (printRegister != null) {
generatePrint(printRegister);
} else {
System.out.println("Error: No register found for variable '" + variableName + "' in print statement.");
}
} else {
System.out.println("Error: Malformed print statement: " + statement);
}
}
/**********************************************************
* METHOD: assignRegister(String variable) *
* DESCRIPTION: Assigns a register for a given variable. If the variable is a constant, it is handled differently by loading the immediate value into a register. *
* PARAMETERS: String variable - The variable for which a register is assigned. *
* RETURN VALUE: String - The register that holds the value of the variable. *
**********************************************************/
public String assignRegister(String variable) {
// Check if the variable is a number or an immediate constant (this includes literal values)
boolean isConstant = isConstant(variable); // Helper method to check if the variable is a constant value
// If the variable is constant, we handle it differently
if (isConstant) {
// Assign register and load immediate value (constant)
String register = availableRegisters.poll(); // Get and remove the first available register
if (register == null) {
throw new RuntimeException("No available registers for constant: " + variable);
}
addMipsInstruction("li " + register + ", " + variable); // Load immediate value into register
return register;
}
// For regular variables, check if a register already exists
String register = symbolTable.getRegister(variable); // Retrieve register if it exists
if (register != null) {
return register; // Return the existing register
}
// Determine if the variable is stored in the data section
boolean isInDataSection = isVariableInDataSection(variable);
register = availableRegisters.poll(); // Get and remove the first available register
if (register == null) {
throw new RuntimeException("No available registers for variable: " + variable);
}
// Add the register to the symbol table
symbolTable.addRegisterToVariable(variable, register);
// If the variable is in the data section, load its value from memory
if (isInDataSection) {
addMipsInstruction("lw " + register + ", " + variable); // Load word for data section variable
} else {
// Otherwise, assume it's an immediate value
addMipsInstruction("li " + register + ", " + variable); // Load immediate value
}
return register;
}
/**********************************************************
* METHOD: isConstant(String variable) *
* DESCRIPTION: Helper method to check if a variable is a constant (numeric value). *
* PARAMETERS: String variable - The variable to check. *
* RETURN VALUE: boolean - True if the variable is a constant, false otherwise. *
**********************************************************/
// Helper method to check if a variable is a constant (numeric value)
private boolean isConstant(String variable) {
try {
Integer.parseInt(variable); // Try to parse the variable as an integer
return true; // It's a constant if it parses successfully
} catch (NumberFormatException e) {
return false; // Not a constant if it throws an exception
}
}
/**********************************************************
* METHOD: generatePrint(String register) *
* DESCRIPTION: Generates MIPS code to print an integer using the value stored in the given register. *
* PARAMETERS: String register - The register containing the integer value to be printed. *
* RETURN VALUE: None *
**********************************************************/
// Method to generate the MIPS code for printing an integer (value in $a0)
public void generatePrint(String register) {
if (register != null) {
addMipsInstruction("li $v0, 1"); // Load syscall number for print integer
addMipsInstruction("move $a0, " + register); // Move the value (register) to $a0
addMipsInstruction("syscall"); // Perform the syscall to print the value
} else {
System.out.println("No valid register for print statement.");
}
}
/**********************************************************
* METHOD: handleIncrementOrDecrement(String expression) *
* DESCRIPTION: Handles increment or decrement operations (e.g., "i++" or "i--"). This method extracts the variable and calls generateIncrementOrDecrement for processing. *
* PARAMETERS: String expression - The expression to process, which can be either an increment (++) or decrement (--). *
* RETURN VALUE: None *
**********************************************************/
public void handleIncrementOrDecrement(String expression) {
// Check if the expression contains '++' or '--'
if (expression.contains("++")) {
// Extract the variable part before '++' (e.g., "i" from "i++")
String variable = expression.split("\\+\\+")[0].trim();
if (!variable.isEmpty()) {
// If the variable is valid, treat it as an increment
generateIncrementOrDecrement(variable, true); // True for increment
} else {
System.out.println("Error: Invalid increment expression: " + expression);
}
} else if (expression.contains("--")) {
// Extract the variable part before '--' (e.g., "i" from "i--")
String variable = expression.split("--")[0].trim();
if (!variable.isEmpty()) {
// If the variable is valid, treat it as a decrement
generateIncrementOrDecrement(variable, false); // False for decrement
} else {
System.out.println("Error: Invalid decrement expression: " + expression);
}
} else {
System.out.println("Invalid increment/decrement operation: " + expression);
}
}
/**********************************************************
* METHOD: generateIncrementOrDecrement(String variable, boolean isIncrement) *
* DESCRIPTION: Generates MIPS code to increment or decrement a variable. *
* PARAMETERS: String variable - The variable to increment or decrement. *
* boolean isIncrement - True for increment, false for decrement. *
* RETURN VALUE: None *
**********************************************************/
public void generateIncrementOrDecrement(String variable, boolean isIncrement) {