-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymbol.ml
More file actions
318 lines (290 loc) · 9.13 KB
/
Symbol.ml
File metadata and controls
318 lines (290 loc) · 9.13 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
open Identifier
open Error
open Types
open Parsing
module H = Hashtbl.Make (
struct
type t = id
let equal = (==)
let hash = Hashtbl.hash
end
)
type pass_mode = PASS_BY_VALUE | PASS_BY_REFERENCE
type param_status =
| PARDEF_COMPLETE
| PARDEF_DEFINE
| PARDEF_CHECK
type scope = {
sco_parent : scope option;
sco_nesting : int;
mutable sco_entries : entry list;
mutable sco_negofs : int;
}
and variable_info = {
variable_type : Types.typ;
variable_offset : int
}
and function_info = {
mutable function_isForward : bool;
mutable function_paramlist : entry list;
mutable function_redeflist : entry list;
mutable function_result : Types.typ;
mutable function_pstatus : param_status;
mutable function_initquad : int;
mutable function_name : string
}
and parameter_info = {
parameter_type : Types.typ;
mutable parameter_offset : int;
parameter_mode : pass_mode
}
and temporary_info = {
temporary_type : Types.typ;
temporary_offset : int
}
and entry_info = ENTRY_none
| ENTRY_variable of variable_info
| ENTRY_function of function_info
| ENTRY_parameter of parameter_info
| ENTRY_temporary of temporary_info
and entry = {
entry_id : Identifier.id;
entry_scope : scope;
entry_info : entry_info
}
type lookup_type = LOOKUP_CURRENT_SCOPE | LOOKUP_ALL_SCOPES
let start_positive_offset = 8
let start_negative_offset = 0
let the_outer_scope = {
sco_parent = None;
sco_nesting = 0;
sco_entries = [];
sco_negofs = start_negative_offset;
}
let no_entry id = {
entry_id = id;
entry_scope = the_outer_scope;
entry_info = ENTRY_none
}
let currentScope = ref the_outer_scope
let quadNext = ref 1
let tempNumber = ref 1
let tab = ref (H.create 0)
let initSymbolTable size =
tab := H.create size;
currentScope := the_outer_scope
let openScope () =
let sco = {
sco_parent = Some !currentScope;
sco_nesting = !currentScope.sco_nesting + 1;
sco_entries = [];
sco_negofs = start_negative_offset;
} in
currentScope := sco
let closeScope () =
let sco = !currentScope in
let manyentry e = H.remove !tab e.entry_id in
List.iter manyentry sco.sco_entries;
match sco.sco_parent with
| Some scp ->
currentScope := scp
| None ->
internal "cannot close the outer scope!"
exception Failure_NewEntry of entry
let newEntry id inf err =
try
if err then begin
try
let e = H.find !tab id in
if e.entry_scope.sco_nesting = !currentScope.sco_nesting then
raise (Failure_NewEntry e)
with Not_found ->
()
end;
let e = {
entry_id = id;
entry_scope = !currentScope;
entry_info = inf
} in
H.add !tab id e;
!currentScope.sco_entries <- e :: !currentScope.sco_entries;
e
with Failure_NewEntry e ->
error "%aIdentifier %a already defined"
print_position (position_point (symbol_start_pos())) pretty_id id;
e
let lookupEntry id how err =
let scc = !currentScope in
let lookup () =
match how with
| LOOKUP_CURRENT_SCOPE ->
let e = H.find !tab id in
if e.entry_scope.sco_nesting = scc.sco_nesting then
e
else
raise Not_found
| LOOKUP_ALL_SCOPES ->
H.find !tab id in
if err then
try
lookup ()
with Not_found ->
fatal "%aUnknown identifier %a"
print_position (position_point (symbol_start_pos())) pretty_id id;
(* this can cause many errors if we continue semantic analysis
* better stop (fatal error) *)
raise Terminate
else
lookup ()
let newVariable id typ err =
let parent_scope = match !currentScope.sco_parent with
| Some x -> x
| None -> internal "attempted to declare variable at outer scope";
raise Terminate
in
let inf = {
variable_type = typ;
variable_offset = parent_scope.sco_negofs
} in
parent_scope.sco_negofs <- parent_scope.sco_negofs + 1;
(* using negofs to calculate next free position in corresponding frame *)
newEntry id (ENTRY_variable inf) err
let newFunction id err =
try
let e = lookupEntry id LOOKUP_CURRENT_SCOPE false in
match e.entry_info with
| ENTRY_function inf when inf.function_isForward ->
inf.function_isForward <- false;
inf.function_pstatus <- PARDEF_CHECK;
inf.function_redeflist <- inf.function_paramlist;
e
| _ ->
if err then
fatal "%aIdentifier %a already defined in same scope"
print_position (position_point (symbol_start_pos())) pretty_id id;
raise Terminate
with Not_found ->
let par_scope_name =
let par_scope_opt = (!currentScope).sco_parent in
match par_scope_opt with
| None -> ""
| Some par_scope ->
begin
let rec g l = match l with
| h::tl ->
begin
match h.entry_info with
| ENTRY_function f -> f.function_name
| _ -> g tl
end
| [] -> ""
in g par_scope.sco_entries
end
in
let name_prefix = match par_scope_name with
| "" -> ""
| n -> n ^ "-" in
let inf = {
function_isForward = false;
function_paramlist = [];
function_redeflist = [];
function_result = TYPE_none;
function_pstatus = PARDEF_DEFINE;
function_initquad = 0;
function_name = name_prefix ^ (id_name id)
} in
newEntry id (ENTRY_function inf) false
let newParameter id typ mode f err =
match f.entry_info with
| ENTRY_function inf -> begin
match inf.function_pstatus with
| PARDEF_DEFINE ->
let inf_p = {
parameter_type = typ;
parameter_offset = 0;
parameter_mode = mode
} in
let e = newEntry id (ENTRY_parameter inf_p) err in
inf.function_paramlist <- e :: inf.function_paramlist;
e
| PARDEF_CHECK -> begin
match inf.function_redeflist with
| p :: ps -> begin
inf.function_redeflist <- ps;
match p.entry_info with
| ENTRY_parameter inf ->
if not (equalType inf.parameter_type typ) then
error "Parameter type mismatch in redeclaration \
of function %a" pretty_id f.entry_id
else if inf.parameter_mode != mode then
error "Parameter passing mode mismatch in redeclaration \
of function %a" pretty_id f.entry_id
else if p.entry_id != id then
error "Parameter name mismatch in redeclaration \
of function %a" pretty_id f.entry_id
else
H.add !tab id p;
p
| _ ->
internal "I found a parameter that is not a parameter!";
raise Exit
end
| [] ->
error "More parameters than expected in redeclaration \
of function %a" pretty_id f.entry_id;
raise Exit
end
| PARDEF_COMPLETE ->
internal "Cannot add a parameter to an already defined function";
raise Exit
end
| _ ->
internal "Cannot add a parameter to a non-function";
raise Exit
let newTemporary typ =
let id = id_make ("$" ^ string_of_int !tempNumber) in
!currentScope.sco_negofs <- !currentScope.sco_negofs - sizeOfType typ;
let inf = {
temporary_type = typ;
temporary_offset = !currentScope.sco_negofs
} in
incr tempNumber;
newEntry id (ENTRY_temporary inf) false
let forwardFunction e =
match e.entry_info with
| ENTRY_function inf ->
inf.function_isForward <- true
| _ ->
internal "Cannot make a non-function forward"
let endFunctionHeader e typ =
match e.entry_info with
| ENTRY_function inf ->
begin
match inf.function_pstatus with
| PARDEF_COMPLETE ->
internal "Cannot end parameters in an already defined function"
| PARDEF_DEFINE ->
inf.function_result <- typ;
let offset = if e.entry_scope.sco_nesting = 0 then ref 0 else ref 1 in
let fix_offset e =
match e.entry_info with
| ENTRY_parameter inf ->
inf.parameter_offset <- !offset;
let size = 1 in
offset := !offset + size
| _ ->
internal "Cannot fix offset to a non parameter" in
inf.function_paramlist <- List.rev inf.function_paramlist;
List.iter fix_offset inf.function_paramlist;
e.entry_scope.sco_negofs <- !offset;
| PARDEF_CHECK ->
if inf.function_redeflist <> [] then
error "Fewer parameters than expected in redeclaration \
of function %a" pretty_id e.entry_id;
if not (equalType inf.function_result typ) then
error "Result type mismatch in redeclaration of function %a"
pretty_id e.entry_id;
end;
inf.function_pstatus <- PARDEF_COMPLETE
| _ ->
internal "Cannot end parameters in a non-function"