-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcode_generator.py
More file actions
1769 lines (1624 loc) · 95.1 KB
/
code_generator.py
File metadata and controls
1769 lines (1624 loc) · 95.1 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
from typing import Union
import scanner
from enum import Enum, IntEnum
class Constants(IntEnum):
OUTPUT_FUNCTION = -1
INT_TYPE = 1
VOID_TYPE = 2
class VariableScope(Enum):
GLOBAL_VARIABLE = 1
LOCAL_VARIABLE = 2
"""
Special type a.k.a. TOF SAG
This means that the variable is stored in the local scope and it is an address
to another variable. This address is absolute
"""
ARRAY = 3
class MathOperator(Enum):
PLUS = 1
MINUS = 2
MULT = 3
LESS_THAN = 4
EQUALS = 5
class VariableType(Enum):
"""
Type of the variable in symbol table
"""
INT = 1
INT_ARRAY = 2
VOID_FUNCTION = 3
INT_FUNCTION = 4
def __str__(self) -> str:
if self == VariableType.INT:
return "int"
if self == VariableType.INT_ARRAY:
return "array"
if self == VariableType.VOID_FUNCTION:
return "function (void)"
if self == VariableType.INT_FUNCTION:
return "function (int)"
raise Exception("SHASH")
class SymbolTableEntry:
"""
Each entry in the symbol table has this type
"""
def __init__(self, lexeme: str, var_type: VariableType, parameters: Union[list[VariableType], int, None]):
self.lexeme = lexeme
self.var_type = var_type
# List of parameters for function or the size of array
self.parameters = parameters
# The address is either the absolute address if this is global variable.
# Otherwise it is the offset of this variable from top of the stack.
self.address = -1
def __repr__(self) -> str:
return str(self)
def __str__(self) -> str:
return f"({self.lexeme}, {self.var_type}, {self.parameters}, {self.address})"
class ThreeAddressInstructionNumberType(Enum):
DIRECT_ADDRESS = 1
IMMEDIATE = 2
INDIRECT_ADDRESS = 3
class ThreeAddressInstructionOperand:
"""
Each operand of each instruction must be this type
"""
def __init__(self, value: int, number_type: ThreeAddressInstructionNumberType):
self.value = value
self.number_type = number_type
def __str__(self) -> str:
if self.number_type == ThreeAddressInstructionNumberType.DIRECT_ADDRESS:
return str(self.value)
elif self.number_type == ThreeAddressInstructionNumberType.IMMEDIATE:
return "#" + str(self.value)
elif self.number_type == ThreeAddressInstructionNumberType.INDIRECT_ADDRESS:
return "@" + str(self.value)
def __repr__(self) -> str:
return str(self)
class ThreeAddressInstructionOpcode(Enum):
ADD = 1
MULT = 2
SUB = 3
EQ = 4
LT = 5
ASSIGN = 6
JPF = 7
JP = 8
PRINT = 9
def __str__(self):
return str(self.name)
def __repr__(self) -> str:
return str(self.name)
class ThreeAddressInstruction:
"""
Each emitted instruction should be this type
"""
def __init__(
self,
opcode: ThreeAddressInstructionOpcode,
operands: list[ThreeAddressInstructionOperand],
):
self.opcode = opcode
self.operands = operands
def __str__(self) -> str:
to_join = [str(self.opcode)]
for operand in self.operands:
to_join.append(str(operand))
while len(to_join) != 4:
to_join.append("")
return "(" + ", ".join(to_join) + ")"
class SemanticAnalyzer:
def __init__(self):
# A stack which each entry contains a list of variables in a scope
self.has_error = False
self.virtual_scopes: list[int] = []
self.scope_stack: list[list[SymbolTableEntry]] = [[SymbolTableEntry("output", VariableType.VOID_FUNCTION, [VariableType.INT])]]
self.error_list: list[str] = []
# A list which each entry is a list of break statements in each scope.
# Each entry represents a nested for loop. We break always breaks the inner loop.
self.break_addresses: list[list[int]] = []
self.function_list : dict[str, list] = {
"output": [VariableType.VOID_FUNCTION, [VariableType.INT]]
}
def enter_scope(self):
self.scope_stack.append([])
def exit_scope(self):
self.scope_stack.pop()
def enter_for(self):
self.break_addresses.append([])
def exit_for(self):
self.break_addresses.pop()
def enter_virtual_scope(self):
self.virtual_scopes.append(len(self.scope_stack[-1]))
def exit_virual_scope(self):
self.scope_stack[-1] = self.scope_stack[-1][:self.virtual_scopes[-1]]
self.virtual_scopes.pop()
def get_entry(self, lexeme: str) -> Union[SymbolTableEntry, None]:
"""
Checks if a lexeme has been defined before and returns it if it has
"""
for scope in reversed(self.scope_stack):
for entry in scope:
if lexeme == entry.lexeme:
return entry
return None
def is_declared_in_current_scope(self, lexeme: str) -> bool:
"""
Checks if a variable is defined in current scope
"""
for entry in self.scope_stack[-1]:
if lexeme == entry.lexeme:
return True
return False
def is_global_variable(self, lexeme: str) -> bool:
"""
Checks if a variable is a global variable or not.
It simply checks if it has been declared in the first scope stack or not.
"""
# First check if we are shadowing something
for scope in self.scope_stack[1:]:
for entry in scope:
if lexeme == entry.lexeme:
return False
# Otherwise check the global scope
for entry in self.scope_stack[0]:
if lexeme == entry.lexeme:
return True
# This means that the variable is not defined?
return False
def get_variable_type_by_address(self, address: int, scope: VariableScope) -> VariableType:
"""
Gets the variable type based on the address of the variable
"""
if scope == VariableScope.GLOBAL_VARIABLE:
for entry in self.scope_stack[0]:
if entry.address == address and entry.var_type in [VariableType.INT, VariableType.INT_ARRAY]:
return entry.var_type
if address == 0: # special case: Another semantic error!
return VariableType.INT
raise Exception("Undefined global variable at address " + str(address))
if scope == VariableScope.ARRAY or scope == VariableScope.LOCAL_VARIABLE:
for entry in self.scope_stack[1]:
if entry.address == address and entry.var_type in [VariableType.INT, VariableType.INT_ARRAY]:
return entry.var_type
raise Exception("Undefined local variable at address " + str(address))
raise Exception("Shash")
def declare_variable(self, name: str):
"""
Declare a new int variable in the current scope
"""
assert not self.is_declared_in_current_scope(name)
self.scope_stack[-1].append(SymbolTableEntry(name, VariableType.INT, None))
def declare_array(self, name: str, size: int):
"""
Declare a new int array in the current scope
"""
assert not self.is_declared_in_current_scope(name)
self.scope_stack[-1].append(SymbolTableEntry(name, VariableType.INT_ARRAY, size))
def declare_function(self, name: str, return_type: Constants, start_address: int):
"""
Declare a new int array in the current scope
"""
assert not self.is_declared_in_current_scope(name)
# Convert type on stack to function type
if return_type == Constants.INT_TYPE:
function_type = VariableType.INT_FUNCTION
elif return_type == Constants.VOID_TYPE:
function_type = VariableType.VOID_FUNCTION
else:
raise Exception("RIDEMAN BOZORG")
# Create entry
entry = SymbolTableEntry(name, function_type, [])
self.function_list[name] = [function_type]
entry.address = start_address
self.scope_stack[-1].append(entry)
def declare_old_function_arguments(self, arguments: list[VariableType]):
"""
After the arguments of a function has been parsed, this method is called to fix them
in the scope stack for semantic analyzer
"""
# Check if everything is correct and we are actually modifying a function
assert self.scope_stack[-2][-1].var_type in [VariableType.INT_FUNCTION, VariableType.VOID_FUNCTION]
# Set the params
print("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx")
self.function_list[self.scope_stack[-2][-1].lexeme].append(arguments)
print(self.function_list)
self.scope_stack[-2][-1].parameters = arguments
def assign_scope_addresses(self):
"""
Assign the addresses to scope variables.
This is done by iterating over all previous variables and increasing an offset
"""
scope = self.scope_stack[-1]
if len(scope) == 0: # welp...
return
if scope[0].address != -1: # we have already assigned the addresses
# We reach here in for/if statements
return
# We should assign addresses
declared_variable_count = 0
for entry in scope:
assert entry.address == -1 # nothing should be assigned
if entry.var_type == VariableType.INT:
entry.address = declared_variable_count * 4 # each variable is 4 bytes
declared_variable_count += 1
elif entry.var_type == VariableType.INT_ARRAY:
# In case of array we should declare a space for array pointer
entry.address = declared_variable_count * 4 # pointer is 4 bytes
declared_variable_count += 1
if entry.parameters != -1: # -1 is when this is the argument
declared_variable_count += entry.parameters # get space just after the pointer
else:
raise Exception("SHASH AZIM")
def get_temp(self) -> int:
"""
Gets the offset of a temporary variables from top of the stack
"""
if len(self.scope_stack[-1]) == 0:
tmp_address = 0
else:
tmp_address = self.scope_stack[-1][-1].address + 4
self.scope_stack[-1].append(SymbolTableEntry("_temp_var", VariableType.INT, None))
self.scope_stack[-1][-1].address = tmp_address
return tmp_address
class ProgramBlock:
def __init__(self):
self.program_block: list[ThreeAddressInstruction] = []
self.pc = 0
self.pc_stack = []
self.return_stack = []
self.has_error = False
def add_return(self):
self.return_stack.append(self.pc)
self.pc += 1
self.program_block.append("Return Empty")
return
def add_instruction(self,ThreeAddressInstruction, i=None, empty=None, add_to_stack: bool = True):
if empty != None :
print("Chopi ", self.pc)
if add_to_stack:
self.pc_stack.append(self.pc)
self.pc += 1
self.program_block.append("empty")
return
# Add instruction to program block
if i == None :
print(self.pc, ThreeAddressInstruction)
self.program_block.append(ThreeAddressInstruction)
self.pc += 1
else :
print("back patch ----> ",i, ThreeAddressInstruction)
self.program_block[i] = ThreeAddressInstruction
def dump(self):
with open("output.txt", "w") as code:
if self.has_error :
code.write("The code has not been generated.")
else :
for i, block in enumerate(self.program_block):
code.write(f"{i}\t{block}\n")
def get_pc(self) -> int :
return self.pc
class StackPointer():
TOP_STACK_ADDRESS = 10000000
STACK_POINTER_ADDRESS = 100
STACK_SIZE = 1000
def __init__(self):
self.stack_size = self.STACK_SIZE
self.pointer = self.TOP_STACK_ADDRESS
self.address = self.STACK_POINTER_ADDRESS
class EAX():
"General purpose register used for return values"
EAX_ADDRESS = 104
def __init__(self):
self.address = self.EAX_ADDRESS
class RAX():
"General purpose register"
RAX_ADDRESS = 108
def __init__(self):
self.address = self.RAX_ADDRESS
class ARG_MEM():
"A place to store arguments"
ADDR = 2000
def __init__(self):
self.address = self.ADDR
def reset(self) :
self.address = self.ADDR
class TempRegisters():
"""
Temp registers required for variable address calculations
"""
TEMP_R1 = 112
TEMP_R2 = 116
TEMP_R3 = 120
TEMP_R4 = 124
class PC():
"General purpose register"
PC_ADDRESS = 128
def __init__(self):
self.address = self.PC_ADDRESS
class CodeGenerator:
FIRST_GLOBAL_VARIABLE_ADDRESS = 100
FIRST_TEMP_VARIABLE_ADDRESS = 500
def __init__(self, scanner: scanner.Scanner):
self.call_stack = []
self.ss: list[int] = [] # Semantic stack
self.scanner = scanner
self.semantic_analyzer = SemanticAnalyzer()
self.param_leftover = []
self.program_block = ProgramBlock()
self.func_params = 0
self.arg_mem = ARG_MEM()
self.args_start = False
self.args = []
# We can have nested function calls so this is an array
self.arg_nums: list[int] = []
# We always define stack pointer the first global variable
self.declared_global_variables = 10
# Name of the variable we are declaring
self.declaring_pid_value: Union[None, str] = None
# List of parameters of function we are declaring
self.declaring_function_params: Union[None, list[VariableType]] = None
# Setup Stack Pointer
self.sp = StackPointer()
# Setup EAX
self.eax = EAX()
# Setup RAX
self.rax = RAX()
# Setup temp registers
self.temp_registers = TempRegisters()
# Each time we want to push an address into ss, we push if it's global or local
# in this stack
self.pid_scope_stack: list[VariableScope] = []
# Each time we want to push an operator in the stack, instead of pushing it into ss
# we push it here to make everything clear
self.operator_stack: list[MathOperator] = []
# Setup PC
self.pc = PC()
# initiallize Stack pointer and EAX
self.initiallize()
def initiallize(self) :
self.program_block.add_instruction(
ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(self.sp.pointer,ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address,ThreeAddressInstructionNumberType.DIRECT_ADDRESS)]))
self.program_block.add_instruction(
ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(0,ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.eax.address,ThreeAddressInstructionNumberType.DIRECT_ADDRESS)]))
self.program_block.add_instruction(
ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(0,ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.rax.address,ThreeAddressInstructionNumberType.DIRECT_ADDRESS)]))
self.program_block.add_instruction(
ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(1000000000000,ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.pc.address,ThreeAddressInstructionNumberType.DIRECT_ADDRESS)]))
self.program_block.add_instruction(
ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(0,ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.pointer - 4,ThreeAddressInstructionNumberType.DIRECT_ADDRESS)]))
self.program_block.add_instruction(
ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(100000000000000,ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.pointer - 8 ,ThreeAddressInstructionNumberType.DIRECT_ADDRESS)]))
self.program_block.add_instruction(
ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(self.sp.pointer - 12,ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.pointer - 12,ThreeAddressInstructionNumberType.DIRECT_ADDRESS)]))
self.current_global_array_assigner = self.program_block.get_pc()
for _ in range(100):
self.program_block.add_instruction(ThreeAddressInstruction(
ThreeAddressInstructionOpcode.ADD,
[
# Same as NOP
ThreeAddressInstructionOperand(100, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(0, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(100,ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
]))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
self.program_block.add_instruction("",empty=True)
# for inst in self.program_block.program_block() :
# print(inst)
def find_absolute_address(self, address: int, scope: VariableScope, temp_register: int):
"""
This function is intended to generate runtime code to move the address of a variable to
a temporary register.
"""
if scope == VariableScope.GLOBAL_VARIABLE:
# In this case, just generate code to move the address to temp register
self.program_block.add_instruction(ThreeAddressInstruction(
# TEMP_REG = #Address
ThreeAddressInstructionOpcode.ASSIGN,
[
ThreeAddressInstructionOperand(address, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(temp_register, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
]))
else: # Either array or local variable. Pointers are also stored in local variable so we are fine
# Add the stack pointer to the register address and boom
self.program_block.add_instruction(ThreeAddressInstruction(
# TEMP_REG = SP + #Address
ThreeAddressInstructionOpcode.ADD,
[
ThreeAddressInstructionOperand(address, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(temp_register, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
]))
if scope == VariableScope.ARRAY:
# Now if this is an array, dereference the variable and place the absolute address in register
self.program_block.add_instruction(ThreeAddressInstruction(
# [TEMP_REG] = [TEMP_REG]
ThreeAddressInstructionOpcode.ASSIGN,
[
ThreeAddressInstructionOperand(temp_register, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
ThreeAddressInstructionOperand(temp_register, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
]))
def check_error(self):
self.program_block.has_error = self.semantic_analyzer.has_error
def int_type(self):
self.ss.append(int(Constants.INT_TYPE))
def void_type(self):
self.ss.append(int(Constants.VOID_TYPE))
def declaring_pid(self):
"""
Declaring pid is called when we are declaring a new variable. In this case,
we should keep the pid as string in order to define the variable later.
"""
assert self.scanner.lookahead_token[0] == scanner.TokenType.ID
assert self.declaring_pid_value == None
self.declaring_pid_value = self.scanner.lookahead_token[1]
if self.declaring_pid_value == "main" :
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.JP,
[ThreeAddressInstructionOperand(self.program_block.get_pc(), ThreeAddressInstructionNumberType.IMMEDIATE)]
),i = self.program_block.pc_stack.pop())
def variable_declared(self):
assert self.declaring_pid_value != None
# Top of the stack is the variable type
if self.ss[-1] == int(Constants.INT_TYPE):
self.semantic_analyzer.declare_variable(self.declaring_pid_value)
if len(self.semantic_analyzer.scope_stack) == 1: # is this a global variable?
# The addressing is absolute. Assign the address to it
self.semantic_analyzer.get_entry(self.declaring_pid_value).address = self.FIRST_GLOBAL_VARIABLE_ADDRESS + self.declared_global_variables * 4
self.declared_global_variables += 1
elif self.ss[-1] == int(Constants.VOID_TYPE):
self.semantic_analyzer.error_list.append(f"#{self.scanner.line_number} : Semantic Error! Illegal type of void for '{self.declaring_pid_value}'.")
else:
raise Exception("RIDEMAN BOZORG")
# Empty stack
print("stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
print(self.ss)
print(self.pid_scope_stack)
self.ss.pop()
print(self.ss)
print(self.pid_scope_stack)
print("stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
self.declaring_pid_value = None
def array_size(self):
assert self.scanner.lookahead_token[0] == scanner.TokenType.NUM
self.ss.append(int(self.scanner.lookahead_token[1]))
def array_declared(self):
assert self.declaring_pid_value != None
# Top of the stack is the size of array
print("stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
print(self.ss)
print(self.pid_scope_stack)
size_of_array = self.ss.pop()
print(self.ss)
print(self.pid_scope_stack)
print("stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
# Top of the stack is the variable type
if self.ss[-1] == int(Constants.INT_TYPE):
self.semantic_analyzer.declare_array(self.declaring_pid_value, size_of_array)
if len(self.semantic_analyzer.scope_stack) == 1: # is this a global variable?
# In case of global variable array we should just increase the number of global variables
# by size of array
self.semantic_analyzer.get_entry(self.declaring_pid_value).address = self.FIRST_GLOBAL_VARIABLE_ADDRESS + self.declared_global_variables * 4
self.declared_global_variables += size_of_array + 1
self.program_block.add_instruction(
ThreeAddressInstruction(
ThreeAddressInstructionOpcode.ASSIGN,
[
ThreeAddressInstructionOperand(self.semantic_analyzer.get_entry(self.declaring_pid_value).address + 4, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.semantic_analyzer.get_entry(self.declaring_pid_value).address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
]), i = self.current_global_array_assigner)
print("SHASH IN ", self.current_global_array_assigner)
self.current_global_array_assigner += 1
elif self.ss[-1] == int(Constants.VOID_TYPE):
self.semantic_analyzer.error_list.append(f"#{self.scanner.line_number} : Semantic Error! Illegal type of void for '{self.declaring_pid_value}'.")
else:
raise Exception("RIDEMAN BOZORG")
# Empty stack
print("stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
print(self.ss)
print(self.pid_scope_stack)
self.ss.pop()
print(self.ss)
print(self.pid_scope_stack)
print("stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
self.declaring_pid_value = None
def function_start(self):
"""
This language is simple and we only declare scopes in functions
"""
assert self.declaring_pid_value != None
assert self.declaring_function_params == None
print("stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
print(self.ss)
print(self.pid_scope_stack)
return_type = self.ss.pop()
print(self.ss)
print(self.pid_scope_stack)
print("stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
assert return_type in [int(Constants.INT_TYPE), int(Constants.VOID_TYPE)]
self.semantic_analyzer.declare_function(self.declaring_pid_value, return_type, self.program_block.get_pc())
self.declaring_pid_value = None
self.declaring_function_params = [] # create a fresh list of parameters
self.semantic_analyzer.enter_scope()
# When a function starts, we need to modify the stack and stack pointer first
# We first save the return address and the return program block
# TODO : When get temp is completed do this
print("Start Func ----------------")
self.func_params = 0
self.param_leftover = []
# Sp points to the pc
self.sp.pointer -= 8
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(111, ThreeAddressInstructionNumberType.IMMEDIATE),
# ]))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.SUB,
[ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(8, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
# Save the last pc for later use
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(self.pc.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS)] ))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
# ]))
self.sp.pointer -= 4
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.SUB,
[ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(4, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(self.rax.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
# Add memory to stack
self.sp.pointer -= 100
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.SUB,
[ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(self.sp.stack_size, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(self.rax.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS)] ))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
# ]))
self.sp.pointer += 4
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ADD,
[ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(4, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
# TODO : Needs to be completed
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(0, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS)] ))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(87654321, ThreeAddressInstructionNumberType.IMMEDIATE),
# ]))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
# ]))
# Skip a block for return value
self.sp.pointer += 8
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ADD,
[ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(4, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(0, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS)] ))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ADD,
[ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(4, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(0, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS)] ))
# self.program_block.add_instruction(ThreeAddressInstruction(
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
# self.program_block.add_instruction(ThreeAddressInstruction(
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(112, ThreeAddressInstructionNumberType.IMMEDIATE),
# ]))
print("Shoomb Start +++++++++++++++++++++++++++")
# Now the stack pointer is pointing at the first local variable
def return_func(self) :
"Return value from a fuction"
print("Start Returnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn")
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(222222222, ThreeAddressInstructionNumberType.IMMEDIATE),
# ]))
print("stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
print(self.ss)
print(self.pid_scope_stack)
self.find_absolute_address(self.ss.pop(), self.pid_scope_stack.pop(), self.temp_registers.TEMP_R1)
print(self.ss)
print(self.pid_scope_stack)
print("stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
# ]))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
ThreeAddressInstructionOperand(self.rax.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.SUB,
[ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(4, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.rax.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
[ThreeAddressInstructionOperand(self.rax.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS)] ))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
# ]))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ADD,
[ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
ThreeAddressInstructionOperand(4, ThreeAddressInstructionNumberType.IMMEDIATE),
ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
# ]))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.sp.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(333333333, ThreeAddressInstructionNumberType.IMMEDIATE),
# ]))
print("Shoomb Returnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnnn")
def push_param(self) :
self.func_params += 1
def save_if(self):
print("save iffffffffffffffffffff")
print("this is stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
print(self.ss)
print(self.pid_scope_stack)
# self.find_absolute_address(self.ss[-1], self.pid_scope_stack[-1], self.temp_registers.TEMP_R1)
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
# ]))
self.find_absolute_address(self.ss.pop(), self.pid_scope_stack.pop(), self.temp_registers.TEMP_R1)
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
# ]))
self.program_block.add_instruction("", empty=True)
def jpf(self):
print("jpfffffffffffffffffffffff")
print("save iffffffffffffffffffff")
print("this is stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
print(self.ss)
print(self.pid_scope_stack)
# self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
# [ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
# ThreeAddressInstructionOperand(self.rax.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.JPF,
[ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
ThreeAddressInstructionOperand(self.program_block.get_pc(), ThreeAddressInstructionNumberType.IMMEDIATE)]
),i = self.program_block.pc_stack.pop())
def jpf_save(self):
print("save would yaffffffffffffffffffffff")
print("save iffffffffffffffffffff")
print("this is stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
print(self.ss)
print(self.pid_scope_stack)
# self.find_absolute_address(self.ss.pop(), self.pid_scope_stack.pop(), self.temp_registers.TEMP_R1)
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.DIRECT_ADDRESS),
# ]))
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
# ]))
# self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
# [ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
#
# ThreeAddressInstructionOperand(self.rax.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.JPF,
[ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
ThreeAddressInstructionOperand(self.program_block.get_pc() + 1, ThreeAddressInstructionNumberType.IMMEDIATE)]
),i = self.program_block.pc_stack.pop())
# self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
# [ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
# ThreeAddressInstructionOperand(self.rax.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
# self.find_absolute_address(self.ss.pop(), self.pid_scope_stack.pop(), self.temp_registers.TEMP_R1)
self.program_block.add_instruction("", empty= True)
def jp(self):
print("save iffffffffffffffffffff")
print("this is stackkkkkkkkkkkkkkkkkkkkkkkkkkk")
print(self.ss)
print(self.pid_scope_stack)
print("jppppppppppppppppppppppppppppppp")
# self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.ASSIGN,
# [ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
# ThreeAddressInstructionOperand(self.rax.address, ThreeAddressInstructionNumberType.DIRECT_ADDRESS)] ))
self.program_block.add_instruction(ThreeAddressInstruction(ThreeAddressInstructionOpcode.JP,
[ThreeAddressInstructionOperand(self.program_block.get_pc(), ThreeAddressInstructionNumberType.IMMEDIATE)]
),i = self.program_block.pc_stack.pop())
def jump_to_end(self):
print("ppppppppppppooooooooooopppppppppppppppppp")
self.program_block.add_return()
def call(self):
print("Call aaaaaaaaaaaaaaaaaaaaaaaaaaaa")
print(self.ss)
print(self.pid_scope_stack)
# Check if this is the output function
if self.ss[-self.arg_nums[-1] - 1] == int(Constants.OUTPUT_FUNCTION):
print("self op")
assert self.arg_nums[-1] == 1
self.arg_nums.pop()
# Load the address in registers
self.find_absolute_address(self.ss[-1], self.pid_scope_stack[-1], self.temp_registers.TEMP_R1)
self.program_block.add_instruction(ThreeAddressInstruction(
# PRINT(R2)
ThreeAddressInstructionOpcode.PRINT,
[
ThreeAddressInstructionOperand(self.temp_registers.TEMP_R1, ThreeAddressInstructionNumberType.INDIRECT_ADDRESS),
]))
# NOTE: We do not push anything to stack here. This is because that the
# Expression-stmt will pop the last variable in stack. To fix this, we push a dummy
# value in the stack always. But this time, because we have the argument in stack,
# we do not push anything and thus the Expression-stmt will remove it.
# But we need to pop the Constants.OUTPUT_FUNCTION from the stack. So we pop one from ss
self.ss.pop()
self.call_stack.pop()
return
# self.program_block.add_instruction(ThreeAddressInstruction(
# # PRINT(R2)
# ThreeAddressInstructionOpcode.PRINT,
# [
# ThreeAddressInstructionOperand(55555, ThreeAddressInstructionNumberType.IMMEDIATE),
# ]))
self.arg_mem.reset()
fixed_args = []
print("ARG CHECKING:", self.arg_nums[-1], self.semantic_analyzer.function_list[self.call_stack[-1]][1])
expected_argument = len(self.semantic_analyzer.function_list[self.call_stack[-1]][1])
if self.arg_nums[-1] != expected_argument:
self.semantic_analyzer.error_list.append(f"#{self.scanner.line_number} : Semantic Error! Mismatch in numbers of arguments of '{self.call_stack[-1]}'.")
# Fix the ss and actually create the call
if self.arg_nums[-1] > expected_argument: # remove from stack
for _ in range(self.arg_nums[-1] - expected_argument):
self.ss.pop()
self.pid_scope_stack.pop()