-
Notifications
You must be signed in to change notification settings - Fork 0
/
code generation.txt
148 lines (104 loc) · 3.55 KB
/
code generation.txt
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
code generation:
1. add codegenerator.class providing as many methods as low level instructions (in a filewriter object). the main method must receive both the input and the output files.
2. code functions:
execute: generates the code to execute statements and definitions
value: generates the code to push value of an epression
address: generates the code to push the mem address of an lvalue expression
use a new abstract visitor (theory).
3. one method for each low level instruction.
a[i] => we need the address of a and the value of i.
TODO:
specify all the code templates as a comment in the visitor in each visitor
CODE TEMPLATES:
execute[[Program: program -> definition*]]() =
for (Definition definition : definition*) --> 1st the variable definitions
if (definition instanceof VARDEF)
execute[[definition]]()
<call main>
<halt>
for (Definition definition: definition*) --> 2nd the function definitions
if (definition instanceof FUNDEF)
execute[[definition]]()
execute[[Print: statement -> expression]]() =
value[[expression]]()
<out> expression.tipo.suffix()
execute[[Input: statement -> expression]]() =
address[[expresion]]()
<in> expression.type.suffix()
<store> expression.type.suffix()
address[[Variable: expression -> ID]]() =
if (expression.definition.scope == 0)
<pusha> expression.definition.offset
else
<push bp>
<pushi> expression.definition.offset
<addi>
value[[Variable :expression -> ID]]() =
address[[expression]]()
<load> expression.type.suffix()
specify code generation templates except: if, while, return, invocation, fieldaccess and indexing
implement all code generation visitors
new suffix() char method in type
modify main to include output.txt
test your implementation: input.txt and test result with mapl
# line 5 --> write this at the beginning of each statement and at the beginning of each function definition. for each statement of the funcdefinition write the line
# the comments ' * -> write them in the execute variables
Control structures
<label><:>
JMP label
jz label
jnz label
execute[[WhileSt: statement -> expression statement*]] =
int condition = cg.getlabel()
int end = cg.getlabel()
<label> condition <:>
value[[expresion]]
<jz label> end
statement.foreach(st -> execute[[st]])
<jmp label> condition
<label> end <:>
execute[[IfSt: statement -> expression statementIf* statementElse*]] =
int else = cg.getLabel()
int end = cg.getLabel()
value[[expression]]
<jz label> else
statementif*.foreach(st -> execute[[st]])
<jmp label> end
<label> else <:>
statementElse*.foreach(st -> execute[[st]])
<label> end <:>
address[[Indexing: exp1 -> exp2 exp3]] =
address[[exp2]]
value[[exp3]]
<pushi> exp1.type.numberofbytes()
<muli>
<addi>
value[[Indexing: exp1 -> exp2 exp3]]=
address[[expression1]]
<load> exp1.type.suffix()
address[[FieldAccess: exp1 -> exp2 ID]] =
address[[exp2]]
<push> exp2.type.getfield(ID).offset
<addi>
value[[FieldAcess: exp1 -> exp2 ID]] =
address[[exp1]]
<load> exp1.type.suffix()
Lexical
Syntactical
Semantic
Typechekcking
Offset
CG
value[[invocation: exp1 -> exp2 expr*]] =
for (expression arg: expression*)
value[[arg]]
<call> expression2.name
execute[[invocation: statement -> exp2 expr*]] =
value[[(expression) statement]]
if (!((expression)statement).type instanceof voidtype))
<pop> ((expression)statement).type.suffix()
execute[[return: statement -> expression]](funcDefinition)=
value[[€xpression]]()
<ret> funcDefinition.type.returntype.numberofbytes <,>
funcDefinition.byteslocalssum <,>
funcdefinition.type.bytesparamssum