-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSymbol.ml
345 lines (310 loc) · 9.73 KB
/
Symbol.ml
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
open Identifier
open Error
open Types
module H = Hashtbl.Make (
struct
type t = id
let equal = (==)
let hash = Hashtbl.hash
end
)
type global_status = GLOBAL_DEFINED | GLOBAL_USED
type pass_mode = PASS_BY_VALUE | PASS_BY_REFERENCE | PASS_RET
type param_status =
| PARDEF_COMPLETE
| PARDEF_DEFINE
| PARDEF_CHECK
type scope = {
sco_parent : scope option;
mutable sco_nesting : int;
mutable sco_entries : entry list;
mutable sco_negofs : int;
sco_ret_type : Types.typ;
}
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_scope : scope option;
function_isLibrary : bool;
function_global : (entry, global_status) Hashtbl.t;
}
and parameter_info = {
parameter_type : Types.typ;
mutable parameter_offset : int;
parameter_mode : pass_mode
}
and temporary_info = {
temporary_type : Types.typ;
mutable 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;
sco_ret_type = Types.TYPE_proc;
}
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 typ =
let sco = {
sco_parent = Some !currentScope;
sco_nesting = !currentScope.sco_nesting + 1;
sco_entries = [];
sco_negofs = start_negative_offset;
sco_ret_type = typ;
} in
currentScope := sco
let closeScope f =
let sco = !currentScope in
begin
match f.entry_info with
| ENTRY_function(info) ->
info.function_scope <- Some sco
| _ -> internal "Function not a function!"
end;
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 "duplicate identifier %a" 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 ->
error "unknown identifier %a (first occurrence)"
pretty_id id;
(* put it in, so we don't see more errors *)
H.add !tab id (no_entry id);
raise Exit
else
lookup ()
let newVariable id typ err =
!currentScope.sco_negofs <- !currentScope.sco_negofs - sizeOfType typ;
let inf = {
variable_type = typ;
variable_offset = !currentScope.sco_negofs
} in
newEntry id (ENTRY_variable inf) err
let newFunction id err lib =
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
error "duplicate identifier: %a" pretty_id id;
raise Exit
with Not_found ->
let inf = {
function_isForward = false;
function_paramlist = [];
function_redeflist = [];
function_result = TYPE_none;
function_pstatus = PARDEF_DEFINE;
function_initquad = 0;
function_scope = None;
function_isLibrary = lib;
function_global = Hashtbl.create 2;
} 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 =
match typ with
| TYPE_proc -> ref start_positive_offset
| TYPE_byte
| TYPE_int ->
ignore(newParameter (id_make "$$")
typ PASS_BY_REFERENCE e true);
ref (start_positive_offset - 2);
| _ ->
internal "Return type must be int or byte";
raise Terminate
in
let fix_offset e =
match e.entry_info with
| ENTRY_parameter inf ->
inf.parameter_offset <- !offset;
let size =
match inf.parameter_mode with
| PASS_BY_VALUE -> sizeOfType inf.parameter_type
| PASS_BY_REFERENCE -> 2
| PASS_RET -> (
internal "Parameter cannot be return parameter";
raise Terminate
)
in
offset := !offset + size
| _ ->
internal "Cannot fix offset to a non parameter"
in
List.iter fix_offset inf.function_paramlist;
inf.function_paramlist <- List.rev inf.function_paramlist
| 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"
let equalEntries e1 e2 =
e1.entry_scope.sco_nesting = e2.entry_scope.sco_nesting &&
e1.entry_id = e2.entry_id
let isLibraryFunction ent =
match ent.entry_info with
| ENTRY_function fun_info -> fun_info.function_isLibrary
| _ -> false
let find_first_temporary_offset scope =
let rec helper acc = function
| [] -> (acc,[])
| (h::t) ->
match h.entry_info with
| ENTRY_temporary temp_info ->
helper (acc + (sizeOfType temp_info.temporary_type)) t
| _ -> (acc,t) in
helper scope.sco_negofs scope.sco_entries