File tree Expand file tree Collapse file tree 3 files changed +88
-13
lines changed Expand file tree Collapse file tree 3 files changed +88
-13
lines changed Original file line number Diff line number Diff line change @@ -15,9 +15,27 @@ class Compiler:
15
15
# Remove Complex Operands
16
16
############################################################################
17
17
18
- def rco_exp (self , e : expr , need_atomic : bool ) -> Tuple [expr , Temporaries ]:
19
- # YOUR CODE HERE
20
- raise Exception ('rco_exp not implemented' )
18
+ def rco_exp (self , e : expr ) -> Tuple [expr , Temporaries ]:
19
+ match e :
20
+ case Constant (int ):
21
+
22
+ case Call (Name ('input_int' ),[]):
23
+
24
+ case UnaryOp (USub (), exp ):
25
+ (new_exp , temps ) = self .rco_exp (exp )
26
+
27
+ case BinOp (exp1 , Add (), exp2 ):
28
+ (new_exp1 , temps1 ) = self .rco_exp (exp1 )
29
+ (new_exp2 , temps2 ) = self .rco_exp (exp2 )
30
+
31
+ case BinOp (exp1 , Sub (), exp2 ):
32
+ (new_exp1 , temps1 ) = self .rco_exp (exp1 )
33
+ (new_exp2 , temps2 ) = self .rco_exp (exp2 )
34
+
35
+ case Name (var ):
36
+
37
+ case _:
38
+ raise Exception ('rco_exp unexpected ' + repr (e ))
21
39
22
40
def rco_stmt (self , s : stmt ) -> List [stmt ]:
23
41
# YOUR CODE HERE
Original file line number Diff line number Diff line change
1
+
2
+ L_var
3
+
4
+ exp ::= Constant(int)
5
+ | Call(Name('input_int'),[])
6
+ | UnaryOp(USub(), exp)
7
+ | BinOp(exp, Add(), exp)
8
+ | BinOp(exp, Sub(), exp)
9
+ | Name(var)
10
+ stmt ::= Expr(Call(Name('print'), [exp]))
11
+ | Expr(exp)
12
+ | Assign([Name(var)], exp)
13
+ LVar ::= Module(stmt∗)
14
+
15
+ L_var^mon
16
+
17
+ atm ::= Constant(int)
18
+ | Name(var)
19
+ exp ::= atm
20
+ | Call(Name('input_int'),[])
21
+ | UnaryOp(USub(), atm)
22
+ | BinOp(atm, Add(), atm)
23
+ | BinOp(atm, Sub(), atm)
24
+ stmt ::= Expr(Call(Name('print'), [atm]))
25
+ | Expr(exp)
26
+ | Assign([Name(var)], exp)
27
+ LVar ::= Module(stmt∗)
28
+
29
+ X86_Var
30
+
31
+ reg ::= 'rsp' | 'rbp' | 'rax' | 'rbx' | 'rcx' | 'rdx' | 'rsi' | 'rdi'
32
+ | 'r8' | 'r9' | 'r10' | 'r11' | 'r12' | 'r13' | 'r14' | 'r15'
33
+ arg ::= Immediate(int)
34
+ | Reg(reg)
35
+ | Deref(reg,int)
36
+ | Variable(var)
37
+ instr ::= Instr('addq',[arg,arg])
38
+ | Instr('subq',[arg,arg])
39
+ | Instr('movq',[arg,arg])
40
+ | Instr('negq',[arg])
41
+ | Instr('pushq',[arg])
42
+ | Instr('popq',[arg])
43
+ | Callq(label,int)
44
+ | Instr('retq',[])
45
+ | Jump(label)
46
+ x86_Var ::= X86Program(instr∗)
47
+
48
+
49
+ X86_Int
50
+
51
+ Remove Variable(var) from arg.
52
+
53
+ arg ::= Immediate(int)
54
+ | Reg(reg)
55
+ | Deref(reg,int)
56
+
Original file line number Diff line number Diff line change @@ -96,19 +96,20 @@ def execute_and_check(program_filename, program_ast):
96
96
print (repr (program_ast ))
97
97
print ()
98
98
99
+ # Remove Complex Operands
100
+ program_ast = C .remove_complex_operands (program_ast )
101
+ print ('After RCO' )
102
+ print ('Concrete Syntax:' )
103
+ print (program_ast )
104
+ print ('Abstract Syntax:' )
105
+ print (repr (program_ast ))
106
+ interp_and_check (program_filename , program_ast ,
107
+ interp_Lvar , 'remove complex operands' )
108
+ print ()
109
+
99
110
if False :
100
111
# Move up each pass once it is completed
101
112
102
- # Remove Complex Operands
103
- program_ast = C .remove_complex_operands (program_ast )
104
- print ('After RCO' )
105
- print ('Concrete Syntax:' )
106
- print (program_ast )
107
- print ('Abstract Syntax:' )
108
- print (repr (program_ast ))
109
- interp_and_check (program_filename , program_ast ,
110
- interp_Lvar , 'remove complex operands' )
111
- print ()
112
113
113
114
# Select Instructions
114
115
program_ast = C .select_instructions (program_ast )
You can’t perform that action at this time.
0 commit comments