-
Notifications
You must be signed in to change notification settings - Fork 0
/
code_maker.py
704 lines (539 loc) · 21.3 KB
/
code_maker.py
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
# TODO change the if array
from typing import TextIO, List
import scanner
from config import STACK_OFFSET, CALL_FRAME_OFFSET
from scanner import IdentifierType as Type
PB: List[str] = [] # program block | each block of program block contains a 3 address command
SEM_ERROR_FILE: TextIO # file which the semantic errors is written in to it
semantic_correctness = True # flag to check if program is correct semantically
global_scope_finish = False
in_call_assign = False
semantic_stack = [] # semantic stack | use for saving temps and creating PB commands
break_stack = [] # separate break for relop to resolve PB fraction
last_temp = STACK_OFFSET # start address of heap and stack
last_sem_error_line = 0 # Sina can explain this :)
frame_stack = []
def report_semantic_error(lineno, prompt):
global semantic_correctness, last_sem_error_line
if lineno <= last_sem_error_line:
pass # I AM REALLY NOT HAPPY TO WRITE THIS :(
last_sem_error_line = lineno
semantic_correctness = False
SEM_ERROR_FILE.write(f'#{lineno}: Semantic Error! {prompt}.\n')
def next_temp():
"""
Returns: get next temp address
"""
global last_temp
last_temp += 4
return last_temp - 4
def check_var_call_relativity(src, dst):
if str(dst) != '@12' and '#' not in str(src):
if str(src) not in str(dst) and str(dst) not in str(src):
if '@' in str(src):
if int(src[1:]) >= 500:
remove_call_not_related_var(int(src))
elif int(src) >= 500:
remove_call_not_related_var(int(src))
def generate_code(operation: str, *components):
"""
Generates code for a 3-address operation and its associated components.
This function takes an operation and a variable number of components as inputs, and generates code for a
3-address operation using these inputs. It first checks the semantic correctness of the code, and if it is not
correct, it returns without generating code. It then creates a new code string, appends the operation and
components to it, and appends the code string to the PB (program block) list.
Args:
operation (str): The 3-address operation to perform.
*components: The addresses of the components to be used in the operation.
Returns:
None
"""
if semantic_correctness is False:
return
new_code = str(len(PB))
new_code += '\t(' + operation
for j in range(3):
if j < len(components):
new_code += ', ' + str(components[j])
elif j < 2:
new_code += ', '
else:
new_code += ', '
new_code += ')'
PB.append(new_code)
if operation == 'ASSIGN' and not in_call_assign:
check_var_call_relativity(components[0], components[1])
def pnum():
"""
Assigns a numerical constant to a temporary variable and generates code for it.
This function gets the next available temporary variable and assigns the value of the last numerical constant to it.
It then appends the temporary variable to the semantic stack.
Returns:
None
"""
t = next_temp()
last_type, last_lexeme = scanner.get_last_token()
if last_type != 'NUM':
raise Exception(f'code maker : improper type : {last_type} instead of NUM')
generate_code('ASSIGN', '#' + last_lexeme, t)
semantic_stack.append(t)
save_call_related_var(t)
def temp_exch():
"""
Exchange an array or function symbol on the semantic stack with a temporary variable.
This function pops the address and line number of a symbol from the semantic stack, and checks if it is an array
or function. If the symbol is an array, it pushes its address, line number, and the string 'array' onto the
semantic stack. If the symbol is a function, it reports a semantic error indicating a type mismatch. Otherwise,
it generates code to assign the symbol's address to a new temporary variable, and pushes the temporary variable
onto the semantic stack.
Returns:
None
"""
a_index = semantic_stack.pop()
a_line = semantic_stack.pop()
a_entry = scanner.symbol_list[a_index]
if a_entry.is_function:
report_semantic_error(a_line, "Type mismatch in operands, Got function instead of int")
if a_entry.id_type == Type.array:
semantic_stack.append(a_index)
semantic_stack.append(a_line)
semantic_stack.append('array')
return
a = a_entry.address
t = next_temp()
generate_code('ASSIGN', a, t)
semantic_stack.append(t)
save_call_related_var(t)
def pid():
"""
Processes an identifier and adds its symbol to the semantic stack.
This function gets the last processed token from the scanner, which should be an identifier. It then looks up the
identifier in the symbol table, and if it is found, it appends its line number and index to the semantic stack.
If it is not found, it reports a semantic error and appends a default symbol index of 0 to the semantic stack.
Returns:
None
"""
last_type, last_lexeme = scanner.get_last_token()
if last_type != 'ID':
raise Exception("code maker : last token type expected to be ID, but was " + last_type)
sym_index = scanner.symbol_table_lookup(last_lexeme)
if sym_index == -1:
report_semantic_error(scanner.line_number, f"'{last_lexeme}' is not defined")
sym_index = 0
semantic_stack.append(scanner.line_number)
semantic_stack.append(sym_index)
def pplus():
"""
Adds an 'ADD' operation to the semantic stack.
This function is called when the parser encounters a + operation in the input.txt.
It appends the 'ADD' operation to the semantic stack.
Returns:
None
"""
semantic_stack.append('ADD')
def pminus():
"""
Adds a 'SUB' operation to the semantic stack.
This function is called when the parser encounters a - operation in the input.txt.
It appends the 'SUB' operation to the semantic stack.
Returns:
None
"""
semantic_stack.append('SUB')
def pless():
"""
Adds an 'LT' operation to the semantic stack.
This function is called when the parser encounters a < operation in the input.txt.
It appends the 'LT' operation to the semantic stack.
Returns:
None
"""
semantic_stack.append('LT')
def peq():
"""
Adds an 'EQ' operation to the semantic stack.
This function is called when the parser encounters a = operation in the input.txt.
It appends the 'EQ' operation to the semantic stack.
Returns:
None
"""
semantic_stack.append('EQ')
def ptimes():
"""
Adds a 'MULT' operation to the semantic stack.
This function is called when the parser encounters a * operation in the input.txt.
It appends the 'MULT' operation to the semantic stack.
Returns:
None
"""
semantic_stack.append('MULT')
def assign():
"""
Handles assignment statements.
This function pops the value to be assigned from the semantic stack, as well as the symbol to which the value
will be assigned. It checks that the symbol is an integer variable and not a function, and that the value to be
assigned is also an integer. If either of these conditions is not met, it reports a semantic error. If the symbol
is an array, it pops the line number and a useless symbol table entry from the semantic stack. It then generates
code to assign the value to the symbol's address, and pushes the value back onto the semantic stack.
Returns:
None
"""
t_line = 0
t = semantic_stack.pop()
if t == 'array':
t_line = semantic_stack.pop()
semantic_stack.pop() # pop the useless symbol table entry
if t == 'int':
t = semantic_stack.pop()
# check type
a_index = semantic_stack.pop()
a_line = semantic_stack.pop()
a_entry = scanner.symbol_list[a_index]
if a_entry.id_type != Type.int:
report_semantic_error(a_line,
f"Type mismatch in operands, Got {a_entry.id_type.name} instead of int")
if a_entry.is_function:
report_semantic_error(a_line,
"Type mismatch in operands, Got function instead of int")
if t == 'array':
report_semantic_error(t_line, "Type mismatch in operands, Got array instead of int")
t = 0
a = a_entry.address
generate_code('ASSIGN', t, a)
semantic_stack.append(t)
def assign_arr():
"""
Handles assignment statements for arrays.
This function pops the index and value to be assigned from the semantic stack, as well as the symbol
representing the array. It checks that the array symbol is actually an array and not a function, and that the
index is an integer and the value is an integer or an integer array. If any of these conditions is not met,
it reports a semantic error. It then generates code to compute the address of the element to be assigned,
and to assign the value to that address. Finally, it pushes the value back onto the semantic stack.
Returns:
None
"""
val_line, ind_line = 0, 0
val = semantic_stack.pop()
if val == 'array':
val_line = semantic_stack.pop()
semantic_stack.pop()
ind = semantic_stack.pop()
if ind == 'array':
ind_line = semantic_stack.pop()
semantic_stack.pop()
a_index = semantic_stack.pop()
a_line = semantic_stack.pop()
a_entry = scanner.symbol_list[a_index]
if a_entry.id_type != Type.array:
report_semantic_error(a_line,
f"Type mismatch in operands, Got {a_entry.id_type.name} instead of array")
if a_entry.is_function:
report_semantic_error(a_line,
"Type mismatch in operands, Got function instead of array")
if ind == 'array':
report_semantic_error(ind_line, "Type mismatch in operands, Got array instead of int")
if val == 'array':
report_semantic_error(val_line, "Type mismatch in operands, Got array instead of int")
val = 0
a = a_entry.address
generate_code('MULT', ind, '#4', ind)
generate_code('ADD', str(a), ind, ind)
generate_code('ASSIGN', val, '@' + str(ind))
semantic_stack.append(val)
def end_expression():
"""
Handles the end of an expression.
This function pops the top element from the semantic stack, which should be the value of the expression.
If the value is an array, it also pops a useless symbol table entry from the semantic stack.
Returns:
None
"""
exp = semantic_stack.pop()
if exp == 'array':
semantic_stack.pop()
semantic_stack.pop()
def do_op():
"""
Handles operations.
This function pops the top two elements from the semantic stack, which should be the second operand and the first
operand. If either operand is an array, it also pops a useless symbol table entry from the semantic stack. It
checks that the operands are integers and not arrays, and reports a semantic error if either of these conditions
is not met. It generates code to perform the operation on the operands, and pushes the result back onto the
semantic stack.
Returns:
None
"""
t1_line, t2_line = 0, 0
t2 = semantic_stack.pop()
if t2 == 'array':
t2_line = semantic_stack.pop()
semantic_stack.pop()
op = semantic_stack.pop()
t1 = semantic_stack.pop()
if t1 == 'array':
t1_line = semantic_stack.pop()
semantic_stack.pop()
report_semantic_error(t1_line, f'Type mismatch in operands, Got array instead of int')
t1 = 0
if t2 == 'array':
report_semantic_error(t2_line, f'Type mismatch in operands, Got array instead of int')
generate_code(op, t1, t2, t2)
semantic_stack.append(t2)
def eval_ind():
"""
Evaluates an array index.
This function pops the top element from the semantic stack, which should be the index of the array element to be
accessed. It then looks at the second-to-top element of the stack, which should be the symbol table index for the
array symbol. It generates code to compute the address of the element to be accessed, and to assign that address
to the second-to-top element of the stack.
Returns:
None
"""
ind = semantic_stack.pop()
t = semantic_stack[-1]
generate_code('MULT', ind, '#4', ind)
generate_code('ADD', t, ind, t)
generate_code('ASSIGN', '@' + str(t), t)
def eval_ind_orig():
t_line = 0
t = semantic_stack.pop()
if t == 'array':
t_line = semantic_stack.pop()
semantic_stack.pop()
a_index = semantic_stack.pop()
a_line = semantic_stack.pop()
a_entry = scanner.symbol_list[a_index]
if a_entry.id_type != Type.array:
report_semantic_error(a_line,
f"Type mismatch in operands, Got {a_entry.id_type.name} instead of array")
if a_entry.is_function:
report_semantic_error(a_line,
"Type mismatch in operands, Got function instead of array")
if t == 'array':
report_semantic_error(t_line, "Type mismatch in operands, Got array instead of int")
t = 0
a = a_entry.address
generate_code('MULT', t, '#4', t)
generate_code('ADD', str(a), t, t)
generate_code('ASSIGN', '@' + str(t), t)
semantic_stack.append(t)
def expect_temp():
t = semantic_stack.pop()
if t == 'array':
t_line = semantic_stack.pop()
semantic_stack.pop()
report_semantic_error(t_line, f'Type mismatch in operands, Got array instead of int')
t = 0
semantic_stack.append(t)
def make_patch():
semantic_stack.append(len(PB))
PB.append('')
def end_if():
a2 = semantic_stack.pop()
a1 = semantic_stack.pop()
t = semantic_stack.pop()
PB[a2] = f'{a2}\t(JP, {len(PB)}, , )'
PB[a1] = f'{a1}\t(JPF, {t}, {a2 + 1}, )'
def start_repeat():
semantic_stack.append(len(PB))
break_stack.append(0)
def brk():
if not break_stack:
report_semantic_error(scanner.line_number, "No 'repeat ... until' found for 'break'")
return
n = break_stack.pop()
break_stack.append(len(PB))
break_stack.append(n + 1)
PB.append('')
def end_repeat():
expect_temp()
t = semantic_stack.pop()
a = semantic_stack.pop()
generate_code('JPF', t, a)
break_cnt = break_stack.pop()
for _ in range(break_cnt):
break_addr = break_stack.pop()
PB[break_addr] = f'{break_addr}\t(JP, {len(PB)}, , )'
def start_args():
semantic_stack.append(0)
def new_arg():
last_arg_line = -1
last_arg_index = -1
last_arg = semantic_stack.pop()
if last_arg == 'array':
last_arg_line = semantic_stack.pop()
last_arg_index = semantic_stack.pop()
n = semantic_stack.pop()
if last_arg == 'array':
semantic_stack.append(last_arg_index)
semantic_stack.append(last_arg_line)
semantic_stack.append(last_arg)
semantic_stack.append(n + 1)
def call_frame_push(reg: int):
sp = 12 # TODO : clean code
generate_code('ASSIGN', str(reg), f'@{sp}')
generate_code('SUB', f'{sp}', '#4', f'{sp}')
def call_frame_pop(reg: int):
sp = 12 # TODO: clean code
generate_code('ADD', f'{sp}', '#4', f'{sp}')
generate_code('ASSIGN', f'@{sp}', str(reg))
def create_frame():
# get caller data
st_index = scanner.get_current_scope()
caller = scanner.symbol_list[st_index]
caller_params_number = len(caller.parameter_list)
reg = 0
# push return address into frame
ra = 8 # TODO : clean
call_frame_push(ra)
# push params
for i in range(caller_params_number):
reg = caller.address + ((i + 1) * 4)
call_frame_push(reg)
# push return temp reg
for reg in caller.return_temp_reg:
call_frame_push(reg)
def destroy_frame():
# get caller data
st_index = scanner.get_current_scope()
caller = scanner.symbol_list[st_index]
caller_params_number = len(caller.parameter_list)
reg = 0
# pop return temp reg
for reg in caller.return_temp_reg[::-1]:
call_frame_pop(reg)
# pop params
for i in range(caller_params_number, 0, -1):
reg = caller.address + (i * 4)
call_frame_pop(reg)
# pop return address
ra = 8
call_frame_pop(ra)
def call_set_params(parameters, callee_func_address):
for i in range(len(parameters)):
if parameters[i][0] == Type.array:
generate_code('ASSIGN', str(parameters[i][1]), callee_func_address + ((i + 1) << 2))
else:
generate_code('ASSIGN', str(parameters[i][1]), callee_func_address + ((i + 1) << 2))
def default_assign_callee_return_temp(callee):
for reg in callee.return_temp_reg:
generate_code('ASSIGN', '#0', reg)
def call():
n = semantic_stack.pop()
parameters = []
for _ in range(n):
arg = semantic_stack.pop()
if arg == 'array':
line = semantic_stack.pop()
arg = semantic_stack.pop()
parameters.append((Type.array, scanner.symbol_list[arg].address))
else:
parameters.append((Type.int, arg))
parameters.reverse()
f_index = semantic_stack.pop()
semantic_stack.pop() # f_line
f = scanner.symbol_list[f_index]
if f.is_function is False:
raise Exception(f'code maker : expected {f.lexeme} to be function but it was not')
if len(f.parameter_list) != n:
report_semantic_error(scanner.line_number, f"Mismatch in numbers of arguments of '{f.lexeme}'")
else:
for i in range(len(parameters)):
if parameters[i][0] != f.parameter_list[i]:
report_semantic_error(scanner.line_number,
f"Mismatch in type of argument {i + 1} of '{f.lexeme}'. " +
f"Expected '{f.parameter_list[i].name}' but got '{parameters[i][0].name}'"
f" instead")
if f.lexeme == 'output':
generate_code('PRINT', parameters[0][1])
else:
global in_call_assign
in_call_assign = True
create_frame()
call_set_params(parameters, f.address)
default_assign_callee_return_temp(f)
ra = 8 # TODO clean code
generate_code('ASSIGN', f'#{len(PB) + 2}', ra)
generate_code('JP', scanner.get_func_entry_point(f.lexeme))
destroy_frame()
in_call_assign = False
if f.id_type == Type.array:
semantic_stack.append(scanner.line_number)
if f.id_type != Type.void:
v = 16 # TODO clean this
tmp = next_temp()
generate_code('ASSIGN', v, tmp)
semantic_stack.append(tmp)
save_call_related_var(tmp)
else:
semantic_stack.append(f.id_type.name)
def remove_call_not_related_var(tmp):
f_index = scanner.get_current_scope() # get caller
if f_index != -1:
f = scanner.symbol_list[f_index]
ls = f.return_temp_reg
if tmp in ls:
ls.remove(tmp)
def save_call_related_var(tmp):
f_index = scanner.get_current_scope() # get caller
if f_index != -1:
f = scanner.symbol_list[f_index]
ls = f.return_temp_reg
if tmp not in ls:
ls.append(tmp)
def check_void():
if scanner.symbol_list[-1].id_type == Type.void and not scanner.symbol_list[-1].is_function:
report_semantic_error(scanner.last_type_line_number,
f"Illegal type of void for '{scanner.symbol_list[-1].lexeme}'")
def start_declaration():
scanner.start_declaration()
def start_params():
# if it is main then append to PB
# otherwise make a patch and turn flag to true
global global_scope_finish
if scanner.symbol_list[-1].lexeme == 'main':
patched_line = semantic_stack.pop()
PB[patched_line] = f'{patched_line}\t(JP, {len(PB)}, , )'
else:
if not global_scope_finish:
global_scope_finish = True
make_patch()
scanner.start_params(len(PB))
def end_declaration():
scanner.set_declaration_mode(scanner.DeclarationMode.Disabled)
check_void()
def end_var_declaration():
scanner.set_declaration_mode(scanner.DeclarationMode.Disabled)
check_void()
var = scanner.symbol_list[-1]
generate_code('ASSIGN', '#0', var.address)
save_call_related_var(var.address)
def end_arr_declaration():
scanner.set_declaration_mode(scanner.DeclarationMode.Disabled)
check_void()
arr = scanner.symbol_list[-1]
generate_code('ASSIGN', '#' + str(arr.address + 4), arr.address)
save_call_related_var(arr.address)
def return_call():
f_index = scanner.get_current_scope()
f = scanner.symbol_list[f_index]
if f.lexeme != 'main':
if f.id_type != Type.void:
v = 16 # TODO clean this
generate_code('ASSIGN', last_temp - 4, v)
generate_code('JP', '@8')
def end_scope():
f_index = scanner.get_current_scope()
f = scanner.symbol_list[f_index]
if f.id_type != Type.void:
v = 16 # TODO clean this
generate_code('ASSIGN', last_temp - 4, v)
if f.lexeme != 'main':
generate_code('JP', '@8')
scanner.end_scope()
def init():
generate_code('ASSIGN', '#0', 8)
generate_code('ASSIGN', f'#{CALL_FRAME_OFFSET}', 12)
def do_action(action_symbol: str):
func_name = action_symbol[1:].replace('-', '_')
globals()[func_name]()